Trying to make connections more robust.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4020 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2022-11-09 01:31:18 +00:00
parent f015c8727d
commit 06529fddfb
3 changed files with 39 additions and 7 deletions

View File

@ -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); _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 (tf_ssb_connection_t** it = &connection->ssb->connections; *it; it = &(*it)->next)
{ {
for (int i = 0; i < (*it)->requests_count; i++) 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); uv_async_init(ssb->loop, &connection->async, _tf_ssb_connection_process_message_async);
connection->object = JS_NewObjectClass(ssb->context, _connection_class_id); 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_SetOpaque(connection->object, connection);
JS_SetPropertyStr(context, connection->object, "send_json", JS_NewCFunction(context, _tf_ssb_connection_send_json, "send_json", 2)); 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 }; 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); uv_async_init(ssb->loop, &tunnel->async, _tf_ssb_connection_process_message_async);
tunnel->object = JS_NewObjectClass(ssb->context, _connection_class_id); 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_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, "send_json", JS_NewCFunction(context, _tf_ssb_connection_send_json, "send_json", 2));
JS_SetPropertyStr(context, tunnel->object, "id", JS_NewString(context, target_id)); 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) static void _tf_ssb_on_connection(uv_stream_t* stream, int status)
{ {
tf_ssb_t* ssb = stream->data; 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); uv_async_init(ssb->loop, &connection->async, _tf_ssb_connection_process_message_async);
connection->object = JS_NewObjectClass(ssb->context, _connection_class_id); 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_SetPropertyStr(ssb->context, connection->object, "send_json", JS_NewCFunction(ssb->context, _tf_ssb_connection_send_json, "send_json", 2));
JS_SetOpaque(connection->object, connection); JS_SetOpaque(connection->object, connection);

View File

@ -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); tf_ssb_t* tf_ssb_connection_get_ssb(tf_ssb_connection_t* connection);
JSContext* tf_ssb_connection_get_context(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); 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); int32_t tf_ssb_connection_next_request_number(tf_ssb_connection_t* connection);

View File

@ -262,6 +262,19 @@ static JSValue _tf_ssb_getConnection(JSContext* context, JSValueConst this_val,
return JS_DupValue(context, tf_ssb_connection_get_object(connection)); 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 typedef struct _sqlStream_callback_t
{ {
JSContext* context; JSContext* context;
@ -388,10 +401,17 @@ static JSValue _tf_ssb_connect(JSContext* context, JSValueConst this_val, int ar
int32_t port_int = 0; int32_t port_int = 0;
JS_ToInt32(context, &port_int, port); JS_ToInt32(context, &port_int, port);
const char* pubkey_str = JS_ToCString(context, pubkey); const char* pubkey_str = JS_ToCString(context, pubkey);
uint8_t pubkey_bin[k_id_bin_len]; if (pubkey_str)
printf("Connecting to %s:%d\n", address_str, port_int); {
tf_ssb_id_str_to_bin(pubkey_bin, pubkey_str); printf("Connecting to %s:%d\n", address_str, port_int);
tf_ssb_connect(ssb, address_str, port_int, pubkey_bin); 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, pubkey_str);
JS_FreeCString(context, address_str); JS_FreeCString(context, address_str);
JS_FreeValue(context, address); 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, "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, "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, "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, "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, "storeMessage", JS_NewCFunction(context, _tf_ssb_storeMessage, "storeMessage", 1));
JS_SetPropertyStr(context, object, "getBroadcasts", JS_NewCFunction(context, _tf_ssb_getBroadcasts, "getBroadcasts", 0)); JS_SetPropertyStr(context, object, "getBroadcasts", JS_NewCFunction(context, _tf_ssb_getBroadcasts, "getBroadcasts", 0));