[Assorted-commits] SF.net SVN: assorted:[991] cpp-commons/trunk/src/commons
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-10-06 22:24:23
|
Revision: 991 http://assorted.svn.sourceforge.net/assorted/?rev=991&view=rev Author: yangzhang Date: 2008-10-06 22:24:13 +0000 (Mon, 06 Oct 2008) Log Message: ----------- added closingfd; reworked tcp_listen into tcp_listen (with non-blocking option) and server_socket Modified Paths: -------------- cpp-commons/trunk/src/commons/closing.h cpp-commons/trunk/src/commons/sockets.h Modified: cpp-commons/trunk/src/commons/closing.h =================================================================== --- cpp-commons/trunk/src/commons/closing.h 2008-10-05 18:44:39 UTC (rev 990) +++ cpp-commons/trunk/src/commons/closing.h 2008-10-06 22:24:13 UTC (rev 991) @@ -14,6 +14,11 @@ T x; }; + class closingfd : closing<int> { + public: + closingfd(int x) : closing<int>(x) {} + }; + } #endif Modified: cpp-commons/trunk/src/commons/sockets.h =================================================================== --- cpp-commons/trunk/src/commons/sockets.h 2008-10-05 18:44:39 UTC (rev 990) +++ cpp-commons/trunk/src/commons/sockets.h 2008-10-06 22:24:13 UTC (rev 991) @@ -14,17 +14,28 @@ { /** - * Create a listener socket, with SO_REUSEADDR. + * Create a server socket bound to localhost, with SO_REUSEADDR enabled. * \param[in] port The port to listen on. - * \return The listener socket. + * \param[in] nb Whether the socket should be non-blocking. + * \return The server socket. */ int - tcp_listen(int port) + server_socket(int port, bool nb = false) { // Create the socket. - int sfd = checknneg(socket(PF_INET, SOCK_STREAM, 0)); + int fd = checknneg(socket(PF_INET, SOCK_STREAM, 0)); try { + // Configure the socket. + int n = 1; + check0x(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, + sizeof(n))); + + // Make our socket non-blocking if desired. + if (nb) { + checknneg(fcntl(fd, F_SETFL, O_NONBLOCK | fcntl(fd, F_GETFL, 0))); + } + // Create the local socket address. struct sockaddr_in sa; bzero(&sa, sizeof(sa)); @@ -32,19 +43,33 @@ sa.sin_port = htons(port); sa.sin_addr.s_addr = htonl(INADDR_ANY); - // Configure the socket. - int n = 1; - check0x(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof(n))); - // Bind the socket to the local socket address. - check0x(bind(sfd, (struct sockaddr*) &sa, sizeof(struct sockaddr_in))); + check0x(bind(fd, (struct sockaddr*) &sa, sizeof(struct sockaddr_in))); - // Start listening. - check0x(listen(sfd, 256)); + return fd; + } catch (...) { + close(fd); + throw; + } + } - return sfd; + /** + * Create a server socket and listen on it. + * \param[in] port The port to listen on. + * \param[in] nb Whether the socket should be non-blocking. + * \return The listener socket. + */ + int + tcp_listen(int port, bool nb = false) + { + int fd = server_socket(port, nb); + try { + // SOMAXCONN is the kernel's limit on the maximum number of socket + // connections. + check0x(listen(fd, SOMAXCONN)); + return fd; } catch (...) { - close(sfd); + close(fd); throw; } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |