Update mojo sdk to rev 59145288bae55b0fce4276b017df6a1117bcf00f
[chromium-blink-merge.git] / mojo / services / network / public / cpp / udp_socket_wrapper.h
blobc95fd31ff15d0ac47573a61e73f5c7d058b5f284
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.
5 #ifndef MOJO_SERVICES_NETWORK_PUBLIC_CPP_UDP_SOCKET_WRAPPER_H_
6 #define MOJO_SERVICES_NETWORK_PUBLIC_CPP_UDP_SOCKET_WRAPPER_H_
8 #include <queue>
10 #include "mojo/services/network/public/interfaces/udp_socket.mojom.h"
12 namespace mojo {
14 // 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 // - You call ReceiveFrom() to retrieve one datagram. If there is already cached
17 // data, the operation completes synchronously.
18 // - 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 // period of time, it caches excessive requests and sends them later.
21 class UDPSocketWrapper : public UDPSocketClient {
22 public:
23 typedef Callback<void(NetworkErrorPtr, NetAddressPtr, Array<uint8_t>)>
24 ReceiveCallback;
25 typedef Callback<void(NetworkErrorPtr)> ErrorCallback;
27 explicit UDPSocketWrapper(UDPSocketPtr socket);
29 // |receive_queue_slots| determines the size (in datagrams) of the local
30 // receive queue, which caches incoming datagrams.
31 // |requested_max_pending_sends| is used to call
32 // NegotiateMaxPendingSendRequests() on |socket|.
33 // The two numbers should be greater than 0. If you would like to use default
34 // values, please use the other constructor.
35 UDPSocketWrapper(UDPSocketPtr socket,
36 uint32_t receive_queue_slots,
37 uint32_t requested_max_pending_sends);
39 ~UDPSocketWrapper() override;
41 void AllowAddressReuse(const ErrorCallback& callback);
43 void Bind(NetAddressPtr addr,
44 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback);
46 void SetSendBufferSize(uint32_t size, const ErrorCallback& callback);
48 void SetReceiveBufferSize(uint32_t size, const ErrorCallback& callback);
50 // If there are already incoming datagrams cached locally, this method runs
51 // |callback| before it returns, and the return value is set to true.
52 // Otherwise, the return value is set to false and the callback will be run
53 // asynchronously.
54 bool ReceiveFrom(const ReceiveCallback& callback);
56 // This method is aware of the max pending send requests allowed by the
57 // service, and caches send requests locally if necessary.
58 void SendTo(NetAddressPtr dest_addr,
59 Array<uint8_t> data,
60 const ErrorCallback& callback);
62 private:
63 class NegotiateCallbackHandler : public Callback<void(uint32_t)>::Runnable {
64 public:
65 explicit NegotiateCallbackHandler(UDPSocketWrapper* delegate);
66 ~NegotiateCallbackHandler() override;
68 // Callback<void(uint32_t)>::Runnable implementation:
69 void Run(uint32_t actual_size) const override;
71 private:
72 // Because this callback is passed to a method of |socket_|, and |socket_|
73 // is owned by |delegate_|, it should be safe to assume that |delegate_| is
74 // valid if/when Run() is called.
75 UDPSocketWrapper* delegate_;
78 class SendCallbackHandler : public ErrorCallback::Runnable {
79 public:
80 explicit SendCallbackHandler(UDPSocketWrapper* delegate,
81 const ErrorCallback& forward_callback);
82 ~SendCallbackHandler() override;
84 // ErrorCallback::Runnable implementation:
85 void Run(NetworkErrorPtr result) const override;
87 private:
88 // Because this callback is passed to a method of |socket_|, and |socket_|
89 // is owned by |delegate_|, it should be safe to assume that |delegate_| is
90 // valid if/when Run() is called.
91 UDPSocketWrapper* delegate_;
92 ErrorCallback forward_callback_;
95 struct ReceivedData {
96 ReceivedData();
97 ~ReceivedData();
99 NetworkErrorPtr result;
100 NetAddressPtr src_addr;
101 Array<uint8_t> data;
104 struct SendRequest {
105 SendRequest();
106 ~SendRequest();
108 NetAddressPtr dest_addr;
109 Array<uint8_t> data;
110 ErrorCallback callback;
113 // UDPSocketClient implementation:
114 void OnReceived(NetworkErrorPtr result,
115 NetAddressPtr src_addr,
116 Array<uint8_t> data) override;
118 void Initialize(uint32_t requested_max_pending_sends);
119 void OnNegotiateMaxPendingSendRequestsCompleted(uint32_t actual_size);
121 void OnSendToCompleted(NetworkErrorPtr result,
122 const ErrorCallback& forward_callback);
124 // Returns true if a send request in |send_requests_| has been processed.
125 bool ProcessNextSendRequest();
127 UDPSocketPtr socket_;
129 uint32_t max_receive_queue_size_;
131 // Owns all the objects that its elements point to.
132 std::queue<ReceivedData*> receive_queue_;
134 std::queue<ReceiveCallback> receive_requests_;
136 uint32_t max_pending_sends_;
137 uint32_t current_pending_sends_;
139 // Owns all the objects that its elements point to.
140 std::queue<SendRequest*> send_requests_;
143 } // namespace mojo
145 #endif // MOJO_SERVICES_NETWORK_PUBLIC_CPP_UDP_SOCKET_WRAPPER_H_