Use tf_ssb_connection_rpc_send_json everywhere I can. Less code, and fixes some leaks.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4122 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2023-01-14 19:32:36 +00:00
parent 8a1b4cceec
commit 3269c7ca45
3 changed files with 17 additions and 88 deletions

View File

@ -709,33 +709,6 @@ static JSValue _tf_ssb_createTunnel(JSContext* context, JSValueConst this_val, i
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);
}
static JSValue _tf_ssb_followingDeep(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
int depth = 2;
@ -807,7 +780,6 @@ 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));

View File

@ -120,12 +120,7 @@ static void _tf_ssb_rpc_blob_wants_added_callback(tf_ssb_t* ssb, const char* id,
JSContext* context = tf_ssb_get_context(ssb);
JSValue message = JS_NewObject(context);
JS_SetPropertyStr(context, message, id, JS_NewInt64(context, -1));
JSValue json = JS_JSONStringify(context, message, JS_NULL, JS_NULL);
size_t size = 0;
const char* message_str = JS_ToCStringLen(context, &size, json);
tf_ssb_connection_rpc_send(connection, k_ssb_rpc_flag_json | k_ssb_rpc_flag_stream, -blob_wants->request_number, (const uint8_t*)message_str, size, NULL, NULL, NULL);
JS_FreeCString(context, message_str);
JS_FreeValue(context, json);
tf_ssb_connection_rpc_send_json(connection, k_ssb_rpc_flag_stream, -blob_wants->request_number, message, NULL, NULL, NULL);
JS_FreeValue(context, message);
}
@ -145,12 +140,7 @@ static void _tf_ssb_rpc_request_more_blobs(tf_ssb_connection_t* connection)
const char* blob = (const char*)sqlite3_column_text(statement, 0);
JSValue message = JS_NewObject(context);
JS_SetPropertyStr(context, message, blob, JS_NewInt32(context, -1));
JSValue json = JS_JSONStringify(context, message, JS_NULL, JS_NULL);
size_t size = 0;
const char* message_str = JS_ToCStringLen(context, &size, json);
tf_ssb_connection_rpc_send(connection, k_ssb_rpc_flag_json | k_ssb_rpc_flag_stream, -blob_wants->request_number, (const uint8_t*)message_str, size, NULL, NULL, NULL);
JS_FreeCString(context, message_str);
JS_FreeValue(context, json);
tf_ssb_connection_rpc_send_json(connection, k_ssb_rpc_flag_stream, -blob_wants->request_number, message, NULL, NULL, NULL);
JS_FreeValue(context, message);
snprintf(blob_wants->last_id, sizeof(blob_wants->last_id), "%s", blob);
blob_wants->wants_sent++;
@ -280,22 +270,16 @@ static void _tf_ssb_rpc_tunnel_connect(tf_ssb_connection_t* connection, uint8_t
JS_SetPropertyUint32(context, arg_array, 0, arg_obj);
JS_SetPropertyStr(context, message, "args", arg_array);
JS_SetPropertyStr(context, message, "type", JS_NewString(context, "duplex"));
JSValue message_val = JS_JSONStringify(context, message, JS_NULL, JS_NULL);
size_t size;
const char* message_str = JS_ToCStringLen(context, &size, message_val);
tf_ssb_connection_rpc_send(
tf_ssb_connection_rpc_send_json(
target_connection,
k_ssb_rpc_flag_json | k_ssb_rpc_flag_stream,
k_ssb_rpc_flag_stream,
tunnel_request_number,
(const uint8_t*)message_str,
size,
message,
NULL,
NULL,
NULL);
JS_FreeCString(context, message_str);
tunnel_t* data0 = tf_malloc(sizeof(tunnel_t));
*data0 = (tunnel_t)
{
@ -308,11 +292,9 @@ static void _tf_ssb_rpc_tunnel_connect(tf_ssb_connection_t* connection, uint8_t
.connection = connection,
.request_number = -request_number,
};
printf("MAKE TUNNEL %p %p\n", connection, target_connection);
tf_ssb_connection_add_request(connection, -request_number, _tf_ssb_rpc_tunnel_callback, _tf_ssb_rpc_tunnel_cleanup, data0, target_connection);
tf_ssb_connection_add_request(target_connection, tunnel_request_number, _tf_ssb_rpc_tunnel_callback, _tf_ssb_rpc_tunnel_cleanup, data1, connection);
JS_FreeValue(context, message_val);
JS_FreeValue(context, message);
JS_FreeCString(context, portal_str);
}
@ -486,21 +468,16 @@ static void _tf_ssb_rpc_connection_blobs_get(tf_ssb_connection_t* connection, co
JSValue args = JS_NewArray(context);
JS_SetPropertyUint32(context, args, 0, JS_NewString(context, blob_id));
JS_SetPropertyStr(context, message, "args", args);
JSValue message_val = JS_JSONStringify(context, message, JS_NULL, JS_NULL);
size_t message_size;
const char* message_str = JS_ToCStringLen(context, &message_size, message_val);
tf_ssb_connection_rpc_send(
tf_ssb_connection_rpc_send_json(
connection,
k_ssb_rpc_flag_json | k_ssb_rpc_flag_stream,
k_ssb_rpc_flag_stream,
tf_ssb_connection_next_request_number(connection),
(const uint8_t*)message_str,
message_size,
message,
_tf_ssb_rpc_connection_blobs_get_callback,
_tf_ssb_rpc_connection_blobs_get_cleanup,
get);
JS_FreeCString(context, message_str);
JS_FreeValue(context, message);
}
@ -551,42 +528,28 @@ static void _tf_ssb_rpc_connection_blobs_createWants_callback(tf_ssb_connection_
{
JSValue message = JS_NewObject(context);
JS_SetPropertyStr(context, message, blob_id, JS_NewInt64(context, blob_size));
JSValue message_val = JS_JSONStringify(context, message, JS_NULL, JS_NULL);
size_t message_size;
const char* message_str = JS_ToCStringLen(context, &message_size, message_val);
tf_ssb_connection_rpc_send(
tf_ssb_connection_rpc_send_json(
connection,
k_ssb_rpc_flag_json | k_ssb_rpc_flag_stream,
k_ssb_rpc_flag_stream,
-blob_wants->request_number,
(const uint8_t*)message_str,
message_size,
message,
NULL,
NULL,
NULL);
JS_FreeCString(context, message_str);
JS_FreeValue(context, message);
}
else if (size == -1LL)
{
JSValue message = JS_NewObject(context);
JS_SetPropertyStr(context, message, blob_id, JS_NewInt64(context, -2));
JSValue message_val = JS_JSONStringify(context, message, JS_NULL, JS_NULL);
size_t message_size;
const char* message_str = JS_ToCStringLen(context, &message_size, message_val);
tf_ssb_connection_rpc_send(
tf_ssb_connection_rpc_send_json(
connection,
k_ssb_rpc_flag_json | k_ssb_rpc_flag_stream,
k_ssb_rpc_flag_stream,
-blob_wants->request_number,
(const uint8_t*)message_str,
message_size,
message,
NULL,
NULL,
NULL);
JS_FreeCString(context, message_str);
JS_FreeValue(context, message);
}
}

View File

@ -411,20 +411,14 @@ void tf_ssb_test_rooms(const tf_test_options_t* options)
JS_SetPropertyStr(context, message, "args", args);
JS_SetPropertyStr(context, message, "type", JS_NewString(context, "duplex"));
JSValue message_json = JS_JSONStringify(context, message, JS_NULL, JS_NULL);
size_t size;
const char* raw = JS_ToCStringLen(context, &size, message_json);
tf_ssb_connection_rpc_send(
tf_ssb_connection_rpc_send_json(
connections[0],
k_ssb_rpc_flag_json | k_ssb_rpc_flag_stream,
k_ssb_rpc_flag_stream,
tunnel_request_number,
(const uint8_t*)raw,
size,
message,
NULL,
NULL,
NULL);
JS_FreeCString(context, raw);
JS_FreeValue(context, message_json);
JS_FreeValue(context, message);
tf_ssb_connection_t* tun0 = tf_ssb_connection_tunnel_create(ssb1, id0, tunnel_request_number, id2);