diff options
Diffstat (limited to 'src/sock')
| -rw-r--r-- | src/sock/Makefile | 23 | ||||
| -rw-r--r-- | src/sock/README | 1 | ||||
| -rwxr-xr-x | src/sock/sock.cpp | 133 | ||||
| -rwxr-xr-x | src/sock/sock.h | 39 |
4 files changed, 117 insertions, 79 deletions
diff --git a/src/sock/Makefile b/src/sock/Makefile new file mode 100644 index 0000000..f5895f1 --- /dev/null +++ b/src/sock/Makefile @@ -0,0 +1,23 @@ +#CC=gcc33 +CC=g++ +INCLUDE= +DEBUG=-D_GNU_SOURCE +#CFLAGS=-Wall -O3 -s $(DEBUG) $(INCLUDE) +#CFLAGS=-Wall -g -ansi -pedantic $(DEBUG) $(INCLUDE) +CFLAGS=-Wall -g $(DEBUG) $(INCLUDE) +LIBS=-lstdc++ + +OBJECTS=main-test.o tcp-client.o exception.o +PROGRAM=main-test + +all: $(PROGRAM) + +$(PROGRAM): $(OBJECTS) + $(CC) $(CFLAGS) -o $@ $(OBJECTS) $(LIBS) + +.cpp.o: + $(CC) $(CFLAGS) -c -o $@ $< + +clean: + rm -f $(PROGRAM) $(OBJECTS) + diff --git a/src/sock/README b/src/sock/README new file mode 100644 index 0000000..9d3aac9 --- /dev/null +++ b/src/sock/README @@ -0,0 +1 @@ +ossl.h not yet implemented (see header info) diff --git a/src/sock/sock.cpp b/src/sock/sock.cpp index bc5eb55..531ccb9 100755 --- a/src/sock/sock.cpp +++ b/src/sock/sock.cpp @@ -18,20 +18,20 @@ sock::sock() this->i_threads = 0; this->req_parser = new reqp(); this->thrd_pool = new pool(); -#ifdef LOGGING this->log_daemon = new logd( wrap::CONF->get_elem( "httpd.logging.accessfile" ), - wrap::CONF->get_elem( "httpd.logging.accesslines" ) ); -#endif + + pthread_mutex_init( &mut_threads, NULL ); } sock::~sock() { + pthread_mutex_destroy( &mut_threads ); } //<<* void -sock::chat_stream( int i_sock, user *p_user, map<string,string> &map_params ) +sock::chat_stream( int i_sock, user* p_user, map_string &map_params ) { string s_msg( "\n" ); @@ -45,10 +45,8 @@ sock::chat_stream( int i_sock, user *p_user, map<string,string> &map_params ) do { s_msg = p_user->get_mess( ); - if ( 0 > send( i_sock, s_msg.c_str(), s_msg.size(), 0 ) ) p_user->set_online( false ); - pthread_cond_wait( &(p_user->cond_message), &mutex ); } while( p_user->get_online() ); @@ -57,14 +55,12 @@ sock::chat_stream( int i_sock, user *p_user, map<string,string> &map_params ) // if there is still a message to send: s_msg = p_user->get_mess( ); - if ( ! s_msg.empty() ) send( i_sock, s_msg.c_str(), s_msg.size(), 0 ); // remove the user from its room. string s_user( p_user->get_name() ); string s_user_lowercase( p_user->get_lowercase_name() ); - p_user->get_room()->del_elem( s_user_lowercase ); // post the room that the user has left the chat. @@ -87,17 +83,17 @@ sock::chat_stream( int i_sock, user *p_user, map<string,string> &map_params ) int sock::make_server_socket( int i_port ) { - size_t i_sock; + size_t sock; struct sockaddr_in name; // create the server socket. - i_sock = socket (PF_INET, SOCK_STREAM, 0); - if (i_sock < 0) + sock = socket (PF_INET, SOCK_STREAM, 0); + if (sock < 0) { wrap::system_message( SOCKERR ); if ( ++i_port > MAXPORT ) - exit(1); + exit(-1); wrap::system_message( SOCKERR ); @@ -106,23 +102,22 @@ sock::make_server_socket( int i_port ) // give the server socket a name. name.sin_family = AF_INET; - name.sin_port = htons(i_port); - name.sin_addr.s_addr = htonl(INADDR_ANY); - int i_optval = 1; + name.sin_port = htons (i_port); + name.sin_addr.s_addr = htonl (INADDR_ANY); + int optval=1; - setsockopt( i_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&i_optval, sizeof(int) ); + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, sizeof(int)); - if ( bind(i_sock, (struct sockaddr *) &name, sizeof (name)) < 0 ) + if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) { wrap::system_message( BINDERR ); if ( ++i_port > MAXPORT ) - exit(1); + exit(-1); wrap::system_message( string(SOCKERR) + tool::int2string(i_port) ); - // Rerun recursive. return make_server_socket( i_port ); } @@ -133,61 +128,47 @@ sock::make_server_socket( int i_port ) refresh(); #endif - return i_sock; + return sock; } int -sock::read_write( int* p_sock ) +sock::read_write( thrd* p_thrd, int i_sock ) { - int i_sock = *p_sock; char c_req[READSOCK]; - int i_bytes = read(i_sock, c_req, READSOCK); + int i_bytes = read (i_sock, c_req, READSOCK); if (i_bytes <= 0) { wrap::system_message( READERR ); } - else { // stores the request params. - map<string,string> map_params; + map_string map_params; // get the s_rep ( s_html response which will be send imediatly to the client struct sockaddr_in client; size_t size = sizeof(client); -#ifdef CYGWIN - getpeername( i_sock, (struct sockaddr *)&client, (int*)&size); -#else getpeername( i_sock, (struct sockaddr *)&client, &size); -#endif map_params["REMOTE_ADDR"] = inet_ntoa(client.sin_addr); //map_params["REMOTE_PORT"] = ntohs(client.sin_port); - string s_rep = req_parser->parse( i_sock, string( c_req ), map_params ); + string s_rep = req_parser->parse( p_thrd, string( c_req ), map_params ); -#ifdef LOGGING log_daemon->log_access(map_params); -#endif // send s_rep to the client. - send(i_sock, s_rep.c_str(), s_rep.size(), 0); + send( i_sock, s_rep.c_str(), s_rep.size(), 0 ); // dont need those vals anymore. map_params.clear(); - shutdown( i_sock, 2 ); - close ( i_sock ); - return 0; } - shutdown( i_sock, 2 ); - close ( i_sock ); - - return 1; + return -1; } int @@ -197,20 +178,23 @@ sock::start() #ifdef NCURSES print_hits(); + print_threads(); thrd_pool->print_pool_size(); #endif - int i_port = tool::string2int( wrap::CONF->get_elem( "httpd.serverport" ) ); - int i_sock, i; + auto int i_port = tool::string2int( wrap::CONF->get_elem( "httpd.serverport" ) ); + + int sock; fd_set active_fd_set, read_fd_set; + int i; struct sockaddr_in clientname; size_t size; // create the server socket and set it up to accept connections. - i_sock = make_server_socket ( i_port ); + sock = make_server_socket ( i_port ); - if (listen (i_sock, 1) < 0) + if (listen (sock, 1) < 0) { wrap::system_message( LISTERR ); exit( EXIT_FAILURE ); @@ -220,7 +204,7 @@ sock::start() // initialize the set of active sockets. FD_ZERO (&active_fd_set); - FD_SET (i_sock, &active_fd_set); + FD_SET (sock, &active_fd_set); while( b_run ) { @@ -237,26 +221,21 @@ sock::start() for ( i = 0; i < FD_SETSIZE; i++ ) if ( FD_ISSET (i, &read_fd_set) ) { - if ( i == i_sock ) + if ( i == sock ) { // connection request on original socket. - ++i_req; - + i_req++; #ifdef NCURSES print_hits(); #endif - int i_new_sock; - size = sizeof(clientname); -#ifdef CYGWIN - i_new_sock = accept (i_sock, (struct sockaddr *) &clientname, (int*)&size); -#else - i_new_sock = accept (i_sock, (struct sockaddr *) &clientname, &size); -#endif + int new_sock; + size = sizeof (clientname); + new_sock = accept (sock, (struct sockaddr *) &clientname, &size); - if (i_new_sock < 0) + if (new_sock < 0) { wrap::system_message( ACCPERR ); - close(i_new_sock); + close ( new_sock ); } else @@ -268,23 +247,55 @@ sock::start() + tool::int2string(ntohs ( clientname.sin_port )) ); #endif - FD_SET (i_new_sock, &active_fd_set); + FD_SET (new_sock, &active_fd_set); } } else { - int *p_sock = new int; - *p_sock = i; - thrd_pool->run( (void*) p_sock ); + thrd_pool->run( (void*) new thrd( i ) ); FD_CLR( i, &active_fd_set ); } } } } +void +sock::increase_num_threads() +{ + pthread_mutex_lock( &mut_threads ); + i_threads++; + pthread_mutex_unlock( &mut_threads ); + +#ifdef NCURSES + print_threads(); +#endif +} + +void +sock::decrease_num_threads() +{ + pthread_mutex_lock( &mut_threads ); + i_threads--; + pthread_mutex_unlock( &mut_threads ); + +#ifdef NCURSES + print_threads(); +#endif +} + #ifdef NCURSES void +sock::print_threads() +{ + if ( wrap::NCUR->is_ready() ) + { + mvprintw( NCUR_POOL_RUNNING_X,NCUR_POOL_RUNNING_Y, "In use: %d ", i_threads); + refresh(); + } +} + +void sock::print_hits() { if ( wrap::NCUR->is_ready() ) diff --git a/src/sock/sock.h b/src/sock/sock.h index 7023be5..6f3419e 100755 --- a/src/sock/sock.h +++ b/src/sock/sock.h @@ -3,19 +3,18 @@ #ifndef SOCK_H #define SOCK_H + #include <queue> #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> - #include "../thrd/pool.h" +#include "../thrd/thrd.h" #include "../reqp.h" #include "../chat/user.h" -#ifdef LOGGING #include "../logd.h" -#endif using namespace std; @@ -23,46 +22,50 @@ class sock { private: // total number of server requests. - unsigned long long i_req; + unsigned long long int i_req; bool b_run; // true while socket manager is running. - reqp *req_parser; // parses the http requests from clients. - pool *thrd_pool; // the thread pool. -#ifdef LOGGING - logd *log_daemon; // the log daemon -#endif - char *c_buffer; // char buffer! - int i_threads; // total amount of threads inside the thread pool. + reqp* req_parser; // parses the http requests from clients. + pool* thrd_pool; // the thread pool. + logd* log_daemon; // the log daemon + char* c_buffer; // char buffer! + int i_threads; // total amount of threads inside the thread pool. + pthread_mutex_t mut_threads; pthread_mutex_t mut_hits; + public: // creates a server socket. - int make_server_socket( int i_port ); + int make_server_socket( int port ); // small inline methods: bool get_run() const { return b_run; } - bool set_run( bool b_run ) { this->b_run = b_run; } - sock( ); + // public methods. + explicit sock( ); // simple constructor. ~sock( ); - - int read_write( int* p_sock ); - int start(); + virtual int read_write( thrd* p_thrd, int filedes ); + virtual int start(); // the chat stream there all the chat messages will sent through. - static void chat_stream( int i_sock, user* p_user, map<string,string> &map_params ); //<< + static void chat_stream( int i_sock, user* p_user, map_string &map_params ); //<< + void increase_num_threads(); + void decrease_num_threads(); #ifdef NCURSES + + void print_threads(); void print_hits(); #endif + }; #endif |
