File: tcp_server_socket.h

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (116 lines) | stat: -rw-r--r-- 3,888 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SERVICES_NETWORK_TCP_SERVER_SOCKET_H_
#define SERVICES_NETWORK_TCP_SERVER_SOCKET_H_

#include <memory>
#include <vector>

#include "base/component_export.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/types/expected.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "net/base/ip_endpoint.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/mojom/tcp_socket.mojom.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "services/network/public/mojom/socket_connection_tracker.mojom.h"
#endif  // BUILDFLAG(IS_CHROMEOS)

namespace net {
class NetLog;
class ServerSocket;
class StreamSocket;
}  // namespace net

namespace network {
class TCPConnectedSocket;

class COMPONENT_EXPORT(NETWORK_SERVICE) TCPServerSocket
    : public mojom::TCPServerSocket {
 public:
  // A delegate interface that is notified when new connections are established.
  class Delegate {
   public:
    Delegate() {}
    ~Delegate() {}

    // Invoked when a new connection is accepted. The delegate should take
    // ownership of |socket| and set up binding for |receiver|.
    virtual void OnAccept(
        std::unique_ptr<TCPConnectedSocket> socket,
        mojo::PendingReceiver<mojom::TCPConnectedSocket> receiver) = 0;
  };

  // Constructs a TCPServerSocket. |delegate| must outlive |this|. When a new
  // connection is accepted, |delegate| will be notified to take ownership of
  // the connection.
  TCPServerSocket(Delegate* delegate,
                  net::NetLog* net_log,
                  const net::NetworkTrafficAnnotationTag& traffic_annotation);

  // As above, but takes an already listening socket.
  TCPServerSocket(std::unique_ptr<net::ServerSocket> server_socket,
                  int backlog,
                  Delegate* delegate,
                  const net::NetworkTrafficAnnotationTag& traffic_annotation);

  TCPServerSocket(const TCPServerSocket&) = delete;
  TCPServerSocket& operator=(const TCPServerSocket&) = delete;

  ~TCPServerSocket() override;

  base::expected<net::IPEndPoint, int32_t> Listen(
      const net::IPEndPoint& local_addr,
      int backlog,
      std::optional<bool> ipv6_only);

  // TCPServerSocket implementation.
  void Accept(mojo::PendingRemote<mojom::SocketObserver> observer,
              AcceptCallback callback) override;

#if BUILDFLAG(IS_CHROMEOS)
  void AttachConnectionTracker(
      mojo::PendingRemote<mojom::SocketConnectionTracker>);
#endif  // BUILDFLAG(IS_CHROMEOS)

  // Replaces the underlying socket implementation with |socket| in tests.
  void SetSocketForTest(std::unique_ptr<net::ServerSocket> socket);

 private:
  struct PendingAccept {
    PendingAccept(AcceptCallback callback,
                  mojo::PendingRemote<mojom::SocketObserver> observer);
    ~PendingAccept();

    AcceptCallback callback;
    mojo::PendingRemote<mojom::SocketObserver> observer;
  };
  // Invoked when socket_->Accept() completes.
  void OnAcceptCompleted(int result);
  // Process the next Accept() from |pending_accepts_queue_|.
  void ProcessNextAccept();

  const raw_ptr<Delegate> delegate_;
  std::unique_ptr<net::ServerSocket> socket_;
  int backlog_;
  std::vector<std::unique_ptr<PendingAccept>> pending_accepts_queue_;
  std::unique_ptr<net::StreamSocket> accepted_socket_;
  net::IPEndPoint accepted_address_;
  net::NetworkTrafficAnnotationTag traffic_annotation_;

#if BUILDFLAG(IS_CHROMEOS)
  mojo::PendingRemote<mojom::SocketConnectionTracker> connection_tracker_;
#endif  // BUILDFLAG(IS_CHROMEOS)

  base::WeakPtrFactory<TCPServerSocket> weak_factory_{this};
};

}  // namespace network

#endif  // SERVICES_NETWORK_TCP_SERVER_SOCKET_H_