OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_CURVECP_SERVER_PACKETIZER_H_ | 5 #ifndef NET_CURVECP_SERVER_PACKETIZER_H_ |
6 #define NET_CURVECP_SERVER_PACKETIZER_H_ | 6 #define NET_CURVECP_SERVER_PACKETIZER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "net/base/completion_callback.h" | 13 #include "net/base/completion_callback.h" |
14 #include "net/base/ip_endpoint.h" | 14 #include "net/base/ip_endpoint.h" |
15 #include "net/curvecp/packetizer.h" | 15 #include "net/curvecp/packetizer.h" |
16 #include "net/curvecp/protocol.h" | 16 #include "net/curvecp/protocol.h" |
17 | 17 |
18 namespace net { | 18 namespace net { |
19 | 19 |
20 class IOBuffer; | 20 class IOBuffer; |
21 class IPEndPoint; | 21 class IPEndPoint; |
22 class UDPServerSocket; | 22 class UDPServerSocket; |
23 | 23 |
24 class ServerPacketizer : public base::RefCounted<ServerPacketizer>, | 24 class ServerPacketizer : public base::RefCounted<ServerPacketizer>, |
25 public Packetizer { | 25 public Packetizer { |
26 public: | 26 public: |
27 ServerPacketizer(); | 27 ServerPacketizer(); |
28 virtual ~ServerPacketizer(); | |
29 | 28 |
30 // Listen for new connections from the Packetizer. | 29 // Listen for new connections from the Packetizer. |
31 int Listen(const IPEndPoint& endpoint, Packetizer::Listener* listener); | 30 int Listen(const IPEndPoint& endpoint, Packetizer::Listener* listener); |
32 | 31 |
33 // Register a listener for a connection. | 32 // Register a listener for a connection. |
34 // To revoke the registration, call Close(). | 33 // To revoke the registration, call Close(). |
35 bool Open(ConnectionKey key, Packetizer::Listener* listener); | 34 bool Open(ConnectionKey key, Packetizer::Listener* listener); |
36 | 35 |
37 // Packetizer methods | 36 // Packetizer methods |
38 virtual int SendMessage(ConnectionKey key, | 37 virtual int SendMessage(ConnectionKey key, |
39 const char* data, | 38 const char* data, |
40 size_t length, | 39 size_t length, |
41 const CompletionCallback& callback) OVERRIDE; | 40 const CompletionCallback& callback) OVERRIDE; |
42 virtual void Close(ConnectionKey key) OVERRIDE; | 41 virtual void Close(ConnectionKey key) OVERRIDE; |
43 virtual int GetPeerAddress(IPEndPoint* endpoint) const OVERRIDE; | 42 virtual int GetPeerAddress(IPEndPoint* endpoint) const OVERRIDE; |
44 virtual int max_message_payload() const OVERRIDE; | 43 virtual int max_message_payload() const OVERRIDE; |
45 | 44 |
46 private: | 45 private: |
| 46 friend class base::RefCounted<ServerPacketizer>; |
| 47 |
47 enum State { | 48 enum State { |
48 NONE, // The initial state, before listen. | 49 NONE, // The initial state, before listen. |
49 LISTENING, // Listening for packets. | 50 LISTENING, // Listening for packets. |
50 }; | 51 }; |
51 | 52 |
52 typedef std::map<ConnectionKey, Packetizer::Listener*> ListenerMap; | 53 typedef std::map<ConnectionKey, Packetizer::Listener*> ListenerMap; |
53 typedef std::map<ConnectionKey, IPEndPoint> ConnectionMap; | 54 typedef std::map<ConnectionKey, IPEndPoint> ConnectionMap; |
54 | 55 |
| 56 virtual ~ServerPacketizer(); |
| 57 |
55 // Callbacks when an internal IO is completed. | 58 // Callbacks when an internal IO is completed. |
56 void OnReadComplete(int result); | 59 void OnReadComplete(int result); |
57 void OnWriteComplete(int result); | 60 void OnWriteComplete(int result); |
58 | 61 |
59 // Process the result of a Read operation. | 62 // Process the result of a Read operation. |
60 void ProcessRead(int bytes_read); | 63 void ProcessRead(int bytes_read); |
61 | 64 |
62 // Read packets until an error occurs. | 65 // Read packets until an error occurs. |
63 int ReadPackets(); | 66 int ReadPackets(); |
64 | 67 |
(...skipping 21 matching lines...) Expand all Loading... |
86 ConnectionMap connection_map_; | 89 ConnectionMap connection_map_; |
87 // The listener map tracks active message listeners known to the packetizer. | 90 // The listener map tracks active message listeners known to the packetizer. |
88 ListenerMap listener_map_; | 91 ListenerMap listener_map_; |
89 | 92 |
90 DISALLOW_COPY_AND_ASSIGN(ServerPacketizer); | 93 DISALLOW_COPY_AND_ASSIGN(ServerPacketizer); |
91 }; | 94 }; |
92 | 95 |
93 } // namespace net | 96 } // namespace net |
94 | 97 |
95 #endif // NET_CURVECP_SERVER_PACKETIZER_H_ | 98 #endif // NET_CURVECP_SERVER_PACKETIZER_H_ |
OLD | NEW |