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(const UDPClientSocket&) = delete; 34 UDPClientSocket& operator=(const UDPClientSocket&) = delete; 35 36 ~UDPClientSocket() override; 37 38 // DatagramClientSocket implementation. 39 int Connect(const IPEndPoint& address) override; 40 int ConnectUsingNetwork(handles::NetworkHandle network, 41 const IPEndPoint& address) override; 42 int ConnectUsingDefaultNetwork(const IPEndPoint& address) override; 43 int ConnectAsync(const IPEndPoint& address, 44 CompletionOnceCallback callback) override; 45 int ConnectUsingNetworkAsync(handles::NetworkHandle network, 46 const IPEndPoint& address, 47 CompletionOnceCallback callback) override; 48 int ConnectUsingDefaultNetworkAsync(const IPEndPoint& address, 49 CompletionOnceCallback callback) override; 50 51 handles::NetworkHandle GetBoundNetwork() const override; 52 void ApplySocketTag(const SocketTag& tag) override; 53 int Read(IOBuffer* buf, 54 int buf_len, 55 CompletionOnceCallback callback) override; 56 int Write(IOBuffer* buf, 57 int buf_len, 58 CompletionOnceCallback callback, 59 const NetworkTrafficAnnotationTag& traffic_annotation) override; 60 61 void Close() override; 62 int GetPeerAddress(IPEndPoint* address) const override; 63 int GetLocalAddress(IPEndPoint* address) const override; 64 // Switch to use non-blocking IO. Must be called right after construction and 65 // before other calls. 66 void UseNonBlockingIO() override; 67 int SetReceiveBufferSize(int32_t size) override; 68 int SetSendBufferSize(int32_t size) override; 69 int SetDoNotFragment() override; 70 void SetMsgConfirm(bool confirm) override; 71 const NetLogWithSource& NetLog() const override; 72 void EnableRecvOptimization() override; 73 74 int SetMulticastInterface(uint32_t interface_index) override; 75 void SetIOSNetworkServiceType(int ios_network_service_type) override; 76 77 // Takes ownership of an opened but unconnected and unbound `socket`. 78 void AdoptOpenedSocket(AddressFamily address_family, SocketDescriptor socket); 79 80 private: 81 UDPSocket socket_; 82 bool connect_called_ = false; 83 // The network the socket is currently bound to. 84 handles::NetworkHandle network_; 85 handles::NetworkHandle connect_using_network_; 86 }; 87 88 } // namespace net 89 90 #endif // NET_SOCKET_UDP_CLIENT_SOCKET_H_ 91