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_
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/strong_binding.h"
20 class IOBufferWithSize
;
25 class UDPSocketImpl
: public UDPSocket
{
27 // The lifetime of a new UDPSocketImpl is bound to the connection associated
29 UDPSocketImpl(InterfaceRequest
<UDPSocket
> request
,
30 scoped_ptr
<mojo::AppRefCount
> app_refcount
);
31 ~UDPSocketImpl() override
;
33 // UDPSocket implementation.
34 void AllowAddressReuse(
35 const Callback
<void(NetworkErrorPtr
)>& callback
) override
;
37 void Bind(NetAddressPtr addr
,
38 const Callback
<void(NetworkErrorPtr
,
40 InterfaceRequest
<UDPSocketReceiver
>)>& callback
)
43 void Connect(NetAddressPtr remote_addr
,
44 const Callback
<void(NetworkErrorPtr
,
46 InterfaceRequest
<UDPSocketReceiver
>)>&
49 void SetSendBufferSize(
51 const Callback
<void(NetworkErrorPtr
)>& callback
) override
;
53 void SetReceiveBufferSize(
55 const Callback
<void(NetworkErrorPtr
)>& callback
) override
;
57 void NegotiateMaxPendingSendRequests(
58 uint32_t requested_size
,
59 const Callback
<void(uint32_t)>& callback
) override
;
61 void ReceiveMore(uint32_t datagram_number
) override
;
63 void SendTo(NetAddressPtr dest_addr
,
65 const Callback
<void(NetworkErrorPtr
)>& callback
) override
;
69 NOT_BOUND_OR_CONNECTED
,
74 struct PendingSendRequest
{
76 ~PendingSendRequest();
80 Callback
<void(NetworkErrorPtr
)> callback
;
84 void DoSendTo(NetAddressPtr addr
,
86 const Callback
<void(NetworkErrorPtr
)>& callback
);
88 void OnRecvFromCompleted(int net_result
);
89 void OnSendToCompleted(const Callback
<void(NetworkErrorPtr
)>& callback
,
92 bool IsBoundOrConnected() const {
93 return state_
== BOUND
|| state_
== CONNECTED
;
96 StrongBinding
<UDPSocket
> binding_
;
98 net::UDPSocket socket_
;
102 bool allow_address_reuse_
;
104 // Non-null when there is a pending RecvFrom operation on |socket_|.
105 scoped_refptr
<net::IOBuffer
> recvfrom_buffer_
;
106 // Non-null when there is a pending SendTo operation on |socket_|.
107 scoped_refptr
<net::IOBufferWithSize
> sendto_buffer_
;
109 // The address of the pending RecvFrom operation, if any.
110 net::IPEndPoint recvfrom_address_
;
112 // The interface which gets data from fulfilled receive requests.
113 UDPSocketReceiverPtr receiver_
;
115 // How many more packets the client side expects to receive.
116 size_t remaining_recv_slots_
;
118 // The queue owns the PendingSendRequest instances.
119 std::deque
<PendingSendRequest
*> pending_send_requests_
;
120 // The maximum size of the |pending_send_requests_| queue.
121 size_t max_pending_send_requests_
;
123 scoped_ptr
<mojo::AppRefCount
> app_refcount_
;
125 DISALLOW_COPY_AND_ASSIGN(UDPSocketImpl
);
130 #endif // MOJO_SERVICES_NETWORK_UDP_SOCKET_IMPL_H_