diff options
Diffstat (limited to 'src/nvim/os/msgpack_rpc.c')
-rw-r--r-- | src/nvim/os/msgpack_rpc.c | 408 |
1 files changed, 408 insertions, 0 deletions
diff --git a/src/nvim/os/msgpack_rpc.c b/src/nvim/os/msgpack_rpc.c new file mode 100644 index 0000000000..e429bf0519 --- /dev/null +++ b/src/nvim/os/msgpack_rpc.c @@ -0,0 +1,408 @@ +#include <msgpack.h> + +#include "msgpack_rpc.h" +#include "vim.h" +#include "memory.h" + +static bool msgpack_rpc_to_uint16_t(msgpack_object *obj, uint16_t *arg); + +void msgpack_rpc_call(msgpack_object *req, msgpack_packer *res) +{ + // The initial response structure is the same no matter what happens, + // we set it up here + // Array of size 4 + msgpack_pack_array(res, 4); + // Response type is 1 + msgpack_pack_int(res, 1); + + // Validate the basic structure of the msgpack-rpc payload + if (req->type != MSGPACK_OBJECT_ARRAY) { + msgpack_pack_int(res, 0); // no message id yet + msgpack_rpc_error("Request is not an array", res); + return; + } + + if (req->via.array.size != 4) { + msgpack_pack_int(res, 0); // no message id yet + char error_msg[256]; + snprintf(error_msg, + sizeof(error_msg), + "Request array size is %u, it should be 4", + req->via.array.size); + msgpack_rpc_error(error_msg, res); + } + + if (req->via.array.ptr[1].type != MSGPACK_OBJECT_POSITIVE_INTEGER) { + msgpack_pack_int(res, 0); // no message id yet + msgpack_rpc_error("Id must be a positive integer", res); + } + + // Set the response id, which is the same as the request + msgpack_pack_int(res, req->via.array.ptr[1].via.u64); + + if (req->via.array.ptr[0].type != MSGPACK_OBJECT_POSITIVE_INTEGER) { + msgpack_rpc_error("Message type must be an integer", res); + return; + } + + if (req->via.array.ptr[0].via.u64 != 0) { + msgpack_rpc_error("Message type must be 0", res); + return; + } + + if (req->via.array.ptr[2].type != MSGPACK_OBJECT_POSITIVE_INTEGER) { + msgpack_rpc_error("Method id must be a positive integer", res); + return; + } + + if (req->via.array.ptr[3].type != MSGPACK_OBJECT_ARRAY) { + msgpack_rpc_error("Paremeters must be an array", res); + return; + } + + // dispatch the message + msgpack_rpc_dispatch(req, res); +} + +void msgpack_rpc_error(char *msg, msgpack_packer *res) +{ + size_t len = strlen(msg); + + // error message + msgpack_pack_raw(res, len); + msgpack_pack_raw_body(res, msg, len); + // Nil result + msgpack_pack_nil(res); +} + +bool msgpack_rpc_to_bool(msgpack_object *obj, bool *arg) +{ + *arg = obj->via.boolean; + return obj->type == MSGPACK_OBJECT_BOOLEAN; +} + +bool msgpack_rpc_to_int64_t(msgpack_object *obj, int64_t *arg) +{ + *arg = obj->via.i64; + return obj->type == MSGPACK_OBJECT_POSITIVE_INTEGER + || obj->type == MSGPACK_OBJECT_NEGATIVE_INTEGER; +} + +bool msgpack_rpc_to_double(msgpack_object *obj, double *arg) +{ + *arg = obj->via.dec; + return obj->type == MSGPACK_OBJECT_DOUBLE; +} + +bool msgpack_rpc_to_string(msgpack_object *obj, String *arg) +{ + arg->data = (char *)obj->via.raw.ptr; + arg->size = obj->via.raw.size; + return obj->type == MSGPACK_OBJECT_RAW; +} + +bool msgpack_rpc_to_buffer(msgpack_object *obj, Buffer *arg) +{ + return msgpack_rpc_to_uint16_t(obj, arg); +} + +bool msgpack_rpc_to_window(msgpack_object *obj, Window *arg) +{ + return msgpack_rpc_to_uint16_t(obj, arg); +} + +bool msgpack_rpc_to_tabpage(msgpack_object *obj, Tabpage *arg) +{ + return msgpack_rpc_to_uint16_t(obj, arg); +} + +bool msgpack_rpc_to_object(msgpack_object *obj, Object *arg) +{ + switch (obj->type) { + case MSGPACK_OBJECT_NIL: + arg->type = kObjectTypeNil; + return true; + + case MSGPACK_OBJECT_BOOLEAN: + arg->type = kObjectTypeBool; + return msgpack_rpc_to_bool(obj, &arg->data.boolean); + + case MSGPACK_OBJECT_POSITIVE_INTEGER: + case MSGPACK_OBJECT_NEGATIVE_INTEGER: + arg->type = kObjectTypeInt; + return msgpack_rpc_to_int64_t(obj, &arg->data.integer); + + case MSGPACK_OBJECT_DOUBLE: + arg->type = kObjectTypeFloat; + return msgpack_rpc_to_double(obj, &arg->data.floating_point); + + case MSGPACK_OBJECT_RAW: + arg->type = kObjectTypeString; + return msgpack_rpc_to_string(obj, &arg->data.string); + + case MSGPACK_OBJECT_ARRAY: + arg->type = kObjectTypeArray; + return msgpack_rpc_to_array(obj, &arg->data.array); + + case MSGPACK_OBJECT_MAP: + arg->type = kObjectTypeDictionary; + return msgpack_rpc_to_dictionary(obj, &arg->data.dictionary); + + default: + return false; + } +} + +bool msgpack_rpc_to_stringarray(msgpack_object *obj, StringArray *arg) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + arg->size = obj->via.array.size; + arg->items = xcalloc(obj->via.array.size, sizeof(String)); + + for (uint32_t i = 0; i < obj->via.array.size; i++) { + if (!msgpack_rpc_to_string(obj->via.array.ptr + i, &arg->items[i])) { + return false; + } + } + + return true; +} + +bool msgpack_rpc_to_position(msgpack_object *obj, Position *arg) +{ + // positions are represented by integer arrays of size 2 + if (obj->type != MSGPACK_OBJECT_ARRAY + || obj->via.array.size != 2 + || obj->via.array.ptr[0].type != MSGPACK_OBJECT_POSITIVE_INTEGER + || obj->via.array.ptr[1].type != MSGPACK_OBJECT_POSITIVE_INTEGER) { + return false; + } + + arg->row = obj->via.array.ptr[0].via.u64; + arg->col = obj->via.array.ptr[1].via.u64; + + return true; +} + + +bool msgpack_rpc_to_array(msgpack_object *obj, Array *arg) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + arg->size = obj->via.array.size; + arg->items = xcalloc(obj->via.array.size, sizeof(Object)); + + for (uint32_t i = 0; i < obj->via.array.size; i++) { + if (!msgpack_rpc_to_object(obj->via.array.ptr + i, &arg->items[i])) { + return false; + } + } + + return true; +} + +bool msgpack_rpc_to_dictionary(msgpack_object *obj, Dictionary *arg) +{ + if (obj->type != MSGPACK_OBJECT_MAP) { + return false; + } + + arg->size = obj->via.array.size; + arg->items = xcalloc(obj->via.map.size, sizeof(KeyValuePair)); + + + for (uint32_t i = 0; i < obj->via.map.size; i++) { + if (!msgpack_rpc_to_string(&obj->via.map.ptr[i].key, + &arg->items[i].key)) { + return false; + } + + if (!msgpack_rpc_to_object(&obj->via.map.ptr[i].val, + &arg->items[i].value)) { + return false; + } + } + + return true; +} + +void msgpack_rpc_from_bool(bool result, msgpack_packer *res) +{ + if (result) { + msgpack_pack_true(res); + } else { + msgpack_pack_false(res); + } +} + +void msgpack_rpc_from_int64_t(int64_t result, msgpack_packer *res) +{ + msgpack_pack_int64(res, result); +} + +void msgpack_rpc_from_uint64_t(uint64_t result, msgpack_packer *res) +{ + msgpack_pack_uint64(res, result); +} + +void msgpack_rpc_from_double(double result, msgpack_packer *res) +{ + msgpack_pack_double(res, result); +} + +void msgpack_rpc_from_string(String result, msgpack_packer *res) +{ + msgpack_pack_raw(res, result.size); + msgpack_pack_raw_body(res, result.data, result.size); +} + +void msgpack_rpc_from_buffer(Buffer result, msgpack_packer *res) +{ + msgpack_rpc_from_uint64_t(result, res); +} + +void msgpack_rpc_from_window(Window result, msgpack_packer *res) +{ + msgpack_rpc_from_uint64_t(result, res); +} + +void msgpack_rpc_from_tabpage(Tabpage result, msgpack_packer *res) +{ + msgpack_rpc_from_uint64_t(result, res); +} + +void msgpack_rpc_from_object(Object result, msgpack_packer *res) +{ + switch (result.type) { + case kObjectTypeNil: + msgpack_pack_nil(res); + break; + + case kObjectTypeBool: + msgpack_rpc_from_bool(result.data.boolean, res); + break; + + case kObjectTypeInt: + msgpack_rpc_from_int64_t(result.data.integer, res); + break; + + case kObjectTypeFloat: + msgpack_rpc_from_double(result.data.floating_point, res); + break; + + case kObjectTypeString: + msgpack_rpc_from_string(result.data.string, res); + break; + + case kObjectTypeArray: + msgpack_rpc_from_array(result.data.array, res); + break; + + case kObjectTypeDictionary: + msgpack_rpc_from_dictionary(result.data.dictionary, res); + break; + + default: + abort(); + } +} + +void msgpack_rpc_from_stringarray(StringArray result, msgpack_packer *res) +{ + msgpack_pack_array(res, result.size); + + for (size_t i = 0; i < result.size; i++) { + msgpack_rpc_from_string(result.items[i], res); + } +} + +void msgpack_rpc_from_position(Position result, msgpack_packer *res) +{ + msgpack_pack_array(res, 2);; + msgpack_pack_uint16(res, result.row); + msgpack_pack_uint16(res, result.col); +} + +void msgpack_rpc_from_array(Array result, msgpack_packer *res) +{ + msgpack_pack_array(res, result.size); + + for (size_t i = 0; i < result.size; i++) { + msgpack_rpc_from_object(result.items[i], res); + } +} + +void msgpack_rpc_from_dictionary(Dictionary result, msgpack_packer *res) +{ + msgpack_pack_map(res, result.size); + + for (size_t i = 0; i < result.size; i++) { + msgpack_rpc_from_string(result.items[i].key, res); + msgpack_rpc_from_object(result.items[i].value, res); + } +} + +void msgpack_rpc_free_object(Object value) +{ + switch (value.type) { + case kObjectTypeNil: + case kObjectTypeBool: + case kObjectTypeInt: + case kObjectTypeFloat: + break; + + case kObjectTypeString: + msgpack_rpc_free_string(value.data.string); + break; + + case kObjectTypeArray: + msgpack_rpc_free_array(value.data.array); + break; + + case kObjectTypeDictionary: + msgpack_rpc_free_dictionary(value.data.dictionary); + break; + + default: + abort(); + } +} + +void msgpack_rpc_free_stringarray(StringArray value) { + for (uint32_t i = 0; i < value.size; i++) { + msgpack_rpc_free_string(value.items[i]); + } + + free(value.items); +} + +void msgpack_rpc_free_array(Array value) +{ + for (uint32_t i = 0; i < value.size; i++) { + msgpack_rpc_free_object(value.items[i]); + } + + free(value.items); +} + +void msgpack_rpc_free_dictionary(Dictionary value) +{ + for (uint32_t i = 0; i < value.size; i++) { + msgpack_rpc_free_string(value.items[i].key); + msgpack_rpc_free_object(value.items[i].value); + } + + free(value.items); +} + +static bool msgpack_rpc_to_uint16_t(msgpack_object *obj, uint16_t *arg) +{ + *arg = obj->via.u64; + return obj->type == MSGPACK_OBJECT_POSITIVE_INTEGER; +} + |