From 1e84b74ced73be1043798d5c9c811bf4c061b418 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sun, 13 Nov 2022 03:24:30 +0000 Subject: [PATCH] Can actually attempt to connect to a room from the web interface, now. No actual success yet. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4036 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/ssb.js | 2 +- src/ssb.c | 4 ++-- src/ssb.h | 2 +- src/ssb.js.c | 36 +++++++++++++++++++++++++++++++++--- src/ssb.tests.c | 2 +- 5 files changed, 38 insertions(+), 8 deletions(-) diff --git a/core/ssb.js b/core/ssb.js index 3ecd53b8..219c91f3 100644 --- a/core/ssb.js +++ b/core/ssb.js @@ -275,7 +275,7 @@ ssb.addRpc(['tunnel', 'connect'], function(request) { } else if (request.args[0].origin && request.args[0].portal && request.args[0].target) { - ssb.createTunnel(request.connection, -request.request_number, request.args[0].origin); + ssb.createTunnel(request.connection.id, -request.request_number, request.args[0].origin); } }); diff --git a/src/ssb.c b/src/ssb.c index d220098e..323a4edd 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -2116,9 +2116,9 @@ static void _tf_ssb_connection_tunnel_callback( _tf_ssb_connection_on_tcp_recv_internal(tunnel, message, size); } -tf_ssb_connection_t* tf_ssb_connection_tunnel_create(tf_ssb_connection_t* connection, int32_t request_number, const char* target_id) +tf_ssb_connection_t* tf_ssb_connection_tunnel_create(tf_ssb_t* ssb, const char* portal_id, int32_t request_number, const char* target_id) { - tf_ssb_t* ssb = connection->ssb; + tf_ssb_connection_t* connection = tf_ssb_connection_get(ssb, portal_id); JSContext* context = ssb->context; tf_ssb_connection_t* tunnel = tf_malloc(sizeof(tf_ssb_connection_t)); diff --git a/src/ssb.h b/src/ssb.h index 26e64f10..ce62588a 100644 --- a/src/ssb.h +++ b/src/ssb.h @@ -141,7 +141,7 @@ void tf_ssb_connection_add_request(tf_ssb_connection_t* connection, int32_t requ void tf_ssb_connection_add_room_attendant(tf_ssb_connection_t* connection, const char* id); void tf_ssb_connection_remove_room_attendant(tf_ssb_connection_t* connection, const char* id); -tf_ssb_connection_t* tf_ssb_connection_tunnel_create(tf_ssb_connection_t* connection, int32_t request_number, const char* target_id); +tf_ssb_connection_t* tf_ssb_connection_tunnel_create(tf_ssb_t* ssb, const char* portal_id, int32_t request_number, const char* target_id); JSClassID tf_ssb_get_connection_class_id(); diff --git a/src/ssb.js.c b/src/ssb.js.c index 71cfbbc9..ce42cf1f 100644 --- a/src/ssb.js.c +++ b/src/ssb.js.c @@ -450,7 +450,7 @@ static JSValue _tf_ssb_rpc_send_json(JSContext* context, JSValueConst this_val, JS_FreeValue(context, connection_val); JS_FreeCString(context, message); JS_FreeValue(context, message_val); - return JS_UNDEFINED; + return JS_NewInt32(context, -request_number); } static JSValue _tf_ssb_rpc_send_json_end(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) @@ -952,12 +952,14 @@ static JSValue _tf_ssb_hmacsha256_verify(JSContext* context, JSValueConst this_v static JSValue _tf_ssb_createTunnel(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { - tf_ssb_connection_t* connection = JS_GetOpaque(argv[0], tf_ssb_get_connection_class_id()); + tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); + const char* portal_id = JS_ToCString(context, argv[0]); int32_t request_number = 0; JS_ToInt32(context, &request_number, argv[1]); const char* target_id = JS_ToCString(context, argv[2]); - tf_ssb_connection_tunnel_create(connection, request_number, target_id); + tf_ssb_connection_tunnel_create(ssb, portal_id, request_number, target_id); JS_FreeCString(context, target_id); + JS_FreeCString(context, portal_id); return JS_UNDEFINED; } @@ -1007,6 +1009,33 @@ static JSValue _tf_ssb_tunnel(JSContext* context, JSValueConst this_val, int arg return JS_UNDEFINED; } +static JSValue _tf_ssb_connectionSendJson(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) +{ + tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); + const char* connection_id = JS_ToCString(context, argv[0]); + tf_ssb_connection_t* connection = tf_ssb_connection_get(ssb, connection_id); + + JSValue message_val = JS_JSONStringify(context, argv[1], JS_NULL, JS_NULL); + size_t size; + const char* message = JS_ToCStringLen(context, &size, message_val); + + uint32_t request_number = tf_ssb_connection_next_request_number(connection); + + tf_ssb_connection_rpc_send( + connection, + k_ssb_rpc_flag_json | k_ssb_rpc_flag_stream, + request_number, + (const uint8_t*)message, + size, + NULL, + NULL, + NULL); + JS_FreeCString(context, connection_id); + JS_FreeCString(context, message); + JS_FreeValue(context, message_val); + return JS_NewInt32(context, request_number); +} + void tf_ssb_register(JSContext* context, tf_ssb_t* ssb) { JS_NewClassID(&_tf_ssb_classId); @@ -1039,6 +1068,7 @@ void tf_ssb_register(JSContext* context, tf_ssb_t* ssb) JS_SetPropertyStr(context, object, "messageContentGet", JS_NewCFunction(context, _tf_ssb_messageContentGet, "messageContentGet", 1)); JS_SetPropertyStr(context, object, "connections", JS_NewCFunction(context, _tf_ssb_connections, "connections", 0)); JS_SetPropertyStr(context, object, "getConnection", JS_NewCFunction(context, _tf_ssb_getConnection, "getConnection", 1)); + JS_SetPropertyStr(context, object, "connectionSendJson", JS_NewCFunction(context, _tf_ssb_connectionSendJson, "connectionSendJson", 2)); JS_SetPropertyStr(context, object, "closeConnection", JS_NewCFunction(context, _tf_ssb_closeConnection, "closeConnection", 1)); JS_SetPropertyStr(context, object, "sqlStream", JS_NewCFunction(context, _tf_ssb_sqlStream, "sqlStream", 3)); JS_SetPropertyStr(context, object, "storeMessage", JS_NewCFunction(context, _tf_ssb_storeMessage, "storeMessage", 1)); diff --git a/src/ssb.tests.c b/src/ssb.tests.c index 61ddec75..ced5f16b 100644 --- a/src/ssb.tests.c +++ b/src/ssb.tests.c @@ -418,7 +418,7 @@ void tf_ssb_test_rooms(const tf_test_options_t* options) JS_FreeValue(context, message_json); JS_FreeValue(context, message); - tf_ssb_connection_t* tun0 = tf_ssb_connection_tunnel_create(connections[0], tunnel_request_number, id2); + tf_ssb_connection_t* tun0 = tf_ssb_connection_tunnel_create(ssb1, id0, tunnel_request_number, id2); printf("tun0 = %p\n", tun0); printf("Done.\n");