From 971713ae0f1d86f41c5fd1c8f7b51fbe969164b1 Mon Sep 17 00:00:00 2001 From: "Srivats P." Date: Wed, 21 Jul 2010 14:10:06 +0530 Subject: [PATCH] Replaced HTONX() and NTOHX() with qToBigEndian() and qFromBigEndian() --- rpc/pbrpcchannel.cpp | 26 ++++++++++++++------------ rpc/pbrpccommon.h | 39 --------------------------------------- rpc/rpcserver.cpp | 26 ++++++++++++++------------ 3 files changed, 28 insertions(+), 63 deletions(-) diff --git a/rpc/pbrpcchannel.cpp b/rpc/pbrpcchannel.cpp index c89a2d6..47f1611 100644 --- a/rpc/pbrpcchannel.cpp +++ b/rpc/pbrpcchannel.cpp @@ -19,6 +19,8 @@ along with this program. If not, see #include "pbrpcchannel.h" +#include + PbRpcChannel::PbRpcChannel(QHostAddress ip, quint16 port) { isPending = false; @@ -130,9 +132,9 @@ void PbRpcChannel::CallMethod( Q_ASSERT(ret == true); len = req->ByteSize(); - *((quint16*)(msg+0)) = HTONS(PB_MSG_TYPE_REQUEST); // type - *((quint16*)(msg+2)) = HTONS(method->index()); // method id - *((quint32*)(msg+4)) = HTONL(len); // len + *((quint16*)(msg+0)) = qToBigEndian(quint16(PB_MSG_TYPE_REQUEST)); // type + *((quint16*)(msg+2)) = qToBigEndian(quint16(method->index())); // method id + *((quint32*)(msg+4)) = qToBigEndian(quint32(len)); // len // Avoid printing stats since it happens every couple of seconds if (pendingMethodId != 13) @@ -147,8 +149,8 @@ void PbRpcChannel::CallMethod( void PbRpcChannel::on_mpSocket_readyRead() { - char msg[MSGBUF_SIZE]; - char *p = (char*)&msg; + uchar msg[MSGBUF_SIZE]; + uchar *p = (uchar*) &msg; int msgLen; static bool parsing = false; static quint16 type, method; @@ -165,13 +167,13 @@ void PbRpcChannel::on_mpSocket_readyRead() return; } - msgLen = mpSocket->read(msg, PB_HDR_SIZE); + msgLen = mpSocket->read((char*)msg, PB_HDR_SIZE); Q_ASSERT(msgLen == PB_HDR_SIZE); - type = NTOHS(GET16(p+0)); - method = NTOHS(GET16(p+2)); - len = NTOHL(GET32(p+4)); + type = qFromBigEndian(p+0); + method = qFromBigEndian(p+2); + len = qFromBigEndian(p+4); //BUFDUMP(msg, PB_HDR_SIZE); //qDebug("type = %hu, method = %hu, len = %u", type, method, len); @@ -193,8 +195,8 @@ void PbRpcChannel::on_mpSocket_readyRead() { int l; - l = mpSocket->read(msg, sizeof(msg)); - blob->write(msg, l); + l = mpSocket->read((char*)msg, sizeof(msg)); + blob->write((char*)msg, l); cumLen += l; } @@ -229,7 +231,7 @@ void PbRpcChannel::on_mpSocket_readyRead() return; } - msgLen = mpSocket->read(msg, sizeof(msg)); + msgLen = mpSocket->read((char*)msg, sizeof(msg)); Q_ASSERT((unsigned) msgLen == len); diff --git a/rpc/pbrpccommon.h b/rpc/pbrpccommon.h index 5cbbb57..0f4acf9 100644 --- a/rpc/pbrpccommon.h +++ b/rpc/pbrpccommon.h @@ -20,45 +20,6 @@ along with this program. If not, see #ifndef _PB_RPC_COMMON_H #define _PB_RPC_COMMON_H -//! \todo (LOW) check which one is right - wrong one seems to be working!!!!! -#if 0 -#define GET16(p) (quint16)( \ - (*((quint8*)(p)+0) << 8 ) \ - | (*((quint8*)(p)+1))) -#else -#define GET16(p) (quint16)( \ - (*((quint8*)(p)+1) << 8 ) \ - | (*((quint8*)(p)+0))) -#define GET32(p) (quint32)( \ - (*((quint8*)(p)+3) << 24) \ - | (*((quint8*)(p)+2) << 16) \ - | (*((quint8*)(p)+1) << 8 ) \ - | (*((quint8*)(p)+0))) -#endif - -#define BYTESWAP4(x) \ - (((x & 0xFF000000) >> 24) | \ - ((x & 0x00FF0000) >> 8) | \ - ((x & 0x0000FF00) << 8) | \ - ((x & 0x000000FF) << 24)) - -#define BYTESWAP2(x) \ - (((x & 0xFF00) >> 8) | \ - ((x & 0x00FF) << 8)) - -//! \todo (LOW) : portability -#if 1 -#define HTONL(x) BYTESWAP4(x) -#define NTOHL(x) BYTESWAP4(x) -#define HTONS(x) BYTESWAP2(x) -#define NTOHS(x) BYTESWAP2(x) -#else -#define HTONL(x) (x) -#define NTOHL(x) (x) -#define HTONS(x) (x) -#define NTOHS(x) (x) -#endif - // Print a HexDump #define BUFDUMP(ptr, len) qDebug("%s", QString(QByteArray((char*)(ptr), \ (len)).toHex()).toAscii().data()); diff --git a/rpc/rpcserver.cpp b/rpc/rpcserver.cpp index 4b86b25..d48de21 100644 --- a/rpc/rpcserver.cpp +++ b/rpc/rpcserver.cpp @@ -20,6 +20,8 @@ along with this program. If not, see //#include "pbhelper.h" #include "rpcserver.h" +#include + RpcServer::RpcServer() { server = NULL; @@ -87,9 +89,9 @@ void RpcServer::done(PbRpcController *controller) len = blob->size(); qDebug("is binary blob of len %d", len); - *((quint16*)(msg+0)) = HTONS(PB_MSG_TYPE_BINBLOB); // type - *((quint16*)(msg+2)) = HTONS(pendingMethodId); // method - (*(quint32*)(msg+4)) = HTONL(len); // len + *((quint16*)(msg+0)) = qToBigEndian(quint16(PB_MSG_TYPE_BINBLOB)); // type + *((quint16*)(msg+2)) = qToBigEndian(quint16(pendingMethodId)); // method + (*(quint32*)(msg+4)) = qToBigEndian(quint32(len)); // len clientSock->write(msg, PB_HDR_SIZE); @@ -118,9 +120,9 @@ void RpcServer::done(PbRpcController *controller) len = response->ByteSize(); - *((quint16*)(msg+0)) = HTONS(PB_MSG_TYPE_RESPONSE); // type - *((quint16*)(msg+2)) = HTONS(pendingMethodId); // method - *((quint32*)(msg+4)) = HTONL(len); // len + *((quint16*)(msg+0)) = qToBigEndian(quint16(PB_MSG_TYPE_RESPONSE)); // type + *((quint16*)(msg+2)) = qToBigEndian(quint16(pendingMethodId)); // method + *((quint32*)(msg+4)) = qToBigEndian(quint32(len)); // len // Avoid printing stats since it happens once every couple of seconds if (pendingMethodId != 13) @@ -187,7 +189,7 @@ void RpcServer::when_error(QAbstractSocket::SocketError socketError) void RpcServer::when_dataAvail() { - char msg[MSGBUF_SIZE]; + uchar msg[MSGBUF_SIZE]; int msgLen; static bool parsing = false; static quint16 type, method; @@ -201,13 +203,13 @@ void RpcServer::when_dataAvail() if (clientSock->bytesAvailable() < PB_HDR_SIZE) return; - msgLen = clientSock->read(msg, PB_HDR_SIZE); + msgLen = clientSock->read((char*)msg, PB_HDR_SIZE); Q_ASSERT(msgLen == PB_HDR_SIZE); - type = NTOHS(GET16(&msg[0])); - method = NTOHS(GET16(&msg[2])); - len = NTOHL(GET32(&msg[4])); + type = qFromBigEndian(&msg[0]); + method = qFromBigEndian(&msg[2]); + len = qFromBigEndian(&msg[4]); //qDebug("type = %d, method = %d, len = %d", type, method, len); parsing = true; @@ -216,7 +218,7 @@ void RpcServer::when_dataAvail() if (clientSock->bytesAvailable() < len) return; - msgLen = clientSock->read(msg, sizeof(msg)); + msgLen = clientSock->read((char*)msg, sizeof(msg)); Q_ASSERT((unsigned) msgLen == len); if (type != PB_MSG_TYPE_REQUEST)