Rooms JS => C.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4107 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-01-08 00:25:38 +00:00
parent c09e043812
commit 3506d9dec1
7 changed files with 363 additions and 129 deletions

103
src/ssb.c
View File

@ -208,6 +208,8 @@ typedef struct _tf_ssb_connection_t
int port;
tf_ssb_state_t state;
bool is_attendant;
int32_t attendant_request_number;
uint8_t epub[crypto_box_PUBLICKEYBYTES];
uint8_t epriv[crypto_box_SECRETKEYBYTES];
@ -516,6 +518,7 @@ static void _tf_ssb_connection_remove_request(tf_ssb_connection_t* connection, i
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, tf_ssb_callback_cleanup_t* cleanup, void* user_data)
{
printf("SEND %p\n", connection);
if (!connection)
{
return;
@ -540,6 +543,50 @@ void tf_ssb_connection_rpc_send(tf_ssb_connection_t* connection, uint8_t flags,
connection->ssb->rpc_out++;
}
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)
{
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_flag_stream),
request_number,
(const uint8_t*)json_string,
size,
callback,
cleanup,
user_data);
JS_FreeCString(context, json_string);
JS_FreeValue(context, json);
}
void tf_ssb_connection_rpc_send_error(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, const char* error)
{
JSContext* context = connection->ssb->context;
JSValue message = JS_NewObject(context);
JS_SetPropertyStr(context, message, "name", JS_NewString(context, "Error"));
JS_SetPropertyStr(context, message, "stack", JS_NewString(context, "none"));
JS_SetPropertyStr(context, message, "message", JS_NewString(context, error));
tf_ssb_connection_rpc_send_json(connection, flags, request_number, message, NULL, NULL, NULL);
JS_FreeValue(context, message);
}
void tf_ssb_connection_rpc_send_error_method_not_allowed(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number)
{
const char* k_unsupported = "{\"message\": \"method: is not in list of allowed methods\", \"name\": \"Error\", \"stack\": \"none\"}";
tf_ssb_connection_rpc_send(
connection,
k_ssb_rpc_flag_json | k_ssb_rpc_flag_end_error | (flags & k_ssb_rpc_flag_stream),
request_number,
(const uint8_t*)k_unsupported,
strlen(k_unsupported),
NULL,
NULL,
NULL);
}
static int _utf8_len(uint8_t ch)
{
static const uint8_t k_length[] =
@ -875,6 +922,11 @@ static void _tf_ssb_connection_verify_identity(tf_ssb_connection_t* connection,
_tf_ssb_notify_connections_changed(connection->ssb, k_tf_ssb_change_connect, connection);
}
bool tf_ssb_connection_is_client(tf_ssb_connection_t* connection)
{
return connection->state == k_tf_ssb_state_verified;
}
const char* tf_ssb_connection_get_host(tf_ssb_connection_t* connection)
{
return connection->host;
@ -1201,11 +1253,9 @@ static void _tf_ssb_connection_rpc_recv(tf_ssb_connection_t* connection, uint8_t
callback(connection, flags, request_number, val, NULL, 0, user_data);
}
}
else
else if (!_tf_ssb_name_equals(context, val, (const char*[]) { "Error", NULL }))
{
const char* k_unsupported = "{\"message\": \"method: is not in list of allowed methods\", \"name\": \"Error\", \"stack\": \"none\"}";
tf_ssb_connection_rpc_send(connection, k_ssb_rpc_flag_json | k_ssb_rpc_flag_end_error | (flags & k_ssb_rpc_flag_stream), -request_number,
(const uint8_t*)k_unsupported, strlen(k_unsupported), NULL, NULL, NULL);
tf_ssb_connection_rpc_send_error_method_not_allowed(connection, flags, -request_number);
}
}
}
@ -1417,6 +1467,7 @@ void tf_ssb_append_message(tf_ssb_t* ssb, JSValue message)
void _tf_ssb_connection_destroy(tf_ssb_connection_t* connection, const char* reason)
{
printf("DESTROY %p\n", connection);
tf_ssb_t* ssb = connection->ssb;
if (!connection->destroy_reason)
{
@ -1437,7 +1488,7 @@ void _tf_ssb_connection_destroy(tf_ssb_connection_t* connection, const char* rea
}
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 = (*it)->requests_count - 1; i >= 0; i--)
{
if ((*it)->requests[i].dependent_connection == connection)
{
@ -2929,6 +2980,48 @@ void tf_ssb_connection_remove_room_attendant(tf_ssb_connection_t* connection, co
}
}
bool tf_ssb_connection_is_attendant(tf_ssb_connection_t* connection)
{
return connection->is_attendant;
}
int32_t tf_ssb_connection_get_attendant_request_number(tf_ssb_connection_t* connection)
{
return connection->attendant_request_number;
}
void tf_ssb_connection_set_attendant(tf_ssb_connection_t* connection, bool attendant, int request_number)
{
connection->is_attendant = attendant;
connection->attendant_request_number = request_number;
_tf_ssb_notify_broadcasts_changed(connection->ssb);
}
void tf_ssb_connection_clear_room_attendants(tf_ssb_connection_t* connection)
{
int modified = 0;
for (tf_ssb_broadcast_t** it = &connection->ssb->broadcasts; *it;)
{
if ((*it)->tunnel_connection == connection)
{
tf_ssb_broadcast_t* node = *it;
*it = node->next;
tf_free(node);
connection->ssb->broadcasts_count--;
modified++;
}
else
{
it = &(*it)->next;
}
}
if (modified)
{
_tf_ssb_notify_broadcasts_changed(connection->ssb);
}
}
tf_ssb_blob_wants_t* tf_ssb_connection_get_blob_wants_state(tf_ssb_connection_t* connection)
{
return connection ? &connection->blob_wants : NULL;