xref: /aosp_15_r20/external/cronet/net/socket/tcp_server_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_TCP_SERVER_SOCKET_H_
6 #define NET_SOCKET_TCP_SERVER_SOCKET_H_
7 
8 #include <memory>
9 
10 #include "net/base/completion_once_callback.h"
11 #include "net/base/ip_endpoint.h"
12 #include "net/base/net_export.h"
13 #include "net/socket/server_socket.h"
14 #include "net/socket/socket_descriptor.h"
15 #include "net/socket/tcp_socket.h"
16 
17 namespace net {
18 
19 class NetLog;
20 struct NetLogSource;
21 
22 // A server socket that uses TCP as the transport layer.
23 class NET_EXPORT TCPServerSocket : public ServerSocket {
24  public:
25   TCPServerSocket(NetLog* net_log, const NetLogSource& source);
26 
27   // Adopts the provided socket, which must not be a connected socket.
28   explicit TCPServerSocket(std::unique_ptr<TCPSocket> socket);
29 
30   TCPServerSocket(const TCPServerSocket&) = delete;
31   TCPServerSocket& operator=(const TCPServerSocket&) = delete;
32 
33   ~TCPServerSocket() override;
34 
35   // Takes ownership of |socket|, which has been opened, but may or may not be
36   // bound or listening. The caller must determine this based on the provenance
37   // of the socket and act accordingly. The socket may have connections waiting
38   // to be accepted, but must not be actually connected.
39   int AdoptSocket(SocketDescriptor socket);
40 
41   // net::ServerSocket implementation.
42   int Listen(const IPEndPoint& address,
43              int backlog,
44              std::optional<bool> ipv6_only) override;
45   int GetLocalAddress(IPEndPoint* address) const override;
46   int Accept(std::unique_ptr<StreamSocket>* socket,
47              CompletionOnceCallback callback) override;
48   int Accept(std::unique_ptr<StreamSocket>* socket,
49              CompletionOnceCallback callback,
50              IPEndPoint* peer_address) override;
51 
52   // Detaches from the current thread, to allow the socket to be transferred to
53   // a new thread. Should only be called when the object is no longer used by
54   // the old thread.
55   void DetachFromThread();
56 
57  private:
58   // Converts |accepted_socket_| and stores the result in
59   // |output_accepted_socket|.
60   // |output_accepted_socket| is untouched on failure. But |accepted_socket_| is
61   // set to NULL in any case.
62   int ConvertAcceptedSocket(
63       int result,
64       std::unique_ptr<StreamSocket>* output_accepted_socket,
65       IPEndPoint* output_accepted_address);
66   // Completion callback for calling TCPSocket::Accept().
67   void OnAcceptCompleted(std::unique_ptr<StreamSocket>* output_accepted_socket,
68                          IPEndPoint* output_accepted_address,
69                          CompletionOnceCallback forward_callback,
70                          int result);
71 
72   std::unique_ptr<TCPSocket> socket_;
73 
74   std::unique_ptr<TCPSocket> accepted_socket_;
75   IPEndPoint accepted_address_;
76   bool pending_accept_ = false;
77   bool adopted_opened_socket_ = false;
78 };
79 
80 }  // namespace net
81 
82 #endif  // NET_SOCKET_TCP_SERVER_SOCKET_H_
83