OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef MOJO_SERVICES_NETWORK_UDP_SOCKET_IMPL_H_ | |
6 #define MOJO_SERVICES_NETWORK_UDP_SOCKET_IMPL_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <deque> | |
12 | |
13 #include "base/macros.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "mojo/public/cpp/bindings/strong_binding.h" | |
16 #include "mojo/services/network/public/interfaces/udp_socket.mojom.h" | |
17 #include "mojo/shell/public/cpp/message_loop_ref.h" | |
18 #include "net/base/ip_endpoint.h" | |
19 #include "net/udp/udp_socket.h" | |
20 | |
21 namespace net { | |
22 class IOBuffer; | |
23 class IOBufferWithSize; | |
24 } | |
25 | |
26 namespace mojo { | |
27 | |
28 class UDPSocketImpl : public UDPSocket { | |
29 public: | |
30 // The lifetime of a new UDPSocketImpl is bound to the connection associated | |
31 // with |request|. | |
32 UDPSocketImpl(InterfaceRequest<UDPSocket> request, | |
33 scoped_ptr<mojo::MessageLoopRef> app_refcount); | |
34 ~UDPSocketImpl() override; | |
35 | |
36 // UDPSocket implementation. | |
37 void AllowAddressReuse( | |
38 const Callback<void(NetworkErrorPtr)>& callback) override; | |
39 | |
40 void Bind(NetAddressPtr addr, | |
41 const Callback<void(NetworkErrorPtr, | |
42 NetAddressPtr, | |
43 InterfaceRequest<UDPSocketReceiver>)>& callback) | |
44 override; | |
45 | |
46 void Connect(NetAddressPtr remote_addr, | |
47 const Callback<void(NetworkErrorPtr, | |
48 NetAddressPtr, | |
49 InterfaceRequest<UDPSocketReceiver>)>& | |
50 callback) override; | |
51 | |
52 void SetSendBufferSize( | |
53 uint32_t size, | |
54 const Callback<void(NetworkErrorPtr)>& callback) override; | |
55 | |
56 void SetReceiveBufferSize( | |
57 uint32_t size, | |
58 const Callback<void(NetworkErrorPtr)>& callback) override; | |
59 | |
60 void NegotiateMaxPendingSendRequests( | |
61 uint32_t requested_size, | |
62 const Callback<void(uint32_t)>& callback) override; | |
63 | |
64 void ReceiveMore(uint32_t datagram_number) override; | |
65 | |
66 void SendTo(NetAddressPtr dest_addr, | |
67 Array<uint8_t> data, | |
68 const Callback<void(NetworkErrorPtr)>& callback) override; | |
69 | |
70 private: | |
71 enum State { | |
72 NOT_BOUND_OR_CONNECTED, | |
73 BOUND, | |
74 CONNECTED | |
75 }; | |
76 | |
77 struct PendingSendRequest { | |
78 PendingSendRequest(); | |
79 ~PendingSendRequest(); | |
80 | |
81 NetAddressPtr addr; | |
82 Array<uint8_t> data; | |
83 Callback<void(NetworkErrorPtr)> callback; | |
84 }; | |
85 | |
86 void DoRecvFrom(); | |
87 void DoSendTo(NetAddressPtr addr, | |
88 Array<uint8_t> data, | |
89 const Callback<void(NetworkErrorPtr)>& callback); | |
90 | |
91 void OnRecvFromCompleted(int net_result); | |
92 void OnSendToCompleted(const Callback<void(NetworkErrorPtr)>& callback, | |
93 int net_result); | |
94 | |
95 bool IsBoundOrConnected() const { | |
96 return state_ == BOUND || state_ == CONNECTED; | |
97 } | |
98 | |
99 StrongBinding<UDPSocket> binding_; | |
100 | |
101 net::UDPSocket socket_; | |
102 | |
103 State state_; | |
104 | |
105 bool allow_address_reuse_; | |
106 | |
107 // Non-null when there is a pending RecvFrom operation on |socket_|. | |
108 scoped_refptr<net::IOBuffer> recvfrom_buffer_; | |
109 // Non-null when there is a pending SendTo operation on |socket_|. | |
110 scoped_refptr<net::IOBufferWithSize> sendto_buffer_; | |
111 | |
112 // The address of the pending RecvFrom operation, if any. | |
113 net::IPEndPoint recvfrom_address_; | |
114 | |
115 // The interface which gets data from fulfilled receive requests. | |
116 UDPSocketReceiverPtr receiver_; | |
117 | |
118 // How many more packets the client side expects to receive. | |
119 size_t remaining_recv_slots_; | |
120 | |
121 // The queue owns the PendingSendRequest instances. | |
122 std::deque<PendingSendRequest*> pending_send_requests_; | |
123 // The maximum size of the |pending_send_requests_| queue. | |
124 size_t max_pending_send_requests_; | |
125 | |
126 scoped_ptr<mojo::MessageLoopRef> app_refcount_; | |
127 | |
128 DISALLOW_COPY_AND_ASSIGN(UDPSocketImpl); | |
129 }; | |
130 | |
131 } // namespace mojo | |
132 | |
133 #endif // MOJO_SERVICES_NETWORK_UDP_SOCKET_IMPL_H_ | |
OLD | NEW |