xref: /aosp_15_r20/external/cronet/net/socket/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_SERVER_SOCKET_H_
6 #define NET_SOCKET_SERVER_SOCKET_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <optional>
12 #include <string>
13 
14 #include "net/base/completion_once_callback.h"
15 #include "net/base/net_export.h"
16 
17 namespace net {
18 
19 class IPEndPoint;
20 class StreamSocket;
21 
22 class NET_EXPORT ServerSocket {
23  public:
24   ServerSocket();
25 
26   ServerSocket(const ServerSocket&) = delete;
27   ServerSocket& operator=(const ServerSocket&) = delete;
28 
29   virtual ~ServerSocket();
30 
31   // Binds the socket and starts listening. Destroys the socket to stop
32   // listening.
33   // |ipv6_only| can be used by inheritors to control whether the server listens
34   // on IPv4/IPv6 or just IPv6 -- |true| limits connections to IPv6 only,
35   // |false| allows both IPv4/IPv6 connections; leaving the value unset implies
36   // default behavior (|true| on Windows, |false| on Posix).
37   virtual int Listen(const IPEndPoint& address,
38                      int backlog,
39                      std::optional<bool> ipv6_only) = 0;
40 
41   // Binds the socket with address and port, and starts listening. It expects
42   // a valid IPv4 or IPv6 address. Otherwise, it returns ERR_ADDRESS_INVALID.
43   virtual int ListenWithAddressAndPort(const std::string& address_string,
44                                        uint16_t port,
45                                        int backlog);
46 
47   // Gets current address the socket is bound to.
48   virtual int GetLocalAddress(IPEndPoint* address) const = 0;
49 
50   // Accepts connection. Callback is called when new connection is
51   // accepted.
52   virtual int Accept(std::unique_ptr<StreamSocket>* socket,
53                      CompletionOnceCallback callback) = 0;
54 
55   // Accepts connection. Callback is called when new connection is accepted.
56   // Note: |peer_address| may or may not be populated depending on the
57   // implementation.
58   virtual int Accept(std::unique_ptr<StreamSocket>* socket,
59                      CompletionOnceCallback callback,
60                      IPEndPoint* peer_address);
61 };
62 
63 }  // namespace net
64 
65 #endif  // NET_SOCKET_SERVER_SOCKET_H_
66