From 06529fddfb7062c1112e7708b46b72b147200079 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 9 Nov 2022 01:31:18 +0000 Subject: [PATCH] Trying to make connections more robust. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4020 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/ssb.c | 16 +++++++++++++--- src/ssb.h | 1 + src/ssb.js.c | 29 +++++++++++++++++++++++++---- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/ssb.c b/src/ssb.c index e31ca039..2f154d5c 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -1398,6 +1398,14 @@ void _tf_ssb_connection_destroy(tf_ssb_connection_t* connection, const char* rea { _tf_ssb_connection_remove_request(connection, connection->requests->request_number); } + for (tf_ssb_broadcast_t* node = ssb->broadcasts; node; node = node->next) + { + if (node->tunnel_connection == connection) + { + node->tunnel_connection = NULL; + node->mtime = 0; + } + } for (tf_ssb_connection_t** it = &connection->ssb->connections; *it; it = &(*it)->next) { for (int i = 0; i < (*it)->requests_count; i++) @@ -2049,7 +2057,6 @@ tf_ssb_connection_t* tf_ssb_connection_create(tf_ssb_t* ssb, const char* host, c uv_async_init(ssb->loop, &connection->async, _tf_ssb_connection_process_message_async); connection->object = JS_NewObjectClass(ssb->context, _connection_class_id); - printf("%s = %p\n", connection->name, JS_VALUE_GET_PTR(connection->object)); JS_SetOpaque(connection->object, connection); JS_SetPropertyStr(context, connection->object, "send_json", JS_NewCFunction(context, _tf_ssb_connection_send_json, "send_json", 2)); char public_key_str[k_id_base64_len] = { 0 }; @@ -2109,7 +2116,6 @@ tf_ssb_connection_t* tf_ssb_connection_tunnel_create(tf_ssb_connection_t* connec uv_async_init(ssb->loop, &tunnel->async, _tf_ssb_connection_process_message_async); tunnel->object = JS_NewObjectClass(ssb->context, _connection_class_id); - printf("%s = %p\n", tunnel->name, JS_VALUE_GET_PTR(connection->object)); JS_SetOpaque(tunnel->object, tunnel); JS_SetPropertyStr(context, tunnel->object, "send_json", JS_NewCFunction(context, _tf_ssb_connection_send_json, "send_json", 2)); JS_SetPropertyStr(context, tunnel->object, "id", JS_NewString(context, target_id)); @@ -2186,6 +2192,11 @@ void tf_ssb_connect(tf_ssb_t* ssb, const char* host, int port, const uint8_t* ke } } +void tf_ssb_connection_close(tf_ssb_connection_t* connection) +{ + _tf_ssb_connection_close(connection, "tf_ssb_connection_close"); +} + static void _tf_ssb_on_connection(uv_stream_t* stream, int status) { tf_ssb_t* ssb = stream->data; @@ -2205,7 +2216,6 @@ static void _tf_ssb_on_connection(uv_stream_t* stream, int status) uv_async_init(ssb->loop, &connection->async, _tf_ssb_connection_process_message_async); connection->object = JS_NewObjectClass(ssb->context, _connection_class_id); - printf("%s = %p\n", connection->name, JS_VALUE_GET_PTR(connection->object)); JS_SetPropertyStr(ssb->context, connection->object, "send_json", JS_NewCFunction(ssb->context, _tf_ssb_connection_send_json, "send_json", 2)); JS_SetOpaque(connection->object, connection); diff --git a/src/ssb.h b/src/ssb.h index 9c55e084..184f82c4 100644 --- a/src/ssb.h +++ b/src/ssb.h @@ -103,6 +103,7 @@ int tf_ssb_connection_get_port(tf_ssb_connection_t* connection); tf_ssb_t* tf_ssb_connection_get_ssb(tf_ssb_connection_t* connection); JSContext* tf_ssb_connection_get_context(tf_ssb_connection_t* connection); sqlite3* tf_ssb_connection_get_db(tf_ssb_connection_t* connection); +void tf_ssb_connection_close(tf_ssb_connection_t* connect); int32_t tf_ssb_connection_next_request_number(tf_ssb_connection_t* connection); diff --git a/src/ssb.js.c b/src/ssb.js.c index ab541431..7423de70 100644 --- a/src/ssb.js.c +++ b/src/ssb.js.c @@ -262,6 +262,19 @@ static JSValue _tf_ssb_getConnection(JSContext* context, JSValueConst this_val, return JS_DupValue(context, tf_ssb_connection_get_object(connection)); } +static JSValue _tf_ssb_closeConnection(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) +{ + tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId); + const char* id = JS_ToCString(context, argv[0]); + tf_ssb_connection_t* connection = tf_ssb_connection_get(ssb, id); + if (connection) + { + tf_ssb_connection_close(connection); + } + JS_FreeCString(context, id); + return connection ? JS_TRUE : JS_FALSE; +} + typedef struct _sqlStream_callback_t { JSContext* context; @@ -388,10 +401,17 @@ static JSValue _tf_ssb_connect(JSContext* context, JSValueConst this_val, int ar int32_t port_int = 0; JS_ToInt32(context, &port_int, port); const char* pubkey_str = JS_ToCString(context, pubkey); - uint8_t pubkey_bin[k_id_bin_len]; - printf("Connecting to %s:%d\n", address_str, port_int); - tf_ssb_id_str_to_bin(pubkey_bin, pubkey_str); - tf_ssb_connect(ssb, address_str, port_int, pubkey_bin); + if (pubkey_str) + { + printf("Connecting to %s:%d\n", address_str, port_int); + uint8_t pubkey_bin[k_id_bin_len]; + tf_ssb_id_str_to_bin(pubkey_bin, pubkey_str); + tf_ssb_connect(ssb, address_str, port_int, pubkey_bin); + } + else + { + printf("Not connecting to null.\n"); + } JS_FreeCString(context, pubkey_str); JS_FreeCString(context, address_str); JS_FreeValue(context, address); @@ -1021,6 +1041,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, "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)); JS_SetPropertyStr(context, object, "getBroadcasts", JS_NewCFunction(context, _tf_ssb_getBroadcasts, "getBroadcasts", 0));