tcp_server_socket.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef SERVICES_NETWORK_TCP_SERVER_SOCKET_H_
  5. #define SERVICES_NETWORK_TCP_SERVER_SOCKET_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/component_export.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "mojo/public/cpp/bindings/pending_receiver.h"
  13. #include "mojo/public/cpp/bindings/pending_remote.h"
  14. #include "net/base/ip_endpoint.h"
  15. #include "net/traffic_annotation/network_traffic_annotation.h"
  16. #include "services/network/public/mojom/tcp_socket.mojom.h"
  17. namespace net {
  18. class NetLog;
  19. class ServerSocket;
  20. class StreamSocket;
  21. } // namespace net
  22. namespace network {
  23. class TCPConnectedSocket;
  24. class COMPONENT_EXPORT(NETWORK_SERVICE) TCPServerSocket
  25. : public mojom::TCPServerSocket {
  26. public:
  27. // A delegate interface that is notified when new connections are established.
  28. class Delegate {
  29. public:
  30. Delegate() {}
  31. ~Delegate() {}
  32. // Invoked when a new connection is accepted. The delegate should take
  33. // ownership of |socket| and set up binding for |receiver|.
  34. virtual void OnAccept(
  35. std::unique_ptr<TCPConnectedSocket> socket,
  36. mojo::PendingReceiver<mojom::TCPConnectedSocket> receiver) = 0;
  37. };
  38. // Constructs a TCPServerSocket. |delegate| must outlive |this|. When a new
  39. // connection is accepted, |delegate| will be notified to take ownership of
  40. // the connection.
  41. TCPServerSocket(Delegate* delegate,
  42. net::NetLog* net_log,
  43. const net::NetworkTrafficAnnotationTag& traffic_annotation);
  44. // As above, but takes an already listening socket.
  45. TCPServerSocket(std::unique_ptr<net::ServerSocket> server_socket,
  46. int backlog,
  47. Delegate* delegate,
  48. const net::NetworkTrafficAnnotationTag& traffic_annotation);
  49. TCPServerSocket(const TCPServerSocket&) = delete;
  50. TCPServerSocket& operator=(const TCPServerSocket&) = delete;
  51. ~TCPServerSocket() override;
  52. int Listen(const net::IPEndPoint& local_addr,
  53. int backlog,
  54. net::IPEndPoint* local_addr_out);
  55. // TCPServerSocket implementation.
  56. void Accept(mojo::PendingRemote<mojom::SocketObserver> observer,
  57. AcceptCallback callback) override;
  58. // Replaces the underlying socket implementation with |socket| in tests.
  59. void SetSocketForTest(std::unique_ptr<net::ServerSocket> socket);
  60. private:
  61. struct PendingAccept {
  62. PendingAccept(AcceptCallback callback,
  63. mojo::PendingRemote<mojom::SocketObserver> observer);
  64. ~PendingAccept();
  65. AcceptCallback callback;
  66. mojo::PendingRemote<mojom::SocketObserver> observer;
  67. };
  68. // Invoked when socket_->Accept() completes.
  69. void OnAcceptCompleted(int result);
  70. // Process the next Accept() from |pending_accepts_queue_|.
  71. void ProcessNextAccept();
  72. const raw_ptr<Delegate> delegate_;
  73. std::unique_ptr<net::ServerSocket> socket_;
  74. int backlog_;
  75. std::vector<std::unique_ptr<PendingAccept>> pending_accepts_queue_;
  76. std::unique_ptr<net::StreamSocket> accepted_socket_;
  77. net::IPEndPoint accepted_address_;
  78. net::NetworkTrafficAnnotationTag traffic_annotation_;
  79. base::WeakPtrFactory<TCPServerSocket> weak_factory_{this};
  80. };
  81. } // namespace network
  82. #endif // SERVICES_NETWORK_TCP_SERVER_SOCKET_H_