OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_SERVICES_PUBLIC_CPP_NETWORK_UDP_SOCKET_WRAPPER_H_ | |
6 #define MOJO_SERVICES_PUBLIC_CPP_NETWORK_UDP_SOCKET_WRAPPER_H_ | |
7 | |
8 #include <queue> | |
9 | |
10 #include "mojo/services/public/interfaces/network/udp_socket.mojom.h" | |
11 | |
12 namespace mojo { | |
13 | |
14 class UDPSocketWrapper : public UDPSocketClient { | |
brettw
2014/10/21 18:21:33
It seems like this should have an extensive commen
yzshen1
2014/10/21 21:59:30
Done.
| |
15 public: | |
16 typedef Callback<void(NetworkErrorPtr, NetAddressPtr, Array<uint8_t>)> | |
17 ReceiveCallback; | |
18 typedef Callback<void(NetworkErrorPtr)> ErrorCallback; | |
19 | |
20 explicit UDPSocketWrapper(UDPSocketPtr socket); | |
21 | |
22 // |receive_queue_slots| determines the size (in datagrams) of the local | |
23 // receive queue, which caches incoming datagrams. | |
24 // |requested_max_pending_sends| is used to call | |
25 // NegotiateMaxPendingSendRequests() on |socket|. | |
26 // The two numbers should be greater than 0. If you would like to use default | |
27 // values, please use the other constructor. | |
28 UDPSocketWrapper(UDPSocketPtr socket, | |
29 uint32_t receive_queue_slots, | |
30 uint32_t requested_max_pending_sends); | |
31 | |
32 virtual ~UDPSocketWrapper(); | |
33 | |
34 void AllowAddressReuse(const ErrorCallback& callback); | |
35 | |
36 void Bind(NetAddressPtr addr, | |
37 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback); | |
38 | |
39 void SetSendBufferSize(uint32_t size, const ErrorCallback& callback); | |
40 | |
41 void SetReceiveBufferSize(uint32_t size, const ErrorCallback& callback); | |
42 | |
43 // If there are already incoming datagrams cached locally, this method runs | |
44 // |callback| before it returns, and the return value is set to true. | |
45 // Otherwise, the return value is set to false and the callback will be run | |
46 // asynchronously. | |
47 bool ReceiveFrom(const ReceiveCallback& callback); | |
48 | |
49 // This method is aware of the max pending send requests allowed by the | |
50 // service, and caches send requests locally if necessary. | |
51 void SendTo(NetAddressPtr dest_addr, | |
52 Array<uint8_t> data, | |
53 const ErrorCallback& callback); | |
54 | |
55 private: | |
56 class NegotiateCallbackHandler : public Callback<void(uint32_t)>::Runnable { | |
57 public: | |
58 explicit NegotiateCallbackHandler(UDPSocketWrapper* delegate); | |
59 virtual ~NegotiateCallbackHandler(); | |
60 | |
61 // Callback<void(uint32_t)>::Runnable implementation: | |
62 virtual void Run(uint32_t actual_size) const override; | |
63 | |
64 private: | |
65 // Because this callback is passed to a method of |socket_|, and |socket_| | |
66 // is owned by |delegate_|, it should be safe to assume that |delegate_| is | |
67 // valid if/when Run() is called. | |
68 UDPSocketWrapper* delegate_; | |
69 }; | |
70 | |
71 class SendCallbackHandler : public ErrorCallback::Runnable { | |
72 public: | |
73 explicit SendCallbackHandler(UDPSocketWrapper* delegate, | |
74 const ErrorCallback& forward_callback); | |
75 virtual ~SendCallbackHandler(); | |
76 | |
77 // ErrorCallback::Runnable implementation: | |
78 virtual void Run(NetworkErrorPtr result) const override; | |
79 | |
80 private: | |
81 // Because this callback is passed to a method of |socket_|, and |socket_| | |
82 // is owned by |delegate_|, it should be safe to assume that |delegate_| is | |
83 // valid if/when Run() is called. | |
84 UDPSocketWrapper* delegate_; | |
85 ErrorCallback forward_callback_; | |
86 }; | |
87 | |
88 struct ReceivedData { | |
89 ReceivedData(); | |
90 ~ReceivedData(); | |
91 | |
92 NetworkErrorPtr result; | |
93 NetAddressPtr src_addr; | |
94 Array<uint8_t> data; | |
95 }; | |
96 | |
97 struct SendRequest { | |
98 SendRequest(); | |
99 ~SendRequest(); | |
100 | |
101 NetAddressPtr dest_addr; | |
102 Array<uint8_t> data; | |
103 ErrorCallback callback; | |
104 }; | |
105 | |
106 // UDPSocketClient implementation: | |
107 virtual void OnReceived(NetworkErrorPtr result, | |
108 NetAddressPtr src_addr, | |
109 Array<uint8_t> data) override; | |
110 | |
111 void Initialize(uint32_t requested_max_pending_sends); | |
112 void OnNegotiateMaxPendingSendRequestsCompleted(uint32_t actual_size); | |
113 | |
114 void OnSendToCompleted(NetworkErrorPtr result, | |
115 const ErrorCallback& forward_callback); | |
116 | |
117 // Returns true if a send request in |send_requests_| has been processed. | |
118 bool ProcessNextSendRequest(); | |
119 | |
120 UDPSocketPtr socket_; | |
121 | |
122 uint32_t max_receive_queue_size_; | |
123 | |
124 // Owns all the objects that its elements point to. | |
125 std::queue<ReceivedData*> receive_queue_; | |
126 | |
127 std::queue<ReceiveCallback> receive_requests_; | |
128 | |
129 uint32_t max_pending_sends_; | |
130 uint32_t current_pending_sends_; | |
131 | |
132 // Owns all the objects that its elements point to. | |
133 std::queue<SendRequest*> send_requests_; | |
134 }; | |
135 | |
136 } // namespace mojo | |
137 | |
138 #endif // MOJO_SERVICES_PUBLIC_CPP_NETWORK_UDP_SOCKET_WRAPPER_H_ | |
OLD | NEW |