aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjörn Linse <bjorn.linse@gmail.com>2018-07-15 19:55:53 +0200
committerGitHub <noreply@github.com>2018-07-15 19:55:53 +0200
commitdfe79b67a5621ebd779a40a87a85304ffd89a5af (patch)
treefc6d5d8082429faec13207bc11f2acb7d9b25c6b
parentc230ef24a2b5bcd21902ca7fd89ff9ecd455638f (diff)
parent099718ae6d57239164d4348e6c7576edf7a2ebb7 (diff)
downloadrneovim-dfe79b67a5621ebd779a40a87a85304ffd89a5af.tar.gz
rneovim-dfe79b67a5621ebd779a40a87a85304ffd89a5af.tar.bz2
rneovim-dfe79b67a5621ebd779a40a87a85304ffd89a5af.zip
Merge pull request #8651 from MichaHoffmann/feature_refactor_channel
channel.c: refactor spaghetti code
-rw-r--r--src/nvim/api/private/dispatch.c7
-rw-r--r--src/nvim/api/vim.c11
-rw-r--r--src/nvim/msgpack_rpc/channel.c34
-rw-r--r--src/nvim/msgpack_rpc/helpers.c19
4 files changed, 25 insertions, 46 deletions
diff --git a/src/nvim/api/private/dispatch.c b/src/nvim/api/private/dispatch.c
index 5207a57b88..dec2b6c185 100644
--- a/src/nvim/api/private/dispatch.c
+++ b/src/nvim/api/private/dispatch.c
@@ -32,16 +32,17 @@ static void msgpack_rpc_add_method_handler(String method,
/// @param name API method name
/// @param name_len name size (includes terminating NUL)
MsgpackRpcRequestHandler msgpack_rpc_get_handler_for(const char *name,
- size_t name_len)
+ size_t name_len,
+ Error *error)
{
String m = { .data = (char *)name, .size = name_len };
MsgpackRpcRequestHandler rv =
map_get(String, MsgpackRpcRequestHandler)(methods, m);
if (!rv.fn) {
- rv.fn = msgpack_rpc_handle_missing_method;
+ api_set_error(error, kErrorTypeException, "Invalid method: %s",
+ m.size > 0 ? m.data : "<empty>");
}
-
return rv;
}
diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c
index 002d9da781..03567ddfd8 100644
--- a/src/nvim/api/vim.c
+++ b/src/nvim/api/vim.c
@@ -1162,11 +1162,12 @@ Array nvim_call_atomic(uint64_t channel_id, Array calls, Error *err)
}
Array args = call.items[1].data.array;
- MsgpackRpcRequestHandler handler = msgpack_rpc_get_handler_for(name.data,
- name.size);
- if (handler.fn == msgpack_rpc_handle_missing_method) {
- api_set_error(&nested_error, kErrorTypeException, "Invalid method: %s",
- name.size > 0 ? name.data : "<empty>");
+ MsgpackRpcRequestHandler handler =
+ msgpack_rpc_get_handler_for(name.data,
+ name.size,
+ &nested_error);
+
+ if (ERROR_SET(&nested_error)) {
break;
}
Object result = handler.fn(channel_id, args, &nested_error);
diff --git a/src/nvim/msgpack_rpc/channel.c b/src/nvim/msgpack_rpc/channel.c
index 6d0c270a51..3244d83a93 100644
--- a/src/nvim/msgpack_rpc/channel.c
+++ b/src/nvim/msgpack_rpc/channel.c
@@ -287,7 +287,6 @@ static void parse_msgpack(Channel *channel)
}
}
-
static void handle_request(Channel *channel, msgpack_object *request)
FUNC_ATTR_NONNULL_ALL
{
@@ -313,27 +312,24 @@ static void handle_request(Channel *channel, msgpack_object *request)
return;
}
- // Retrieve the request handler
MsgpackRpcRequestHandler handler;
- Array args = ARRAY_DICT_INIT;
msgpack_object *method = msgpack_rpc_method(request);
+ handler = msgpack_rpc_get_handler_for(method->via.bin.ptr,
+ method->via.bin.size,
+ &error);
- if (method) {
- handler = msgpack_rpc_get_handler_for(method->via.bin.ptr,
- method->via.bin.size);
- if (handler.fn == msgpack_rpc_handle_missing_method) {
- String m = method->via.bin.size > 0
- ? cbuf_to_string(method->via.bin.ptr, method->via.bin.size)
- : cstr_to_string("<empty>");
- ADD(args, STRING_OBJ(m));
- handler.async = true;
- } else if (!msgpack_rpc_to_array(msgpack_rpc_args(request), &args)) {
- handler.fn = msgpack_rpc_handle_invalid_arguments;
- handler.async = true;
- }
- } else {
- handler.fn = msgpack_rpc_handle_missing_method;
- handler.async = true;
+ // check method arguments
+ Array args = ARRAY_DICT_INIT;
+ if (!ERROR_SET(&error)
+ && !msgpack_rpc_to_array(msgpack_rpc_args(request), &args)) {
+ api_set_error(&error, kErrorTypeException, "Invalid method arguments");
+ }
+
+ if (ERROR_SET(&error)) {
+ send_error(channel, request_id, error.msg);
+ api_clear_error(&error);
+ api_free_array(args);
+ return;
}
RequestEvent *evdata = xmalloc(sizeof(RequestEvent));
diff --git a/src/nvim/msgpack_rpc/helpers.c b/src/nvim/msgpack_rpc/helpers.c
index e18c4472b5..19cc31f6a6 100644
--- a/src/nvim/msgpack_rpc/helpers.c
+++ b/src/nvim/msgpack_rpc/helpers.c
@@ -488,25 +488,6 @@ void msgpack_rpc_from_dictionary(Dictionary result, msgpack_packer *res)
}
}
-/// Handler executed when an invalid method name is passed
-Object msgpack_rpc_handle_missing_method(uint64_t channel_id,
- Array args,
- Error *error)
-{
- api_set_error(error, kErrorTypeException, "Invalid method: %s",
- args.size > 0 ? args.items[0].data.string.data : "?");
- return NIL;
-}
-
-/// Handler executed when malformated arguments are passed
-Object msgpack_rpc_handle_invalid_arguments(uint64_t channel_id,
- Array args,
- Error *error)
-{
- api_set_error(error, kErrorTypeException, "Invalid method arguments");
- return NIL;
-}
-
/// Serializes a msgpack-rpc request or notification(id == 0)
void msgpack_rpc_serialize_request(uint64_t request_id,
const String method,