Replaced HTONX() and NTOHX() with qToBigEndian() and qFromBigEndian()

This commit is contained in:
Srivats P. 2010-07-21 14:10:06 +05:30
parent c59306d78d
commit 971713ae0f
3 changed files with 28 additions and 63 deletions

View File

@ -19,6 +19,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include "pbrpcchannel.h" #include "pbrpcchannel.h"
#include <qendian.h>
PbRpcChannel::PbRpcChannel(QHostAddress ip, quint16 port) PbRpcChannel::PbRpcChannel(QHostAddress ip, quint16 port)
{ {
isPending = false; isPending = false;
@ -130,9 +132,9 @@ void PbRpcChannel::CallMethod(
Q_ASSERT(ret == true); Q_ASSERT(ret == true);
len = req->ByteSize(); len = req->ByteSize();
*((quint16*)(msg+0)) = HTONS(PB_MSG_TYPE_REQUEST); // type *((quint16*)(msg+0)) = qToBigEndian(quint16(PB_MSG_TYPE_REQUEST)); // type
*((quint16*)(msg+2)) = HTONS(method->index()); // method id *((quint16*)(msg+2)) = qToBigEndian(quint16(method->index())); // method id
*((quint32*)(msg+4)) = HTONL(len); // len *((quint32*)(msg+4)) = qToBigEndian(quint32(len)); // len
// Avoid printing stats since it happens every couple of seconds // Avoid printing stats since it happens every couple of seconds
if (pendingMethodId != 13) if (pendingMethodId != 13)
@ -147,8 +149,8 @@ void PbRpcChannel::CallMethod(
void PbRpcChannel::on_mpSocket_readyRead() void PbRpcChannel::on_mpSocket_readyRead()
{ {
char msg[MSGBUF_SIZE]; uchar msg[MSGBUF_SIZE];
char *p = (char*)&msg; uchar *p = (uchar*) &msg;
int msgLen; int msgLen;
static bool parsing = false; static bool parsing = false;
static quint16 type, method; static quint16 type, method;
@ -165,13 +167,13 @@ void PbRpcChannel::on_mpSocket_readyRead()
return; return;
} }
msgLen = mpSocket->read(msg, PB_HDR_SIZE); msgLen = mpSocket->read((char*)msg, PB_HDR_SIZE);
Q_ASSERT(msgLen == PB_HDR_SIZE); Q_ASSERT(msgLen == PB_HDR_SIZE);
type = NTOHS(GET16(p+0)); type = qFromBigEndian<quint16>(p+0);
method = NTOHS(GET16(p+2)); method = qFromBigEndian<quint16>(p+2);
len = NTOHL(GET32(p+4)); len = qFromBigEndian<quint32>(p+4);
//BUFDUMP(msg, PB_HDR_SIZE); //BUFDUMP(msg, PB_HDR_SIZE);
//qDebug("type = %hu, method = %hu, len = %u", type, method, len); //qDebug("type = %hu, method = %hu, len = %u", type, method, len);
@ -193,8 +195,8 @@ void PbRpcChannel::on_mpSocket_readyRead()
{ {
int l; int l;
l = mpSocket->read(msg, sizeof(msg)); l = mpSocket->read((char*)msg, sizeof(msg));
blob->write(msg, l); blob->write((char*)msg, l);
cumLen += l; cumLen += l;
} }
@ -229,7 +231,7 @@ void PbRpcChannel::on_mpSocket_readyRead()
return; return;
} }
msgLen = mpSocket->read(msg, sizeof(msg)); msgLen = mpSocket->read((char*)msg, sizeof(msg));
Q_ASSERT((unsigned) msgLen == len); Q_ASSERT((unsigned) msgLen == len);

View File

@ -20,45 +20,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#ifndef _PB_RPC_COMMON_H #ifndef _PB_RPC_COMMON_H
#define _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 // Print a HexDump
#define BUFDUMP(ptr, len) qDebug("%s", QString(QByteArray((char*)(ptr), \ #define BUFDUMP(ptr, len) qDebug("%s", QString(QByteArray((char*)(ptr), \
(len)).toHex()).toAscii().data()); (len)).toHex()).toAscii().data());

