RpcConnection now uses the common idiom of peekHdr-getMsgLen-waitForLenBytes-readMsg for reading RPC messages from client
This commit is contained in:
parent
f7c3c06845
commit
87982a4227
@ -42,8 +42,6 @@ RpcConnection::RpcConnection(int socketDescriptor,
|
|||||||
|
|
||||||
isPending = false;
|
isPending = false;
|
||||||
pendingMethodId = -1; // don't care as long as isPending is false
|
pendingMethodId = -1; // don't care as long as isPending is false
|
||||||
|
|
||||||
parsing = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RpcConnection::~RpcConnection()
|
RpcConnection::~RpcConnection()
|
||||||
@ -185,33 +183,39 @@ void RpcConnection::on_clientSock_error(QAbstractSocket::SocketError socketError
|
|||||||
void RpcConnection::on_clientSock_dataAvail()
|
void RpcConnection::on_clientSock_dataAvail()
|
||||||
{
|
{
|
||||||
uchar msg[PB_HDR_SIZE];
|
uchar msg[PB_HDR_SIZE];
|
||||||
int msgLen;
|
int msgLen;
|
||||||
#if 0
|
quint16 type, method;
|
||||||
static bool parsing = false;
|
quint32 len;
|
||||||
static quint16 type, method;
|
|
||||||
static quint32 len;
|
|
||||||
#endif
|
|
||||||
const ::google::protobuf::MethodDescriptor *methodDesc;
|
const ::google::protobuf::MethodDescriptor *methodDesc;
|
||||||
::google::protobuf::Message *req, *resp;
|
::google::protobuf::Message *req, *resp;
|
||||||
PbRpcController *controller;
|
PbRpcController *controller;
|
||||||
|
|
||||||
if (!parsing)
|
// 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)
|
if (clientSock->bytesAvailable() < PB_HDR_SIZE)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
msgLen = clientSock->read((char*)msg, PB_HDR_SIZE);
|
msgLen = clientSock->peek((char*)msg, PB_HDR_SIZE);
|
||||||
|
if (msgLen != PB_HDR_SIZE) {
|
||||||
Q_ASSERT(msgLen == PB_HDR_SIZE);
|
qWarning("asked to peek %d bytes, was given only %d bytes",
|
||||||
|
PB_HDR_SIZE, msgLen);
|
||||||
type = qFromBigEndian<quint16>(&msg[0]);
|
return;
|
||||||
method = qFromBigEndian<quint16>(&msg[2]);
|
|
||||||
len = qFromBigEndian<quint32>(&msg[4]);
|
|
||||||
//qDebug("type = %d, method = %d, len = %d", type, method, len);
|
|
||||||
|
|
||||||
parsing = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
len = qFromBigEndian<quint32>(&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<quint16>(&msg[0]);
|
||||||
|
method = qFromBigEndian<quint16>(&msg[2]);
|
||||||
|
len = qFromBigEndian<quint32>(&msg[4]);
|
||||||
|
//qDebug("type = %d, method = %d, len = %d", type, method, len);
|
||||||
|
|
||||||
if (type != PB_MSG_TYPE_REQUEST)
|
if (type != PB_MSG_TYPE_REQUEST)
|
||||||
{
|
{
|
||||||
qDebug("server(%s): unexpected msg type %d (expected %d)", __FUNCTION__,
|
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,
|
google::protobuf::NewCallback(this, &RpcConnection::sendRpcReply,
|
||||||
controller));
|
controller));
|
||||||
|
|
||||||
parsing = false;
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_error_exit:
|
_error_exit:
|
||||||
inStream->Skip(len);
|
inStream->Skip(len);
|
||||||
_error_exit2:
|
_error_exit2:
|
||||||
parsing = false;
|
|
||||||
qDebug("server(%s): discarding msg from client", __FUNCTION__);
|
qDebug("server(%s): discarding msg from client", __FUNCTION__);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -65,11 +65,6 @@ private:
|
|||||||
|
|
||||||
bool isPending;
|
bool isPending;
|
||||||
int pendingMethodId;
|
int pendingMethodId;
|
||||||
|
|
||||||
bool parsing;
|
|
||||||
quint16 type;
|
|
||||||
quint16 method;
|
|
||||||
quint32 len;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user