Fixed some plumbing so that I can actually stay connected to a go-ssb-room.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4032 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2022-11-12 02:00:49 +00:00
parent de59a7f338
commit 2dc7f58c80
2 changed files with 71 additions and 50 deletions

View File

@ -94,8 +94,45 @@ function tunnel_attendants(request) {
}); });
} }
function send_blobs_create_wants(connection) {
connection.send_json({'name': ['blobs', 'createWants'], 'type': 'source', 'args': []}, function on_blob_create_wants(message) {
Object.keys(message.message).forEach(function(id) {
if (message.message[id] < 0) {
if (g_wants_requests[connection.id]) {
delete connection.active_blob_wants[id];
var blob = ssb.blobGet(id);
if (blob) {
var out_message = {};
out_message[id] = blob.byteLength;
g_wants_requests[connection.id].send_json(out_message);
}
}
} else {
var received_bytes = 0;
var expected_bytes = message.message[id];
var buffer = new Uint8Array(expected_bytes);
connection.send_json({'name': ['blobs', 'get'], 'type': 'source', 'args': [id]}, function(message) {
if (message.flags & 0x4 /* end */) {
delete connection.active_blob_wants[id];
} else {
buffer.set(new Uint8Array(message.message, 0, message.message.byteLength), received_bytes);
received_bytes += message.message.byteLength;
if (received_bytes == expected_bytes) {
ssb.blobStore(buffer);
}
}
});
if (g_wants_requests[connection.id] && Object.keys(connection.active_blob_wants).length < k_blobs_concurrent_target) {
requestMoreBlobs(g_wants_requests[connection.id]);
}
}
});
});
}
ssb.addEventListener('connections', function on_connections_changed(change, connection) { ssb.addEventListener('connections', function on_connections_changed(change, connection) {
if (change == 'add') { if (change == 'add') {
connection.active_blob_wants = {};
var sequence = get_latest_sequence_for_author(connection.id); var sequence = get_latest_sequence_for_author(connection.id);
if (k_use_create_history_stream) { if (k_use_create_history_stream) {
connection.send_json({'name': ['createHistoryStream'], 'type': 'source', 'args': [{'id': connection.id, 'seq': sequence, 'live': true, 'keys': false}]}, storeMessage); connection.send_json({'name': ['createHistoryStream'], 'type': 'source', 'args': [{'id': connection.id, 'seq': sequence, 'live': true, 'keys': false}]}, storeMessage);
@ -111,49 +148,16 @@ ssb.addEventListener('connections', function on_connections_changed(change, conn
}); });
} else { } else {
if (connection.is_client) { if (connection.is_client) {
connection.send_json({'name': ['tunnel', 'isRoom'], 'args': []}, function tunnel_is_room(request) { connection.send_json({"name": ["ebt", "replicate"], "args": [{"version": 3, "format": "classic"}], "type": "duplex"}, ebtReplicateClient);
connection.send_json_async({'name': ['tunnel', 'isRoom'], 'args': []}, function tunnel_is_room(request) {
if (request.message) { if (request.message) {
connection.send_json({'name': ['room', 'attendants'], 'args': [], 'type': 'source'}, tunnel_attendants); request.connection.send_json({'name': ['room', 'attendants'], 'args': [], 'type': 'source'}, tunnel_attendants);
} }
}); });
connection.send_json({"name": ["ebt", "replicate"], "args": [{"version": 3, "format": "classic"}], "type": "duplex"}, ebtReplicateClient);
} }
send_blobs_create_wants(connection);
} }
connection.active_blob_wants = {};
connection.send_json({'name': ['blobs', 'createWants'], 'type': 'source', 'args': []}, function on_blob_create_wants(message) {
Object.keys(message.message).forEach(function(id) {
if (message.message[id] < 0) {
if (g_wants_requests[connection.id]) {
delete connection.active_blob_wants[id];
var blob = ssb.blobGet(id);
if (blob) {
var out_message = {};
out_message[id] = blob.byteLength;
g_wants_requests[connection.id].send_json(out_message);
}
}
} else {
var received_bytes = 0;
var expected_bytes = message.message[id];
var buffer = new Uint8Array(expected_bytes);
connection.send_json({'name': ['blobs', 'get'], 'type': 'source', 'args': [id]}, function(message) {
if (message.flags & 0x4 /* end */) {
delete connection.active_blob_wants[id];
} else {
buffer.set(new Uint8Array(message.message, 0, message.message.byteLength), received_bytes);
received_bytes += message.message.byteLength;
if (received_bytes == expected_bytes) {
ssb.blobStore(buffer);
}
}
});
if (g_wants_requests[connection.id] && Object.keys(connection.active_blob_wants).length < k_blobs_concurrent_target) {
requestMoreBlobs(g_wants_requests[connection.id]);
}
}
});
});
} else if (change == 'remove') { } else if (change == 'remove') {
print('REMOVE', connection.id); print('REMOVE', connection.id);
notify_attendant_changed(connection.id, 'left'); notify_attendant_changed(connection.id, 'left');
@ -181,9 +185,6 @@ function requestMoreBlobs(request) {
row => blob_want_discovered(request, row.id)); row => blob_want_discovered(request, row.id));
} }
ssb.addRpc(['manifest'], function(request) {
});
ssb.addRpc(['blobs', 'createWants'], function(request) { ssb.addRpc(['blobs', 'createWants'], function(request) {
g_wants_requests[request.connection.id] = request; g_wants_requests[request.connection.id] = request;
ssb.addEventListener('blob_want_added', id => blob_want_discovered(request, id)); ssb.addEventListener('blob_want_added', id => blob_want_discovered(request, id));
@ -229,7 +230,7 @@ ssb.addRpc(['gossip', 'ping'], function(request) {
}); });
ssb.addRpc(['tunnel', 'isRoom'], function(request) { ssb.addRpc(['tunnel', 'isRoom'], function(request) {
request.send_json(true); request.send_json({"name":"tilde friends tunnel","membership":false,"features":["tunnel","room1"]});
}); });
function notify_attendant_changed(id, type) { function notify_attendant_changed(id, type) {
@ -369,11 +370,13 @@ function ebtReplicateCommon(request) {
} }
function ebtReplicateClient(request) { function ebtReplicateClient(request) {
if (!request.connection.message_registered) { if (request.message?.name !== 'Error') {
ebtReplicateRegisterMessageCallback(request); if (!request.connection.message_registered) {
request.connection.message_registered = true; ebtReplicateRegisterMessageCallback(request);
request.connection.message_registered = true;
}
ebtReplicateCommon(request);
} }
ebtReplicateCommon(request);
} }
function ebtReplicateServer(request) { function ebtReplicateServer(request) {

View File

@ -1124,6 +1124,13 @@ static bool _tf_ssb_name_equals(JSContext* context, JSValue object, const char**
result = false; result = false;
} }
} }
else if (JS_IsString(name))
{
/* Manifest is traditionally sent as not an array for some reason. */
const char* str = JS_ToCString(context, name);
result = str && match[0] && strcmp(str, match[0]) == 0 && !match[1];
JS_FreeCString(context, str);
}
else else
{ {
result = false; result = false;
@ -1625,7 +1632,6 @@ static void _tf_ssb_connection_on_connect(uv_connect_t* connect, int status)
} }
else else
{ {
printf("connect => %s\n", uv_strerror(status));
_tf_ssb_connection_close(connection, "uv_tcp_connect failed"); _tf_ssb_connection_close(connection, "uv_tcp_connect failed");
} }
} }
@ -1986,7 +1992,7 @@ static void _tf_ssb_connection_cleanup_value(tf_ssb_t* ssb, void* user_data)
} }
} }
static JSValue _tf_ssb_connection_send_json(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) static JSValue _tf_ssb_connection_send_json_internal(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int flags)
{ {
tf_ssb_connection_t* connection = JS_GetOpaque(this_val, _connection_class_id); tf_ssb_connection_t* connection = JS_GetOpaque(this_val, _connection_class_id);
if (!connection) if (!connection)
@ -2003,7 +2009,7 @@ static JSValue _tf_ssb_connection_send_json(JSContext* context, JSValueConst thi
tf_ssb_connection_rpc_send( tf_ssb_connection_rpc_send(
connection, connection,
k_ssb_rpc_flag_json | k_ssb_rpc_flag_stream, flags,
request_number, request_number,
(const uint8_t*)message, (const uint8_t*)message,
size, size,
@ -2014,6 +2020,16 @@ static JSValue _tf_ssb_connection_send_json(JSContext* context, JSValueConst thi
return JS_NewInt32(context, request_number); return JS_NewInt32(context, request_number);
} }
static JSValue _tf_ssb_connection_send_json(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
return _tf_ssb_connection_send_json_internal(context, this_val, argc, argv, k_ssb_rpc_flag_json | k_ssb_rpc_flag_stream);
}
static JSValue _tf_ssb_connection_send_json_async(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
return _tf_ssb_connection_send_json_internal(context, this_val, argc, argv, k_ssb_rpc_flag_json);
}
static void _tf_ssb_connection_process_message_async(uv_async_t* async) static void _tf_ssb_connection_process_message_async(uv_async_t* async)
{ {
tf_ssb_connection_t* connection = async->data; tf_ssb_connection_t* connection = async->data;
@ -2059,6 +2075,7 @@ tf_ssb_connection_t* tf_ssb_connection_create(tf_ssb_t* ssb, const char* host, c
connection->object = JS_NewObjectClass(ssb->context, _connection_class_id); connection->object = JS_NewObjectClass(ssb->context, _connection_class_id);
JS_SetOpaque(connection->object, connection); JS_SetOpaque(connection->object, connection);
JS_SetPropertyStr(context, connection->object, "send_json", JS_NewCFunction(context, _tf_ssb_connection_send_json, "send_json", 2)); JS_SetPropertyStr(context, connection->object, "send_json", JS_NewCFunction(context, _tf_ssb_connection_send_json, "send_json", 2));
JS_SetPropertyStr(context, connection->object, "send_json_async", JS_NewCFunction(context, _tf_ssb_connection_send_json_async, "send_json_async", 2));
char public_key_str[k_id_base64_len] = { 0 }; char public_key_str[k_id_base64_len] = { 0 };
if (tf_ssb_id_bin_to_str(public_key_str, sizeof(public_key_str), public_key)) if (tf_ssb_id_bin_to_str(public_key_str, sizeof(public_key_str), public_key))
{ {
@ -2069,7 +2086,6 @@ tf_ssb_connection_t* tf_ssb_connection_create(tf_ssb_t* ssb, const char* host, c
memcpy(connection->serverpub, public_key, sizeof(connection->serverpub)); memcpy(connection->serverpub, public_key, sizeof(connection->serverpub));
uv_tcp_init(ssb->loop, &connection->tcp); uv_tcp_init(ssb->loop, &connection->tcp);
printf("uv_tcp_connect\n");
int result = uv_tcp_connect(&connection->connect, &connection->tcp, (const struct sockaddr*)addr, _tf_ssb_connection_on_connect); int result = uv_tcp_connect(&connection->connect, &connection->tcp, (const struct sockaddr*)addr, _tf_ssb_connection_on_connect);
if (result) if (result)
{ {
@ -2118,6 +2134,7 @@ tf_ssb_connection_t* tf_ssb_connection_tunnel_create(tf_ssb_connection_t* connec
tunnel->object = JS_NewObjectClass(ssb->context, _connection_class_id); tunnel->object = JS_NewObjectClass(ssb->context, _connection_class_id);
JS_SetOpaque(tunnel->object, tunnel); JS_SetOpaque(tunnel->object, tunnel);
JS_SetPropertyStr(context, tunnel->object, "send_json", JS_NewCFunction(context, _tf_ssb_connection_send_json, "send_json", 2)); JS_SetPropertyStr(context, tunnel->object, "send_json", JS_NewCFunction(context, _tf_ssb_connection_send_json, "send_json", 2));
JS_SetPropertyStr(context, tunnel->object, "send_json_async", JS_NewCFunction(context, _tf_ssb_connection_send_json_async, "send_json_async", 2));
JS_SetPropertyStr(context, tunnel->object, "id", JS_NewString(context, target_id)); JS_SetPropertyStr(context, tunnel->object, "id", JS_NewString(context, target_id));
JS_SetPropertyStr(context, tunnel->object, "is_client", JS_TRUE); JS_SetPropertyStr(context, tunnel->object, "is_client", JS_TRUE);
@ -2217,6 +2234,7 @@ static void _tf_ssb_on_connection(uv_stream_t* stream, int status)
connection->object = JS_NewObjectClass(ssb->context, _connection_class_id); connection->object = JS_NewObjectClass(ssb->context, _connection_class_id);
JS_SetPropertyStr(ssb->context, connection->object, "send_json", JS_NewCFunction(ssb->context, _tf_ssb_connection_send_json, "send_json", 2)); JS_SetPropertyStr(ssb->context, connection->object, "send_json", JS_NewCFunction(ssb->context, _tf_ssb_connection_send_json, "send_json", 2));
JS_SetPropertyStr(ssb->context, connection->object, "send_json_async", JS_NewCFunction(ssb->context, _tf_ssb_connection_send_json_async, "send_json_async", 2));
JS_SetOpaque(connection->object, connection); JS_SetOpaque(connection->object, connection);
if (uv_tcp_init(ssb->loop, &connection->tcp) != 0) if (uv_tcp_init(ssb->loop, &connection->tcp) != 0)