From c1d0bec52e19d51117b05d3f7d80a366df657817 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sat, 12 Jul 2014 15:42:51 +0000 Subject: minor code cleaning git-svn-id: http://svn.miranda-ng.org/main/trunk@9774 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- src/modules/netlib/netlibpktrecver.cpp | 14 +++--- src/modules/netlib/netlibsock.cpp | 85 +++++++++++++--------------------- 2 files changed, 41 insertions(+), 58 deletions(-) (limited to 'src') diff --git a/src/modules/netlib/netlibpktrecver.cpp b/src/modules/netlib/netlibpktrecver.cpp index 9ba78d299a..b8abc81144 100644 --- a/src/modules/netlib/netlibpktrecver.cpp +++ b/src/modules/netlib/netlibpktrecver.cpp @@ -53,9 +53,8 @@ INT_PTR NetlibPacketRecverGetMore(WPARAM wParam, LPARAM lParam) { struct NetlibPacketRecver *nlpr = (struct NetlibPacketRecver*)wParam; NETLIBPACKETRECVER *nlprParam = (NETLIBPACKETRECVER*)lParam; - INT_PTR recvResult; - if (GetNetlibHandleType(nlpr) != NLH_PACKETRECVER || nlprParam == NULL || nlprParam->cbSize != sizeof(NETLIBPACKETRECVER) || nlprParam->bytesUsed>nlpr->packetRecver.bytesAvailable) { + if (GetNetlibHandleType(nlpr) != NLH_PACKETRECVER || nlprParam == NULL || nlprParam->cbSize != sizeof(NETLIBPACKETRECVER) || nlprParam->bytesUsed > nlpr->packetRecver.bytesAvailable) { SetLastError(ERROR_INVALID_PARAMETER); return SOCKET_ERROR; } @@ -71,17 +70,20 @@ INT_PTR NetlibPacketRecverGetMore(WPARAM wParam, LPARAM lParam) } } else { - MoveMemory(nlpr->packetRecver.buffer, nlpr->packetRecver.buffer+nlprParam->bytesUsed, nlpr->packetRecver.bytesAvailable-nlprParam->bytesUsed); - nlpr->packetRecver.bytesAvailable-=nlprParam->bytesUsed; + 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; } } - 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; + + 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; } diff --git a/src/modules/netlib/netlibsock.cpp b/src/modules/netlib/netlibsock.cpp index e3afd442cd..c494cc5ce4 100644 --- a/src/modules/netlib/netlibsock.cpp +++ b/src/modules/netlib/netlibsock.cpp @@ -33,8 +33,7 @@ INT_PTR NetlibSend(WPARAM wParam, LPARAM lParam) NETLIBBUFFER *nlb = (NETLIBBUFFER*)lParam; INT_PTR result; - if (nlb == NULL) - { + if (nlb == NULL) { SetLastError(ERROR_INVALID_PARAMETER); return SOCKET_ERROR; } @@ -42,10 +41,8 @@ INT_PTR NetlibSend(WPARAM wParam, LPARAM lParam) if (!NetlibEnterNestedCS(nlc, NLNCS_SEND)) return SOCKET_ERROR; - if (nlc->usingHttpGateway && !(nlb->flags & MSG_RAW)) - { - if (!(nlb->flags & MSG_NOHTTPGATEWAYWRAP) && nlc->nlu->user.pfnHttpGatewayWrapSend) - { + if (nlc->usingHttpGateway && !(nlb->flags & MSG_RAW)) { + if (!(nlb->flags & MSG_NOHTTPGATEWAYWRAP) && nlc->nlu->user.pfnHttpGatewayWrapSend) { NetlibDumpData(nlc, (PBYTE)nlb->buf, nlb->len, 1, nlb->flags); result = nlc->nlu->user.pfnHttpGatewayWrapSend((HANDLE)nlc, (PBYTE)nlb->buf, nlb->len, nlb->flags | MSG_NOHTTPGATEWAYWRAP, NetlibSend); } @@ -82,8 +79,7 @@ INT_PTR NetlibRecv(WPARAM wParam, LPARAM lParam) if (nlc->usingHttpGateway && !(nlb->flags & MSG_RAW)) recvResult = NetlibHttpGatewayRecv(nlc, nlb->buf, nlb->len, nlb->flags); - else - { + else { if (nlc->hSsl) recvResult = si.read(nlc->hSsl, nlb->buf, nlb->len, (nlb->flags & MSG_PEEK) != 0); else @@ -104,15 +100,10 @@ INT_PTR NetlibRecv(WPARAM wParam, LPARAM lParam) static int ConnectionListToSocketList(HANDLE *hConns, fd_set *fd, int& pending) { - NetlibConnection *nlcCheck; - int i; - FD_ZERO(fd); - for (i=0; hConns[i] && hConns[i] != INVALID_HANDLE_VALUE && i < FD_SETSIZE; i++) - { - nlcCheck = (NetlibConnection*)hConns[i]; - if (nlcCheck->handleType != NLH_CONNECTION && nlcCheck->handleType != NLH_BOUNDPORT) - { + for (int i = 0; hConns[i] && hConns[i] != INVALID_HANDLE_VALUE && i < FD_SETSIZE; i++) { + NetlibConnection *nlcCheck = (NetlibConnection*)hConns[i]; + if (nlcCheck->handleType != NLH_CONNECTION && nlcCheck->handleType != NLH_BOUNDPORT) { SetLastError(ERROR_INVALID_DATA); return 0; } @@ -126,8 +117,7 @@ static int ConnectionListToSocketList(HANDLE *hConns, fd_set *fd, int& pending) INT_PTR NetlibSelect(WPARAM, LPARAM lParam) { NETLIBSELECT *nls = (NETLIBSELECT*)lParam; - if (nls == NULL || nls->cbSize != sizeof(NETLIBSELECT)) - { + if (nls == NULL || nls->cbSize != sizeof(NETLIBSELECT)) { SetLastError(ERROR_INVALID_PARAMETER); return SOCKET_ERROR; } @@ -140,11 +130,11 @@ INT_PTR NetlibSelect(WPARAM, LPARAM lParam) fd_set readfd, writefd, exceptfd; WaitForSingleObject(hConnectionHeaderMutex, INFINITE); if (!ConnectionListToSocketList(nls->hReadConns, &readfd, pending) - || !ConnectionListToSocketList(nls->hWriteConns, &writefd, pending) - || !ConnectionListToSocketList(nls->hExceptConns, &exceptfd, pending)) + || !ConnectionListToSocketList(nls->hWriteConns, &writefd, pending) + || !ConnectionListToSocketList(nls->hExceptConns, &exceptfd, pending)) { - ReleaseMutex(hConnectionHeaderMutex); - return SOCKET_ERROR; + ReleaseMutex(hConnectionHeaderMutex); + return SOCKET_ERROR; } ReleaseMutex(hConnectionHeaderMutex); if (pending) @@ -156,8 +146,7 @@ INT_PTR NetlibSelect(WPARAM, LPARAM lParam) INT_PTR NetlibSelectEx(WPARAM, LPARAM lParam) { NETLIBSELECTEX *nls = (NETLIBSELECTEX*)lParam; - if (nls == NULL || nls->cbSize != sizeof(NETLIBSELECTEX)) - { + if (nls == NULL || nls->cbSize != sizeof(NETLIBSELECTEX)) { SetLastError(ERROR_INVALID_PARAMETER); return SOCKET_ERROR; } @@ -170,11 +159,11 @@ INT_PTR NetlibSelectEx(WPARAM, LPARAM lParam) int pending = 0; fd_set readfd, writefd, exceptfd; if (!ConnectionListToSocketList(nls->hReadConns, &readfd, pending) - || !ConnectionListToSocketList(nls->hWriteConns, &writefd, pending) - || !ConnectionListToSocketList(nls->hExceptConns, &exceptfd, pending)) + || !ConnectionListToSocketList(nls->hWriteConns, &writefd, pending) + || !ConnectionListToSocketList(nls->hExceptConns, &exceptfd, pending)) { - ReleaseMutex(hConnectionHeaderMutex); - return SOCKET_ERROR; + ReleaseMutex(hConnectionHeaderMutex); + return SOCKET_ERROR; } ReleaseMutex(hConnectionHeaderMutex); @@ -186,8 +175,7 @@ INT_PTR NetlibSelectEx(WPARAM, LPARAM lParam) This happens for read/write/except */ NetlibConnection *conn = NULL; int j; - for (j = 0; j < FD_SETSIZE; j++) - { + for (j = 0; j < FD_SETSIZE; j++) { conn = (NetlibConnection*)nls->hReadConns[j]; if (conn == NULL || conn == INVALID_HANDLE_VALUE) break; @@ -198,14 +186,12 @@ INT_PTR NetlibSelectEx(WPARAM, LPARAM lParam) else nls->hReadStatus[j] = FD_ISSET(conn->s, &readfd); } - for (j = 0; j < FD_SETSIZE; j++) - { + for (j = 0; j < FD_SETSIZE; j++) { conn = (NetlibConnection*)nls->hWriteConns[j]; if (conn == NULL || conn == INVALID_HANDLE_VALUE) break; nls->hWriteStatus[j] = FD_ISSET(conn->s, &writefd); } - for (j = 0; j < FD_SETSIZE; j++) - { + for (j = 0; j < FD_SETSIZE; j++) { conn = (NetlibConnection*)nls->hExceptConns[j]; if (conn == NULL || conn == INVALID_HANDLE_VALUE) break; nls->hExceptStatus[j] = FD_ISSET(conn->s, &exceptfd); @@ -244,9 +230,8 @@ void NetlibGetConnectionInfo(NetlibConnection* nlc, NETLIBCONNINFO *connInfo) { if (!nlc || !connInfo || connInfo->cbSize < sizeof(NETLIBCONNINFO)) return; - SOCKADDR_INET_M sin = {0}; + SOCKADDR_INET_M sin = { 0 }; int len = sizeof(sin); - if (!getsockname(nlc->s, (PSOCKADDR)&sin, &len)) { connInfo->wPort = ntohs(sin.Ipv4.sin_port); connInfo->dwIpv4 = sin.si_family == AF_INET ? htonl(sin.Ipv4.sin_addr.s_addr) : 0; @@ -266,7 +251,7 @@ inline bool IsAddrGlobal(const IN6_ADDR *a) static NETLIBIPLIST* GetMyIpv6(unsigned flags) { - addrinfo *air = NULL, *ai, hints = {0}; + addrinfo *air = NULL, *ai, hints = { 0 }; const char *szMyHost = ""; hints.ai_family = AF_UNSPEC; @@ -276,29 +261,25 @@ static NETLIBIPLIST* GetMyIpv6(unsigned flags) return NULL; unsigned n = 0; - for (ai = air; ai; ai = ai->ai_next) - { - SOCKADDR_INET_M* iaddr = (SOCKADDR_INET_M*)ai->ai_addr; - if (ai->ai_family == AF_INET || - (ai->ai_family == AF_INET6 && - (!(flags & 1) || IsAddrGlobal(&iaddr->Ipv6.sin6_addr)))) + for (ai = air; ai; ai = ai->ai_next) { + SOCKADDR_INET_M *iaddr = (SOCKADDR_INET_M*)ai->ai_addr; + if (ai->ai_family == AF_INET || (ai->ai_family == AF_INET6 && (!(flags & 1) || IsAddrGlobal(&iaddr->Ipv6.sin6_addr)))) ++n; } NETLIBIPLIST *addr = (NETLIBIPLIST*)mir_calloc(n * 64 + 4); addr->cbNum = n; - unsigned i=0; - for (ai = air; ai; ai = ai->ai_next) - { - SOCKADDR_INET_M* iaddr = (SOCKADDR_INET_M*)ai->ai_addr; - if (ai->ai_family == AF_INET || + unsigned i = 0; + for (ai = air; ai; ai = ai->ai_next) { + SOCKADDR_INET_M *iaddr = (SOCKADDR_INET_M*)ai->ai_addr; + if (ai->ai_family == AF_INET || (ai->ai_family == AF_INET6 && - (!(flags & 1) || IsAddrGlobal(&iaddr->Ipv6.sin6_addr)))) - { + (!(flags & 1) || IsAddrGlobal(&iaddr->Ipv6.sin6_addr)))) { char* szIp = NetlibAddressToString(iaddr); - if (szIp) strcpy(addr->szIp[i++], szIp); + if (szIp) + strcpy(addr->szIp[i++], szIp); mir_free(szIp); } } @@ -319,7 +300,7 @@ static NETLIBIPLIST* GetMyIpv4(void) NETLIBIPLIST *addr = (NETLIBIPLIST*)mir_calloc(n * 64 + 4); addr->cbNum = n; - for (unsigned i=0; i < n; i++) + for (unsigned i = 0; i < n; i++) strcpy(addr->szIp[i], inet_ntoa(*(PIN_ADDR)he->h_addr_list[i])); return addr; -- cgit v1.2.3