在 C 中使用 msgpack 接收流
Receiving a stream with msgpack in C
我正在实现一个从客户端接收字节流的服务器。
流使用 msgpack 序列化,(序列化的第一件事是剩余流的长度)。
我的问题是,在 C 中接收此流的正确方法是什么?
在 python 中,我可以按如下方式执行此操作:
# Feed each byte into the Unpacker
unpacker.feed(client.recv(1))
# Unpack whatever we can from the bytes we have received
for value in unpacker:
print(value)
有什么方法可以用 C 实现吗?
我解决了,我所要做的就是:
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 脚本)
我正在实现一个从客户端接收字节流的服务器。
流使用 msgpack 序列化,(序列化的第一件事是剩余流的长度)。
我的问题是,在 C 中接收此流的正确方法是什么?
在 python 中,我可以按如下方式执行此操作:
# Feed each byte into the Unpacker
unpacker.feed(client.recv(1))
# Unpack whatever we can from the bytes we have received
for value in unpacker:
print(value)
有什么方法可以用 C 实现吗?
我解决了,我所要做的就是:
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 脚本)