Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / mojo / services / network / udp_socket_impl.h
blobc2070bf54cf6edb5c58a80f6a3acdc3690f517bc
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/services/network/public/interfaces/udp_socket.mojom.h"
13 #include "net/base/ip_endpoint.h"
14 #include "net/udp/udp_socket.h"
15 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_impl.h"
16 #include "third_party/mojo/src/mojo/public/cpp/bindings/strong_binding.h"
18 namespace net {
19 class IOBuffer;
20 class IOBufferWithSize;
23 namespace mojo {
25 class UDPSocketImpl : public UDPSocket {
26 public:
27 // The lifetime of a new UDPSocketImpl is bound to the connection associated
28 // with |request|.
29 explicit UDPSocketImpl(InterfaceRequest<UDPSocket> request);
30 ~UDPSocketImpl() override;
32 // UDPSocket implementation.
33 void AllowAddressReuse(
34 const Callback<void(NetworkErrorPtr)>& callback) override;
36 void Bind(NetAddressPtr addr,
37 const Callback<void(NetworkErrorPtr,
38 NetAddressPtr,
39 InterfaceRequest<UDPSocketReceiver>)>& callback)
40 override;
42 void Connect(NetAddressPtr remote_addr,
43 const Callback<void(NetworkErrorPtr,
44 NetAddressPtr,
45 InterfaceRequest<UDPSocketReceiver>)>&
46 callback) override;
48 void SetSendBufferSize(
49 uint32_t size,
50 const Callback<void(NetworkErrorPtr)>& callback) override;
52 void SetReceiveBufferSize(
53 uint32_t size,
54 const Callback<void(NetworkErrorPtr)>& callback) override;
56 void NegotiateMaxPendingSendRequests(
57 uint32_t requested_size,
58 const Callback<void(uint32_t)>& callback) override;
60 void ReceiveMore(uint32_t datagram_number) override;
62 void SendTo(NetAddressPtr dest_addr,
63 Array<uint8_t> data,
64 const Callback<void(NetworkErrorPtr)>& callback) override;
66 private:
67 enum State {
68 NOT_BOUND_OR_CONNECTED,
69 BOUND,
70 CONNECTED
73 struct PendingSendRequest {
74 PendingSendRequest();
75 ~PendingSendRequest();
77 NetAddressPtr addr;
78 Array<uint8_t> data;
79 Callback<void(NetworkErrorPtr)> callback;
82 void DoRecvFrom();
83 void DoSendTo(NetAddressPtr addr,
84 Array<uint8_t> data,
85 const Callback<void(NetworkErrorPtr)>& callback);
87 void OnRecvFromCompleted(int net_result);
88 void OnSendToCompleted(const Callback<void(NetworkErrorPtr)>& callback,
89 int net_result);
91 bool IsBoundOrConnected() const {
92 return state_ == BOUND || state_ == CONNECTED;
95 StrongBinding<UDPSocket> binding_;
97 net::UDPSocket socket_;
99 State state_;
101 bool allow_address_reuse_;
103 // Non-null when there is a pending RecvFrom operation on |socket_|.
104 scoped_refptr<net::IOBuffer> recvfrom_buffer_;
105 // Non-null when there is a pending SendTo operation on |socket_|.
106 scoped_refptr<net::IOBufferWithSize> sendto_buffer_;
108 // The address of the pending RecvFrom operation, if any.
109 net::IPEndPoint recvfrom_address_;
111 // The interface which gets data from fulfilled receive requests.
112 UDPSocketReceiverPtr receiver_;
114 // How many more packets the client side expects to receive.
115 size_t remaining_recv_slots_;
117 // The queue owns the PendingSendRequest instances.
118 std::deque<PendingSendRequest*> pending_send_requests_;
119 // The maximum size of the |pending_send_requests_| queue.
120 size_t max_pending_send_requests_;
122 DISALLOW_COPY_AND_ASSIGN(UDPSocketImpl);
125 } // namespace mojo
127 #endif // MOJO_SERVICES_NETWORK_UDP_SOCKET_IMPL_H_