void AsyncSocketSelect(SOCKET sock,int event,int error) { session_node *s; EnterSessionLock(); if (error != 0) { LeaveSessionLock(); s = GetSessionBySocket(sock); if (s != NULL) { /* we can get events for sockets that have been closed by main thread (and hence get NULL here), so be aware! */ /* eprintf("AsyncSocketSelect got error %i session %i\n",error,s->session_id); */ HangupSession(s); return; } /* eprintf("AsyncSocketSelect got socket that matches no session %i\n",sock); */ return; } switch (event) { case FD_CLOSE : AsyncSocketClose(sock); break; case FD_WRITE : AsyncSocketWrite(sock); break; case FD_READ : AsyncSocketRead(sock); break; default : eprintf("AsyncSocketSelect got unknown event %i\n",event); break; } LeaveSessionLock(); }
void CloseSession(int session_id) { session_node *s; s = GetSessionByID(session_id); if (s == NULL) { eprintf("CloseSession can't find session %i\n",session_id); return; } if (!s->connected) { eprintf("CloseSession can't close unconnected session %i\n", s->session_id); return; } EnterSessionLock(); s->connected = False; if (!s->exiting_state) /* if a write error occurred during an exit, don't */ ExitSessionState(s); /* go into infinite loop */ if ((s->state != STATE_MAINTENANCE) && (s->account == NULL)) lprintf("CloseSession closing session with no account from %s\n", s->conn.name); else { AccountLogoff(s->account); if (s->account != NULL) lprintf("CloseSession/4 logging off %i\n",s->account->account_id); } s->account = NULL; InterfaceLogoff(s); if (s->conn.type == CONN_SOCKET) { if (WaitForSingleObject(s->muxSend,10000) != WAIT_OBJECT_0) eprintf("CloseSession couldn't get session %i muxSend\n",s->session_id); else { DeleteBufferList(s->send_list); s->send_list = NULL; /* no need to release mutex... we're closing it */ /* if (!ReleaseMutex(s->muxSend)) eprintf("File %s line %i release of non-owned mutex\n",__FILE__,__LINE__); */ } if (WaitForSingleObject(s->muxReceive,10000) != WAIT_OBJECT_0) eprintf("CloseSession couldn't get session %i muxReceive\n",s->session_id); else { DeleteBufferList(s->receive_list); s->receive_list = NULL; /* no need to release mutex... we're closing it */ /* if (!ReleaseMutex(s->muxReceive)) eprintf("File %s line %i release of non-owned mutex\n",__FILE__,__LINE__); */ } if (!CloseHandle(s->muxSend)) eprintf("CloseSession error (%s) closing send mutex %i in session %i\n", GetLastErrorStr(),s->muxSend,s->session_id); if (!CloseHandle(s->muxReceive)) eprintf("CloseSession error (%s) closing receive mutex %i in session %i\n", GetLastErrorStr(),s->muxReceive,s->session_id); CloseConnection(s->conn); } s->session_id = -1; s->state = -1; LeaveSessionLock(); }