Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(219)

Side by Side Diff: mojo/services/public/cpp/network/udp_socket_wrapper.h

Issue 789243002: Restructure public side of network service. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Rebase Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #ifndef MOJO_SERVICES_PUBLIC_CPP_NETWORK_UDP_SOCKET_WRAPPER_H_
6 #define MOJO_SERVICES_PUBLIC_CPP_NETWORK_UDP_SOCKET_WRAPPER_H_
7
8 #include <queue>
9
10 #include "mojo/services/public/interfaces/network/udp_socket.mojom.h"
11
12 namespace mojo {
13
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;
26
27 explicit UDPSocketWrapper(UDPSocketPtr socket);
28
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);
38
39 ~UDPSocketWrapper() override;
40
41 void AllowAddressReuse(const ErrorCallback& callback);
42
43 void Bind(NetAddressPtr addr,
44 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback);
45
46 void SetSendBufferSize(uint32_t size, const ErrorCallback& callback);
47
48 void SetReceiveBufferSize(uint32_t size, const ErrorCallback& callback);
49
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);
55
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);
61
62 private:
63 class NegotiateCallbackHandler : public Callback<void(uint32_t)>::Runnable {
64 public:
65 explicit NegotiateCallbackHandler(UDPSocketWrapper* delegate);
66 ~NegotiateCallbackHandler() override;
67
68 // Callback<void(uint32_t)>::Runnable implementation:
69 void Run(uint32_t actual_size) const override;
70
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_;
76 };
77
78 class SendCallbackHandler : public ErrorCallback::Runnable {
79 public:
80 explicit SendCallbackHandler(UDPSocketWrapper* delegate,
81 const ErrorCallback& forward_callback);
82 ~SendCallbackHandler() override;
83
84 // ErrorCallback::Runnable implementation:
85 void Run(NetworkErrorPtr result) const override;
86
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_;
93 };
94
95 struct ReceivedData {
96 ReceivedData();
97 ~ReceivedData();
98
99 NetworkErrorPtr result;
100 NetAddressPtr src_addr;
101 Array<uint8_t> data;
102 };
103
104 struct SendRequest {
105 SendRequest();
106 ~SendRequest();
107
108 NetAddressPtr dest_addr;
109 Array<uint8_t> data;
110 ErrorCallback callback;
111 };
112
113 // UDPSocketClient implementation:
114 void OnReceived(NetworkErrorPtr result,
115 NetAddressPtr src_addr,
116 Array<uint8_t> data) override;
117
118 void Initialize(uint32_t requested_max_pending_sends);
119 void OnNegotiateMaxPendingSendRequestsCompleted(uint32_t actual_size);
120
121 void OnSendToCompleted(NetworkErrorPtr result,
122 const ErrorCallback& forward_callback);
123
124 // Returns true if a send request in |send_requests_| has been processed.
125 bool ProcessNextSendRequest();
126
127 UDPSocketPtr socket_;
128
129 uint32_t max_receive_queue_size_;
130
131 // Owns all the objects that its elements point to.
132 std::queue<ReceivedData*> receive_queue_;
133
134 std::queue<ReceiveCallback> receive_requests_;
135
136 uint32_t max_pending_sends_;
137 uint32_t current_pending_sends_;
138
139 // Owns all the objects that its elements point to.
140 std::queue<SendRequest*> send_requests_;
141 };
142
143 } // namespace mojo
144
145 #endif // MOJO_SERVICES_PUBLIC_CPP_NETWORK_UDP_SOCKET_WRAPPER_H_
OLDNEW
« no previous file with comments | « mojo/services/public/cpp/network/BUILD.gn ('k') | mojo/services/public/cpp/network/udp_socket_wrapper.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698