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

Unified Diff: mojo/services/public/cpp/network/udp_socket_wrapper.h

Issue 664243002: Mojo UDP: add a client-side wrapper which handles the client<->server buffering (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git/+/master
Patch Set: Created 6 years, 2 months 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 side-by-side diff with in-line comments
Download patch
Index: mojo/services/public/cpp/network/udp_socket_wrapper.h
diff --git a/mojo/services/public/cpp/network/udp_socket_wrapper.h b/mojo/services/public/cpp/network/udp_socket_wrapper.h
new file mode 100644
index 0000000000000000000000000000000000000000..48b3eb9bb6c6fc0c620723862e0e6e2718d84183
--- /dev/null
+++ b/mojo/services/public/cpp/network/udp_socket_wrapper.h
@@ -0,0 +1,138 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MOJO_SERVICES_PUBLIC_CPP_NETWORK_UDP_SOCKET_WRAPPER_H_
+#define MOJO_SERVICES_PUBLIC_CPP_NETWORK_UDP_SOCKET_WRAPPER_H_
+
+#include <queue>
+
+#include "mojo/services/public/interfaces/network/udp_socket.mojom.h"
+
+namespace mojo {
+
+class UDPSocketWrapper : public UDPSocketClient {
brettw 2014/10/21 18:21:33 It seems like this should have an extensive commen
yzshen1 2014/10/21 21:59:30 Done.
+ public:
+ typedef Callback<void(NetworkErrorPtr, NetAddressPtr, Array<uint8_t>)>
+ ReceiveCallback;
+ typedef Callback<void(NetworkErrorPtr)> ErrorCallback;
+
+ explicit UDPSocketWrapper(UDPSocketPtr socket);
+
+ // |receive_queue_slots| determines the size (in datagrams) of the local
+ // receive queue, which caches incoming datagrams.
+ // |requested_max_pending_sends| is used to call
+ // NegotiateMaxPendingSendRequests() on |socket|.
+ // The two numbers should be greater than 0. If you would like to use default
+ // values, please use the other constructor.
+ UDPSocketWrapper(UDPSocketPtr socket,
+ uint32_t receive_queue_slots,
+ uint32_t requested_max_pending_sends);
+
+ virtual ~UDPSocketWrapper();
+
+ void AllowAddressReuse(const ErrorCallback& callback);
+
+ void Bind(NetAddressPtr addr,
+ const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback);
+
+ void SetSendBufferSize(uint32_t size, const ErrorCallback& callback);
+
+ void SetReceiveBufferSize(uint32_t size, const ErrorCallback& callback);
+
+ // If there are already incoming datagrams cached locally, this method runs
+ // |callback| before it returns, and the return value is set to true.
+ // Otherwise, the return value is set to false and the callback will be run
+ // asynchronously.
+ bool ReceiveFrom(const ReceiveCallback& callback);
+
+ // This method is aware of the max pending send requests allowed by the
+ // service, and caches send requests locally if necessary.
+ void SendTo(NetAddressPtr dest_addr,
+ Array<uint8_t> data,
+ const ErrorCallback& callback);
+
+ private:
+ class NegotiateCallbackHandler : public Callback<void(uint32_t)>::Runnable {
+ public:
+ explicit NegotiateCallbackHandler(UDPSocketWrapper* delegate);
+ virtual ~NegotiateCallbackHandler();
+
+ // Callback<void(uint32_t)>::Runnable implementation:
+ virtual void Run(uint32_t actual_size) const override;
+
+ private:
+ // Because this callback is passed to a method of |socket_|, and |socket_|
+ // is owned by |delegate_|, it should be safe to assume that |delegate_| is
+ // valid if/when Run() is called.
+ UDPSocketWrapper* delegate_;
+ };
+
+ class SendCallbackHandler : public ErrorCallback::Runnable {
+ public:
+ explicit SendCallbackHandler(UDPSocketWrapper* delegate,
+ const ErrorCallback& forward_callback);
+ virtual ~SendCallbackHandler();
+
+ // ErrorCallback::Runnable implementation:
+ virtual void Run(NetworkErrorPtr result) const override;
+
+ private:
+ // Because this callback is passed to a method of |socket_|, and |socket_|
+ // is owned by |delegate_|, it should be safe to assume that |delegate_| is
+ // valid if/when Run() is called.
+ UDPSocketWrapper* delegate_;
+ ErrorCallback forward_callback_;
+ };
+
+ struct ReceivedData {
+ ReceivedData();
+ ~ReceivedData();
+
+ NetworkErrorPtr result;
+ NetAddressPtr src_addr;
+ Array<uint8_t> data;
+ };
+
+ struct SendRequest {
+ SendRequest();
+ ~SendRequest();
+
+ NetAddressPtr dest_addr;
+ Array<uint8_t> data;
+ ErrorCallback callback;
+ };
+
+ // UDPSocketClient implementation:
+ virtual void OnReceived(NetworkErrorPtr result,
+ NetAddressPtr src_addr,
+ Array<uint8_t> data) override;
+
+ void Initialize(uint32_t requested_max_pending_sends);
+ void OnNegotiateMaxPendingSendRequestsCompleted(uint32_t actual_size);
+
+ void OnSendToCompleted(NetworkErrorPtr result,
+ const ErrorCallback& forward_callback);
+
+ // Returns true if a send request in |send_requests_| has been processed.
+ bool ProcessNextSendRequest();
+
+ UDPSocketPtr socket_;
+
+ uint32_t max_receive_queue_size_;
+
+ // Owns all the objects that its elements point to.
+ std::queue<ReceivedData*> receive_queue_;
+
+ std::queue<ReceiveCallback> receive_requests_;
+
+ uint32_t max_pending_sends_;
+ uint32_t current_pending_sends_;
+
+ // Owns all the objects that its elements point to.
+ std::queue<SendRequest*> send_requests_;
+};
+
+} // namespace mojo
+
+#endif // MOJO_SERVICES_PUBLIC_CPP_NETWORK_UDP_SOCKET_WRAPPER_H_

Powered by Google App Engine
This is Rietveld 408576698