Try to fix some connection issues. Urge to rewrite rising.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3778 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2022-01-20 04:01:45 +00:00
parent df94378b96
commit 1734c88627
2 changed files with 59 additions and 33 deletions

View File

@ -278,7 +278,11 @@ static void _tf_ssb_write(tf_ssb_connection_t* connection, void* data, size_t si
uv_write_t* write = malloc(sizeof(uv_write_t) + size);
*write = (uv_write_t) { .data = connection };
memcpy(write + 1, data, size);
uv_write(write, (uv_stream_t*)&connection->tcp, &(uv_buf_t) { .base = (char*)(write + 1), .len = size }, 1, _tf_ssb_connection_on_write);
int result = uv_write(write, (uv_stream_t*)&connection->tcp, &(uv_buf_t) { .base = (char*)(write + 1), .len = size }, 1, _tf_ssb_connection_on_write);
if (result)
{
_tf_ssb_connection_close(connection, "write failed");
}
}
static void _tf_ssb_connection_send_identity(tf_ssb_connection_t* connection, uint8_t* hmac, uint8_t* pubkey)
@ -1347,8 +1351,16 @@ static void _tf_ssb_connection_on_connect(uv_connect_t* connect, int status)
if (status == 0)
{
connection->state = k_tf_ssb_state_connected;
uv_read_start(connect->handle, _tf_ssb_connection_on_tcp_alloc, _tf_ssb_connection_on_tcp_recv);
_tf_ssb_connection_client_send_hello(connect->handle);
int result = uv_read_start(connect->handle, _tf_ssb_connection_on_tcp_alloc, _tf_ssb_connection_on_tcp_recv);
if (result)
{
printf("uv_read_start => %s\n", uv_strerror(status));
uv_close((uv_handle_t*)&connection->tcp, _tf_ssb_connection_on_close);
}
else
{
_tf_ssb_connection_client_send_hello(connect->handle);
}
}
else
{
@ -1808,6 +1820,24 @@ static JSValue _tf_ssb_connection_send_json(JSContext* context, JSValueConst thi
tf_ssb_connection_t* tf_ssb_connection_create(tf_ssb_t* ssb, const char* host, const struct sockaddr_in* addr, const uint8_t* public_key)
{
for (tf_ssb_connection_t* connection = ssb->connections; connection; connection = connection->next)
{
if (memcmp(connection->serverpub, public_key, k_id_bin_len) == 0)
{
char id[k_id_base64_len];
tf_ssb_id_bin_to_str(id, sizeof(id), public_key);
printf("Not connecting to %s:%d, because we are already connected to %s.\n", host, ntohs(addr->sin_port), id);
return NULL;
}
else if (memcmp(ssb->pub, public_key, k_id_bin_len) == 0)
{
char id[k_id_base64_len];
tf_ssb_id_bin_to_str(id, sizeof(id), public_key);
printf("Not connecting to %s:%d, because they appear to be ourselves %s.\n", host, ntohs(addr->sin_port), id);
return NULL;
}
}
JSContext* context = ssb->context;
tf_ssb_connection_t* connection = malloc(sizeof(tf_ssb_connection_t));
memset(connection, 0, sizeof(*connection));
@ -1831,12 +1861,22 @@ tf_ssb_connection_t* tf_ssb_connection_create(tf_ssb_t* ssb, const char* host, c
memcpy(connection->serverpub, public_key, sizeof(connection->serverpub));
uv_tcp_init(ssb->loop, &connection->tcp);
uv_tcp_connect(&connection->connect, &connection->tcp, (const struct sockaddr*)addr, _tf_ssb_connection_on_connect);
connection->next = ssb->connections;
ssb->connections = connection;
ssb->connections_count++;
_tf_ssb_notify_connections_changed(ssb, k_tf_ssb_change_create, connection);
printf("uv_tcp_connect\n");
int result = uv_tcp_connect(&connection->connect, &connection->tcp, (const struct sockaddr*)addr, _tf_ssb_connection_on_connect);
if (result)
{
printf("uv_tcp_connect(%s): %s\n", host, uv_strerror(result));
JS_SetOpaque(connection->object, NULL);
JS_FreeValue(ssb->context, connection->object);
free(connection);
}
else
{
connection->next = ssb->connections;
ssb->connections = connection;
ssb->connections_count++;
_tf_ssb_notify_connections_changed(ssb, k_tf_ssb_change_create, connection);
}
return connection;
}
@ -1856,31 +1896,17 @@ static void _tf_on_connect_getaddrinfo(uv_getaddrinfo_t* addrinfo, int result, s
struct sockaddr_in addr = *(struct sockaddr_in*)info->ai_addr;
addr.sin_port = htons(connect->port);
tf_ssb_connection_create(connect->ssb, connect->host, &addr, connect->key);
free(connect);
}
else
{
printf("getaddrinfo => %s\n", uv_strerror(result));
}
free(connect);
uv_freeaddrinfo(info);
}
void tf_ssb_connect(tf_ssb_t* ssb, const char* host, int port, const uint8_t* key)
{
for (tf_ssb_connection_t* connection = ssb->connections; connection; connection = connection->next)
{
if (memcmp(connection->serverpub, key, k_id_bin_len) == 0)
{
char id[k_id_base64_len];
tf_ssb_id_bin_to_str(id, sizeof(id), key);
printf("Not connecting to %s:%d, because we are already connected to %s.\n", host, port, id);
return;
}
else if (memcmp(key, ssb->pub, k_id_bin_len) == 0)
{
char id[k_id_base64_len];
tf_ssb_id_bin_to_str(id, sizeof(id), key);
printf("Not connecting to %s:%d, because they appear to be ourselves %s.\n", host, port, id);
return;
}
}
connect_t* connect = malloc(sizeof(connect_t));
*connect = (connect_t)
{
@ -1894,6 +1920,7 @@ void tf_ssb_connect(tf_ssb_t* ssb, const char* host, int port, const uint8_t* ke
if (r < 0)
{
printf("uv_getaddrinfo: %s\n", uv_strerror(r));
free(connect);
}
}

View File

@ -1476,11 +1476,6 @@ void tf_task_destroy(tf_task_t* task)
free(export);
}
if (task->_trace)
{
tf_trace_destroy(task->_trace);
}
if (task->_ssb)
{
tf_ssb_destroy(task->_ssb);
@ -1507,6 +1502,10 @@ void tf_task_destroy(tf_task_t* task)
{
uv_print_all_handles(&task->_loop, stdout);
}
if (task->_trace)
{
tf_trace_destroy(task->_trace);
}
--_count;
free((void*)task->_path);
free(task);