cwUi.cpp : Improve websocket msg buffering.
This commit is contained in:
parent
984cb3a11a
commit
841aa8f744
61
cwUi.cpp
61
cwUi.cpp
@ -89,6 +89,7 @@ namespace cw
|
|||||||
char* recvBuf;
|
char* recvBuf;
|
||||||
unsigned recvBufN;
|
unsigned recvBufN;
|
||||||
unsigned recvBufIdx;
|
unsigned recvBufIdx;
|
||||||
|
unsigned recvShiftN;
|
||||||
|
|
||||||
unsigned* sessA; // sessA[ sessN ] array of wsSessId's
|
unsigned* sessA; // sessA[ sessN ] array of wsSessId's
|
||||||
unsigned sessN;
|
unsigned sessN;
|
||||||
@ -1048,6 +1049,49 @@ namespace cw
|
|||||||
return _setPropertyValue( h, propertyStr,uuId,enableFl ? 1 : 0 );
|
return _setPropertyValue( h, propertyStr,uuId,enableFl ? 1 : 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rc_t _copy_msg_to_recv_buffer( ui_t* p, const void* msg, unsigned msgByteN )
|
||||||
|
{
|
||||||
|
rc_t rc;
|
||||||
|
|
||||||
|
if( p->recvBufIdx + msgByteN > p->recvBufN )
|
||||||
|
rc = cwLogError(kBufTooSmallRC,"The UI input buffer (%i) is too small.", p->recvBufN);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
memcpy(p->recvBuf + p->recvBufIdx, msg, msgByteN );
|
||||||
|
p->recvBufIdx += msgByteN;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* _get_msg_from_recv_buffer( ui_t* p )
|
||||||
|
{
|
||||||
|
const char* msg = nullptr;
|
||||||
|
unsigned i;
|
||||||
|
|
||||||
|
// shift off the previous msg
|
||||||
|
if( p->recvShiftN > 0 )
|
||||||
|
{
|
||||||
|
memmove(p->recvBuf, p->recvBuf+p->recvShiftN, p->recvBufIdx - p->recvShiftN );
|
||||||
|
p->recvShiftN = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// locate the end of the next msg.
|
||||||
|
for(i=0; p->recvBuf[i]!=0 and i<p->recvBufIdx; ++i)
|
||||||
|
{}
|
||||||
|
|
||||||
|
// if the end of the next msg was found
|
||||||
|
if( p->recvBuf[i] == 0 )
|
||||||
|
{
|
||||||
|
p->recvShiftN = i+1;
|
||||||
|
msg = p->recvBuf;
|
||||||
|
}
|
||||||
|
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1089,6 +1133,7 @@ cw::rc_t cw::ui::create(
|
|||||||
p->recvBuf = mem::allocZ<char>(fmtBufByteN);
|
p->recvBuf = mem::allocZ<char>(fmtBufByteN);
|
||||||
p->recvBufN = fmtBufByteN;
|
p->recvBufN = fmtBufByteN;
|
||||||
p->recvBufIdx = 0;
|
p->recvBufIdx = 0;
|
||||||
|
p->recvShiftN = 0;
|
||||||
|
|
||||||
// create the root element
|
// create the root element
|
||||||
if((ele = _createBaseEle(p, nullptr, kRootAppId, kInvalidId, "uiDivId" )) == nullptr || ele->uuId != kRootUuId )
|
if((ele = _createBaseEle(p, nullptr, kRootAppId, kInvalidId, "uiDivId" )) == nullptr || ele->uuId != kRootUuId )
|
||||||
@ -1194,9 +1239,11 @@ cw::rc_t cw::ui::onReceive( handle_t h, unsigned wsSessId, const void* void_msg,
|
|||||||
rc_t rc = kOkRC;
|
rc_t rc = kOkRC;
|
||||||
ui_t* p = _handleToPtr(h);
|
ui_t* p = _handleToPtr(h);
|
||||||
opId_t opId = kInvalidOpId;
|
opId_t opId = kInvalidOpId;
|
||||||
|
ele_t* ele = nullptr;
|
||||||
|
const char* msg = nullptr;
|
||||||
value_t value;
|
value_t value;
|
||||||
ele_t* ele;
|
|
||||||
|
|
||||||
|
/*
|
||||||
const char* src_msg = (const char*)void_msg;
|
const char* src_msg = (const char*)void_msg;
|
||||||
const char* msg = src_msg;
|
const char* msg = src_msg;
|
||||||
|
|
||||||
@ -1233,6 +1280,15 @@ cw::rc_t cw::ui::onReceive( handle_t h, unsigned wsSessId, const void* void_msg,
|
|||||||
// the message is being processed so the buffer will end up empty
|
// the message is being processed so the buffer will end up empty
|
||||||
// (if it was being used)
|
// (if it was being used)
|
||||||
p->recvBufIdx = 0;
|
p->recvBufIdx = 0;
|
||||||
|
*/
|
||||||
|
|
||||||
|
// buffer the incoming msg
|
||||||
|
if((rc = _copy_msg_to_recv_buffer( p, void_msg, msgByteN )) != kOkRC )
|
||||||
|
goto errLabel;
|
||||||
|
|
||||||
|
// remove and and act on each buffered msg
|
||||||
|
while( (msg = _get_msg_from_recv_buffer(p)) != NULL )
|
||||||
|
{
|
||||||
|
|
||||||
// parse the 'opId' from the message
|
// parse the 'opId' from the message
|
||||||
opId = _labelToOpId(msg);
|
opId = _labelToOpId(msg);
|
||||||
@ -1306,7 +1362,8 @@ cw::rc_t cw::ui::onReceive( handle_t h, unsigned wsSessId, const void* void_msg,
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
} // switch opId
|
} // switch opId
|
||||||
|
}
|
||||||
|
errLabel:
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user