| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_SERVICES_NETWORK_UDP_SOCKET_IMPL_H_ | 5 #ifndef MOJO_SERVICES_NETWORK_UDP_SOCKET_IMPL_H_ |
| 6 #define MOJO_SERVICES_NETWORK_UDP_SOCKET_IMPL_H_ | 6 #define MOJO_SERVICES_NETWORK_UDP_SOCKET_IMPL_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "mojo/public/cpp/bindings/interface_impl.h" | 12 #include "mojo/public/cpp/bindings/interface_impl.h" |
| 13 #include "mojo/services/network/public/interfaces/udp_socket.mojom.h" | 13 #include "mojo/services/network/public/interfaces/udp_socket.mojom.h" |
| 14 #include "net/base/ip_endpoint.h" | 14 #include "net/base/ip_endpoint.h" |
| 15 #include "net/udp/udp_server_socket.h" | 15 #include "net/udp/udp_socket.h" |
| 16 | 16 |
| 17 namespace net { | 17 namespace net { |
| 18 class IOBuffer; | 18 class IOBuffer; |
| 19 class IOBufferWithSize; | 19 class IOBufferWithSize; |
| 20 } | 20 } |
| 21 | 21 |
| 22 namespace mojo { | 22 namespace mojo { |
| 23 | 23 |
| 24 class UDPSocketImpl : public InterfaceImpl<UDPSocket> { | 24 class UDPSocketImpl : public InterfaceImpl<UDPSocket> { |
| 25 public: | 25 public: |
| (...skipping 24 matching lines...) Expand all Loading... |
| 50 uint32_t requested_size, | 50 uint32_t requested_size, |
| 51 const Callback<void(uint32_t)>& callback) override; | 51 const Callback<void(uint32_t)>& callback) override; |
| 52 | 52 |
| 53 void ReceiveMore(uint32_t datagram_number) override; | 53 void ReceiveMore(uint32_t datagram_number) override; |
| 54 | 54 |
| 55 void SendTo(NetAddressPtr dest_addr, | 55 void SendTo(NetAddressPtr dest_addr, |
| 56 Array<uint8_t> data, | 56 Array<uint8_t> data, |
| 57 const Callback<void(NetworkErrorPtr)>& callback) override; | 57 const Callback<void(NetworkErrorPtr)>& callback) override; |
| 58 | 58 |
| 59 private: | 59 private: |
| 60 enum State { |
| 61 NOT_BOUND_OR_CONNECTED, |
| 62 BOUND, |
| 63 CONNECTED |
| 64 }; |
| 65 |
| 60 struct PendingSendRequest { | 66 struct PendingSendRequest { |
| 61 PendingSendRequest(); | 67 PendingSendRequest(); |
| 62 ~PendingSendRequest(); | 68 ~PendingSendRequest(); |
| 63 | 69 |
| 64 NetAddressPtr addr; | 70 NetAddressPtr addr; |
| 65 Array<uint8_t> data; | 71 Array<uint8_t> data; |
| 66 Callback<void(NetworkErrorPtr)> callback; | 72 Callback<void(NetworkErrorPtr)> callback; |
| 67 }; | 73 }; |
| 68 | 74 |
| 69 void DoRecvFrom(); | 75 void DoRecvFrom(); |
| 70 void DoSendTo(NetAddressPtr addr, | 76 void DoSendTo(NetAddressPtr addr, |
| 71 Array<uint8_t> data, | 77 Array<uint8_t> data, |
| 72 const Callback<void(NetworkErrorPtr)>& callback); | 78 const Callback<void(NetworkErrorPtr)>& callback); |
| 73 | 79 |
| 74 void OnRecvFromCompleted(int net_result); | 80 void OnRecvFromCompleted(int net_result); |
| 75 void OnSendToCompleted(const Callback<void(NetworkErrorPtr)>& callback, | 81 void OnSendToCompleted(const Callback<void(NetworkErrorPtr)>& callback, |
| 76 int net_result); | 82 int net_result); |
| 77 | 83 |
| 78 net::UDPServerSocket socket_; | 84 bool IsBoundOrConnected() const { |
| 85 return state_ == BOUND || state_ == CONNECTED; |
| 86 } |
| 79 | 87 |
| 80 bool bound_; | 88 net::UDPSocket socket_; |
| 81 | 89 |
| 82 // Non-NULL when there is a pending RecvFrom operation on |socket_|. | 90 State state_; |
| 91 |
| 92 bool allow_address_reuse_; |
| 93 |
| 94 // Non-null when there is a pending RecvFrom operation on |socket_|. |
| 83 scoped_refptr<net::IOBuffer> recvfrom_buffer_; | 95 scoped_refptr<net::IOBuffer> recvfrom_buffer_; |
| 84 // Non-NULL when there is a pending SendTo operation on |socket_|. | 96 // Non-null when there is a pending SendTo operation on |socket_|. |
| 85 scoped_refptr<net::IOBufferWithSize> sendto_buffer_; | 97 scoped_refptr<net::IOBufferWithSize> sendto_buffer_; |
| 86 | 98 |
| 87 net::IPEndPoint recvfrom_address_; | 99 net::IPEndPoint recvfrom_address_; |
| 88 | 100 |
| 89 // How many more packets the client side expects to receive. | 101 // How many more packets the client side expects to receive. |
| 90 size_t remaining_recv_slots_; | 102 size_t remaining_recv_slots_; |
| 91 | 103 |
| 92 // The queue owns the PendingSendRequest instances. | 104 // The queue owns the PendingSendRequest instances. |
| 93 std::deque<PendingSendRequest*> pending_send_requests_; | 105 std::deque<PendingSendRequest*> pending_send_requests_; |
| 94 // The maximum size of the |pending_send_requests_| queue. | 106 // The maximum size of the |pending_send_requests_| queue. |
| 95 size_t max_pending_send_requests_; | 107 size_t max_pending_send_requests_; |
| 96 | 108 |
| 97 DISALLOW_COPY_AND_ASSIGN(UDPSocketImpl); | 109 DISALLOW_COPY_AND_ASSIGN(UDPSocketImpl); |
| 98 }; | 110 }; |
| 99 | 111 |
| 100 } // namespace mojo | 112 } // namespace mojo |
| 101 | 113 |
| 102 #endif // MOJO_SERVICES_NETWORK_UDP_SOCKET_IMPL_H_ | 114 #endif // MOJO_SERVICES_NETWORK_UDP_SOCKET_IMPL_H_ |
| OLD | NEW |