static int RecvWithTimeoutTime(NetlibConnection *nlc, unsigned dwTimeoutTime, char *buf, int len, int flags) { DWORD dwTimeNow; if (!sslApi.pending(nlc->hSsl)) { while ((dwTimeNow = GetTickCount()) < dwTimeoutTime) { unsigned dwDeltaTime = min(dwTimeoutTime - dwTimeNow, 1000); int res = WaitUntilReadable(nlc->s, dwDeltaTime); switch (res) { case SOCKET_ERROR: return SOCKET_ERROR; case 1: return NLRecv(nlc, buf, len, flags); } if (nlc->termRequested || Miranda_Terminated()) return 0; } SetLastError(ERROR_TIMEOUT); return SOCKET_ERROR; } return NLRecv(nlc, buf, len, flags); }
bool NetlibReconnect(NetlibConnection *nlc) { char buf[4]; bool opened = nlc->s != INVALID_SOCKET; if (opened) { switch (WaitUntilReadable(nlc->s, 0, true)) { case SOCKET_ERROR: opened = false; break; case 0: opened = true; break; case 1: opened = recv(nlc->s, buf, 1, MSG_PEEK) > 0; break; } if (!opened) NetlibDoClose(nlc, true); } if (!opened) { if (Miranda_Terminated()) return false; if (nlc->usingHttpGateway) { nlc->proxyAuthNeeded = true; return my_connect(nlc, &nlc->nloc); } return NetlibDoConnect(nlc); } return true; }
bool RecvUntilTimeout(NetlibConnection *nlc, char *buf, int len, int flags, DWORD dwTimeout) { int nReceived = 0; DWORD dwTimeNow, dwCompleteTime = GetTickCount() + dwTimeout; while ((dwTimeNow = GetTickCount()) < dwCompleteTime) { if (WaitUntilReadable(nlc->s, dwCompleteTime - dwTimeNow) <= 0) return false; nReceived = NLRecv(nlc, buf, len, flags); if (nReceived <= 0) return false; buf += nReceived; len -= nReceived; if (len <= 0) return true; } SetLastError(ERROR_TIMEOUT); return false; }
INT_PTR NetlibPacketRecverGetMore(WPARAM wParam, LPARAM lParam) { struct NetlibPacketRecver *nlpr = (struct NetlibPacketRecver*)wParam; NETLIBPACKETRECVER *nlprParam = (NETLIBPACKETRECVER*)lParam; if (GetNetlibHandleType(nlpr) != NLH_PACKETRECVER || nlprParam == NULL || nlprParam->cbSize != sizeof(NETLIBPACKETRECVER) || nlprParam->bytesUsed > nlpr->packetRecver.bytesAvailable) { SetLastError(ERROR_INVALID_PARAMETER); return SOCKET_ERROR; } if (Miranda_Terminated()) { /* HACK: Lame, break while loops of protocols that can't kill their while loops, (cough, ICQ, cough) */ SetLastError(ERROR_TIMEOUT); return SOCKET_ERROR; } nlpr->packetRecver.dwTimeout = nlprParam->dwTimeout; if (nlprParam->bytesUsed == 0) { if (nlpr->packetRecver.bytesAvailable == nlpr->packetRecver.bufferSize) { nlpr->packetRecver.bytesAvailable = 0; NetlibLogf(nlpr->nlc->nlu, "Packet recver: packet overflowed buffer, ditching"); } } else { MoveMemory(nlpr->packetRecver.buffer, nlpr->packetRecver.buffer + nlprParam->bytesUsed, nlpr->packetRecver.bytesAvailable - nlprParam->bytesUsed); nlpr->packetRecver.bytesAvailable -= nlprParam->bytesUsed; } if (nlprParam->dwTimeout != INFINITE) { if (!si.pending(nlpr->nlc->hSsl) && WaitUntilReadable(nlpr->nlc->s, nlprParam->dwTimeout) <= 0) { *nlprParam = nlpr->packetRecver; return SOCKET_ERROR; } } INT_PTR recvResult = NLRecv(nlpr->nlc, (char*)nlpr->packetRecver.buffer + nlpr->packetRecver.bytesAvailable, nlpr->packetRecver.bufferSize - nlpr->packetRecver.bytesAvailable, 0); if (recvResult > 0) nlpr->packetRecver.bytesAvailable += recvResult; *nlprParam = nlpr->packetRecver; return recvResult; }