From 4bac5e9ce19afd4647ee4d313c9485229d05a334 Mon Sep 17 00:00:00 2001 From: Thiago de Arruda Date: Mon, 26 May 2014 13:39:05 -0300 Subject: API: Refactor: Duplicate/free string arguments coming from msgpack When receiving strings *from* msgpack, we don't need to duplicate/free since the data only lives in the msgpack parse buffer until the end of the call. But in order to reuse `msgpack_rpc_free_object` when sending event data(which is sent *to* msgpack), Strings must be freed, which means they must also be allocated separately. --- src/nvim/os/msgpack_rpc.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src/nvim/os/msgpack_rpc.c') diff --git a/src/nvim/os/msgpack_rpc.c b/src/nvim/os/msgpack_rpc.c index d7ffa6f559..8ca1e0a8c0 100644 --- a/src/nvim/os/msgpack_rpc.c +++ b/src/nvim/os/msgpack_rpc.c @@ -147,9 +147,13 @@ bool msgpack_rpc_to_float(msgpack_object *obj, Float *arg) bool msgpack_rpc_to_string(msgpack_object *obj, String *arg) { - arg->data = (char *)obj->via.raw.ptr; + if (obj->type != MSGPACK_OBJECT_RAW) { + return false; + } + + arg->data = xmemdup(obj->via.raw.ptr, obj->via.raw.size); arg->size = obj->via.raw.size; - return obj->type == MSGPACK_OBJECT_RAW; + return true; } bool msgpack_rpc_to_object(msgpack_object *obj, Object *arg) @@ -328,6 +332,15 @@ void msgpack_rpc_from_dictionary(Dictionary result, msgpack_packer *res) } } +void msgpack_rpc_free_string(String value) +{ + if (!value.data) { + return; + } + + free(value.data); +} + void msgpack_rpc_free_object(Object value) { switch (value.type) { -- cgit From 139c7ffdc785b19297e8c3b2d2586dfa284f97a5 Mon Sep 17 00:00:00 2001 From: Thiago de Arruda Date: Mon, 26 May 2014 13:39:08 -0300 Subject: API: Events: Return channel id from the API discover request This refactors msgapck_rpc_{dipatch,call} to receive the channel id as argument. Now the discovery request returns the [id, metadata] array. --- src/nvim/os/msgpack_rpc.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/nvim/os/msgpack_rpc.c') diff --git a/src/nvim/os/msgpack_rpc.c b/src/nvim/os/msgpack_rpc.c index 8ca1e0a8c0..62d68ebdec 100644 --- a/src/nvim/os/msgpack_rpc.c +++ b/src/nvim/os/msgpack_rpc.c @@ -1,3 +1,6 @@ +#include +#include + #include #include "nvim/os/msgpack_rpc.h" @@ -52,7 +55,7 @@ free(value.items); \ } -void msgpack_rpc_call(msgpack_object *req, msgpack_packer *res) +void msgpack_rpc_call(uint64_t id, msgpack_object *req, msgpack_packer *res) { // The initial response structure is the same no matter what happens, // we set it up here @@ -107,7 +110,7 @@ void msgpack_rpc_call(msgpack_object *req, msgpack_packer *res) } // dispatch the message - msgpack_rpc_dispatch(req, res); + msgpack_rpc_dispatch(id, req, res); } void msgpack_rpc_error(char *msg, msgpack_packer *res) -- cgit From f3dc04bf7f658f7d5d15047494fd15e286b4c7b6 Mon Sep 17 00:00:00 2001 From: Thiago de Arruda Date: Mon, 26 May 2014 13:39:10 -0300 Subject: API: Events: Implement channel_send_event and vimscript wrapper This function can be used to send arbitrary objects via the API channel back to connected clients, identified by channel id. --- src/nvim/os/msgpack_rpc.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/nvim/os/msgpack_rpc.c') diff --git a/src/nvim/os/msgpack_rpc.c b/src/nvim/os/msgpack_rpc.c index 62d68ebdec..423c5d584d 100644 --- a/src/nvim/os/msgpack_rpc.c +++ b/src/nvim/os/msgpack_rpc.c @@ -113,6 +113,15 @@ void msgpack_rpc_call(uint64_t id, msgpack_object *req, msgpack_packer *res) msgpack_rpc_dispatch(id, req, res); } +void msgpack_rpc_notification(String type, Object data, msgpack_packer *pac) +{ + msgpack_pack_array(pac, 3); + msgpack_pack_int(pac, 2); + msgpack_pack_raw(pac, type.size); + msgpack_pack_raw_body(pac, type.data, type.size); + msgpack_rpc_from_object(data, pac); +} + void msgpack_rpc_error(char *msg, msgpack_packer *res) { size_t len = strlen(msg); -- cgit