From: <mik...@us...> - 2003-12-26 22:56:47
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/network In directory sc8-pr-cvs1:/tmp/cvs-serv3848/src/server/network Modified Files: ServerSocket.cpp ServerSocket.h Log Message: 26/12/2003 Mikael Barbeaux * Added a test program for Http sockets. * Fixed a problem about accepting sockets which were an infinite process. * Implemented main http controls : HttpRequest, HttpResponse, HttpSocket, HttpServerSocket and Writers Index: ServerSocket.cpp =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/network/ServerSocket.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- ServerSocket.cpp 26 Dec 2003 10:04:51 -0000 1.1 +++ ServerSocket.cpp 26 Dec 2003 22:56:42 -0000 1.2 @@ -18,9 +18,14 @@ */ #include "ServerSocket.h" +#include "../../thread/Thread.h" +#include <signal.h> +#include <sys/types.h> #ifdef _WIN32_ #define socklen_t int #endif +#include <iostream> +using namespace std; /** * Creates a generic ServerSocket. @@ -101,7 +106,6 @@ // Change server socket state closed = true; - } /** @@ -188,8 +192,10 @@ * Throws a SocketException if an error occurs when * accepting or any other reasons ( not valid, not * bound, not listening ). - */ -Socket* ServerSocket::accept() throw (SocketException) { + * + * @param timeout - timeout for accepting connection + */ +Socket* ServerSocket::accept(int timeout) throw (SocketException) { if(server_id == -1) throw SocketException(InvalidSockExcp, "Invalid server socket.", "ServerSocket::accept"); @@ -204,16 +210,31 @@ "Server socket isn't listening for connections", "ServerSocket::accept"); // Client informations - int client_id; + int client_id = -1; sockaddr_in client_address; int address_length = sizeof(struct sockaddr_in); - // Accepting connection - client_id = ::accept(server_id, (sockaddr *) &client_address, + /** + * In order to add a timeout for accepting connections ( else it will + * wait for a connection forever ), we introduce a fork process + * that will wait for the timeout to achieve, and if no client has + * connected at this time, it will kill the accept process. + */ + int pid; + if((pid = fork())) { + // father process : waiting for the timeout to achieve + Thread::sleep(timeout); + // timeout achieves : father kills his son + kill(pid, SIGKILL); + } + else { + // son process : waiting for a connection... + client_id = ::accept(server_id, (sockaddr *) &client_address, (socklen_t *) &address_length); - + } + if(client_id <= 0) - throw SocketException(InvalidSockExcp, "Received socket is invalid.", + throw SocketException(NotAcceptedSockExcp, "Received socket is invalid.", "ServerSocket::accept"); // Create socket for this connection Index: ServerSocket.h =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/network/ServerSocket.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- ServerSocket.h 26 Dec 2003 10:04:51 -0000 1.1 +++ ServerSocket.h 26 Dec 2003 22:56:42 -0000 1.2 @@ -36,7 +36,7 @@ */ class ServerSocket { - private: + protected: // address of this server socket sockaddr_in server_address; @@ -114,10 +114,11 @@ /** * Accepts a connection. * + * @param timeout * @return Socket* - The connected socket. * @throw SocketException */ - Socket* accept() throw (SocketException); + Socket* accept(int timeout = 3) throw (SocketException); }; |