int32 yahoo_io_thread( void * _data ) { YahooConnection * conn = (YahooConnection*)_data; register_callbacks(); conn->fID = yahoo_init_with_attributes(conn->fYahooID, conn->fPassword, "local_host", "95.252.70.62", "pager_port", 5050, NULL); LOG("yahoo_io_thread: id: %s, pass: %s\n", conn->fYahooID, conn->fPassword ); gYahooConnections[conn->fID] = conn; yahoo_login(conn->fID, YAHOO_STATUS_AVAILABLE); int lfd = 0; fd_set inp, outp; struct timeval tv; while (conn->IsAlive()) { snooze(10000); FD_ZERO(&inp); FD_ZERO(&outp); tv.tv_sec=3; tv.tv_usec=1E4; lfd=0; int i; for(i = 0; i < conn->CountConnections(); i++) { struct conn_handler *c = conn->ConnectionAt(i); if(c->remove) { conn->RemoveConnection(c); c->remove = 0; free(c); } else { if(c->cond & YAHOO_INPUT_READ) FD_SET(c->con->fd, &inp); if(c->cond & YAHOO_INPUT_WRITE) FD_SET(c->con->fd, &outp); if(lfd < c->con->fd) lfd = c->con->fd; } } select(lfd + 1, &inp, &outp, NULL, &tv); time(&curTime); for(i = 0; i < conn->CountConnections(); i++) { struct conn_handler *c = conn->ConnectionAt(i); if(c->con->remove) { free(c->con); c->con = NULL; break; } if(c->remove) continue; if(FD_ISSET(c->con->fd, &inp)) yahoo_callback(c, YAHOO_INPUT_READ); if(FD_ISSET(c->con->fd, &outp)) yahoo_callback(c, YAHOO_INPUT_WRITE); } if(expired(pingTimer)) yahoo_ping_timeout_callback(conn->fID); // if(expired(webcamTimer)) yahoo_webcam_timeout_callback(webcam_id); } LOG("Exited loop"); for(int i = 0; i < conn->CountConnections(); i++) { struct conn_handler *c = conn->ConnectionAt(i); free(c); conn->RemoveConnection(c); } return 0; }
void __cdecl CYahooProto::server_main(void *empty) { enum yahoo_status status = (enum yahoo_status) (int)empty; time_t lLastPing, lLastKeepAlive, t; YList *l; NETLIBSELECTEX nls = { 0 }; int recvResult, ridx = 0, widx = 0, i; debugLogA("Server Thread Starting status: %d", status); do_yahoo_debug = YAHOO_LOG_DEBUG; yahoo_set_log_level((yahoo_log_level)do_yahoo_debug); poll_loop = 1; /* set this so we start looping */ ext_login(status); lLastKeepAlive = lLastPing = time(NULL); while (poll_loop) { nls.cbSize = sizeof(nls); nls.dwTimeout = 1000; // 1000 millis = 1 sec FD_ZERO(&nls.hReadStatus); FD_ZERO(&nls.hWriteStatus); FD_ZERO(&nls.hExceptStatus); nls.hExceptConns[0] = NULL; ridx = 0; widx = 0; for (l = m_connections; l;) { struct _conn *c = (_conn *)l->data; //LOG(("Connection tag:%d id:%d fd:%d remove:%d", c->tag, c->id, c->fd, c->remove)); if (c->remove) { YList *n = y_list_next(l); //LOG(("Removing id:%d fd:%d tag:%d", c->id, c->fd, c->tag)); m_connections = y_list_remove_link(m_connections, l); y_list_free_1(l); FREE(c); l = n; } else { if (c->cond & YAHOO_INPUT_READ) { //LOG(("[YAHOO_INPUT_READ] Waiting on read. Tag: %d fd: %d", c->tag, c->fd )); nls.hReadConns[ridx++] = (HANDLE)c->fd; } if (c->cond & YAHOO_INPUT_WRITE) { //LOG(("[YAHOO_INPUT_WRITE] Waiting on write. Tag: %d fd: %d", c->tag, c->fd )); nls.hWriteConns[widx++] = (HANDLE)c->fd; } l = y_list_next(l); } } //debugLogA("[Yahoo_Server] ridx:%d widx:%d", ridx, widx); nls.hReadConns[ridx] = NULL; nls.hWriteConns[widx] = NULL; if (m_connections == NULL) { debugLogA("Last connection closed."); break; } recvResult = CallService(MS_NETLIB_SELECTEX, 0, (LPARAM)&nls); /* do the timer check */ if (m_id > 0) { #ifdef HTTP_GATEWAY //debugLogA("HTTPGateway: %d", iHTTPGateway); if (!iHTTPGateway) { #endif t = time(NULL); if (m_bLoggedIn && t - lLastKeepAlive >= 60) { LOG(("[TIMER] Sending a keep alive message")); yahoo_keepalive(m_id); lLastKeepAlive = t; } if (m_bLoggedIn && t - lLastPing >= 3600) { LOG(("[TIMER] Sending ping")); yahoo_send_ping(m_id); lLastPing = t; } #ifdef HTTP_GATEWAY } else { debugLogA("[SERVER] Got packets: %d", ylad->rpkts); if ( m_bLoggedIn && ( (ylad->rpkts > 0 && (time(NULL) - lLastSend) >=3) || ( (time(NULL) - lLastSend) >= 13)) ) { LOG(("[TIMER] Sending an idle message...")); yahoo_send_idle_packet(m_id); } // // need to sleep, cause netlibselectex is too fast? // SleepEx(500, TRUE); } #endif } /* do the timer check ends */ for (l = m_connections; l; l = y_list_next(l)) { struct _conn *c = (_conn *)l->data; if (c->remove) continue; /* are we waiting for a Read request? */ if (c->cond & YAHOO_INPUT_READ) { for (i = 0; i < ridx; i++) { if ((HANDLE)c->fd == nls.hReadConns[i]) { if (nls.hReadStatus[i]) { //LOG(("[YAHOO_INPUT_READ] Read Ready. Tag: %d fd: %d", c->tag, c->fd )); yahoo_callback(c, YAHOO_INPUT_READ); } }//if c->fd= }//for i = 0 } /* are we waiting for a Write request? */ if (c->cond & YAHOO_INPUT_WRITE) { for (i = 0; i < widx; i++) { if ((HANDLE)c->fd == nls.hWriteConns[i]) { if (nls.hWriteStatus[i]) { //LOG(("[YAHOO_INPUT_WRITE] Write ready. Tag: %d fd: %d", c->tag, c->fd )); yahoo_callback(c, YAHOO_INPUT_WRITE); } } // if c->fd == nls }// for i = 0 } }// for l=connections /* Check for Miranda Exit Status */ if (Miranda_Terminated()) { debugLogA("Miranda Exiting... stopping the loop."); break; } } debugLogA("Exited loop"); /* cleanup the data stuff and close our connection handles */ while (m_connections) { YList *tmp = m_connections; struct _conn * c = (_conn *)m_connections->data; Netlib_CloseHandle((HANDLE)c->fd); FREE(c); m_connections = y_list_remove_link(m_connections, m_connections); y_list_free_1(tmp); } yahoo_close(m_id); m_bLoggedIn = FALSE; m_status = YAHOO_STATUS_OFFLINE; m_id = 0; /* now set ourselves to offline */ BroadcastStatus(ID_STATUS_OFFLINE); logoff_buddies(); debugLogA("Server thread ending"); }
int32 yahoo_io_thread( void * _data ) { YahooConnection * conn = (YahooConnection*)_data; /* conn->fID = yahoo_init_with_attributes( conn->fYahooID, conn->fPassword, //"local_host", local_host, "pager_port", 23, NULL ); */ conn->fID = yahoo_init( conn->fYahooID, conn->fPassword ); LOG(kProtocolName, liDebug, "yahoo_io_thread: id: %s, pass: %s", conn->fYahooID, conn->fPassword ); gYahooConnections[conn->fID] = conn; yahoo_login( conn->fID, YAHOO_STATUS_AVAILABLE ); int lfd=0; fd_set inp, outp; struct timeval tv; LOG(kProtocolName, liDebug, "yahoo_io_thread: Starting loop"); while( conn->IsAlive() ) { FD_ZERO(&inp); FD_ZERO(&outp); tv.tv_sec=1; tv.tv_usec=0; lfd=0; for(int i=0; i<conn->CountConnections(); i++) { struct fd_conn *c = conn->ConnectionAt(i); if(c->remove) { // LOG(kProtocolName, liDebug, "yahoo_io_thread: Removing id:%d fd:%d", c->id, c->fd); conn->RemoveConnection(c); free(c); } else { if(c->cond & YAHOO_INPUT_READ) { FD_SET(c->fd, &inp); } if(c->cond & YAHOO_INPUT_WRITE) { FD_SET(c->fd, &outp); } if(lfd < c->fd) lfd = c->fd; } } select(lfd + 1, &inp, &outp, NULL, &tv); time(&curTime); for(int i=0; i<conn->CountConnections(); i++) { struct fd_conn *c = conn->ConnectionAt(i); if(c->remove) continue; if(FD_ISSET(c->fd, &inp)) { // LOG(kProtocolName, liDebug, "yahoo_io_thread: data to read"); yahoo_callback(c, YAHOO_INPUT_READ); FD_CLR(c->fd, &inp); } if(FD_ISSET(c->fd, &outp)) { //LOG(kProtocolName, liDebug, "yahoo_io_thread: data to write"); yahoo_callback(c, YAHOO_INPUT_WRITE); FD_CLR(c->fd, &outp); } } if(expired(pingTimer)) yahoo_ping_timeout_callback(conn->fID, pingTimer); } LOG(kProtocolName, liDebug, "yahoo_io_thread: Exited loop"); while( conn->CountConnections() > 0 ) { struct fd_conn * c = conn->ConnectionAt(0); conn->RemoveConnection(c); close(c->fd); FREE(c); } return 0; }