forked from cory/tildefriends
Show active RPC requests in the connections tab. Probably TMI, but I want greater introspection into what is going on, and this seemed like a positive step.
This commit is contained in:
75
src/ssb.c
75
src/ssb.c
@ -98,6 +98,7 @@ typedef struct _tf_ssb_debug_close_t
|
||||
|
||||
typedef struct _tf_ssb_request_t
|
||||
{
|
||||
char name[256];
|
||||
tf_ssb_rpc_callback_t* callback;
|
||||
tf_ssb_callback_cleanup_t* cleanup;
|
||||
void* user_data;
|
||||
@ -347,15 +348,16 @@ static JSClassID _connection_class_id;
|
||||
static int s_connection_index;
|
||||
static int s_tunnel_index;
|
||||
|
||||
static void _tf_ssb_connection_destroy(tf_ssb_connection_t* connection, const char* reason);
|
||||
static void _tf_ssb_connection_client_send_hello(tf_ssb_connection_t* connection);
|
||||
static void _tf_ssb_connection_on_close(uv_handle_t* handle);
|
||||
static void _tf_ssb_connection_close(tf_ssb_connection_t* connection, const char* reason);
|
||||
static void _tf_ssb_nonce_inc(uint8_t* nonce);
|
||||
static void _tf_ssb_write(tf_ssb_connection_t* connection, void* data, size_t size);
|
||||
static void _tf_ssb_connection_destroy(tf_ssb_connection_t* connection, const char* reason);
|
||||
static void _tf_ssb_connection_finalizer(JSRuntime* runtime, JSValue value);
|
||||
static void _tf_ssb_update_settings(tf_ssb_t* ssb);
|
||||
static void _tf_ssb_connection_on_close(uv_handle_t* handle);
|
||||
static void _tf_ssb_nonce_inc(uint8_t* nonce);
|
||||
static void _tf_ssb_notify_connections_changed(tf_ssb_t* ssb, tf_ssb_change_t change, tf_ssb_connection_t* connection);
|
||||
static void _tf_ssb_start_update_settings(tf_ssb_t* ssb);
|
||||
static void _tf_ssb_update_settings(tf_ssb_t* ssb);
|
||||
static void _tf_ssb_write(tf_ssb_connection_t* connection, void* data, size_t size);
|
||||
|
||||
static void _tf_ssb_add_debug_close(tf_ssb_t* ssb, tf_ssb_connection_t* connection, const char* reason)
|
||||
{
|
||||
@ -482,7 +484,7 @@ static void _tf_ssb_write(tf_ssb_connection_t* connection, void* data, size_t si
|
||||
}
|
||||
else if (connection->tunnel_connection)
|
||||
{
|
||||
tf_ssb_connection_rpc_send(connection->tunnel_connection, k_ssb_rpc_flag_binary | k_ssb_rpc_flag_stream, -connection->tunnel_request_number, data, size, NULL, NULL, NULL);
|
||||
tf_ssb_connection_rpc_send(connection->tunnel_connection, k_ssb_rpc_flag_binary | k_ssb_rpc_flag_stream, -connection->tunnel_request_number, NULL, data, size, NULL, NULL, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
@ -640,7 +642,7 @@ static bool _tf_ssb_connection_get_request_callback(tf_ssb_connection_t* connect
|
||||
return false;
|
||||
}
|
||||
|
||||
void tf_ssb_connection_add_request(tf_ssb_connection_t* connection, int32_t request_number, tf_ssb_rpc_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data,
|
||||
void tf_ssb_connection_add_request(tf_ssb_connection_t* connection, int32_t request_number, const char* name, tf_ssb_rpc_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data,
|
||||
tf_ssb_connection_t* dependent_connection)
|
||||
{
|
||||
tf_ssb_request_t* existing =
|
||||
@ -666,6 +668,7 @@ void tf_ssb_connection_add_request(tf_ssb_connection_t* connection, int32_t requ
|
||||
.user_data = user_data,
|
||||
.dependent_connection = dependent_connection,
|
||||
};
|
||||
snprintf(request.name, sizeof(request.name), "%s", name);
|
||||
int index = tf_util_insert_index(&request_number, connection->requests, connection->requests_count, sizeof(tf_ssb_request_t), _request_compare);
|
||||
connection->requests = tf_resize_vec(connection->requests, sizeof(tf_ssb_request_t) * (connection->requests_count + 1));
|
||||
if (connection->requests_count - index)
|
||||
@ -677,6 +680,7 @@ void tf_ssb_connection_add_request(tf_ssb_connection_t* connection, int32_t requ
|
||||
|
||||
connection->ssb->request_count++;
|
||||
}
|
||||
_tf_ssb_notify_connections_changed(connection->ssb, k_tf_ssb_change_update, connection);
|
||||
}
|
||||
|
||||
static int _message_request_compare(const void* a, const void* b)
|
||||
@ -737,10 +741,11 @@ void tf_ssb_connection_remove_request(tf_ssb_connection_t* connection, int32_t r
|
||||
connection->requests_count--;
|
||||
connection->requests = tf_resize_vec(connection->requests, sizeof(tf_ssb_request_t) * connection->requests_count);
|
||||
connection->ssb->request_count--;
|
||||
_tf_ssb_notify_connections_changed(connection->ssb, k_tf_ssb_change_update, connection);
|
||||
}
|
||||
}
|
||||
|
||||
void tf_ssb_connection_rpc_send(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, const uint8_t* message, size_t size, tf_ssb_rpc_callback_t* callback,
|
||||
void tf_ssb_connection_rpc_send(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, const char* new_request_name, const uint8_t* message, size_t size, tf_ssb_rpc_callback_t* callback,
|
||||
tf_ssb_callback_cleanup_t* cleanup, void* user_data)
|
||||
{
|
||||
if (!connection)
|
||||
@ -755,6 +760,7 @@ void tf_ssb_connection_rpc_send(tf_ssb_connection_t* connection, uint8_t flags,
|
||||
{
|
||||
assert(request_number > 0);
|
||||
assert(!_tf_ssb_connection_get_request_callback(connection, request_number, NULL, NULL));
|
||||
assert(new_request_name);
|
||||
}
|
||||
else if (!_tf_ssb_connection_get_request_callback(connection, request_number, NULL, NULL))
|
||||
{
|
||||
@ -785,18 +791,18 @@ void tf_ssb_connection_rpc_send(tf_ssb_connection_t* connection, uint8_t flags,
|
||||
}
|
||||
else if (flags & k_ssb_rpc_flag_new_request)
|
||||
{
|
||||
tf_ssb_connection_add_request(connection, request_number, callback, cleanup, user_data, NULL);
|
||||
tf_ssb_connection_add_request(connection, request_number, new_request_name, callback, cleanup, user_data, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void tf_ssb_connection_rpc_send_json(
|
||||
tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue message, tf_ssb_rpc_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data)
|
||||
tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, const char* new_request_name, JSValue message, tf_ssb_rpc_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data)
|
||||
{
|
||||
JSContext* context = connection->ssb->context;
|
||||
JSValue json = JS_JSONStringify(context, message, JS_NULL, JS_NULL);
|
||||
size_t size = 0;
|
||||
const char* json_string = JS_ToCStringLen(context, &size, json);
|
||||
tf_ssb_connection_rpc_send(connection, k_ssb_rpc_flag_json | (flags & ~k_ssb_rpc_mask_type), request_number, (const uint8_t*)json_string, size, callback, cleanup, user_data);
|
||||
tf_ssb_connection_rpc_send(connection, k_ssb_rpc_flag_json | (flags & ~k_ssb_rpc_mask_type), request_number, new_request_name, (const uint8_t*)json_string, size, callback, cleanup, user_data);
|
||||
JS_FreeCString(context, json_string);
|
||||
JS_FreeValue(context, json);
|
||||
}
|
||||
@ -810,7 +816,7 @@ void tf_ssb_connection_rpc_send_error(tf_ssb_connection_t* connection, uint8_t f
|
||||
JS_SetPropertyStr(context, message, "stack", JS_NewString(context, stack ? stack : "stack unavailable"));
|
||||
JS_SetPropertyStr(context, message, "message", JS_NewString(context, error));
|
||||
tf_ssb_connection_rpc_send_json(
|
||||
connection, ((flags & k_ssb_rpc_flag_stream) ? (k_ssb_rpc_flag_stream) : 0) | k_ssb_rpc_flag_end_error, request_number, message, NULL, NULL, NULL);
|
||||
connection, ((flags & k_ssb_rpc_flag_stream) ? (k_ssb_rpc_flag_stream) : 0) | k_ssb_rpc_flag_end_error, request_number, NULL, message, NULL, NULL, NULL);
|
||||
JS_FreeValue(context, message);
|
||||
tf_free((void*)stack);
|
||||
}
|
||||
@ -1550,7 +1556,30 @@ static void _tf_ssb_connection_rpc_recv(tf_ssb_connection_t* connection, uint8_t
|
||||
else if (JS_IsObject(val))
|
||||
{
|
||||
bool found = false;
|
||||
tf_ssb_connection_add_request(connection, -request_number, NULL, NULL, NULL, NULL);
|
||||
char namebuf[256] = "";
|
||||
JSValue name = JS_GetPropertyStr(context, val, "name");
|
||||
if (JS_IsArray(context, name))
|
||||
{
|
||||
int length = tf_util_get_length(context, name);
|
||||
int offset = 0;
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
JSValue part = JS_GetPropertyUint32(context, name, i);
|
||||
const char* part_str = JS_ToCString(context, part);
|
||||
offset += snprintf(namebuf + offset, sizeof(namebuf) - offset, "%s%s", i == 0 ? "" : ".", part_str);
|
||||
JS_FreeCString(context, part_str);
|
||||
JS_FreeValue(context, part);
|
||||
}
|
||||
}
|
||||
else if (JS_IsString(name))
|
||||
{
|
||||
const char* part_str = JS_ToCString(context, name);
|
||||
snprintf(namebuf, sizeof(namebuf), "%s", part_str);
|
||||
JS_FreeCString(context, part_str);
|
||||
}
|
||||
JS_FreeValue(context, name);
|
||||
|
||||
tf_ssb_connection_add_request(connection, -request_number, namebuf, NULL, NULL, NULL, NULL);
|
||||
for (tf_ssb_rpc_callback_node_t* it = connection->ssb->rpc; it; it = it->next)
|
||||
{
|
||||
if (_tf_ssb_name_equals(context, val, it->name))
|
||||
@ -2607,7 +2636,7 @@ static void _tf_ssb_connection_tunnel_callback(
|
||||
tf_ssb_connection_t* tunnel = user_data;
|
||||
if (flags & k_ssb_rpc_flag_end_error)
|
||||
{
|
||||
tf_ssb_connection_rpc_send(connection, flags, -request_number, (const uint8_t*)"false", strlen("false"), NULL, NULL, NULL);
|
||||
tf_ssb_connection_rpc_send(connection, flags, -request_number, NULL, (const uint8_t*)"false", strlen("false"), NULL, NULL, NULL);
|
||||
tf_ssb_connection_close(tunnel);
|
||||
}
|
||||
else
|
||||
@ -2644,7 +2673,7 @@ tf_ssb_connection_t* tf_ssb_connection_tunnel_create(tf_ssb_t* ssb, const char*
|
||||
ssb->connections_count++;
|
||||
_tf_ssb_notify_connections_changed(ssb, k_tf_ssb_change_create, tunnel);
|
||||
|
||||
tf_ssb_connection_add_request(connection, request_number, _tf_ssb_connection_tunnel_callback, NULL, tunnel, tunnel);
|
||||
tf_ssb_connection_add_request(connection, request_number, "tunnel.connect", _tf_ssb_connection_tunnel_callback, NULL, tunnel, tunnel);
|
||||
if (request_number > 0)
|
||||
{
|
||||
tunnel->state = k_tf_ssb_state_connected;
|
||||
@ -3379,7 +3408,7 @@ void tf_ssb_notify_message_added(tf_ssb_t* ssb, const char* id, JSValue message_
|
||||
if (message_request)
|
||||
{
|
||||
tf_ssb_connection_rpc_send_json(
|
||||
connection, k_ssb_rpc_flag_stream, message_request->request_number, message_request->keys ? message_keys : message, NULL, NULL, NULL);
|
||||
connection, k_ssb_rpc_flag_stream, message_request->request_number, NULL, message_request->keys ? message_keys : message, NULL, NULL, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3937,3 +3966,17 @@ bool tf_ssb_hmacsha256_verify(const char* public_key, const void* payload, size_
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
JSValue tf_ssb_connection_requests_to_object(tf_ssb_connection_t* connection)
|
||||
{
|
||||
JSContext* context = connection->ssb->context;
|
||||
JSValue object = JS_NewArray(context);
|
||||
for (int i = 0; i < connection->requests_count; i++)
|
||||
{
|
||||
JSValue request = JS_NewObject(context);
|
||||
JS_SetPropertyStr(context, request, "name", JS_NewString(context, connection->requests[i].name));
|
||||
JS_SetPropertyStr(context, request, "request_number", JS_NewInt32(context, connection->requests[i].request_number));
|
||||
JS_SetPropertyUint32(context, object, i, request);
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
Reference in New Issue
Block a user