我解决了它,我所要做的就是:
msgpack_unpacker tUnpacker = { 0 };
BOOL bUnpacked = FALSE;
unsigned char abReceivedBytes[1] = { 0 };
msgpack_unpacked tUnpacked = { 0 };
msgpack_unpack_return eUnpackRet = MSGPACK_UNPACK_PARSE_ERROR;
while (FALSE == bUnpacked)
{
// Read here from socket
...
/* Check buffer capacity and reserve more buffer if needed */
if (msgpack_unpacker_buffer_capacity(&tUnpacker) < nNumOfBytes)
{
bResult = msgpack_unpacker_reserve_buffer(&tUnpacker, nNumOfBytes);
if (FALSE == bResult)
{
// Handle error
}
}
/* feeds the buffer. */
memcpy(msgpack_unpacker_buffer(&tUnpacker), &abReceivedBytes[0], nNumOfBytes);
msgpack_unpacker_buffer_consumed(&tUnpacker, nNumOfBytes);
/* initialize the unpacked message structure */
msgpack_unpacked_init(&tUnpacked);
eUnpackRet = msgpack_unpacker_next(&tUnpacker, &tUnpacked);
switch (eUnpackRet) {
case MSGPACK_UNPACK_SUCCESS:
/* Extract msgpack_object and use it. */
bUnpacked = TRUE;
break;
case MSGPACK_UNPACK_CONTINUE:
break;
default:
// Handle failure
...
}
这就是您可以使用 msgpack 读取流的方式(这几乎等同于我的问题中的 python 脚本)