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