From 87982a4227d851dcb19192417204a052792a6d4c Mon Sep 17 00:00:00 2001 From: "Srivats P." Date: Wed, 21 May 2014 05:35:04 +0530 Subject: [PATCH] RpcConnection now uses the common idiom of peekHdr-getMsgLen-waitForLenBytes-readMsg for reading RPC messages from client --- rpc/rpcconn.cpp | 51 +++++++++++++++++++++++++------------------------ rpc/rpcconn.h | 5 ----- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/rpc/rpcconn.cpp b/rpc/rpcconn.cpp index 0e162a0..6f47359 100644 --- a/rpc/rpcconn.cpp +++ b/rpc/rpcconn.cpp @@ -42,8 +42,6 @@ RpcConnection::RpcConnection(int socketDescriptor, isPending = false; pendingMethodId = -1; // don't care as long as isPending is false - - parsing = false; } RpcConnection::~RpcConnection() @@ -185,33 +183,39 @@ void RpcConnection::on_clientSock_error(QAbstractSocket::SocketError socketError void RpcConnection::on_clientSock_dataAvail() { uchar msg[PB_HDR_SIZE]; - int msgLen; -#if 0 - static bool parsing = false; - static quint16 type, method; - static quint32 len; -#endif + int msgLen; + quint16 type, method; + quint32 len; const ::google::protobuf::MethodDescriptor *methodDesc; ::google::protobuf::Message *req, *resp; PbRpcController *controller; - if (!parsing) - { - if (clientSock->bytesAvailable() < PB_HDR_SIZE) - return; + // Do we have enough bytes for a msg header? + // If yes, peek into the header and get msg length + if (clientSock->bytesAvailable() < PB_HDR_SIZE) + return; - msgLen = clientSock->read((char*)msg, PB_HDR_SIZE); - - Q_ASSERT(msgLen == PB_HDR_SIZE); - - type = qFromBigEndian(&msg[0]); - method = qFromBigEndian(&msg[2]); - len = qFromBigEndian(&msg[4]); - //qDebug("type = %d, method = %d, len = %d", type, method, len); - - parsing = true; + msgLen = clientSock->peek((char*)msg, PB_HDR_SIZE); + if (msgLen != PB_HDR_SIZE) { + qWarning("asked to peek %d bytes, was given only %d bytes", + PB_HDR_SIZE, msgLen); + return; } + len = qFromBigEndian(&msg[4]); + + // Is the full msg available to read? If not, wait till such time + if (clientSock->bytesAvailable() < (PB_HDR_SIZE+len)) + return; + + msgLen = clientSock->read((char*)msg, PB_HDR_SIZE); + Q_ASSERT(msgLen == PB_HDR_SIZE); + + type = qFromBigEndian(&msg[0]); + method = qFromBigEndian(&msg[2]); + len = qFromBigEndian(&msg[4]); + //qDebug("type = %d, method = %d, len = %d", type, method, len); + if (type != PB_MSG_TYPE_REQUEST) { qDebug("server(%s): unexpected msg type %d (expected %d)", __FUNCTION__, @@ -276,14 +280,11 @@ void RpcConnection::on_clientSock_dataAvail() google::protobuf::NewCallback(this, &RpcConnection::sendRpcReply, controller)); - parsing = false; - return; _error_exit: inStream->Skip(len); _error_exit2: - parsing = false; qDebug("server(%s): discarding msg from client", __FUNCTION__); return; } diff --git a/rpc/rpcconn.h b/rpc/rpcconn.h index 6d19d12..ce5e5bd 100644 --- a/rpc/rpcconn.h +++ b/rpc/rpcconn.h @@ -65,11 +65,6 @@ private: bool isPending; int pendingMethodId; - - bool parsing; - quint16 type; - quint16 method; - quint32 len; }; #endif