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