EXPORT user* webqq_get_user( void* session_ptr ) { user* u = (user*)loop_search( &user_loop, session_ptr, get_user_searcher ); if( u ){ u->reference ++; u->update_time = time(NULL); // printf("[%u:%u]", (uint)u->update_time, ((qqclient*)u->qq)->number ); } return u; }
int handle_packet( qqclient* qq, qqpacket* p, uchar* data, int len ) { qqpacketmgr* mgr = &qq->packetmgr; mgr->recv_packets ++; bytebuffer* buf; NEW( buf, sizeof( bytebuffer ) ); if( !buf ){ DBG("error: no enough memory to allocate buf."); return -99; } buf->pos = 0; buf->len = buf->size = len; memcpy( buf->data, data, buf->len ); //get packet info if( qq->network == TCP || qq->network == PROXY_HTTP ) get_word( buf ); //packet len p->head = get_byte( buf ); p->tail = buf->data[buf->len-1]; if( p->head != 0x02 || p->tail!=0x03 || buf->len > 2000 ){ DBG("[%u] wrong packet. len=%d head: %d tail: %d", qq->number, buf->len, p->head, p->tail ); DEL( buf ); return -1; } p->version = get_word( buf ); p->command = get_word( buf ); p->seqno = get_word( buf ); uint chk_repeat = (p->seqno<<16)|p->command; //check repeat if( loop_search( &mgr->recv_loop, (void*)chk_repeat, repeat_searcher ) == NULL ){ loop_push_to_tail( &mgr->recv_loop, (void*)chk_repeat ); p->match = match_packet( mgr, p ); p->time_alive = time(NULL); //do a test if ( !p->buf ){ DBG("%u: Error impossible. p->buf: %x p->command: %x", qq->number, p->buf, p->command ); } //deal with the packet process_packet( qq, p, buf ); qqpacket* t; while( (t = loop_pop_from_tail( &mgr->temp_loop )) ){ loop_push_to_head( &mgr->ready_loop, t ); } if( p->match ){ loop_remove( &mgr->sent_loop, p->match ); delete_func( p->match ); } p->match = NULL; mgr->failed_packets = 0; }else{ // DBG("repeated packet: cmd: %x seqno:%x", p->command, p->seqno ); } DEL( buf ); check_ready_packets( qq ); return 0; }
static void* webqq_guarder( void* data ) { int counter = 0; DBG (("webqq_guarder")); while( webqq_running ){ time_t timenow = time(NULL); counter ++; user* u; do{ u = loop_search( &user_loop, (void*)timenow, guarder_searcher ); if( u ){ DBG("removing qq: %u time1: %u - time2: %u > 120", ((qqclient*)u->qq)->number, timenow, u->update_time ); loop_remove( &user_loop, (void*)u ); delete_func( (void*)u ); } }while( u ); SLEEP( 1 ); } DBG (("end.")); return NULL; }
EXPORT user* webqq_create_user( void* session_ptr, uint number, uchar* md5pass ) { user* u; qqclient* qq; u = loop_search( &user_loop, session_ptr, get_user_searcher ); if( u ) return u; NEW( qqclient*, qq, sizeof(qqclient) ); NEW( user*, u, sizeof(user) ); if( !u || !qq ){ DEL( qq ); DEL( u ); return NULL; } u->session_ptr = session_ptr; u->create_time = u->update_time = time(NULL); u->qq = qq; u->reference = 1; qqclient_md5_create( qq, number, md5pass ); qq->auto_accept = 1; //temporarily do this loop_push_to_tail( &user_loop, u ); return u; }
int packetmgr_check_packet( struct qqclient* qq, int timeout ) { qqpacketmgr *mgr = &qq->packetmgr; qqpacket* p; time_t timeout_time = time(NULL) - timeout; //when locked, cannot recv packet till unlock. do{ p = loop_search( &mgr->sent_loop, (void*)timeout_time, timeout_searcher ); if( p ){ loop_remove( &mgr->sent_loop, p ); } if( p ){ if( p->send_times >= 10 ){ MSG("[%u] Failed to send the packet. command: %x\n", qq->number, p->command ); if( p->command == QQ_CMD_SEND_IM ){ buddy_msg_callback( qq, 10000, time(NULL), "刚才某条消息发送失败。" ); } //make an event to tell the program that we have a //problem. char event[64]; sprintf( event, "sendfailed^$%d^$%d", p->command, p->seqno ); qqclient_put_event( qq, event ); delete_func( p ); mgr->failed_packets ++; //To avoid too many failed packets, just shut it down. if( mgr->failed_packets > 5 || qq->process != P_LOGIN ){ qqclient_set_process( qq, P_ERROR ); } }else{ DBG("[%u] resend packet cmd: %x", qq->number, p->command ); packetmgr_put_urge( qq, p, 1 ); } } }while( 0 && p ); check_ready_packets(qq); return 0; }
static qqpacket* match_packet( qqpacketmgr* mgr, qqpacket* p ) { qqpacket* m; m = loop_search( &mgr->sent_loop, (void*)p, match_searcher ); return m; }
static void work_http( connection* conn ) { int i; virtual_host * host; //check if the connection cannot work. if( conn->state != C_READY ) return; conn->state = C_REQUESTING; http_request( conn ); if( conn->state != C_REQUESTING ) return; //response conn->code = 200; conn->state = C_RESPONSING; /* Check Host and then set root directory. */ host = loop_search( &conn->server->loop_vhost, conn->host, vhost_searcher ); if( !host ){ host = loop_search( &conn->server->loop_vhost, "*", vhost_searcher ); } if( host ){ //read root conn->root_dir = host->root_dir; if( !loop_is_empty( &host->loop_rewrite ) ) loop_search( &host->loop_rewrite, (void*)conn, loop_rewrite_match ); http_parse_uri( conn ); DBG("[%s]%s%s", conn->client->ip_string, conn->host, conn->uri ); _RESPONSE: http_parse_path( conn ); conn->document_type = http_doctype( conn->server, conn->extension ); if( !conn->document_type ){ http_error( conn, 404, "<h1>File not found.</h1>" ); }else if( conn->extension[0] && strstr( conn->server->asp_exts, conn->extension ) ) { //php do ... exec_asp( conn ); }else if( host->proxy && ( !host->proxy_exts[0] || strstr( host->proxy_exts, conn->extension ) ) ) { // uses proxy server proxy_request( conn, host->proxy_ip, host->proxy_port ); }else if( access(conn->full_path, 0)==0 ){ if( is_dir(conn->full_path) ){ char* tmp; NEW( tmp, PATH_LEN+32 ); if( conn->script_name[strlen(conn->script_name)-1] != '/' ){ //Are you sure that script starts with '/'? sprintf( tmp, "http://%s%s/", conn->host, conn->script_name ); http_redirect( conn, tmp ); }else{ if( tmp ){ for( i = 0; i<10; i++ ) { if( !conn->server->default_pages[i][0] ) { i=10; break; } sprintf( tmp, "%s/%s", conn->full_path, conn->server->default_pages[i] ); if( access( tmp, 0 ) == 0 ) { //091004 by Huang Guan. sprintf( conn->script_name, "%s%s", conn->script_name, conn->server->default_pages[i] ); DEL( tmp ); goto _RESPONSE; } } } if( i == 10 ){ // List Directory if( host->list ){ int ret; NEW( conn->data_send, MAX_DATASEND+4 ); strcpy( conn->extension, "html" ); conn->document_type = http_doctype( conn->server, conn->extension ); ret = listdir( conn->data_send, MAX_DATASEND, conn->full_path, conn->script_name ); conn->data_size = ret; }else{ http_error( conn, 403, "<h1>Forbidden</h1>" ); } } } DEL( tmp ); }else{ http_sendfile( conn, conn->full_path ); } }else if( strncmp(conn->current_dir, "/system", 7)==0 && conn->root_dir==host->root_dir ){ strcpy(conn->script_name, conn->script_name+7); conn->root_dir = conn->client->server->root_dir; goto _RESPONSE; }else{ http_error( conn, 404, "<h1>File not found.</h1>" ); } }else{ http_error( conn, 403, "<h1>Unknown host name.</h1>" ); } if( conn->state == C_RESPONSING ) http_response( conn ); conn->requests ++; if( conn->form_data ) DEL( conn->form_data ); if( conn->data_send ) DEL( conn->data_send ); if( conn->session ) conn->session->reference --; conn->session = NULL; //next request if( conn->keep_alive ){ conn->state = C_READY; }else{ conn->state = C_END; } }