xref: /aosp_15_r20/external/cronet/net/socket/udp_client_socket.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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   DscpAndEcn GetLastTos() const override;
56 
57   handles::NetworkHandle GetBoundNetwork() const override;
58   void ApplySocketTag(const SocketTag& tag) override;
59   int Read(IOBuffer* buf,
60            int buf_len,
61            CompletionOnceCallback callback) override;
62   int Write(IOBuffer* buf,
63             int buf_len,
64             CompletionOnceCallback callback,
65             const NetworkTrafficAnnotationTag& traffic_annotation) override;
66 
67   void Close() override;
68   int GetPeerAddress(IPEndPoint* address) const override;
69   int GetLocalAddress(IPEndPoint* address) const override;
70   // Switch to use non-blocking IO. Must be called right after construction and
71   // before other calls.
72   void UseNonBlockingIO() override;
73   int SetReceiveBufferSize(int32_t size) override;
74   int SetSendBufferSize(int32_t size) override;
75   int SetDoNotFragment() override;
76   int SetRecvTos() override;
77   int SetTos(DiffServCodePoint dscp, EcnCodePoint ecn) override;
78   void SetMsgConfirm(bool confirm) override;
79   const NetLogWithSource& NetLog() const override;
80   void EnableRecvOptimization() override;
81 
82   int SetMulticastInterface(uint32_t interface_index) override;
83   void SetIOSNetworkServiceType(int ios_network_service_type) override;
84 
85   // Takes ownership of an opened but unconnected and unbound `socket`. This
86   // method must be called after UseNonBlockingIO, otherwise the adopted socket
87   // will not have the non-blocking IO flag set.
88   int AdoptOpenedSocket(AddressFamily address_family, SocketDescriptor socket);
89 
get_multicast_interface_for_testing()90   uint32_t get_multicast_interface_for_testing() {
91     return socket_.get_multicast_interface_for_testing();
92   }
93 #if !BUILDFLAG(IS_WIN)
get_msg_confirm_for_testing()94   bool get_msg_confirm_for_testing() {
95     return socket_.get_msg_confirm_for_testing();
96   }
get_recv_optimization_for_testing()97   bool get_recv_optimization_for_testing() {
98     return socket_.get_experimental_recv_optimization_enabled_for_testing();
99   }
100 #endif
101 #if BUILDFLAG(IS_WIN)
get_use_non_blocking_io_for_testing()102   bool get_use_non_blocking_io_for_testing() {
103     return socket_.get_use_non_blocking_io_for_testing();
104   }
105 #endif
106 
107  private:
108   NetLogWithSource net_log_;
109   UDPSocket socket_;
110   bool adopted_opened_socket_ = false;
111   bool connect_called_ = false;
112   // The network the socket is currently bound to.
113   handles::NetworkHandle network_ = handles::kInvalidNetworkHandle;
114   handles::NetworkHandle connect_using_network_;
115 };
116 
117 }  // namespace net
118 
119 #endif  // NET_SOCKET_UDP_CLIENT_SOCKET_H_
120