Some plumbing to expose the actual bound SHS port so that I can make a dynamic room app.

This commit is contained in:
Cory McWilliams 2024-03-07 21:03:14 -05:00
parent cf187ee46b
commit cb94ed6a2a
4 changed files with 21 additions and 10 deletions

View File

@ -578,6 +578,7 @@ async function getProcessBlob(blobId, key, options) {
imports.ssb = Object.fromEntries(
Object.keys(ssb).map((key) => [key, ssb[key].bind(ssb)])
);
imports.ssb.port = tildefriends.ssb_port;
imports.ssb.createIdentity = function () {
if (
process.credentials &&

View File

@ -2824,19 +2824,19 @@ static void _tf_ssb_broadcast_timer(uv_timer_t* timer)
}
}
void tf_ssb_server_open(tf_ssb_t* ssb, int port)
int tf_ssb_server_open(tf_ssb_t* ssb, int port)
{
if (ssb->server.data)
{
tf_printf("Already listening.\n");
return;
return 0;
}
ssb->server.data = ssb;
if (uv_tcp_init(ssb->loop, &ssb->server) != 0)
{
tf_printf("uv_tcp_init failed\n");
return;
return 0;
}
struct sockaddr_in addr = { 0 };
@ -2847,7 +2847,7 @@ void tf_ssb_server_open(tf_ssb_t* ssb, int port)
if (status != 0)
{
tf_printf("%s:%d: uv_tcp_bind failed: %s\n", __FILE__, __LINE__, uv_strerror(status));
return;
return 0;
}
status = uv_listen((uv_stream_t*)&ssb->server, SOMAXCONN, _tf_ssb_on_connection);
@ -2855,8 +2855,14 @@ void tf_ssb_server_open(tf_ssb_t* ssb, int port)
{
tf_printf("uv_listen failed: %s\n", uv_strerror(status));
/* TODO: cleanup */
return;
return 0;
}
struct sockaddr_storage name = { 0 };
int size = (int)sizeof(name);
status = uv_tcp_getsockname(&ssb->server, (struct sockaddr*)&name, &size);
int assigned_port = ntohs(((struct sockaddr_in*)&name)->sin_port);
return status == 0 ? assigned_port : 0;
}
void tf_ssb_server_close(tf_ssb_t* ssb)

View File

@ -316,8 +316,9 @@ void tf_ssb_connect_str(tf_ssb_t* ssb, const char* address);
** Begin listening for SHS connections on the given port.
** @param ssb The SSB instance.
** @param port The port number.
** @return The assigned port on success or 0 on failure.
*/
void tf_ssb_server_open(tf_ssb_t* ssb, int port);
int tf_ssb_server_open(tf_ssb_t* ssb, int port);
/**
** Stop listening for SHS connections.

View File

@ -1694,9 +1694,6 @@ void tf_task_activate(tf_task_t* task)
JS_FreeAtom(context, atom);
JSValue tildefriends = JS_NewObject(context);
JS_SetPropertyStr(context, tildefriends, "ssb_port", JS_NewInt32(context, task->_ssb_port));
JS_SetPropertyStr(context, tildefriends, "http_port", JS_NewInt32(context, task->_http_port));
JS_SetPropertyStr(context, tildefriends, "https_port", JS_NewInt32(context, task->_https_port));
JSValue args = JS_NewObject(context);
JS_SetPropertyStr(context, tildefriends, "args", args);
if (task->_args)
@ -1749,13 +1746,19 @@ void tf_task_activate(tf_task_t* task)
tf_ssb_register(context, task->_ssb);
tf_ssb_set_hitch_callback(task->_ssb, _tf_task_record_hitch, task);
int actual_ssb_port = task->_ssb_port;
if (task->_ssb_port)
{
tf_ssb_broadcast_listener_start(task->_ssb, false);
tf_ssb_broadcast_sender_start(task->_ssb);
tf_ssb_server_open(task->_ssb, task->_ssb_port);
actual_ssb_port = tf_ssb_server_open(task->_ssb, task->_ssb_port);
}
JS_SetPropertyStr(context, tildefriends, "ssb_port", JS_NewInt32(context, actual_ssb_port));
JS_SetPropertyStr(context, tildefriends, "http_port", JS_NewInt32(context, task->_http_port));
JS_SetPropertyStr(context, tildefriends, "https_port", JS_NewInt32(context, task->_https_port));
JS_SetPropertyStr(context, global, "getStats", JS_NewCFunction(context, _tf_task_getStats, "getStats", 0));
}
else