From c47ca004ba979d23a86211393c9e35deadd66c46 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sat, 24 Jan 2015 16:35:14 +0000 Subject: adaptation of WhatsApp for protocol version 1.5/2.0 git-svn-id: http://svn.miranda-ng.org/main/trunk@11898 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/WhatsApp/src/WASocketConnection.cpp | 49 ++++++++++++++++++--------- 1 file changed, 33 insertions(+), 16 deletions(-) (limited to 'protocols/WhatsApp/src/WASocketConnection.cpp') diff --git a/protocols/WhatsApp/src/WASocketConnection.cpp b/protocols/WhatsApp/src/WASocketConnection.cpp index b5d1b0235c..41d0b78663 100644 --- a/protocols/WhatsApp/src/WASocketConnection.cpp +++ b/protocols/WhatsApp/src/WASocketConnection.cpp @@ -101,36 +101,38 @@ void WASocketConnection::write(const std::vector& bytes, int leng unsigned char WASocketConnection::read() { - char c; - SetLastError(0); - int result; - //do { - result = Netlib_Recv(this->hConn, &c, 1, 0 /*MSG_NOHTTPGATEWAYWRAP | MSG_NODUMP*/); - //} while (WSAGetLastError() == EINTR); - if (result <= 0) { + + char c; + int result = Netlib_Recv(this->hConn, &c, 1, 0); + if (result <= 0) throw WAException(getLastErrorMsg(), WAException::SOCKET_EX, WAException::SOCKET_EX_RECV); - } + return c; } +int WASocketConnection::read(unsigned char *buf, int length) +{ + int result = Netlib_Recv(this->hConn, (char*)buf, length, 0); + if (result <= 0) + throw WAException(getLastErrorMsg(), WAException::SOCKET_EX, WAException::SOCKET_EX_RECV); + + return result; +} + int WASocketConnection::read(std::vector& b, int off, int length) { - if (off < 0 || length < 0) { + if (off < 0 || length < 0) throw new WAException("Out of bounds", WAException::SOCKET_EX, WAException::SOCKET_EX_RECV); - } - char* buffer = new char[length]; - int result = Netlib_Recv(this->hConn, buffer, length, MSG_NOHTTPGATEWAYWRAP | MSG_NODUMP); - if (result <= 0) { + char* buffer = (char*)_alloca(length); + int result = Netlib_Recv(this->hConn, buffer, length, MSG_NOHTTPGATEWAYWRAP | MSG_NODUMP); + if (result <= 0) throw WAException(getLastErrorMsg(), WAException::SOCKET_EX, WAException::SOCKET_EX_RECV); - } for (int i = 0; i < result; i++) b[off + i] = buffer[i]; - delete[] buffer; - return result; } @@ -139,6 +141,21 @@ void WASocketConnection::forceShutdown() Netlib_Shutdown(this->hConn); } +void WASocketConnection::dump(const void *pData, int length) +{ + BYTE *pBuf = (BYTE*)pData; + while (length > 0) { + int portion = (length < 16) ? length : 16; + + char str[100]; + for (int i = 0; i < portion; i++) + sprintf(str + i * 3, "%02X ", *pBuf++); + Netlib_Logf(WASocketConnection::hNetlibUser, "DATA: %s", str); + + length -= portion; + } +} + WASocketConnection::~WASocketConnection() { this->forceShutdown(); -- cgit v1.2.3