aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/os
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/os')
-rw-r--r--src/nvim/os/channel.c44
-rw-r--r--src/nvim/os/msgpack_rpc.c18
-rw-r--r--src/nvim/os/msgpack_rpc.h7
-rw-r--r--src/nvim/os/provider.c6
4 files changed, 44 insertions, 31 deletions
diff --git a/src/nvim/os/channel.c b/src/nvim/os/channel.c
index 2c1928f3b3..39455df862 100644
--- a/src/nvim/os/channel.c
+++ b/src/nvim/os/channel.c
@@ -143,40 +143,48 @@ bool channel_exists(uint64_t id)
&& channel->enabled;
}
-/// Sends event/data to channel
+/// Sends event/arguments to channel
///
/// @param id The channel id. If 0, the event will be sent to all
/// channels that have subscribed to the event type
/// @param name The event name, an arbitrary string
-/// @param arg The event arg
-/// @return True if the data was sent successfully, false otherwise.
-bool channel_send_event(uint64_t id, char *name, Object arg)
+/// @param args Array with event arguments
+/// @return True if the event was sent successfully, false otherwise.
+bool channel_send_event(uint64_t id, char *name, Array args)
{
Channel *channel = NULL;
if (id > 0) {
if (!(channel = pmap_get(uint64_t)(channels, id)) || !channel->enabled) {
- msgpack_rpc_free_object(arg);
+ msgpack_rpc_free_array(args);
return false;
}
- send_event(channel, name, arg);
+ send_event(channel, name, args);
} else {
- broadcast_event(name, arg);
+ broadcast_event(name, args);
}
return true;
}
+/// Sends a method call to a channel
+///
+/// @param id The channel id
+/// @param name The method name, an arbitrary string
+/// @param args Array with method arguments
+/// @param[out] result Pointer to return value received from the channel
+/// @param[out] error True if the return value is an error
+/// @return True if the call was sent successfully, false otherwise.
bool channel_send_call(uint64_t id,
char *name,
- Object arg,
+ Array args,
Object *result,
bool *errored)
{
Channel *channel = NULL;
if (!(channel = pmap_get(uint64_t)(channels, id)) || !channel->enabled) {
- msgpack_rpc_free_object(arg);
+ msgpack_rpc_free_array(args);
return false;
}
@@ -190,13 +198,13 @@ bool channel_send_call(uint64_t id,
"while processing a RPC call",
channel->id);
*result = STRING_OBJ(cstr_to_string(buf));
- msgpack_rpc_free_object(arg);
+ msgpack_rpc_free_array(args);
return false;
}
uint64_t request_id = channel->next_request_id++;
// Send the msgpack-rpc request
- send_request(channel, request_id, name, arg);
+ send_request(channel, request_id, name, args);
EventSource channel_source = channel->is_job
? job_event_source(channel->data.job)
@@ -415,21 +423,21 @@ static void send_error(Channel *channel, uint64_t id, char *err)
static void send_request(Channel *channel,
uint64_t id,
char *name,
- Object arg)
+ Array args)
{
String method = {.size = strlen(name), .data = name};
- channel_write(channel, serialize_request(id, method, arg, &out_buffer, 1));
+ channel_write(channel, serialize_request(id, method, args, &out_buffer, 1));
}
static void send_event(Channel *channel,
char *name,
- Object arg)
+ Array args)
{
String method = {.size = strlen(name), .data = name};
- channel_write(channel, serialize_request(0, method, arg, &out_buffer, 1));
+ channel_write(channel, serialize_request(0, method, args, &out_buffer, 1));
}
-static void broadcast_event(char *name, Object arg)
+static void broadcast_event(char *name, Array args)
{
kvec_t(Channel *) subscribed;
kv_init(subscribed);
@@ -442,14 +450,14 @@ static void broadcast_event(char *name, Object arg)
});
if (!kv_size(subscribed)) {
- msgpack_rpc_free_object(arg);
+ msgpack_rpc_free_array(args);
goto end;
}
String method = {.size = strlen(name), .data = name};
WBuffer *buffer = serialize_request(0,
method,
- arg,
+ args,
&out_buffer,
kv_size(subscribed));
diff --git a/src/nvim/os/msgpack_rpc.c b/src/nvim/os/msgpack_rpc.c
index c03d8dccca..c6e2af2f1c 100644
--- a/src/nvim/os/msgpack_rpc.c
+++ b/src/nvim/os/msgpack_rpc.c
@@ -39,15 +39,14 @@ WBuffer *msgpack_rpc_call(uint64_t channel_id,
return serialize_response(response_id, err, NIL, sbuffer);
}
- uint64_t method_id = req->via.array.ptr[2].via.u64;
-
- if (method_id == 0) {
+ if (req->via.array.ptr[2].type == MSGPACK_OBJECT_POSITIVE_INTEGER
+ && req->via.array.ptr[2].via.u64 == 0) {
return serialize_metadata(response_id, channel_id, sbuffer);
}
// dispatch the call
Error error = { .set = false };
- Object rv = msgpack_rpc_dispatch(channel_id, method_id, req, &error);
+ Object rv = msgpack_rpc_dispatch(channel_id, req, &error);
// send the response
msgpack_packer response;
msgpack_packer_init(&response, sbuffer, msgpack_sbuffer_write);
@@ -119,7 +118,7 @@ void msgpack_rpc_error(char *msg, msgpack_packer *res)
/// Serializes a msgpack-rpc request or notification(id == 0)
WBuffer *serialize_request(uint64_t request_id,
String method,
- Object arg,
+ Array args,
msgpack_sbuffer *sbuffer,
size_t refcount)
FUNC_ATTR_NONNULL_ARG(4)
@@ -135,12 +134,12 @@ WBuffer *serialize_request(uint64_t request_id,
msgpack_pack_raw(&pac, method.size);
msgpack_pack_raw_body(&pac, method.data, method.size);
- msgpack_rpc_from_object(arg, &pac);
+ msgpack_rpc_from_array(args, &pac);
WBuffer *rv = wstream_new_buffer(xmemdup(sbuffer->data, sbuffer->size),
sbuffer->size,
refcount,
free);
- msgpack_rpc_free_object(arg);
+ msgpack_rpc_free_array(args);
msgpack_sbuffer_clear(sbuffer);
return rv;
}
@@ -235,8 +234,9 @@ static char *msgpack_rpc_validate(uint64_t *response_id, msgpack_object *req)
return "Message type must be 0";
}
- if (req->via.array.ptr[2].type != MSGPACK_OBJECT_POSITIVE_INTEGER) {
- return "Method id must be a positive integer";
+ if (req->via.array.ptr[2].type != MSGPACK_OBJECT_POSITIVE_INTEGER
+ && req->via.array.ptr[2].type != MSGPACK_OBJECT_RAW) {
+ return "Method must be a positive integer or a string";
}
if (req->via.array.ptr[3].type != MSGPACK_OBJECT_ARRAY) {
diff --git a/src/nvim/os/msgpack_rpc.h b/src/nvim/os/msgpack_rpc.h
index 5aca900d2e..35f175d2a0 100644
--- a/src/nvim/os/msgpack_rpc.h
+++ b/src/nvim/os/msgpack_rpc.h
@@ -21,6 +21,11 @@ typedef Object (*rpc_method_handler_fn)(uint64_t channel_id,
msgpack_object *req,
Error *error);
+
+/// Initializes the msgpack-rpc method table
+void msgpack_rpc_init(void);
+
+
/// Dispatches to the actual API function after basic payload validation by
/// `msgpack_rpc_call`. It is responsible for validating/converting arguments
/// to C types, and converting the return value back to msgpack types.
@@ -33,11 +38,11 @@ typedef Object (*rpc_method_handler_fn)(uint64_t channel_id,
/// @param error Pointer to error structure
/// @return Some object
Object msgpack_rpc_dispatch(uint64_t channel_id,
- uint64_t method_id,
msgpack_object *req,
Error *error)
FUNC_ATTR_NONNULL_ARG(2) FUNC_ATTR_NONNULL_ARG(3);
+
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "os/msgpack_rpc.h.generated.h"
#endif
diff --git a/src/nvim/os/provider.c b/src/nvim/os/provider.c
index d94203f683..07e757fe0e 100644
--- a/src/nvim/os/provider.c
+++ b/src/nvim/os/provider.c
@@ -98,7 +98,7 @@ bool provider_register(char *method, uint64_t channel_id)
return true;
}
-Object provider_call(char *method, Object arg)
+Object provider_call(char *method, Array args)
{
uint64_t channel_id = get_provider_for(method);
@@ -109,13 +109,13 @@ Object provider_call(char *method, Object arg)
"Provider for \"%s\" is not available",
method);
report_error(buf);
- msgpack_rpc_free_object(arg);
+ msgpack_rpc_free_array(args);
return NIL;
}
bool error = false;
Object result = NIL;
- channel_send_call(channel_id, method, arg, &result, &error);
+ channel_send_call(channel_id, method, args, &result, &error);
if (error) {
report_error(result.data.string.data);