diff options
-rw-r--r-- | include/salticidae/event.h | 20 | ||||
-rw-r--r-- | include/salticidae/network.h | 8 | ||||
-rw-r--r-- | include/salticidae/stream.h | 2 | ||||
-rw-r--r-- | include/salticidae/type.h | 1 | ||||
-rw-r--r-- | src/event.cpp | 38 | ||||
-rw-r--r-- | src/network.cpp | 15 | ||||
-rw-r--r-- | src/stream.cpp | 6 | ||||
-rw-r--r-- | test/test_msgnet_c.c | 27 |
8 files changed, 87 insertions, 30 deletions
diff --git a/include/salticidae/event.h b/include/salticidae/event.h index 5b44027..c82d54f 100644 --- a/include/salticidae/event.h +++ b/include/salticidae/event.h @@ -647,14 +647,20 @@ class ThreadCall { #ifdef SALTICIDAE_CBINDINGS using eventcontext_t = salticidae::EventContext; +using threadcall_t = salticidae::ThreadCall; +using threadcall_handle_t = salticidae::ThreadCall::Handle; using sigev_t = salticidae::SigEvent; +using timerev_t = salticidae::TimerEvent; #endif #else #include "config.h" #ifdef SALTICIDAE_CBINDINGS typedef struct eventcontext_t eventcontext_t; +typedef struct threadcall_t threadcall_t; +typedef struct threadcall_handle_t threadcall_handle_t; typedef struct sigev_t sigev_t; +typedef struct timerev_t timerev_t; #endif #endif @@ -669,11 +675,25 @@ void eventcontext_free(eventcontext_t *self); void eventcontext_dispatch(eventcontext_t *self); void eventcontext_stop(eventcontext_t *self); +typedef void (*threadcall_callback_t)(threadcall_handle_t *handle, void *userdata); +threadcall_t *threadcall_new(const eventcontext_t *ec); +void threadcall_free(threadcall_t *self); +void threadcall_async_call(threadcall_t *self, threadcall_callback_t callback, void *userdata); +eventcontext_t *threadcall_get_ec(threadcall_t *self); + typedef void (*sigev_callback_t)(int events); sigev_t *sigev_new(const eventcontext_t *ec, sigev_callback_t cb); void sigev_free(sigev_t *self); void sigev_add(sigev_t *self, int sig); +typedef void (*timerev_callback_t)(timerev_t *); +timerev_t *timerev_new(const eventcontext_t *ec, timerev_callback_t callback); +void timerev_set_callback(timerev_t *self, timerev_callback_t callback); +void timerev_free(timerev_t *self); +void timerev_add(timerev_t *self, double t_sec); +void timerev_del(timerev_t *self); +void timerev_clear(timerev_t *self); + #ifdef __cplusplus } #endif diff --git a/include/salticidae/network.h b/include/salticidae/network.h index c57f418..3b53f26 100644 --- a/include/salticidae/network.h +++ b/include/salticidae/network.h @@ -842,11 +842,11 @@ msgnetwork_conn_t *msgnetwork_connect(msgnetwork_t *self, const netaddr_t *addr) void msgnetwork_listen(msgnetwork_t *self, const netaddr_t *listen_addr); void msgnetwork_start(msgnetwork_t *self); -typedef void (*msgnetwork_msg_callback_t)(const msg_t *, const msgnetwork_conn_t *); -void msgnetwork_reg_handler(msgnetwork_t *self, _opcode_t opcode, msgnetwork_msg_callback_t cb); +typedef void (*msgnetwork_msg_callback_t)(const msg_t *, const msgnetwork_conn_t *, void *userdata); +void msgnetwork_reg_handler(msgnetwork_t *self, _opcode_t opcode, msgnetwork_msg_callback_t cb, void *userdata); -typedef void (*msgnetwork_conn_callback_t)(const msgnetwork_conn_t *, bool); -void msgnetwork_reg_conn_handler(msgnetwork_t *self, msgnetwork_conn_callback_t cb); +typedef void (*msgnetwork_conn_callback_t)(const msgnetwork_conn_t *, bool, void *userdata); +void msgnetwork_reg_conn_handler(msgnetwork_t *self, msgnetwork_conn_callback_t cb, void *userdata); msgnetwork_t *msgnetwork_conn_get_net(const msgnetwork_conn_t *conn); msgnetwork_conn_mode_t msgnetwork_conn_get_mode(const msgnetwork_conn_t *conn); diff --git a/include/salticidae/stream.h b/include/salticidae/stream.h index 9b676a6..8f0c5b4 100644 --- a/include/salticidae/stream.h +++ b/include/salticidae/stream.h @@ -535,7 +535,7 @@ int64_t datastream_get_i64(datastream_t *self); const uint8_t *datastream_get_data_inplace(datastream_t *self, size_t len); uint256_t *datastream_get_hash(const datastream_t *self); -bytearray_t *datastream_to_bytearray(datastream_t *_moved_self); +bytearray_t *bytearray_new_moved_from_datastream(datastream_t *_moved_self); #ifdef __cplusplus } diff --git a/include/salticidae/type.h b/include/salticidae/type.h index c62272d..2d97ae9 100644 --- a/include/salticidae/type.h +++ b/include/salticidae/type.h @@ -99,6 +99,7 @@ using bytearray_t = salticidae::bytearray_t; #else #include <stdint.h> +#include <stddef.h> #ifdef SALTICIDAE_CBINDINGS typedef struct bytearray_t bytearray_t; #endif diff --git a/src/event.cpp b/src/event.cpp index 6078c37..2e38e5f 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -12,14 +12,48 @@ void eventcontext_dispatch(eventcontext_t *self) { return self->dispatch(); } void eventcontext_stop(eventcontext_t *self) { return self->stop(); } -sigev_t *sigev_new(const eventcontext_t *self, sigev_callback_t cb) { - return new sigev_t(*self, cb); +sigev_t *sigev_new(const eventcontext_t *self, sigev_callback_t callback) { + return new sigev_t(*self, callback); +} + +threadcall_t *threadcall_new(const eventcontext_t *ec) { return new threadcall_t(*ec); } + +void threadcall_free(threadcall_t *self) { delete self; } + +void threadcall_async_call(threadcall_t *self, threadcall_callback_t callback, void *userdata) { + self->async_call([=](salticidae::ThreadCall::Handle &h) { + callback(&h, userdata); + }); +} + +eventcontext_t *threadcall_get_ec(threadcall_t *self) { + return new eventcontext_t(self->get_ec()); } void sigev_free(sigev_t *self) { delete self; } void sigev_add(sigev_t *self, int sig) { self->add(sig); } +timerev_t *timerev_new(const eventcontext_t *ec, timerev_callback_t callback) { + return new timerev_t(*ec, [callback](salticidae::TimerEvent &ev) { + callback(&ev); + }); +} + +void timerev_set_callback(timerev_t *self, timerev_callback_t callback) { + self->set_callback([callback](salticidae::TimerEvent &ev) { + callback(&ev); + }); +} + +void timerev_free(timerev_t *self) { delete self; } + +void timerev_add(timerev_t *self, double t_sec) { self->add(t_sec); } + +void timerev_del(timerev_t *self) { self->del(); } + +void timerev_clear(timerev_t *self) { self->clear(); } + } #endif diff --git a/src/network.cpp b/src/network.cpp index eb32f62..c14531c 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -35,17 +35,20 @@ void msgnetwork_listen(msgnetwork_t *self, const netaddr_t *listen_addr) { void msgnetwork_reg_handler(msgnetwork_t *self, _opcode_t opcode, - msgnetwork_msg_callback_t cb) { + msgnetwork_msg_callback_t cb, + void *userdata) { self->set_handler(opcode, - [cb](const msgnetwork_t::Msg &msg, const msgnetwork_conn_t &conn) { - cb(&msg, &conn); + [=](const msgnetwork_t::Msg &msg, const msgnetwork_conn_t &conn) { + cb(&msg, &conn, userdata); }); } -void msgnetwork_reg_conn_handler(msgnetwork_t *self, msgnetwork_conn_callback_t cb) { - self->reg_conn_handler([cb](const ConnPool::conn_t &_conn, bool connected) { +void msgnetwork_reg_conn_handler(msgnetwork_t *self, + msgnetwork_conn_callback_t cb, + void *userdata) { + self->reg_conn_handler([=](const ConnPool::conn_t &_conn, bool connected) { auto conn = salticidae::static_pointer_cast<msgnetwork_t::Conn>(_conn); - cb(&conn, connected); + cb(&conn, connected, userdata); }); } diff --git a/src/stream.cpp b/src/stream.cpp index 9dfe5fc..9ad41aa 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -77,9 +77,9 @@ uint256_t *datastream_get_hash(const datastream_t *self) { return new uint256_t(self->get_hash()); } -bytearray_t *datastream_to_bytearray(datastream_t *_moved_self) { - auto res = new bytearray_t(std::move(*_moved_self)); - delete _moved_self; +bytearray_t *bytearray_new_moved_from_datastream(datastream_t *_moved_src) { + auto res = new bytearray_t(std::move(*_moved_src)); + delete _moved_src; return res; } diff --git a/test/test_msgnet_c.c b/test/test_msgnet_c.c index b09e375..b717137 100644 --- a/test/test_msgnet_c.c +++ b/test/test_msgnet_c.c @@ -45,7 +45,7 @@ msg_t *msg_hello_serialize(const char *name, const char *text) { datastream_put_i32(serialized, (uint32_t)htole32(name_len)); datastream_put_data(serialized, name, name + name_len); datastream_put_data(serialized, text, text + strlen(text)); - msg_t *msg = msg_new(MSG_OPCODE_HELLO, datastream_to_bytearray(serialized)); + msg_t *msg = msg_new(MSG_OPCODE_HELLO, bytearray_new_moved_from_datastream(serialized)); return msg; } @@ -83,9 +83,9 @@ typedef struct MyNet { } MyNet; MyNet alice, bob; -void on_receive_hello(const msg_t *_msg, const msgnetwork_conn_t *conn) { +void on_receive_hello(const msg_t *_msg, const msgnetwork_conn_t *conn, void *userdata) { msgnetwork_t *net = msgnetwork_conn_get_net(conn); - const char *name = net == alice.net ? alice.name : bob.name; + const char *name = ((MyNet *)userdata)->name; MsgHello msg = msg_hello_unserialize(_msg); printf("[%s] %s says %s\n", name, msg.name, msg.text); msg_t *ack = msg_ack_serialize(); @@ -94,15 +94,14 @@ void on_receive_hello(const msg_t *_msg, const msgnetwork_conn_t *conn) { msg_free(ack); } -void on_receive_ack(const msg_t *msg, const msgnetwork_conn_t *conn) { - msgnetwork_t *net = msgnetwork_conn_get_net(conn); - const char *name = net == alice.net ? alice.name : bob.name; +void on_receive_ack(const msg_t *msg, const msgnetwork_conn_t *conn, void *userdata) { + const char *name = ((MyNet *)userdata)->name; printf("[%s] the peer knows\n", name); } -void conn_handler(const msgnetwork_conn_t *conn, bool connected) { +void conn_handler(const msgnetwork_conn_t *conn, bool connected, void *userdata) { msgnetwork_t *net = msgnetwork_conn_get_net(conn); - MyNet *n = net == alice.net ? &alice: &bob; + MyNet *n = (MyNet *)userdata; const char *name = n->name; if (connected) { @@ -150,13 +149,13 @@ int main() { alice = gen_mynet(ec, "Alice"); bob = gen_mynet(ec, "Bob"); - msgnetwork_reg_handler(alice.net, MSG_OPCODE_HELLO, on_receive_hello); - msgnetwork_reg_handler(alice.net, MSG_OPCODE_ACK, on_receive_ack); - msgnetwork_reg_handler(bob.net, MSG_OPCODE_HELLO, on_receive_hello); - msgnetwork_reg_handler(bob.net, MSG_OPCODE_ACK, on_receive_ack); + msgnetwork_reg_handler(alice.net, MSG_OPCODE_HELLO, on_receive_hello, &alice); + msgnetwork_reg_handler(alice.net, MSG_OPCODE_ACK, on_receive_ack, &alice); + msgnetwork_reg_handler(bob.net, MSG_OPCODE_HELLO, on_receive_hello, &bob); + msgnetwork_reg_handler(bob.net, MSG_OPCODE_ACK, on_receive_ack, &bob); - msgnetwork_reg_conn_handler(alice.net, conn_handler); - msgnetwork_reg_conn_handler(bob.net, conn_handler); + msgnetwork_reg_conn_handler(alice.net, conn_handler, &alice); + msgnetwork_reg_conn_handler(bob.net, conn_handler, &bob); /* start all threads */ msgnetwork_start(alice.net); |