00001 #ifndef DV_SERVER_SOCKET_H 00002 #define DV_SERVER_SOCKET_H 00003 00004 // $Id: serversocket.h,v 1.18 2008/12/14 16:59:20 dvermeir Exp $ 00005 00006 #include <dvnet/inetaddress.h> 00007 #include <dvnet/socket.h> 00008 #include <stdexcept> 00009 00010 /** @file 00011 * A Dv::Net::ServerSocket object represents an internet server. 00012 * Dv::Net::ServerSocket::accept() returns a (reference to) a 00013 * Dv::Net::Socket representing a client connection as an iostream. 00014 */ 00015 namespace Dv { 00016 namespace Net { 00017 /** A class representing an internet server. 00018 * Example usage: 00019 * @code 00020 * try { 00021 * Dv::Net::ServerSocket server(server-port); 00022 * Dv::Util::shared_ptr<Socket> client(server.accept()); 00023 * std::cerr << "Connection from " << client.tostring() << std::endl; 00024 * 00025 * std::string request; 00026 * std::string reply; 00027 * 00028 * while (*client>>request) 00029 * *client << reply; 00030 * } 00031 * catch (std::runtime_error& e) { 00032 * std::cerr << e.what() << std::endl; 00033 * return 1; 00034 * } 00035 * @endcode 00036 */ 00037 class ServerSocket: public DebugSlave { 00038 public: 00039 /** Start a server on port. 00040 * @param port on which server will listen for connections. 00041 * @param backlog no of connections allowed in backlog. 00042 * @param address local address to bind to, this is useful 00043 * if e.g. you want the server to listen to a specific 00044 * network interface, the default (empty string) 00045 * will bind to the localhost (INADDR_ANY) interface. 00046 * @param min_debug_level if a debug_master is connected, logging info 00047 * will only be written if the master's level is at least @a min_debug_level 00048 * @param debug_master from where debug info will be taken 00049 * @see Dv::DebugSlave 00050 * @exception std::runtime_error if anything goes wrong. 00051 */ 00052 explicit ServerSocket(int port=0,int backlog = 10, const std::string& address = std::string(), 00053 unsigned int min_debug_level = 0, Debugable* debug_master =0) throw (std::runtime_error); 00054 /** Destructor; also closes the socket. */ 00055 virtual ~ServerSocket(); 00056 /** @return the port on which server listens. */ 00057 int port(); 00058 /** @return the numeric internet address of the server host */ 00059 unsigned long address(); 00060 /** @return the internet address of server. 00061 * @sa Dv::Net::InetAddress 00062 */ 00063 Dv::shared_ptr<InetAddress> inetaddress(); 00064 00065 /** Accept a new connection from a client. 00066 * The client Dv::Net::Socket will be a Dv::DebugSlave of the same debug_master as the 00067 * Dv::Net::ServerSocket. 00068 * @param delay in milliseconds: the time allowed for any 00069 * subsequent I/O operation on the returned Socket. A delay of 0 00070 * implies that I/O operations will wait indefinitely. 00071 * @param bufsize the size of the buffer in the returned Socket. 00072 * @param non_blocking whether the underlying fdstreambuf for the 00073 * new Socket should be non-blocking 00074 * @return A new socket representing a connection with a client. 00075 * The status of the returned Socket must be tested to 00076 * verify whether the operation was succesful. 00077 * @see Dv::Util::fdstreambuf 00078 */ 00079 virtual Dv::shared_ptr<Socket> accept(time_t delay=0, size_t bufsize=1024, 00080 bool non_blocking=false); 00081 00082 /** Accept a new connection from a client within a certain 00083 * delay. 00084 * The client Dv::Net::Socket will be a Dv::DebugSlave of the same debug_master as the 00085 * Dv::Net::ServerSocket. 00086 * @param timeout number of millisecs within which the accept 00087 * operation must have succeeded. If it does not succeed 00088 * the status of the return socket will be @c Socket::ACCEPT_ERROR. 00089 * @param delay in milliseconds: the time allowed for any 00090 * subsequent I/O operation on the returned Socket. A delay of 0 00091 * implies that I/O operations will wait indefinitely. 00092 * @param bufsize the size of the buffer in the returned Socket. 00093 * @param non_blocking whether the underlying fdstreambuf for the 00094 * new Socket should be non-blocking 00095 * @return A new socket representing a connection with a client. 00096 * The status of the returned Socket must be tested to 00097 * verify whether the operation was succesful. 00098 * @see Dv::Util::fdstreambuf 00099 */ 00100 virtual Dv::shared_ptr<Socket> accept_or_timeout(time_t timeout, time_t delay=0, 00101 size_t bufsize=1024, bool non_blocking=false); 00102 00103 /** Check whether connections are waiting to be accepted. 00104 * @param timeout in milliseconds: 00105 * @param syserr if not 0, it will contain a copy of ::errno 00106 * upon failure 00107 * @return true iff accept should succeed, no iff if no input activity on this 00108 * serversocket was detected for @a timeout millisecs 00109 * 00110 * Example: 00111 * @code 00112 * Dv::Net::ServerSocket ss(port); 00113 * .. 00114 * if ( ss.connection(2000) ) { // only if a connection is waiting within 2 sec 00115 * Dv::Util::ref<Dv::Net::Socket> so(ss.accept()); 00116 * *so << "+OK" << std::endl; 00117 * } 00118 * else { 00119 * // do something else 00120 * } 00121 * @endcode 00122 * @see Dv::Util::fdstreambuf::fdwait 00123 */ 00124 bool connection(time_t timeout, int* syserr = 0); 00125 00126 /** @return underlying file descriptor (-1 if it was closed). */ 00127 int fd() const { return (socket_fd_);} 00128 /** Closes underlying socket, makes ServerSocket unusable by 00129 * performing shut_down(2) en close(2) on it and setting 00130 * the underlying file descriptor to -1. 00131 */ 00132 void close(); 00133 protected: 00134 /** Auxiliary function for accept(). 00135 * @return new socket connected with client 00136 */ 00137 int fd_accept(); 00138 private: 00139 int socket_fd_; 00140 /** A pointer to a sockaddr_in; @a server_address_ is opaque 00141 * so we don't need to include config.h */ 00142 void* server_address_; 00143 bool closed_; 00144 /** Forbidden. */ 00145 ServerSocket(const ServerSocket&); 00146 /** Forbidden. */ 00147 ServerSocket& operator=(const ServerSocket&); 00148 }; 00149 } 00150 } 00151 #endif
dvnet-0.9.24 | [ 5 December, 2009] |