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

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

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)