tcp_bound_socket.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_BOUND_SOCKET_H_
  5. #define SERVICES_NETWORK_TCP_BOUND_SOCKET_H_
  6. #include <memory>
  7. #include "base/component_export.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "mojo/public/cpp/bindings/pending_receiver.h"
  12. #include "mojo/public/cpp/bindings/pending_remote.h"
  13. #include "mojo/public/cpp/bindings/receiver_set.h"
  14. #include "net/base/ip_endpoint.h"
  15. #include "net/socket/tcp_socket.h"
  16. #include "net/traffic_annotation/network_traffic_annotation.h"
  17. #include "services/network/public/mojom/tcp_socket.mojom.h"
  18. #include "services/network/tcp_server_socket.h"
  19. namespace net {
  20. class IPEndPoint;
  21. class NetLog;
  22. } // namespace net
  23. namespace network {
  24. class SocketFactory;
  25. // A socket bound to an address. Can be converted into either a TCPServerSocket
  26. // or a TCPConnectedSocket.
  27. class COMPONENT_EXPORT(NETWORK_SERVICE) TCPBoundSocket
  28. : public mojom::TCPBoundSocket {
  29. public:
  30. // Constructs a TCPBoundSocket. |socket_factory| must outlive |this|. When a
  31. // new connection is accepted, |socket_factory| will be notified to take
  32. // ownership of the connection.
  33. TCPBoundSocket(SocketFactory* socket_factory,
  34. net::NetLog* net_log,
  35. const net::NetworkTrafficAnnotationTag& traffic_annotation);
  36. TCPBoundSocket(const TCPBoundSocket&) = delete;
  37. TCPBoundSocket& operator=(const TCPBoundSocket&) = delete;
  38. ~TCPBoundSocket() override;
  39. // Attempts to bind a socket to the specified address. Returns net::OK on
  40. // success, setting |local_addr_out| to the bound address. Returns a network
  41. // error code on failure. Must be called before Listen() or Connect().
  42. int Bind(const net::IPEndPoint& local_addr, net::IPEndPoint* local_addr_out);
  43. // Sets the id used to remove the socket from the owning ReceiverSet. Must be
  44. // called before Listen() or Connect().
  45. void set_id(mojo::ReceiverId receiver_id) { receiver_id_ = receiver_id; }
  46. // mojom::TCPBoundSocket implementation.
  47. void Listen(uint32_t backlog,
  48. mojo::PendingReceiver<mojom::TCPServerSocket> receiver,
  49. ListenCallback callback) override;
  50. void Connect(const net::AddressList& remote_addr,
  51. mojom::TCPConnectedSocketOptionsPtr tcp_connected_socket_options,
  52. mojo::PendingReceiver<mojom::TCPConnectedSocket> receiver,
  53. mojo::PendingRemote<mojom::SocketObserver> observer,
  54. ConnectCallback callback) override;
  55. private:
  56. void OnConnectComplete(int result,
  57. const absl::optional<net::IPEndPoint>& local_addr,
  58. const absl::optional<net::IPEndPoint>& peer_addr,
  59. mojo::ScopedDataPipeConsumerHandle receive_stream,
  60. mojo::ScopedDataPipeProducerHandle send_stream);
  61. virtual int ListenInternal(int backlog);
  62. net::IPEndPoint bind_address_;
  63. mojo::ReceiverId receiver_id_ = -1;
  64. const raw_ptr<SocketFactory> socket_factory_;
  65. std::unique_ptr<net::TCPSocket> socket_;
  66. const net::NetworkTrafficAnnotationTag traffic_annotation_;
  67. mojo::PendingReceiver<mojom::TCPConnectedSocket> connected_socket_receiver_;
  68. ConnectCallback connect_callback_;
  69. // Takes ownership of |socket_| if Connect() is called.
  70. std::unique_ptr<TCPConnectedSocket> connecting_socket_;
  71. base::WeakPtrFactory<TCPBoundSocket> weak_factory_{this};
  72. };
  73. } // namespace network
  74. #endif // SERVICES_NETWORK_TCP_BOUND_SOCKET_H_