View File

@ -20,6 +20,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
//#include "pbhelper.h" //#include "pbhelper.h"
#include "rpcserver.h" #include "rpcserver.h"
#include <qendian.h>
RpcServer::RpcServer() RpcServer::RpcServer()
{ {
server = NULL; server = NULL;
@ -87,9 +89,9 @@ void RpcServer::done(PbRpcController *controller)
len = blob->size(); len = blob->size();
qDebug("is binary blob of len %d", len); qDebug("is binary blob of len %d", len);
*((quint16*)(msg+0)) = HTONS(PB_MSG_TYPE_BINBLOB); // type *((quint16*)(msg+0)) = qToBigEndian(quint16(PB_MSG_TYPE_BINBLOB)); // type
*((quint16*)(msg+2)) = HTONS(pendingMethodId); // method *((quint16*)(msg+2)) = qToBigEndian(quint16(pendingMethodId)); // method
(*(quint32*)(msg+4)) = HTONL(len); // len (*(quint32*)(msg+4)) = qToBigEndian(quint32(len)); // len
clientSock->write(msg, PB_HDR_SIZE); clientSock->write(msg, PB_HDR_SIZE);
@ -118,9 +120,9 @@ void RpcServer::done(PbRpcController *controller)
len = response->ByteSize(); len = response->ByteSize();
*((quint16*)(msg+0)) = HTONS(PB_MSG_TYPE_RESPONSE); // type *((quint16*)(msg+0)) = qToBigEndian(quint16(PB_MSG_TYPE_RESPONSE)); // type
*((quint16*)(msg+2)) = HTONS(pendingMethodId); // method *((quint16*)(msg+2)) = qToBigEndian(quint16(pendingMethodId)); // method
*((quint32*)(msg+4)) = HTONL(len); // len *((quint32*)(msg+4)) = qToBigEndian(quint32(len)); // len
// Avoid printing stats since it happens once every couple of seconds // Avoid printing stats since it happens once every couple of seconds
if (pendingMethodId != 13) if (pendingMethodId != 13)
@ -187,7 +189,7 @@ void RpcServer::when_error(QAbstractSocket::SocketError socketError)
void RpcServer::when_dataAvail() void RpcServer::when_dataAvail()
{ {
char msg[MSGBUF_SIZE]; uchar msg[MSGBUF_SIZE];
int msgLen; int msgLen;
static bool parsing = false; static bool parsing = false;
static quint16 type, method; static quint16 type, method;
@ -201,13 +203,13 @@ void RpcServer::when_dataAvail()
if (clientSock->bytesAvailable() < PB_HDR_SIZE) if (clientSock->bytesAvailable() < PB_HDR_SIZE)
return; return;
msgLen = clientSock->read(msg, PB_HDR_SIZE); msgLen = clientSock->read((char*)msg, PB_HDR_SIZE);
Q_ASSERT(msgLen == PB_HDR_SIZE); Q_ASSERT(msgLen == PB_HDR_SIZE);
type = NTOHS(GET16(&msg[0])); type = qFromBigEndian<quint16>(&msg[0]);
method = NTOHS(GET16(&msg[2])); method = qFromBigEndian<quint16>(&msg[2]);
len = NTOHL(GET32(&msg[4])); len = qFromBigEndian<quint32>(&msg[4]);
//qDebug("type = %d, method = %d, len = %d", type, method, len); //qDebug("type = %d, method = %d, len = %d", type, method, len);
parsing = true; parsing = true;
@ -216,7 +218,7 @@ void RpcServer::when_dataAvail()
if (clientSock->bytesAvailable() < len) if (clientSock->bytesAvailable() < len)
return; return;
msgLen = clientSock->read(msg, sizeof(msg)); msgLen = clientSock->read((char*)msg, sizeof(msg));
Q_ASSERT((unsigned) msgLen == len); Q_ASSERT((unsigned) msgLen == len);
if (type != PB_MSG_TYPE_REQUEST) if (type != PB_MSG_TYPE_REQUEST)