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_
10 #include "mojo/services/network/public/interfaces/udp_socket.mojom.h"
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
{
23 typedef Callback
<void(NetworkErrorPtr
, NetAddressPtr
, Array
<uint8_t>)>
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
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
,
60 const ErrorCallback
& callback
);
63 class NegotiateCallbackHandler
: public Callback
<void(uint32_t)>::Runnable
{
65 explicit NegotiateCallbackHandler(UDPSocketWrapper
* delegate
);
66 ~NegotiateCallbackHandler() override
;
68 // Callback<void(uint32_t)>::Runnable implementation:
69 void Run(uint32_t actual_size
) const override
;
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
{
80 explicit SendCallbackHandler(UDPSocketWrapper
* delegate
,
81 const ErrorCallback
& forward_callback
);
82 ~SendCallbackHandler() override
;
84 // ErrorCallback::Runnable implementation:
85 void Run(NetworkErrorPtr result
) const override
;
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_
;
99 NetworkErrorPtr result
;
100 NetAddressPtr src_addr
;
108 NetAddressPtr dest_addr
;
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_
;
145 #endif // MOJO_SERVICES_NETWORK_PUBLIC_CPP_UDP_SOCKET_WRAPPER_H_