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_NETWORK_PUBLIC_CPP_UDP_SOCKET_WRAPPER_H_ | |
6 #define MOJO_SERVICES_NETWORK_PUBLIC_CPP_UDP_SOCKET_WRAPPER_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <queue> | |
11 | |
12 #include "mojo/public/cpp/bindings/binding.h" | |
13 #include "network/public/interfaces/udp_socket.mojom.h" | |
14 | |
15 namespace mojo { | |
16 | |
17 // This class is a wrapper around the UDPSocket interface. It provides local | |
18 // cache for received datagrams as well as for (excessive) send requests: | |
19 // - You call ReceiveFrom() to retrieve one datagram. If there is already cached | |
20 // data, the operation completes synchronously. | |
21 // - You don't need to worry about the max-pending-send-requests restriction | |
22 // imposed by the service side. If you make many SendTo() calls in a short | |
23 // period of time, it caches excessive requests and sends them later. | |
24 class UDPSocketWrapper : public UDPSocketReceiver { | |
25 public: | |
26 using ReceiveCallback = | |
27 Callback<void(NetworkErrorPtr, NetAddressPtr, Array<uint8_t>)>; | |
28 using ErrorCallback = Callback<void(NetworkErrorPtr)>; | |
29 using BindOrConnectCallback = | |
30 Callback<void(NetworkErrorPtr, | |
31 NetAddressPtr, | |
32 InterfaceRequest<UDPSocketReceiver>)>; | |
33 | |
34 explicit UDPSocketWrapper(UDPSocketPtr socket); | |
35 | |
36 // |receive_queue_slots| determines the size (in datagrams) of the local | |
37 // receive queue, which caches incoming datagrams. | |
38 // |requested_max_pending_sends| is used to call | |
39 // NegotiateMaxPendingSendRequests() on |socket|. | |
40 // The two numbers should be greater than 0. If you would like to use default | |
41 // values, please use the other constructor. | |
42 UDPSocketWrapper(UDPSocketPtr socket, | |
43 uint32_t receive_queue_slots, | |
44 uint32_t requested_max_pending_sends); | |
45 | |
46 ~UDPSocketWrapper() override; | |
47 | |
48 void AllowAddressReuse(const ErrorCallback& callback); | |
49 | |
50 void Bind(NetAddressPtr addr, | |
51 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback); | |
52 | |
53 void Connect(NetAddressPtr remote_addr, | |
54 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback); | |
55 | |
56 void SetSendBufferSize(uint32_t size, const ErrorCallback& callback); | |
57 | |
58 void SetReceiveBufferSize(uint32_t size, const ErrorCallback& callback); | |
59 | |
60 // If there are already incoming datagrams cached locally, this method runs | |
61 // |callback| before it returns, and the return value is set to true. | |
62 // Otherwise, the return value is set to false and the callback will be run | |
63 // asynchronously. | |
64 // If the socket is connected, the net address pointer passed into the | |
65 // callback is set to null. | |
66 bool ReceiveFrom(const ReceiveCallback& callback); | |
67 | |
68 // This method is aware of the max pending send requests allowed by the | |
69 // service, and caches send requests locally if necessary. | |
70 // |dest_addr| is allowed to be null if the socket is connected. | |
71 void SendTo(NetAddressPtr dest_addr, | |
72 Array<uint8_t> data, | |
73 const ErrorCallback& callback); | |
74 | |
75 private: | |
76 class NegotiateCallbackHandler : public Callback<void(uint32_t)>::Runnable { | |
77 public: | |
78 explicit NegotiateCallbackHandler(UDPSocketWrapper* delegate); | |
79 ~NegotiateCallbackHandler() override; | |
80 | |
81 // Callback<void(uint32_t)>::Runnable implementation: | |
82 void Run(uint32_t actual_size) const override; | |
83 | |
84 private: | |
85 // Because this callback is passed to a method of |socket_|, and |socket_| | |
86 // is owned by |delegate_|, it should be safe to assume that |delegate_| is | |
87 // valid if/when Run() is called. | |
88 UDPSocketWrapper* delegate_; | |
89 }; | |
90 | |
91 class SendCallbackHandler : public ErrorCallback::Runnable { | |
92 public: | |
93 explicit SendCallbackHandler(UDPSocketWrapper* delegate, | |
94 const ErrorCallback& forward_callback); | |
95 ~SendCallbackHandler() override; | |
96 | |
97 // ErrorCallback::Runnable implementation: | |
98 void Run(NetworkErrorPtr result) const override; | |
99 | |
100 private: | |
101 // Because this callback is passed to a method of |socket_|, and |socket_| | |
102 // is owned by |delegate_|, it should be safe to assume that |delegate_| is | |
103 // valid if/when Run() is called. | |
104 UDPSocketWrapper* delegate_; | |
105 ErrorCallback forward_callback_; | |
106 }; | |
107 | |
108 class ReceiverBindingCallback : public BindOrConnectCallback::Runnable { | |
109 public: | |
110 ReceiverBindingCallback( | |
111 UDPSocketWrapper* delegate, | |
112 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& wrapper_callback); | |
113 ~ReceiverBindingCallback() override; | |
114 | |
115 // BindOrConnectCallback::Runnable implementation: | |
116 void Run(NetworkErrorPtr result, | |
117 NetAddressPtr addr, | |
118 InterfaceRequest<UDPSocketReceiver> request) const override; | |
119 | |
120 private: | |
121 // Because this callback is passed to a method of |socket_|, and |socket_| | |
122 // is owned by |delegate_|, it should be safe to assume that |delegate_| is | |
123 // valid if/when Run() is called. | |
124 UDPSocketWrapper* delegate_; | |
125 const Callback<void(NetworkErrorPtr, NetAddressPtr)> wrapper_callback_; | |
126 }; | |
127 | |
128 struct ReceivedData { | |
129 ReceivedData(); | |
130 ~ReceivedData(); | |
131 | |
132 NetworkErrorPtr result; | |
133 NetAddressPtr src_addr; | |
134 Array<uint8_t> data; | |
135 }; | |
136 | |
137 struct SendRequest { | |
138 SendRequest(); | |
139 ~SendRequest(); | |
140 | |
141 NetAddressPtr dest_addr; | |
142 Array<uint8_t> data; | |
143 ErrorCallback callback; | |
144 }; | |
145 | |
146 // UDPSocketReceiver implementation: | |
147 void OnReceived(NetworkErrorPtr result, | |
148 NetAddressPtr src_addr, | |
149 Array<uint8_t> data) override; | |
150 | |
151 void Initialize(uint32_t requested_max_pending_sends); | |
152 void OnNegotiateMaxPendingSendRequestsCompleted(uint32_t actual_size); | |
153 | |
154 void OnSendToCompleted(NetworkErrorPtr result, | |
155 const ErrorCallback& forward_callback); | |
156 | |
157 // Returns true if a send request in |send_requests_| has been processed. | |
158 bool ProcessNextSendRequest(); | |
159 | |
160 // Binds to a UDPSocketReceiver request and notifies |socket_| that we're | |
161 // ready to start receiving data. | |
162 void StartReceivingData(InterfaceRequest<UDPSocketReceiver> request); | |
163 | |
164 Binding<UDPSocketReceiver> binding_; | |
165 | |
166 UDPSocketPtr socket_; | |
167 | |
168 uint32_t max_receive_queue_size_; | |
169 | |
170 // Owns all the objects that its elements point to. | |
171 std::queue<ReceivedData*> receive_queue_; | |
172 | |
173 std::queue<ReceiveCallback> receive_requests_; | |
174 | |
175 uint32_t max_pending_sends_; | |
176 uint32_t current_pending_sends_; | |
177 | |
178 // Owns all the objects that its elements point to. | |
179 std::queue<SendRequest*> send_requests_; | |
180 }; | |
181 | |
182 } // namespace mojo | |
183 | |
184 #endif // MOJO_SERVICES_NETWORK_PUBLIC_CPP_UDP_SOCKET_WRAPPER_H_ | |
OLD | NEW |