actor messages now delivered as blobs
This commit is contained in:
@@ -571,7 +571,9 @@ function actor_send(actor, message) {
|
|||||||
|
|
||||||
// message to actor in same flock
|
// message to actor in same flock
|
||||||
if (actor[ACTORDATA].id && actor_mod.mailbox_exist(actor[ACTORDATA].id)) {
|
if (actor[ACTORDATA].id && actor_mod.mailbox_exist(actor[ACTORDATA].id)) {
|
||||||
actor_mod.mailbox_push(actor[ACTORDATA].id, wota.encode(message))
|
var wota_blob = wota.encode(message)
|
||||||
|
// log.console(`sending wota blob of ${wota_blob.length/8} bytes`)
|
||||||
|
actor_mod.mailbox_push(actor[ACTORDATA].id, wota_blob)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -660,8 +662,8 @@ $_[ACTORDATA].id = cell.id
|
|||||||
// Actor's timeslice for processing a single message
|
// Actor's timeslice for processing a single message
|
||||||
function turn(msg)
|
function turn(msg)
|
||||||
{
|
{
|
||||||
var decoded = wota.decode(msg)
|
var mes = wota.decode(msg)
|
||||||
handle_message(decoded)
|
handle_message(mes)
|
||||||
send_messages()
|
send_messages()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,9 @@
|
|||||||
#include "wota.h"
|
#include "wota.h"
|
||||||
#include "qjs_wota.h"
|
#include "qjs_wota.h"
|
||||||
|
|
||||||
|
#define BLOB_IMPLEMENTATION
|
||||||
|
#include "blob.h"
|
||||||
|
|
||||||
#include "physfs.h"
|
#include "physfs.h"
|
||||||
#include "stb_ds.h"
|
#include "stb_ds.h"
|
||||||
#include "jsffi.h"
|
#include "jsffi.h"
|
||||||
@@ -161,8 +164,8 @@ void actor_free(cell_rt *actor)
|
|||||||
|
|
||||||
/* Free all letters in the queue */
|
/* Free all letters in the queue */
|
||||||
for (int i = 0; i < arrlen(actor->letters); i++) {
|
for (int i = 0; i < arrlen(actor->letters); i++) {
|
||||||
if (actor->letters[i].type == LETTER_WOTA) {
|
if (actor->letters[i].type == LETTER_BLOB) {
|
||||||
free(actor->letters[i].wota_data);
|
blob_destroy(actor->letters[i].blob_data);
|
||||||
} else if (actor->letters[i].type == LETTER_CALLBACK) {
|
} else if (actor->letters[i].type == LETTER_CALLBACK) {
|
||||||
JS_FreeValue(js, actor->letters[i].callback);
|
JS_FreeValue(js, actor->letters[i].callback);
|
||||||
}
|
}
|
||||||
@@ -414,13 +417,13 @@ const char *send_message(const char *id, void *msg)
|
|||||||
{
|
{
|
||||||
cell_rt *target = get_actor(id);
|
cell_rt *target = get_actor(id);
|
||||||
if (!target) {
|
if (!target) {
|
||||||
free(msg);
|
blob_destroy((blob *)msg);
|
||||||
return "Could not get actor from id.";
|
return "Could not get actor from id.";
|
||||||
}
|
}
|
||||||
|
|
||||||
letter l;
|
letter l;
|
||||||
l.type = LETTER_WOTA;
|
l.type = LETTER_BLOB;
|
||||||
l.wota_data = msg;
|
l.blob_data = (blob *)msg;
|
||||||
|
|
||||||
SDL_LockMutex(target->msg_mutex);
|
SDL_LockMutex(target->msg_mutex);
|
||||||
|
|
||||||
@@ -530,9 +533,11 @@ void actor_turn(cell_rt *actor)
|
|||||||
arrdel(actor->letters, 0);
|
arrdel(actor->letters, 0);
|
||||||
SDL_UnlockMutex(actor->msg_mutex);
|
SDL_UnlockMutex(actor->msg_mutex);
|
||||||
|
|
||||||
if (l.type == LETTER_WOTA) {
|
if (l.type == LETTER_BLOB) {
|
||||||
JSValue arg = wota2value(actor->context, l.wota_data);
|
// Create a JS blob from the C blob
|
||||||
free(l.wota_data);
|
size_t size = l.blob_data->length / 8; // Convert bits to bytes
|
||||||
|
JSValue arg = js_new_blob_stoned_copy(actor->context, l.blob_data->data, size);
|
||||||
|
blob_destroy(l.blob_data);
|
||||||
result = JS_Call(actor->context, actor->message_handle, JS_UNDEFINED, 1, &arg);
|
result = JS_Call(actor->context, actor->message_handle, JS_UNDEFINED, 1, &arg);
|
||||||
uncaught_exception(actor->context, result);
|
uncaught_exception(actor->context, result);
|
||||||
JS_FreeValue(actor->context, arg);
|
JS_FreeValue(actor->context, arg);
|
||||||
|
|||||||
@@ -9,14 +9,14 @@
|
|||||||
|
|
||||||
/* Letter type for unified message queue */
|
/* Letter type for unified message queue */
|
||||||
typedef enum {
|
typedef enum {
|
||||||
LETTER_WOTA, /* Raw wota message */
|
LETTER_BLOB, /* Blob message */
|
||||||
LETTER_CALLBACK /* JSValue callback function */
|
LETTER_CALLBACK /* JSValue callback function */
|
||||||
} letter_type;
|
} letter_type;
|
||||||
|
|
||||||
typedef struct letter {
|
typedef struct letter {
|
||||||
letter_type type;
|
letter_type type;
|
||||||
union {
|
union {
|
||||||
void *wota_data; /* For LETTER_WOTA */
|
blob *blob_data; /* For LETTER_BLOB */
|
||||||
JSValue callback; /* For LETTER_CALLBACK */
|
JSValue callback; /* For LETTER_CALLBACK */
|
||||||
};
|
};
|
||||||
} letter;
|
} letter;
|
||||||
|
|||||||
@@ -111,16 +111,23 @@ JSC_CCALL(os_mailbox_push,
|
|||||||
if (stop) return JS_UNDEFINED;
|
if (stop) return JS_UNDEFINED;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
// void *data = value2wota(js, argv[1], JS_UNDEFINED, NULL);
|
|
||||||
size_t size;
|
size_t size;
|
||||||
void *data = js_get_blob_data(js, &size, argv[1]);
|
void *data = js_get_blob_data(js, &size, argv[1]);
|
||||||
|
|
||||||
void *copy = malloc(size);
|
// Create a new blob and copy the data
|
||||||
memcpy(copy, data, size);
|
blob *msg_blob = blob_new(size * 8); // Convert bytes to bits
|
||||||
|
if (!msg_blob) {
|
||||||
|
return JS_ThrowInternalError(js, "Could not allocate blob");
|
||||||
|
}
|
||||||
|
|
||||||
const char *err = send_message(id, copy);
|
// Copy data to blob
|
||||||
|
memcpy(msg_blob->data, data, size);
|
||||||
|
msg_blob->length = size * 8;
|
||||||
|
blob_make_stone(msg_blob); // Make it immutable
|
||||||
|
|
||||||
|
const char *err = send_message(id, msg_blob);
|
||||||
if (err) {
|
if (err) {
|
||||||
free(data);
|
blob_destroy(msg_blob);
|
||||||
return JS_ThrowInternalError(js, "Could not send message: %s", err);
|
return JS_ThrowInternalError(js, "Could not send message: %s", err);
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user