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/interface_impl.h"
17 #include "third_party/mojo/src/mojo/public/cpp/bindings/strong_binding.h"
21 class IOBufferWithSize
;
26 class UDPSocketImpl
: public UDPSocket
{
28 // The lifetime of a new UDPSocketImpl is bound to the connection associated
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
,
41 InterfaceRequest
<UDPSocketReceiver
>)>& callback
)
44 void Connect(NetAddressPtr remote_addr
,
45 const Callback
<void(NetworkErrorPtr
,
47 InterfaceRequest
<UDPSocketReceiver
>)>&
50 void SetSendBufferSize(
52 const Callback
<void(NetworkErrorPtr
)>& callback
) override
;
54 void SetReceiveBufferSize(
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
,
66 const Callback
<void(NetworkErrorPtr
)>& callback
) override
;
70 NOT_BOUND_OR_CONNECTED
,
75 struct PendingSendRequest
{
77 ~PendingSendRequest();
81 Callback
<void(NetworkErrorPtr
)> callback
;
85 void DoSendTo(NetAddressPtr addr
,
87 const Callback
<void(NetworkErrorPtr
)>& callback
);
89 void OnRecvFromCompleted(int net_result
);
90 void OnSendToCompleted(const Callback
<void(NetworkErrorPtr
)>& callback
,
93 bool IsBoundOrConnected() const {
94 return state_
== BOUND
|| state_
== CONNECTED
;
97 StrongBinding
<UDPSocket
> binding_
;
99 net::UDPSocket socket_
;
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
);
131 #endif // MOJO_SERVICES_NETWORK_UDP_SOCKET_IMPL_H_