tcp_server_socket.h 2.8 KB

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