channel: Extract function for sending errors that are not responses

This commit is contained in:
Thiago de Arruda 2014-06-20 10:52:59 -03:00
parent c8297e462a
commit c0b0bd07fd

View File

@ -207,12 +207,12 @@ static void parse_msgpack(RStream *rstream, void *data, bool eof)
msgpack_unpacked unpacked; msgpack_unpacked unpacked;
msgpack_unpacked_init(&unpacked); msgpack_unpacked_init(&unpacked);
UnpackResult result; UnpackResult result;
msgpack_packer response;
// Deserialize everything we can. // Deserialize everything we can.
while ((result = msgpack_rpc_unpack(channel->unpacker, &unpacked)) while ((result = msgpack_rpc_unpack(channel->unpacker, &unpacked))
== kUnpackResultOk) { == kUnpackResultOk) {
// Each object is a new msgpack-rpc request and requires an empty response // Each object is a new msgpack-rpc request and requires an empty response
msgpack_packer response;
msgpack_packer_init(&response, channel->sbuffer, msgpack_sbuffer_write); msgpack_packer_init(&response, channel->sbuffer, msgpack_sbuffer_write);
// Perform the call // Perform the call
msgpack_rpc_call(channel->id, &unpacked.data, &response); msgpack_rpc_call(channel->id, &unpacked.data, &response);
@ -234,26 +234,37 @@ static void parse_msgpack(RStream *rstream, void *data, bool eof)
// A not so uncommon cause for this might be deserializing objects with // A not so uncommon cause for this might be deserializing objects with
// a high nesting level: msgpack will break when it's internal parse stack // a high nesting level: msgpack will break when it's internal parse stack
// size exceeds MSGPACK_EMBED_STACK_SIZE(defined as 32 by default) // size exceeds MSGPACK_EMBED_STACK_SIZE(defined as 32 by default)
msgpack_packer_init(&response, channel->sbuffer, msgpack_sbuffer_write); send_error(channel, "Invalid msgpack payload. "
msgpack_pack_array(&response, 4); "This error can also happen when deserializing "
msgpack_pack_int(&response, 1); "an object with high level of nesting");
msgpack_pack_int(&response, 0);
msgpack_rpc_error("Invalid msgpack payload. "
"This error can also happen when deserializing "
"an object with high level of nesting",
&response);
WBuffer *buffer = wstream_new_buffer(xmemdup(channel->sbuffer->data,
channel->sbuffer->size),
channel->sbuffer->size,
free);
if (!channel_write(channel, buffer)) {
return;
}
// Clear the buffer for future calls
msgpack_sbuffer_clear(channel->sbuffer);
} }
} }
static void send_error(Channel *channel, char *msg)
{
msgpack_packer err;
// See src/msgpack/unpack_template.h in msgpack source tree for
// causes for this error(search for 'goto _failed')
//
// A not so uncommon cause for this might be deserializing objects with
// a high nesting level: msgpack will break when it's internal parse stack
// size exceeds MSGPACK_EMBED_STACK_SIZE(defined as 32 by default)
msgpack_packer_init(&err, channel->sbuffer, msgpack_sbuffer_write);
msgpack_pack_array(&err, 4);
msgpack_pack_int(&err, 1);
msgpack_pack_int(&err, 0);
msgpack_rpc_error(msg, &err);
WBuffer *buffer = wstream_new_buffer(xmemdup(channel->sbuffer->data,
channel->sbuffer->size),
channel->sbuffer->size,
free);
if (!channel_write(channel, buffer)) {
return;
}
// Clear the buffer for future calls
msgpack_sbuffer_clear(channel->sbuffer);
}
static bool channel_write(Channel *channel, WBuffer *buffer) static bool channel_write(Channel *channel, WBuffer *buffer)
{ {
bool success; bool success;