From 01ab72c6afb99b7403eb577a44f35b09eda594f7 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sat, 12 Jul 2014 15:38:46 +0000 Subject: wipe out of the old ugly disconnection code git-svn-id: http://svn.miranda-ng.org/main/trunk@9773 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/IcqOscarJ/src/chan_04close.cpp | 1 - protocols/IcqOscarJ/src/chan_05ping.cpp | 23 +++++------- protocols/IcqOscarJ/src/fam_01service.cpp | 22 +++++------ protocols/IcqOscarJ/src/icq_avatar.cpp | 23 +++++------- protocols/IcqOscarJ/src/icq_proto.cpp | 4 +- protocols/IcqOscarJ/src/icq_server.cpp | 62 ++++++++++++++----------------- 6 files changed, 57 insertions(+), 78 deletions(-) (limited to 'protocols') diff --git a/protocols/IcqOscarJ/src/chan_04close.cpp b/protocols/IcqOscarJ/src/chan_04close.cpp index 409926b3df..b5a1e6388f 100644 --- a/protocols/IcqOscarJ/src/chan_04close.cpp +++ b/protocols/IcqOscarJ/src/chan_04close.cpp @@ -86,7 +86,6 @@ void CIcqProto::handleLoginReply(BYTE *buf, WORD datalen, serverthread_info *inf // we return only if the server did not gave us cookie (possible to connect with soft error) if (!chain->getLength(0x06, 1)) { disposeChain(&chain); - SetCurrentStatus(ID_STATUS_OFFLINE); icq_serverDisconnect(); return; // Failure } diff --git a/protocols/IcqOscarJ/src/chan_05ping.cpp b/protocols/IcqOscarJ/src/chan_05ping.cpp index bd5539b974..62c3377ada 100644 --- a/protocols/IcqOscarJ/src/chan_05ping.cpp +++ b/protocols/IcqOscarJ/src/chan_05ping.cpp @@ -29,7 +29,6 @@ void CIcqProto::handlePingChannel(BYTE *buf, WORD datalen) debugLogA("Warning: Ignoring server packet on PING channel"); } - void __cdecl CIcqProto::KeepAliveThread(void *arg) { serverthread_info *info = (serverthread_info*)arg; @@ -40,23 +39,21 @@ void __cdecl CIcqProto::KeepAliveThread(void *arg) info->hKeepAliveEvent = CreateEvent(NULL, FALSE, FALSE, NULL); - for (;;) - { + for (;;) { DWORD dwWait = ICQWaitForSingleObject(info->hKeepAliveEvent, dwInterval); if (serverThreadHandle == NULL) // connection lost, end break; - if (dwWait == WAIT_TIMEOUT) - { + + // Possible shutdown in progress + if (dwWait == WAIT_IO_COMPLETION) + break; + + if (dwWait == WAIT_TIMEOUT) { // Send a keep alive packet to server packet.wLen = 0; write_flap(&packet, ICQ_PING_CHAN); sendServPacket(&packet); } - else if (dwWait == WAIT_IO_COMPLETION) - // Possible shutdown in progress - if (Miranda_Terminated()) break; - else - break; } debugLogA("Keep alive thread ended."); @@ -65,19 +62,17 @@ void __cdecl CIcqProto::KeepAliveThread(void *arg) info->hKeepAliveEvent = NULL; } - void CIcqProto::StartKeepAlive(serverthread_info *info) { if (info->hKeepAliveEvent) // start only once return; if (getByte("KeepAlive", DEFAULT_KEEPALIVE_ENABLED)) - CloseHandle( ForkThreadEx(&CIcqProto::KeepAliveThread, info, 0)); + ForkThread(&CIcqProto::KeepAliveThread, info); } - void CIcqProto::StopKeepAlive(serverthread_info *info) -{ // finish keep alive thread +{ if (info->hKeepAliveEvent) SetEvent(info->hKeepAliveEvent); } diff --git a/protocols/IcqOscarJ/src/fam_01service.cpp b/protocols/IcqOscarJ/src/fam_01service.cpp index 2debcedfeb..148bfecf83 100644 --- a/protocols/IcqOscarJ/src/fam_01service.cpp +++ b/protocols/IcqOscarJ/src/fam_01service.cpp @@ -848,7 +848,7 @@ void CIcqProto::handleServUINSettings(int nPort, serverthread_info *info) packDWord(&packet, dwDirectCookie); // DC Cookie packDWord(&packet, WEBFRONTPORT); // Web front port packDWord(&packet, CLIENTFEATURES); // Client features - packDWord(&packet, 0x7fffffff); // Abused timestamp + packDWord(&packet, 0x7fffffff); // Abused timestamp packDWord(&packet, ICQ_PLUG_VERSION); // Abused timestamp if (ServiceExists("SecureIM/IsContactSecured")) packDWord(&packet, 0x5AFEC0DE); // SecureIM Abuse @@ -929,16 +929,15 @@ void CIcqProto::handleServUINSettings(int nPort, serverthread_info *info) // login sequence is complete enter logged-in mode info->bLoggedIn = 1; - m_bConnectionLost = FALSE; + m_bConnectionLost = false; // enable auto info-update routine - icq_EnableUserLookup(TRUE); + icq_EnableUserLookup(true); - if (!info->isMigrating) - { /* Get Offline Messages Reqeust */ + if (!info->isMigrating) { + // Get Offline Messages Reqeust cookie_offline_messages *ack = (cookie_offline_messages*)SAFE_MALLOC(sizeof(cookie_offline_messages)); - if (ack) - { + if (ack) { DWORD dwCookie = AllocateCookie(CKT_OFFLINEMESSAGE, ICQ_MSG_CLI_REQ_OFFLINE, 0, ack); serverPacketInit(&packet, 10); @@ -946,8 +945,7 @@ void CIcqProto::handleServUINSettings(int nPort, serverthread_info *info) sendServPacket(&packet); } - else - icq_LogMessage(LOG_WARNING, LPGEN("Failed to request offline messages. They may be received next time you log in.")); + else icq_LogMessage(LOG_WARNING, LPGEN("Failed to request offline messages. They may be received next time you log in.")); // Update our information from the server sendOwnerInfoRequest(); @@ -958,8 +956,7 @@ void CIcqProto::handleServUINSettings(int nPort, serverthread_info *info) // Start sending Keep-Alive packets StartKeepAlive(info); - if (m_bAvatarsEnabled) - { // Send SNAC 1,4 - request avatar family 0x10 connection + if (m_bAvatarsEnabled) { // Send SNAC 1,4 - request avatar family 0x10 connection icq_requestnewfamily(ICQ_AVATAR_FAMILY, &CIcqProto::StartAvatarThread); m_avatarsConnectionPending = TRUE; @@ -971,8 +968,7 @@ void CIcqProto::handleServUINSettings(int nPort, serverthread_info *info) } info->isMigrating = 0; - if (m_bAimEnabled) - { + if (m_bAimEnabled) { char **szAwayMsg = NULL; icq_lock l(m_modeMsgsMutex); diff --git a/protocols/IcqOscarJ/src/icq_avatar.cpp b/protocols/IcqOscarJ/src/icq_avatar.cpp index 58dda500da..cf4d57fc11 100644 --- a/protocols/IcqOscarJ/src/icq_avatar.cpp +++ b/protocols/IcqOscarJ/src/icq_avatar.cpp @@ -178,15 +178,13 @@ BYTE* calcMD5HashOfFile(const TCHAR *tszFile) int CIcqProto::IsAvatarChanged(MCONTACT hContact, const BYTE *pHash, int nHashLen) { DBVARIANT dbvSaved = { 0 }; - if (!getSetting(hContact, "AvatarSaved", &dbvSaved)) { - if ((dbvSaved.cpbVal != nHashLen) || memcmp(dbvSaved.pbVal, pHash, nHashLen)) { // the hashes are different - db_free(&dbvSaved); - return 2; - } - db_free(&dbvSaved); - return 0; // hash is there and is the same - Success - } - return 1; // saved Avatar hash is missing + if (getSetting(hContact, "AvatarSaved", &dbvSaved)) + return 1; // saved Avatar hash is missing + + // are the hashes different? + int ret = (dbvSaved.cpbVal != nHashLen || memcmp(dbvSaved.pbVal, pHash, nHashLen)) ? 2 : 0; + db_free(&dbvSaved); + return ret; } void CIcqProto::StartAvatarThread(HANDLE hConn, char *cookie, WORD cookieLen) // called from event @@ -229,18 +227,18 @@ void CIcqProto::StartAvatarThread(HANDLE hConn, char *cookie, WORD cookieLen) // if (m_avatarsConnection && m_avatarsConnection->isPending()) { debugLogA("Avatar, Multiple start thread attempt, ignored."); - NetLib_CloseConnection(&hConn, FALSE); + NetLib_CloseConnection(&hConn, false); SAFE_FREE((void**)&cookie); return; } + if (m_avatarsConnection) m_avatarsConnection->closeConnection(); - m_avatarsConnection = new avatars_server_connection(this, hConn, cookie, cookieLen); // the old connection should not be used anymore // connection object created, remove the connection request pending flag - m_avatarsConnectionPending = FALSE; + m_avatarsConnectionPending = false; } void CIcqProto::StopAvatarThread() @@ -1078,7 +1076,6 @@ void avatars_server_connection::connectionThread() } SAFE_FREE((void**)&pCookie); - ppro->debugLogA("Avatar thread ended"); } int avatars_server_connection::sendServerPacket(icq_packet *pPacket) diff --git a/protocols/IcqOscarJ/src/icq_proto.cpp b/protocols/IcqOscarJ/src/icq_proto.cpp index 345be0d80d..d19156bd12 100644 --- a/protocols/IcqOscarJ/src/icq_proto.cpp +++ b/protocols/IcqOscarJ/src/icq_proto.cpp @@ -1689,11 +1689,9 @@ int __cdecl CIcqProto::SetStatus(int iNewStatus) if (hServerConn) { // Connected, Send disconnect packet icq_sendCloseConnection(); - icq_serverDisconnect(); - SetCurrentStatus(ID_STATUS_OFFLINE); - + m_bConnectionLost = false; debugLogA("Logged off."); } } diff --git a/protocols/IcqOscarJ/src/icq_server.cpp b/protocols/IcqOscarJ/src/icq_server.cpp index 2a0e433761..ca86fa0eca 100644 --- a/protocols/IcqOscarJ/src/icq_server.cpp +++ b/protocols/IcqOscarJ/src/icq_server.cpp @@ -37,8 +37,8 @@ void icq_newConnectionReceived(HANDLE hNewConnection, DWORD dwRemoteIP, void *pE void __cdecl CIcqProto::ServerThread(serverthread_start_info *infoParam) { - serverthread_info info = {0}; - info.isLoginServer = 1; + serverthread_info info = { 0 }; + info.isLoginServer = info.bReinitRecver = true; info.wAuthKeyLen = infoParam->wPassLen; null_strcpy((char*)info.szAuthKey, infoParam->szPass, info.wAuthKeyLen); // store server port @@ -103,16 +103,15 @@ void __cdecl CIcqProto::ServerThread(serverthread_start_info *infoParam) } // This is the "infinite" loop that receives the packets from the ICQ server + NETLIBPACKETRECVER packetRecv; info.hPacketRecver = (HANDLE)CallService(MS_NETLIB_CREATEPACKETRECVER, (WPARAM)hServerConn, 0x2400); - NETLIBPACKETRECVER packetRecv = { sizeof(packetRecv) }; - packetRecv.dwTimeout = INFINITE; while (serverThreadHandle) { if (info.bReinitRecver) { // we reconnected, reinit struct - info.bReinitRecver = 0; + info.bReinitRecver = false; ZeroMemory(&packetRecv, sizeof(packetRecv)); packetRecv.cbSize = sizeof(packetRecv); - packetRecv.dwTimeout = INFINITE; + packetRecv.dwTimeout = 1000; } int recvResult = CallService(MS_NETLIB_GETMOREPACKETS, (WPARAM)info.hPacketRecver, (LPARAM)&packetRecv); @@ -122,18 +121,16 @@ void __cdecl CIcqProto::ServerThread(serverthread_start_info *infoParam) } if (recvResult == SOCKET_ERROR) { - debugLogA("Abortive closure of server socket, error: %d", GetLastError()); - break; - } - - if (m_iDesiredStatus == ID_STATUS_OFFLINE) { // Disconnect requested, send disconnect packet - icq_sendCloseConnection(); - - // disconnected upon request - m_bConnectionLost = FALSE; - SetCurrentStatus(ID_STATUS_OFFLINE); - - debugLogA("Logged off."); + DWORD dwError = GetLastError(); + if (dwError == ERROR_TIMEOUT) { + if (m_iDesiredStatus == ID_STATUS_OFFLINE) + break; + continue; + } + if (dwError == WSAESHUTDOWN) // ok, we're going offline + break; + + debugLogA("Abortive closure of server socket, error: %d", dwError); break; } @@ -161,7 +158,7 @@ void __cdecl CIcqProto::ServerThread(serverthread_start_info *infoParam) icq_LogMessage(LOG_FATAL, LPGEN("Connection failed.\nLogin sequence failed for unknown reason.\nTry again later.")); // set flag indicating we were kicked out - m_bConnectionLost = TRUE; + m_bConnectionLost = true; SetCurrentStatus(ID_STATUS_OFFLINE); } @@ -181,15 +178,16 @@ void __cdecl CIcqProto::ServerThread(serverthread_start_info *infoParam) // Offline all contacts debugLogA("Setting offline status to contacts..."); for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) { + if (getContactStatus(hContact) == ID_STATUS_OFFLINE) + continue; + + setWord(hContact, "Status", ID_STATUS_OFFLINE); + DWORD dwUIN; uid_str szUID; if (!getContactUid(hContact, &dwUIN, &szUID)) { - if (getContactStatus(hContact) != ID_STATUS_OFFLINE) { - setWord(hContact, "Status", ID_STATUS_OFFLINE); - - char tmp = 0; - handleXStatusCaps(dwUIN, szUID, hContact, (BYTE*)&tmp, 0, &tmp, 0); - } + char tmp = 0; + handleXStatusCaps(dwUIN, szUID, hContact, (BYTE*)&tmp, 0, &tmp, 0); } } @@ -224,6 +222,8 @@ void CIcqProto::icq_serverDisconnect() CloseHandle(serverThreadHandle); serverThreadHandle = NULL; } + + SetCurrentStatus(ID_STATUS_OFFLINE); } int CIcqProto::handleServerPackets(BYTE *buf, int len, serverthread_info *info) @@ -298,8 +298,7 @@ void CIcqProto::sendServPacket(icq_packet *pPacket) // make sure to have the connection handle connectionHandleMutex->Enter(); - if (hServerConn) - { + if (hServerConn) { int nSendResult; // This critsec makes sure that the sequence order doesn't get screwed up @@ -325,19 +324,14 @@ void CIcqProto::sendServPacket(icq_packet *pPacket) if (dwErrorCode != WSAESHUTDOWN) icq_LogUsingErrorCode(LOG_ERROR, GetLastError(), LPGEN("Your connection with the ICQ server was abortively closed")); icq_serverDisconnect(); - - if (m_iStatus != ID_STATUS_OFFLINE) - SetCurrentStatus(ID_STATUS_OFFLINE); } - else - { // Rates management + else { // Rates management icq_lock l(m_ratesMutex); m_rates->packetSent(pPacket); } } - else - { + else { connectionHandleMutex->Leave(); debugLogA("Error: Failed to send packet (no connection)"); } -- cgit v1.2.3