Get my foot in the door converting ssb.js to C.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4050 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2022-11-17 01:36:24 +00:00
parent 902287292d
commit be6f24b3ee
4 changed files with 90 additions and 28 deletions

View File

@ -205,33 +205,6 @@ ssb.addRpc(['blobs', 'has'], function(request) {
request.send_json(found);
});
ssb.addRpc(['blobs', 'get'], function(request) {
for (let arg of request.args) {
var blob;
if (arg.key) {
blob = ssb.blobGet(arg.key);
} else {
blob = ssb.blobGet(arg);
}
const k_send_max = 8192;
if (blob.byteLength > k_send_max) {
for (var i = 0; i < blob.byteLength; i += k_send_max) {
var buffer = new Uint8Array(blob, i, Math.min(blob.byteLength - i, k_send_max));
request.send_binary(buffer);
}
} else {
request.send_binary(blob);
}
request.send_json_end(true);
}
});
ssb.addRpc(['gossip', 'ping'], function(request) {
request.more(function ping(message) {
message.send_json(Date.now());
});
});
ssb.addRpc(['tunnel', 'isRoom'], function(request) {
request.send_json({"name": "tilde friends tunnel", "membership": false, "features": ["tunnel", "room1"]});
});

View File

@ -3,6 +3,7 @@
#include "mem.h"
#include "ssb.connections.h"
#include "ssb.db.h"
#include "ssb.rpc.h"
#include "trace.h"
#include "util.js.h"
@ -1779,6 +1780,7 @@ tf_ssb_t* tf_ssb_create(uv_loop_t* loop, JSContext* context, sqlite3* db)
ssb->connections_tracker = tf_ssb_connections_create(ssb);
tf_ssb_rpc_register(ssb);
return ssb;
}

82
src/ssb.rpc.c Normal file
View File

@ -0,0 +1,82 @@
#include "ssb.rpc.h"
#include "mem.h"
#include "ssb.h"
#include "ssb.db.h"
#include "util.js.h"
#include <inttypes.h>
#include <string.h>
#include <time.h>
static void _tf_ssb_rpc_gossip_ping(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
{
char buffer[256];
snprintf(buffer, sizeof(buffer), "%" PRId64, (int64_t)time(NULL));
tf_ssb_connection_rpc_send(
connection,
flags,
-request_number,
(const uint8_t*)buffer,
strlen(buffer),
NULL,
NULL,
NULL);
}
static void _tf_ssb_rpc_blobs_get(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
{
tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection);
JSContext* context = tf_ssb_connection_get_context(connection);
JSValue ids = JS_GetPropertyStr(context, args, "args");
int length = tf_util_get_length(context, ids);
bool success = false;
for (int i = 0; i < length; i++)
{
JSValue arg = JS_GetPropertyUint32(context, ids, i);
if (!JS_IsString(arg)) {
JSValue key = JS_GetPropertyStr(context, arg, "key");
JS_FreeValue(context, arg);
arg = key;
}
const char* id = JS_ToCString(context, arg);
uint8_t* blob = NULL;
size_t size = 0;
const int k_send_max = 8192;
if (tf_ssb_db_blob_get(ssb, id, &blob, &size))
{
for (size_t offset = 0; offset < size; offset += k_send_max)
{
tf_ssb_connection_rpc_send(
connection,
k_ssb_rpc_flag_binary | k_ssb_rpc_flag_stream,
-request_number,
blob + offset,
offset + k_send_max <= size ? k_send_max : (size - offset),
NULL,
NULL,
NULL);
}
success = true;
tf_free(blob);
}
JS_FreeCString(context, id);
JS_FreeValue(context, arg);
}
JS_FreeValue(context, ids);
tf_ssb_connection_rpc_send(
connection,
k_ssb_rpc_flag_json | k_ssb_rpc_flag_end_error,
-request_number,
(const uint8_t*)(success ? "true" : "false"),
strlen(success ? "true" : "false"),
NULL,
NULL,
NULL);
}
void tf_ssb_rpc_register(tf_ssb_t* ssb)
{
tf_ssb_add_rpc_callback(ssb, (const char*[]) { "gossip", "ping", NULL }, _tf_ssb_rpc_gossip_ping, NULL, NULL);
tf_ssb_add_rpc_callback(ssb, (const char*[]) { "blobs", "get", NULL }, _tf_ssb_rpc_blobs_get, NULL, NULL);
}

5
src/ssb.rpc.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
typedef struct _tf_ssb_t tf_ssb_t;
void tf_ssb_rpc_register(tf_ssb_t* ssb);