1 // Copyright 2011 The Chromium Authors 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 NET_SOCKET_UDP_CLIENT_SOCKET_H_ 6 #define NET_SOCKET_UDP_CLIENT_SOCKET_H_ 7 8 #include <stdint.h> 9 10 #include "net/base/net_export.h" 11 #include "net/socket/datagram_client_socket.h" 12 #include "net/socket/socket_descriptor.h" 13 #include "net/socket/udp_socket.h" 14 #include "net/traffic_annotation/network_traffic_annotation.h" 15 16 namespace net { 17 18 class NetLog; 19 struct NetLogSource; 20 21 // A client socket that uses UDP as the transport layer. 22 class NET_EXPORT_PRIVATE UDPClientSocket : public DatagramClientSocket { 23 public: 24 // If `network` is specified, the socket will be bound to it. All data traffic 25 // on the socket will be sent and received via `network`. Communication using 26 // this socket will fail if `network` disconnects. 27 UDPClientSocket( 28 DatagramSocket::BindType bind_type, 29 net::NetLog* net_log, 30 const net::NetLogSource& source, 31 handles::NetworkHandle network = handles::kInvalidNetworkHandle); 32 33 UDPClientSocket( 34 DatagramSocket::BindType bind_type, 35 NetLogWithSource source_net_log, 36 handles::NetworkHandle network = handles::kInvalidNetworkHandle); 37 38 UDPClientSocket(const UDPClientSocket&) = delete; 39 UDPClientSocket& operator=(const UDPClientSocket&) = delete; 40 41 ~UDPClientSocket() override; 42 43 // DatagramClientSocket implementation. 44 int Connect(const IPEndPoint& address) override; 45 int ConnectUsingNetwork(handles::NetworkHandle network, 46 const IPEndPoint& address) override; 47 int ConnectUsingDefaultNetwork(const IPEndPoint& address) override; 48 int ConnectAsync(const IPEndPoint& address, 49 CompletionOnceCallback callback) override; 50 int ConnectUsingNetworkAsync(handles::NetworkHandle network, 51 const IPEndPoint& address, 52 CompletionOnceCallback callback) override; 53 int ConnectUsingDefaultNetworkAsync(const IPEndPoint& address, 54 CompletionOnceCallback callback) override; 55 56 handles::NetworkHandle GetBoundNetwork() const override; 57 void ApplySocketTag(const SocketTag& tag) override; 58 int Read(IOBuffer* buf, 59 int buf_len, 60 CompletionOnceCallback callback) override; 61 int Write(IOBuffer* buf, 62 int buf_len, 63 CompletionOnceCallback callback, 64 const NetworkTrafficAnnotationTag& traffic_annotation) override; 65 66 void Close() override; 67 int GetPeerAddress(IPEndPoint* address) const override; 68 int GetLocalAddress(IPEndPoint* address) const override; 69 // Switch to use non-blocking IO. Must be called right after construction and 70 // before other calls. 71 void UseNonBlockingIO() override; 72 int SetReceiveBufferSize(int32_t size) override; 73 int SetSendBufferSize(int32_t size) override; 74 int SetDoNotFragment() override; 75 int SetRecvEcn() override; 76 void SetMsgConfirm(bool confirm) override; 77 const NetLogWithSource& NetLog() const override; 78 void EnableRecvOptimization() override; 79 80 int SetMulticastInterface(uint32_t interface_index) override; 81 void SetIOSNetworkServiceType(int ios_network_service_type) override; 82 83 // Takes ownership of an opened but unconnected and unbound `socket`. This 84 // method must be called after UseNonBlockingIO, otherwise the adopted socket 85 // will not have the non-blocking IO flag set. 86 int AdoptOpenedSocket(AddressFamily address_family, SocketDescriptor socket); 87 get_multicast_interface_for_testing()88 uint32_t get_multicast_interface_for_testing() { 89 return socket_.get_multicast_interface_for_testing(); 90 } 91 #if !BUILDFLAG(IS_WIN) get_msg_confirm_for_testing()92 bool get_msg_confirm_for_testing() { 93 return socket_.get_msg_confirm_for_testing(); 94 } get_recv_optimization_for_testing()95 bool get_recv_optimization_for_testing() { 96 return socket_.get_experimental_recv_optimization_enabled_for_testing(); 97 } 98 #endif 99 #if BUILDFLAG(IS_WIN) get_use_non_blocking_io_for_testing()100 bool get_use_non_blocking_io_for_testing() { 101 return socket_.get_use_non_blocking_io_for_testing(); 102 } 103 #endif 104 105 private: 106 NetLogWithSource net_log_; 107 UDPSocket socket_; 108 bool adopted_opened_socket_ = false; 109 bool connect_called_ = false; 110 // The network the socket is currently bound to. 111 handles::NetworkHandle network_ = handles::kInvalidNetworkHandle; 112 handles::NetworkHandle connect_using_network_; 113 }; 114 115 } // namespace net 116 117 #endif // NET_SOCKET_UDP_CLIENT_SOCKET_H_ 118