| 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 typedef Callback<void(NetworkErrorPtr, NetAddressPtr, Array<uint8_t>)> |
| 24 ReceiveCallback; | 25 ReceiveCallback; |
| 25 typedef Callback<void(NetworkErrorPtr)> ErrorCallback; | 26 typedef Callback<void(NetworkErrorPtr)> ErrorCallback; |
| 26 | 27 |
| 27 explicit UDPSocketWrapper(UDPSocketPtr socket); | 28 explicit UDPSocketWrapper(UDPSocketPtr socket); |
| 28 | 29 |
| 29 // |receive_queue_slots| determines the size (in datagrams) of the local | 30 // |receive_queue_slots| determines the size (in datagrams) of the local |
| 30 // receive queue, which caches incoming datagrams. | 31 // receive queue, which caches incoming datagrams. |
| 31 // |requested_max_pending_sends| is used to call | 32 // |requested_max_pending_sends| is used to call |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 | 110 |
| 110 struct SendRequest { | 111 struct SendRequest { |
| 111 SendRequest(); | 112 SendRequest(); |
| 112 ~SendRequest(); | 113 ~SendRequest(); |
| 113 | 114 |
| 114 NetAddressPtr dest_addr; | 115 NetAddressPtr dest_addr; |
| 115 Array<uint8_t> data; | 116 Array<uint8_t> data; |
| 116 ErrorCallback callback; | 117 ErrorCallback callback; |
| 117 }; | 118 }; |
| 118 | 119 |
| 119 // UDPSocketClient implementation: | 120 // UDPSocketReceiver implementation: |
| 120 void OnReceived(NetworkErrorPtr result, | 121 void OnReceived(NetworkErrorPtr result, |
| 121 NetAddressPtr src_addr, | 122 NetAddressPtr src_addr, |
| 122 Array<uint8_t> data) override; | 123 Array<uint8_t> data) override; |
| 123 | 124 |
| 124 void Initialize(uint32_t requested_max_pending_sends); | 125 void Initialize(uint32_t requested_max_pending_sends); |
| 125 void OnNegotiateMaxPendingSendRequestsCompleted(uint32_t actual_size); | 126 void OnNegotiateMaxPendingSendRequestsCompleted(uint32_t actual_size); |
| 126 | 127 |
| 127 void OnSendToCompleted(NetworkErrorPtr result, | 128 void OnSendToCompleted(NetworkErrorPtr result, |
| 128 const ErrorCallback& forward_callback); | 129 const ErrorCallback& forward_callback); |
| 129 | 130 |
| 130 // Returns true if a send request in |send_requests_| has been processed. | 131 // Returns true if a send request in |send_requests_| has been processed. |
| 131 bool ProcessNextSendRequest(); | 132 bool ProcessNextSendRequest(); |
| 132 | 133 |
| 134 Binding<UDPSocketReceiver> binding_; |
| 135 |
| 133 UDPSocketPtr socket_; | 136 UDPSocketPtr socket_; |
| 134 | 137 |
| 135 uint32_t max_receive_queue_size_; | 138 uint32_t max_receive_queue_size_; |
| 136 | 139 |
| 137 // Owns all the objects that its elements point to. | 140 // Owns all the objects that its elements point to. |
| 138 std::queue<ReceivedData*> receive_queue_; | 141 std::queue<ReceivedData*> receive_queue_; |
| 139 | 142 |
| 140 std::queue<ReceiveCallback> receive_requests_; | 143 std::queue<ReceiveCallback> receive_requests_; |
| 141 | 144 |
| 142 uint32_t max_pending_sends_; | 145 uint32_t max_pending_sends_; |
| 143 uint32_t current_pending_sends_; | 146 uint32_t current_pending_sends_; |
| 144 | 147 |
| 145 // Owns all the objects that its elements point to. | 148 // Owns all the objects that its elements point to. |
| 146 std::queue<SendRequest*> send_requests_; | 149 std::queue<SendRequest*> send_requests_; |
| 147 }; | 150 }; |
| 148 | 151 |
| 149 } // namespace mojo | 152 } // namespace mojo |
| 150 | 153 |
| 151 #endif // MOJO_SERVICES_NETWORK_PUBLIC_CPP_UDP_SOCKET_WRAPPER_H_ | 154 #endif // MOJO_SERVICES_NETWORK_PUBLIC_CPP_UDP_SOCKET_WRAPPER_H_ |
| OLD | NEW |