diff options
| author | Paul Buetow <paul@buetow.org> | 2013-04-06 13:14:46 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2013-04-06 13:14:46 +0200 |
| commit | 07706f56cd421bf8b101a5c0fe44674d36e161a0 (patch) | |
| tree | bec8b45765788370fbd148d68e05d8b437e60670 /sock.cpp | |
| parent | 56187184e11b085b82584d3b088c62f427aae83c (diff) | |
tagging ychat-0.5.1ychat-0.5.1
Diffstat (limited to 'sock.cpp')
| -rwxr-xr-x | sock.cpp | 70 |
1 files changed, 45 insertions, 25 deletions
@@ -1,17 +1,18 @@ // class sock implementation. the multiplex socket implementation has been token from the // GNU C Library Examples and modified in order to fit in here ( POSIX threads etc. ). -#ifndef SOCK_CXX -#define SOCK_CXX +#ifndef s_sock_CXX +#define s_sock_CXX #include <unistd.h> #include "sock.h" -#include "CHAT.h" -#include "CONF.h" -#include "MUTX.h" -#include "TOOL.h" - +#include "s_chat.h" +#include "s_conf.h" +#include "s_mutx.h" +#include "s_tool.h" +#include "s_lang.h" +#include "s_sman.h" #include "chat.h" #include "user.h" @@ -23,25 +24,32 @@ sock::sock() this->i_req = 0; this->req_parser = new reqp(); this->thrd_pool = new pool(); + this->log_daemon = new logd(s_conf::get().get_val( "ACCESS_LOG" )); } void -sock::chat_stream( int i_sock, map_string &map_params ) +sock::chat_stream( int i_sock, user* p_user, map_string &map_params ) { - user* p_user = CHAT::get().get_user( map_params["nick"] ); - p_user->set_sock( i_sock ); - - string s_msg( "" ); + string s_msg( "\n" ); pthread_mutex_lock ( &(p_user->mut_message) ); - while( p_user->get_online() ) + for ( int i = 0; i < PUSHSTR; i++ ) + send( i_sock, s_msg.c_str(), s_msg.size(), 0 ); + + do { - pthread_cond_wait( &(p_user->cond_message), &(p_user->mut_message) ); 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), &(p_user->mut_message) ); + } + while( p_user->get_online() ); + + // 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 ); pthread_mutex_unlock( &(p_user->mut_message) ); @@ -50,7 +58,11 @@ sock::chat_stream( int i_sock, map_string &map_params ) p_user->get_p_room()->del_elem( s_user ); // post the room that the user has left the chat. - p_user->get_p_room()->msg_post( new string( p_user->get_name().append( USERLEAV ) ) ); + p_user->get_p_room()->msg_post( new string( p_user->get_name().append( s_lang::get().get_val( "USERLEAV" ) ) ) ); + s_sman::get().destroySession( p_user->get_id() ); + #ifdef VERBOSE + cout << s_user << " left | SessionCount: " << s_sman::get().getSessionCount() << endl; + #endif p_user->~user(); } @@ -114,18 +126,26 @@ sock::read_write( thrd* p_thrd, int i_sock ) // stores the request params. map_string map_params; - // get the s_rep ( HTML response which will be send imediatly to the client + // get the s_rep ( s_html response which will be send imediatly to the client // and fill map_params with request values. auto string s_temp=(string)c_req; + struct sockaddr_in client; + size_t size=sizeof(client); + + getpeername( i_sock, (struct sockaddr *)&client, &size); + + map_params["REMOTE_ADDR"]=inet_ntoa(client.sin_addr); + map_params["REMOTE_PORT"]=ntohs( client.sin_port); - string s_rep = req_parser->parse( p_thrd, string( c_req ), map_params ); + string s_rep = req_parser->parse( p_thrd, string( c_req ), map_params ); // send s_rep to the client. + log_daemon->log(map_params); + send( i_sock, s_rep.c_str(), s_rep.size(), 0 ); - // prove if this is a request for a chat stream! - if ( map_params["event"] == "stream" ) - chat_stream( i_sock, map_params ); + // dont need those vals anymore. + map_params.clear(); return 0; } @@ -136,7 +156,7 @@ sock::read_write( thrd* p_thrd, int i_sock ) int sock::start() { - auto int i_port = TOOL::string2int( CONF::get().get_val( "SRVRPORT" ) ); + auto int i_port = s_tool::string2int( s_conf::get().get_val( "SRVRPORT" ) ); int sock; fd_set active_fd_set, read_fd_set; @@ -144,7 +164,7 @@ sock::start() struct sockaddr_in clientname; size_t size; -#ifdef _VERBOSE +#ifdef VERBOSE cout << SOCKCRT << "localhost:" << i_port << endl; #endif @@ -157,7 +177,7 @@ sock::start() exit( EXIT_FAILURE ); } -#ifdef _VERBOSE +#ifdef VERBOSE cout << SOCKRDY << endl; #endif @@ -195,7 +215,7 @@ sock::start() close ( new_sock ); } -#ifdef _VERBOSE +#ifdef VERBOSE cout << CONNECT << i_req << " " << inet_ntoa( clientname.sin_addr ) << ":" |
