Update mojo sdk to rev 59145288bae55b0fce4276b017df6a1117bcf00f
[chromium-blink-merge.git] / mojo / services / network / udp_socket_impl.h
blob81951da756aa5f746a7078115190544331c267f7
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/public/cpp/bindings/interface_impl.h"
13 #include "mojo/services/network/public/interfaces/udp_socket.mojom.h"
14 #include "net/base/ip_endpoint.h"
15 #include "net/udp/udp_server_socket.h"
17 namespace net {
18 class IOBuffer;
19 class IOBufferWithSize;
22 namespace mojo {
24 class UDPSocketImpl : public InterfaceImpl<UDPSocket> {
25 public:
26 UDPSocketImpl();
27 ~UDPSocketImpl() override;
29 // UDPSocket implementation.
30 void AllowAddressReuse(
31 const Callback<void(NetworkErrorPtr)>& callback) override;
33 void Bind(
34 NetAddressPtr addr,
35 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback) override;
37 void SetSendBufferSize(
38 uint32_t size,
39 const Callback<void(NetworkErrorPtr)>& callback) override;
41 void SetReceiveBufferSize(
42 uint32_t size,
43 const Callback<void(NetworkErrorPtr)>& callback) override;
45 void NegotiateMaxPendingSendRequests(
46 uint32_t requested_size,
47 const Callback<void(uint32_t)>& callback) override;
49 void ReceiveMore(uint32_t datagram_number) override;
51 void SendTo(NetAddressPtr dest_addr,
52 Array<uint8_t> data,
53 const Callback<void(NetworkErrorPtr)>& callback) override;
55 private:
56 struct PendingSendRequest {
57 PendingSendRequest();
58 ~PendingSendRequest();
60 NetAddressPtr addr;
61 Array<uint8_t> data;
62 Callback<void(NetworkErrorPtr)> callback;
65 void DoRecvFrom();
66 void DoSendTo(NetAddressPtr addr,
67 Array<uint8_t> data,
68 const Callback<void(NetworkErrorPtr)>& callback);
70 void OnRecvFromCompleted(int net_result);
71 void OnSendToCompleted(const Callback<void(NetworkErrorPtr)>& callback,
72 int net_result);
74 net::UDPServerSocket socket_;
76 bool bound_;
78 // Non-NULL when there is a pending RecvFrom operation on |socket_|.
79 scoped_refptr<net::IOBuffer> recvfrom_buffer_;
80 // Non-NULL when there is a pending SendTo operation on |socket_|.
81 scoped_refptr<net::IOBufferWithSize> sendto_buffer_;
83 net::IPEndPoint recvfrom_address_;
85 // How many more packets the client side expects to receive.
86 size_t remaining_recv_slots_;
88 // The queue owns the PendingSendRequest instances.
89 std::deque<PendingSendRequest*> pending_send_requests_;
90 // The maximum size of the |pending_send_requests_| queue.
91 size_t max_pending_send_requests_;
93 DISALLOW_COPY_AND_ASSIGN(UDPSocketImpl);
96 } // namespace mojo
98 #endif // MOJO_SERVICES_NETWORK_UDP_SOCKET_IMPL_H_