server_socket.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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_SERVER_SOCKET_H_
  5. #define NET_SOCKET_SERVER_SOCKET_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include "net/base/completion_once_callback.h"
  10. #include "net/base/net_export.h"
  11. namespace net {
  12. class IPEndPoint;
  13. class StreamSocket;
  14. class NET_EXPORT ServerSocket {
  15. public:
  16. ServerSocket();
  17. ServerSocket(const ServerSocket&) = delete;
  18. ServerSocket& operator=(const ServerSocket&) = delete;
  19. virtual ~ServerSocket();
  20. // Binds the socket and starts listening. Destroys the socket to stop
  21. // listening.
  22. virtual int Listen(const IPEndPoint& address, int backlog) = 0;
  23. // Binds the socket with address and port, and starts listening. It expects
  24. // a valid IPv4 or IPv6 address. Otherwise, it returns ERR_ADDRESS_INVALID.
  25. virtual int ListenWithAddressAndPort(const std::string& address_string,
  26. uint16_t port,
  27. int backlog);
  28. // Gets current address the socket is bound to.
  29. virtual int GetLocalAddress(IPEndPoint* address) const = 0;
  30. // Accepts connection. Callback is called when new connection is
  31. // accepted.
  32. virtual int Accept(std::unique_ptr<StreamSocket>* socket,
  33. CompletionOnceCallback callback) = 0;
  34. // Accepts connection. Callback is called when new connection is accepted.
  35. // Note: |peer_address| may or may not be populated depending on the
  36. // implementation.
  37. virtual int Accept(std::unique_ptr<StreamSocket>* socket,
  38. CompletionOnceCallback callback,
  39. IPEndPoint* peer_address);
  40. };
  41. } // namespace net
  42. #endif // NET_SOCKET_SERVER_SOCKET_H_