Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / mojo / services / network / udp_socket_impl.h
blobb1056fc9a000af71e5da8b0a5d6cac260aa05b01
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_UDP_SOCKET_IMPL_H_
6 #define MOJO_SERVICES_NETWORK_UDP_SOCKET_IMPL_H_
8 #include <deque>
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "mojo/application/public/cpp/app_lifetime_helper.h"
13 #include "mojo/services/network/public/interfaces/udp_socket.mojom.h"
14 #include "net/base/ip_endpoint.h"
15 #include "net/udp/udp_socket.h"
16 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_impl.h"
17 #include "third_party/mojo/src/mojo/public/cpp/bindings/strong_binding.h"
19 namespace net {
20 class IOBuffer;
21 class IOBufferWithSize;
24 namespace mojo {
26 class UDPSocketImpl : public UDPSocket {
27 public:
28 // The lifetime of a new UDPSocketImpl is bound to the connection associated
29 // with |request|.
30 UDPSocketImpl(InterfaceRequest<UDPSocket> request,
31 scoped_ptr<mojo::AppRefCount> app_refcount);
32 ~UDPSocketImpl() override;
34 // UDPSocket implementation.
35 void AllowAddressReuse(
36 const Callback<void(NetworkErrorPtr)>& callback) override;
38 void Bind(NetAddressPtr addr,
39 const Callback<void(NetworkErrorPtr,
40 NetAddressPtr,
41 InterfaceRequest<UDPSocketReceiver>)>& callback)
42 override;
44 void Connect(NetAddressPtr remote_addr,
45 const Callback<void(NetworkErrorPtr,
46 NetAddressPtr,
47 InterfaceRequest<UDPSocketReceiver>)>&
48 callback) override;
50 void SetSendBufferSize(
51 uint32_t size,
52 const Callback<void(NetworkErrorPtr)>& callback) override;
54 void SetReceiveBufferSize(
55 uint32_t size,
56 const Callback<void(NetworkErrorPtr)>& callback) override;
58 void NegotiateMaxPendingSendRequests(
59 uint32_t requested_size,
60 const Callback<void(uint32_t)>& callback) override;
62 void ReceiveMore(uint32_t datagram_number) override;
64 void SendTo(NetAddressPtr dest_addr,
65 Array<uint8_t> data,
66 const Callback<void(NetworkErrorPtr)>& callback) override;
68 private:
69 enum State {
70 NOT_BOUND_OR_CONNECTED,
71 BOUND,
72 CONNECTED
75 struct PendingSendRequest {
76 PendingSendRequest();
77 ~PendingSendRequest();
79 NetAddressPtr addr;
80 Array<uint8_t> data;
81 Callback<void(NetworkErrorPtr)> callback;
84 void DoRecvFrom();
85 void DoSendTo(NetAddressPtr addr,
86 Array<uint8_t> data,
87 const Callback<void(NetworkErrorPtr)>& callback);
89 void OnRecvFromCompleted(int net_result);
90 void OnSendToCompleted(const Callback<void(NetworkErrorPtr)>& callback,
91 int net_result);
93 bool IsBoundOrConnected() const {
94 return state_ == BOUND || state_ == CONNECTED;
97 StrongBinding<UDPSocket> binding_;
99 net::UDPSocket socket_;
101 State state_;
103 bool allow_address_reuse_;
105 // Non-null when there is a pending RecvFrom operation on |socket_|.
106 scoped_refptr<net::IOBuffer> recvfrom_buffer_;
107 // Non-null when there is a pending SendTo operation on |socket_|.
108 scoped_refptr<net::IOBufferWithSize> sendto_buffer_;
110 // The address of the pending RecvFrom operation, if any.
111 net::IPEndPoint recvfrom_address_;
113 // The interface which gets data from fulfilled receive requests.
114 UDPSocketReceiverPtr receiver_;
116 // How many more packets the client side expects to receive.
117 size_t remaining_recv_slots_;
119 // The queue owns the PendingSendRequest instances.
120 std::deque<PendingSendRequest*> pending_send_requests_;
121 // The maximum size of the |pending_send_requests_| queue.
122 size_t max_pending_send_requests_;
124 scoped_ptr<mojo::AppRefCount> app_refcount_;
126 DISALLOW_COPY_AND_ASSIGN(UDPSocketImpl);
129 } // namespace mojo
131 #endif // MOJO_SERVICES_NETWORK_UDP_SOCKET_IMPL_H_