1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef NET_CURVECP_SERVER_PACKETIZER_H_
#define NET_CURVECP_SERVER_PACKETIZER_H_
#pragma once
#include <map>
#include "base/memory/ref_counted.h"
#include "net/base/completion_callback.h"
#include "net/base/ip_endpoint.h"
#include "net/curvecp/packetizer.h"
#include "net/curvecp/protocol.h"
namespace net {
class IOBuffer;
class IPEndPoint;
class UDPServerSocket;
class ServerPacketizer : public base::RefCounted<ServerPacketizer>,
public Packetizer {
public:
ServerPacketizer();
virtual ~ServerPacketizer();
// Listen for new connections from the Packetizer.
int Listen(const IPEndPoint& endpoint, Packetizer::Listener* listener);
// Register a listener for a connection.
// To revoke the registration, call Close().
bool Open(ConnectionKey key, Packetizer::Listener* listener);
// Packetizer methods
virtual int SendMessage(ConnectionKey key,
const char* data,
size_t length,
OldCompletionCallback* callback);
virtual void Close(ConnectionKey key);
virtual int GetPeerAddress(IPEndPoint* endpoint) const;
virtual int max_message_payload() const;
private:
enum State {
NONE, // The initial state, before listen.
LISTENING, // Listening for packets.
};
typedef std::map<ConnectionKey, Packetizer::Listener*> ListenerMap;
typedef std::map<ConnectionKey, IPEndPoint> ConnectionMap;
// Callbacks when an internal IO is completed.
void OnReadComplete(int result);
void OnWriteComplete(int result);
// Process the result of a Read operation.
void ProcessRead(int bytes_read);
// Read packets until an error occurs.
int ReadPackets();
// Handlers for recognized packets.
void HandleHelloPacket(Packet* packet, int length);
void HandleInitiatePacket(Packet* packet, int length);
void HandleClientMessagePacket(Packet* packet, int length);
void HandleMessage(ConnectionKey key, unsigned char* message, int length);
// Manage the list of known "connections".
void AddConnection(ConnectionKey key, const IPEndPoint& endpoint);
// TODO(mbelshe): need to trim out aged/idle connections
void RemoveConnection(ConnectionKey key);
bool IsActiveConnection(ConnectionKey key);
State state_;
scoped_ptr<UDPServerSocket> socket_;
Packetizer::Listener* listener_; // Accept listener.
scoped_refptr<IOBuffer> read_buffer_;
IPEndPoint recv_address_;
// The connection map tracks active client addresses which are known
// to this server.
ConnectionMap connection_map_;
// The listener map tracks active message listeners known to the packetizer.
ListenerMap listener_map_;
OldCompletionCallbackImpl<ServerPacketizer> read_callback_;
OldCompletionCallbackImpl<ServerPacketizer> write_callback_;
DISALLOW_COPY_AND_ASSIGN(ServerPacketizer);
};
} // namespace net
#endif // NET_CURVECP_SERVER_PACKETIZER_H_
|