OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 MOJO_SERVICES_NETWORK_PUBLIC_CPP_UDP_SOCKET_WRAPPER_H_ | 5 #ifndef MOJO_SERVICES_NETWORK_PUBLIC_CPP_UDP_SOCKET_WRAPPER_H_ |
6 #define MOJO_SERVICES_NETWORK_PUBLIC_CPP_UDP_SOCKET_WRAPPER_H_ | 6 #define MOJO_SERVICES_NETWORK_PUBLIC_CPP_UDP_SOCKET_WRAPPER_H_ |
7 | 7 |
8 #include <queue> | 8 #include <queue> |
9 | 9 |
10 #include "network/public/interfaces/udp_socket.mojom.h" | 10 #include "network/public/interfaces/udp_socket.mojom.h" |
| 11 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" |
11 | 12 |
12 namespace mojo { | 13 namespace mojo { |
13 | 14 |
14 // This class is a wrapper around the UDPSocket interface. It provides local | 15 // This class is a wrapper around the UDPSocket interface. It provides local |
15 // cache for received datagrams as well as for (excessive) send requests: | 16 // cache for received datagrams as well as for (excessive) send requests: |
16 // - You call ReceiveFrom() to retrieve one datagram. If there is already cached | 17 // - You call ReceiveFrom() to retrieve one datagram. If there is already cached |
17 // data, the operation completes synchronously. | 18 // data, the operation completes synchronously. |
18 // - You don't need to worry about the max-pending-send-requests restriction | 19 // - You don't need to worry about the max-pending-send-requests restriction |
19 // imposed by the service side. If you make many SendTo() calls in a short | 20 // imposed by the service side. If you make many SendTo() calls in a short |
20 // period of time, it caches excessive requests and sends them later. | 21 // period of time, it caches excessive requests and sends them later. |
21 class UDPSocketWrapper : public UDPSocketClient { | 22 class UDPSocketWrapper : public UDPSocketReceiver { |
22 public: | 23 public: |
23 typedef Callback<void(NetworkErrorPtr, NetAddressPtr, Array<uint8_t>)> | 24 using ReceiveCallback = |
24 ReceiveCallback; | 25 Callback<void(NetworkErrorPtr, NetAddressPtr, Array<uint8_t>)>; |
25 typedef Callback<void(NetworkErrorPtr)> ErrorCallback; | 26 using ErrorCallback = Callback<void(NetworkErrorPtr)>; |
| 27 using BindOrConnectCallback = |
| 28 Callback<void(NetworkErrorPtr, |
| 29 NetAddressPtr, |
| 30 InterfaceRequest<UDPSocketReceiver>)>; |
26 | 31 |
27 explicit UDPSocketWrapper(UDPSocketPtr socket); | 32 explicit UDPSocketWrapper(UDPSocketPtr socket); |
28 | 33 |
29 // |receive_queue_slots| determines the size (in datagrams) of the local | 34 // |receive_queue_slots| determines the size (in datagrams) of the local |
30 // receive queue, which caches incoming datagrams. | 35 // receive queue, which caches incoming datagrams. |
31 // |requested_max_pending_sends| is used to call | 36 // |requested_max_pending_sends| is used to call |
32 // NegotiateMaxPendingSendRequests() on |socket|. | 37 // NegotiateMaxPendingSendRequests() on |socket|. |
33 // The two numbers should be greater than 0. If you would like to use default | 38 // The two numbers should be greater than 0. If you would like to use default |
34 // values, please use the other constructor. | 39 // values, please use the other constructor. |
35 UDPSocketWrapper(UDPSocketPtr socket, | 40 UDPSocketWrapper(UDPSocketPtr socket, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 void Run(NetworkErrorPtr result) const override; | 96 void Run(NetworkErrorPtr result) const override; |
92 | 97 |
93 private: | 98 private: |
94 // Because this callback is passed to a method of |socket_|, and |socket_| | 99 // Because this callback is passed to a method of |socket_|, and |socket_| |
95 // is owned by |delegate_|, it should be safe to assume that |delegate_| is | 100 // is owned by |delegate_|, it should be safe to assume that |delegate_| is |
96 // valid if/when Run() is called. | 101 // valid if/when Run() is called. |
97 UDPSocketWrapper* delegate_; | 102 UDPSocketWrapper* delegate_; |
98 ErrorCallback forward_callback_; | 103 ErrorCallback forward_callback_; |
99 }; | 104 }; |
100 | 105 |
| 106 class ReceiverBindingCallback : public BindOrConnectCallback::Runnable { |
| 107 public: |
| 108 ReceiverBindingCallback( |
| 109 UDPSocketWrapper* delegate, |
| 110 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& wrapper_callback); |
| 111 ~ReceiverBindingCallback() override; |
| 112 |
| 113 // BindOrConnectCallback::Runnable implementation: |
| 114 void Run(NetworkErrorPtr result, |
| 115 NetAddressPtr addr, |
| 116 InterfaceRequest<UDPSocketReceiver> request) const override; |
| 117 |
| 118 private: |
| 119 // Because this callback is passed to a method of |socket_|, and |socket_| |
| 120 // is owned by |delegate_|, it should be safe to assume that |delegate_| is |
| 121 // valid if/when Run() is called. |
| 122 UDPSocketWrapper* delegate_; |
| 123 const Callback<void(NetworkErrorPtr, NetAddressPtr)> wrapper_callback_; |
| 124 }; |
| 125 |
101 struct ReceivedData { | 126 struct ReceivedData { |
102 ReceivedData(); | 127 ReceivedData(); |
103 ~ReceivedData(); | 128 ~ReceivedData(); |
104 | 129 |
105 NetworkErrorPtr result; | 130 NetworkErrorPtr result; |
106 NetAddressPtr src_addr; | 131 NetAddressPtr src_addr; |
107 Array<uint8_t> data; | 132 Array<uint8_t> data; |
108 }; | 133 }; |
109 | 134 |
110 struct SendRequest { | 135 struct SendRequest { |
111 SendRequest(); | 136 SendRequest(); |
112 ~SendRequest(); | 137 ~SendRequest(); |
113 | 138 |
114 NetAddressPtr dest_addr; | 139 NetAddressPtr dest_addr; |
115 Array<uint8_t> data; | 140 Array<uint8_t> data; |
116 ErrorCallback callback; | 141 ErrorCallback callback; |
117 }; | 142 }; |
118 | 143 |
119 // UDPSocketClient implementation: | 144 // UDPSocketReceiver implementation: |
120 void OnReceived(NetworkErrorPtr result, | 145 void OnReceived(NetworkErrorPtr result, |
121 NetAddressPtr src_addr, | 146 NetAddressPtr src_addr, |
122 Array<uint8_t> data) override; | 147 Array<uint8_t> data) override; |
123 | 148 |
124 void Initialize(uint32_t requested_max_pending_sends); | 149 void Initialize(uint32_t requested_max_pending_sends); |
125 void OnNegotiateMaxPendingSendRequestsCompleted(uint32_t actual_size); | 150 void OnNegotiateMaxPendingSendRequestsCompleted(uint32_t actual_size); |
126 | 151 |
127 void OnSendToCompleted(NetworkErrorPtr result, | 152 void OnSendToCompleted(NetworkErrorPtr result, |
128 const ErrorCallback& forward_callback); | 153 const ErrorCallback& forward_callback); |
129 | 154 |
130 // Returns true if a send request in |send_requests_| has been processed. | 155 // Returns true if a send request in |send_requests_| has been processed. |
131 bool ProcessNextSendRequest(); | 156 bool ProcessNextSendRequest(); |
132 | 157 |
| 158 // Binds to a UDPSocketReceiver request and notifies |socket_| that we're |
| 159 // ready to start receiving data. |
| 160 void StartReceivingData(InterfaceRequest<UDPSocketReceiver> request); |
| 161 |
| 162 Binding<UDPSocketReceiver> binding_; |
| 163 |
133 UDPSocketPtr socket_; | 164 UDPSocketPtr socket_; |
134 | 165 |
135 uint32_t max_receive_queue_size_; | 166 uint32_t max_receive_queue_size_; |
136 | 167 |
137 // Owns all the objects that its elements point to. | 168 // Owns all the objects that its elements point to. |
138 std::queue<ReceivedData*> receive_queue_; | 169 std::queue<ReceivedData*> receive_queue_; |
139 | 170 |
140 std::queue<ReceiveCallback> receive_requests_; | 171 std::queue<ReceiveCallback> receive_requests_; |
141 | 172 |
142 uint32_t max_pending_sends_; | 173 uint32_t max_pending_sends_; |
143 uint32_t current_pending_sends_; | 174 uint32_t current_pending_sends_; |
144 | 175 |
145 // Owns all the objects that its elements point to. | 176 // Owns all the objects that its elements point to. |
146 std::queue<SendRequest*> send_requests_; | 177 std::queue<SendRequest*> send_requests_; |
147 }; | 178 }; |
148 | 179 |
149 } // namespace mojo | 180 } // namespace mojo |
150 | 181 |
151 #endif // MOJO_SERVICES_NETWORK_PUBLIC_CPP_UDP_SOCKET_WRAPPER_H_ | 182 #endif // MOJO_SERVICES_NETWORK_PUBLIC_CPP_UDP_SOCKET_WRAPPER_H_ |
OLD | NEW |