aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2019-06-04 23:00:55 -0400
committerDeterminant <[email protected]>2019-06-04 23:00:55 -0400
commitfad6446ce15bcd1303fdd162d6938ab30046467e (patch)
tree524e713d3574042176c1021bbfd610a885e950a2
parentdab48a1825aed9c810ce83ea8054fa435ecb41be (diff)
finish the minimal working example
-rw-r--r--include/salticidae/event.h4
-rw-r--r--include/salticidae/msg.h4
-rw-r--r--include/salticidae/netaddr.h1
-rw-r--r--include/salticidae/network.h22
-rw-r--r--include/salticidae/stream.h15
-rw-r--r--include/salticidae/type.h2
-rw-r--r--src/event.cpp8
-rw-r--r--src/msg.cpp6
-rw-r--r--src/netaddr.cpp3
-rw-r--r--src/network.cpp33
-rw-r--r--src/stream.cpp42
-rw-r--r--src/type.cpp4
-rw-r--r--test/test_msgnet_c.c22
13 files changed, 83 insertions, 83 deletions
diff --git a/include/salticidae/event.h b/include/salticidae/event.h
index 1dcd49d..ae46e78 100644
--- a/include/salticidae/event.h
+++ b/include/salticidae/event.h
@@ -665,14 +665,14 @@ extern "C" {
#endif
eventcontext_t *eventcontext_new();
+void eventcontext_free(eventcontext_t *self);
void eventcontext_dispatch(eventcontext_t *self);
void eventcontext_stop(eventcontext_t *self);
-void eventcontext_free(eventcontext_t *self);
typedef void (*sigev_callback_t)(int events);
sigev_t *sigev_new(const eventcontext_t *self, sigev_callback_t cb);
-void sigev_add(sigev_t *self, int sig);
void sigev_free(sigev_t *self);
+void sigev_add(sigev_t *self, int sig);
#ifdef __cplusplus
}
diff --git a/include/salticidae/msg.h b/include/salticidae/msg.h
index c88d70b..7e8032b 100644
--- a/include/salticidae/msg.h
+++ b/include/salticidae/msg.h
@@ -286,9 +286,9 @@ extern "C" {
#endif
msg_t *msg_new(_opcode_t opcode, bytearray_t *_moved_payload);
-datastream_t *msg_get_payload(const msg_t *msg);
-const _opcode_t msg_get_opcode(const msg_t *msg);
void msg_free(msg_t *msg);
+datastream_t *msg_get_payload(const msg_t *msg);
+_opcode_t msg_get_opcode(const msg_t *msg);
#ifdef __cplusplus
}
diff --git a/include/salticidae/netaddr.h b/include/salticidae/netaddr.h
index d3c34fe..4edf926 100644
--- a/include/salticidae/netaddr.h
+++ b/include/salticidae/netaddr.h
@@ -135,6 +135,7 @@ extern "C" {
#endif
netaddr_t *netaddr_new();
+void netaddr_free(const netaddr_t *self);
netaddr_t *netaddr_new_from_ip_port(uint32_t ip, uint16_t port);
netaddr_t *netaddr_new_from_sip_port(const char *ip, uint16_t port);
netaddr_t *netaddr_new_from_sipport(const char *ip_port_addr);
diff --git a/include/salticidae/network.h b/include/salticidae/network.h
index 6800632..60f08e6 100644
--- a/include/salticidae/network.h
+++ b/include/salticidae/network.h
@@ -144,7 +144,7 @@ class MsgNetwork: public ConnPool {
}
};
- ~MsgNetwork() { stop_workers(); }
+ virtual ~MsgNetwork() { stop_workers(); }
MsgNetwork(const EventContext &ec, const Config &config):
ConnPool(ec, config) {
@@ -811,33 +811,25 @@ extern "C" {
void salticidae_injected_msg_callback(const msg_t *msg, msgnetwork_conn_t *conn);
-msgnetwork_t *msgnetwork_new(const eventcontext_t *ec, const msgnetwork_config_t *config);
+msgnetwork_config_t *msgnetwork_config_new();
+void msgnetwork_config_free(const msgnetwork_config_t *self);
+msgnetwork_t *msgnetwork_new(const eventcontext_t *ec, const msgnetwork_config_t *config);
+void msgnetwork_free(const msgnetwork_t *self);
bool msgnetwork_send_msg(msgnetwork_t *self, const msg_t *msg, const msgnetwork_conn_t *conn);
-
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 *);
-
-#ifdef SALTICIDAE_CBINDINGS_STR_OP
-void msgnetwork_reg_handler(msgnetwork_t *self, const char *opcode, msgnetwork_msg_callback_t cb);
-#else
-void msgnetwork_reg_handler(msgnetwork_t *self, uint8_t opcode, msgnetwork_msg_callback_t cb);
-#endif
+void msgnetwork_reg_handler(msgnetwork_t *self, _opcode_t opcode, msgnetwork_msg_callback_t cb);
typedef void (*msgnetwork_conn_callback_t)(const msgnetwork_conn_t *, bool);
-
void msgnetwork_reg_conn_handler(msgnetwork_t *self, msgnetwork_conn_callback_t cb);
msgnetwork_t *msgnetwork_conn_get_net(const msgnetwork_conn_t *conn);
msgnetwork_conn_mode_t msgnetwork_conn_get_mode(const msgnetwork_conn_t *conn);
netaddr_t *msgnetwork_conn_get_addr(const msgnetwork_conn_t *conn);
-msgnetwork_config_t *msgnetwork_config_new();
-void msgnetwork_config_free(msgnetwork_config_t *self);
-void msgnetwork_listen(msgnetwork_t *self, const netaddr_t *listen_addr);
-void msgnetwork_start(msgnetwork_t *self);
#ifdef __cplusplus
}
diff --git a/include/salticidae/stream.h b/include/salticidae/stream.h
index 3f768b3..95c2ccb 100644
--- a/include/salticidae/stream.h
+++ b/include/salticidae/stream.h
@@ -494,6 +494,8 @@ extern "C" {
uint256_t *uint256_new();
uint256_t *uint256_new_from_bytes(const uint8_t *arr);
+void uint256_free(const uint256_t *self);
+
bool uint256_is_null(const uint256_t *self);
bool uint256_is_eq(const uint256_t *a, const uint256_t *b);
void uint256_serialize(const uint256_t *self, datastream_t *s);
@@ -501,30 +503,39 @@ void uint256_unserialize(uint256_t *self, datastream_t *s);
datastream_t *datastream_new();
datastream_t *datastream_new_from_bytes(const uint8_t *begin, const uint8_t *end);
+void datastream_free(const datastream_t *self);
+
void datastream_assign_by_copy(datastream_t *dst, const datastream_t *src);
void datastream_assign_by_move(datastream_t *dst, datastream_t *src);
uint8_t *datastream_data(datastream_t *self);
void datastream_clear(datastream_t *self);
size_t datastream_size(const datastream_t *self);
+
void datastream_put_u8(datastream_t *self, uint8_t val);
void datastream_put_u16(datastream_t *self, uint16_t val);
void datastream_put_u32(datastream_t *self, uint32_t val);
+void datastream_put_u64(datastream_t *self, uint64_t val);
+
void datastream_put_i8(datastream_t *self, int8_t val);
void datastream_put_i16(datastream_t *self, int16_t val);
void datastream_put_i32(datastream_t *self, int32_t val);
+void datastream_put_i64(datastream_t *self, int64_t val);
void datastream_put_data(datastream_t *self,
const uint8_t *begin, const uint8_t *end);
uint8_t datastream_get_u8(datastream_t *self);
uint16_t datastream_get_u16(datastream_t *self);
uint32_t datastream_get_u32(datastream_t *self);
+uint64_t datastream_get_u64(datastream_t *self);
+
int8_t datastream_get_i8(datastream_t *self);
int16_t datastream_get_i16(datastream_t *self);
int32_t datastream_get_i32(datastream_t *self);
+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);
-void datastream_free(const datastream_t *self);
-bytearray_t *datastream_to_bytearray(datastream_t *self);
+bytearray_t *datastream_to_bytearray(datastream_t *_moved_self);
#ifdef __cplusplus
}
diff --git a/include/salticidae/type.h b/include/salticidae/type.h
index 0bc2ae0..c62272d 100644
--- a/include/salticidae/type.h
+++ b/include/salticidae/type.h
@@ -106,7 +106,7 @@ typedef struct bytearray_t bytearray_t;
#endif
#ifdef SALTICIDAE_CBINDINGS_STR_OP
-typedef char * _opcode_t;
+typedef const char * _opcode_t;
#else
typedef uint8_t _opcode_t;
#endif
diff --git a/src/event.cpp b/src/event.cpp
index 0daa498..6078c37 100644
--- a/src/event.cpp
+++ b/src/event.cpp
@@ -6,19 +6,19 @@ extern "C" {
eventcontext_t *eventcontext_new() { return new eventcontext_t(); }
+void eventcontext_free(eventcontext_t *self) { delete self; }
+
void eventcontext_dispatch(eventcontext_t *self) { return self->dispatch(); }
void eventcontext_stop(eventcontext_t *self) { return self->stop(); }
-void eventcontext_free(eventcontext_t *self) { delete self; }
-
sigev_t *sigev_new(const eventcontext_t *self, sigev_callback_t cb) {
return new sigev_t(*self, cb);
}
-void sigev_add(sigev_t *self, int sig) { self->add(sig); }
+void sigev_free(sigev_t *self) { delete self; }
-void sigev_delete(sigev_t *self) { delete self; }
+void sigev_add(sigev_t *self, int sig) { self->add(sig); }
}
diff --git a/src/msg.cpp b/src/msg.cpp
index caee1c7..33dc31c 100644
--- a/src/msg.cpp
+++ b/src/msg.cpp
@@ -10,16 +10,16 @@ msg_t *msg_new(_opcode_t opcode, bytearray_t *_moved_payload) {
return res;
}
+void msg_free(msg_t *msg) { delete msg; }
+
datastream_t *msg_get_payload(const msg_t *msg) {
return new datastream_t(msg->get_payload());
}
-const _opcode_t msg_get_opcode(const msg_t *msg) {
+_opcode_t msg_get_opcode(const msg_t *msg) {
return msg->get_opcode();
}
-void msg_free(msg_t *msg) { delete msg; }
-
}
#endif
diff --git a/src/netaddr.cpp b/src/netaddr.cpp
index 1803629..3c2618a 100644
--- a/src/netaddr.cpp
+++ b/src/netaddr.cpp
@@ -5,6 +5,9 @@
extern "C" {
netaddr_t *netaddr_new() { return new netaddr_t(); }
+
+void netaddr_free(const netaddr_t *self) { delete self; }
+
netaddr_t *netaddr_new_from_ip_port(uint32_t ip, uint16_t port) {
return new netaddr_t(ip, port);
}
diff --git a/src/network.cpp b/src/network.cpp
index 58b3ce1..9fc07fe 100644
--- a/src/network.cpp
+++ b/src/network.cpp
@@ -6,13 +6,21 @@ using namespace salticidae;
extern "C" {
+msgnetwork_config_t *msgnetwork_config_new() {
+ return new msgnetwork_config_t();
+}
+
+void msgnetwork_config_free(const msgnetwork_config_t *self) { delete self; }
+
msgnetwork_t *msgnetwork_new(const eventcontext_t *ec, const msgnetwork_config_t *config) {
return new msgnetwork_t(*ec, *config);
}
+void msgnetwork_free(const msgnetwork_t *self) { delete self; }
+
bool msgnetwork_send_msg(msgnetwork_t *self,
const msg_t *msg, const msgnetwork_conn_t *conn) {
- return self->send_msg(*msg, *conn);
+ return self->send_msg(*msg, *conn);
}
msgnetwork_conn_t *msgnetwork_connect(msgnetwork_t *self, const netaddr_t *addr) {
@@ -23,25 +31,14 @@ void msgnetwork_listen(msgnetwork_t *self, const netaddr_t *listen_addr) {
self->listen(*listen_addr);
}
-#ifdef SALTICIDAE_CBINDINGS_STR_OP
-void msgnetwork_reg_handler(msgnetwork_t *self,
- const char *opcode,
- msgnetwork_msg_callback_t cb) {
- self->set_handler(std::string(opcode),
- [cb](const msgnetwork_t::Msg &msg, const msgnetwork_t::conn_t &conn) {
- cb(&msg, &conn);
- });
-}
-#else
void msgnetwork_reg_handler(msgnetwork_t *self,
- uint8_t opcode,
+ _opcode_t opcode,
msgnetwork_msg_callback_t cb) {
self->set_handler(opcode,
[cb](const msgnetwork_t::Msg &msg, const msgnetwork_t::conn_t &conn) {
cb(&msg, &conn);
});
}
-#endif
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) {
@@ -50,12 +47,12 @@ void msgnetwork_reg_conn_handler(msgnetwork_t *self, msgnetwork_conn_callback_t
});
}
+void msgnetwork_start(msgnetwork_t *self) { self->start(); }
+
msgnetwork_t *msgnetwork_conn_get_net(const msgnetwork_conn_t *conn) {
return (*conn)->get_net();
}
-void msgnetwork_start(msgnetwork_t *self) { self->start(); }
-
msgnetwork_conn_mode_t msgnetwork_conn_get_mode(const msgnetwork_conn_t *conn) {
return (msgnetwork_conn_mode_t)(*conn)->get_mode();
}
@@ -64,12 +61,6 @@ netaddr_t *msgnetwork_conn_get_addr(const msgnetwork_conn_t *conn) {
return new netaddr_t((*conn)->get_addr());
}
-msgnetwork_config_t *msgnetwork_config_new() {
- return new msgnetwork_config_t();
-}
-
-void msgnetwork_config_free(msgnetwork_config_t *self) { delete self; }
-
}
#endif
diff --git a/src/stream.cpp b/src/stream.cpp
index 2fe1a11..1925419 100644
--- a/src/stream.cpp
+++ b/src/stream.cpp
@@ -8,6 +8,7 @@ uint256_t *uint256_new() { return new uint256_t(); }
uint256_t *uint256_new_from_bytes(const uint8_t *arr) {
return new uint256_t(arr);
}
+void uint256_free(const uint256_t *self) { delete self; }
bool uint256_is_null(const uint256_t *self) { return self->is_null(); }
bool uint256_is_eq(const uint256_t *a, const uint256_t *b) {
@@ -26,6 +27,7 @@ datastream_t *datastream_new() { return new datastream_t(); }
datastream_t *datastream_new_from_bytes(const uint8_t *begin, const uint8_t *end) {
return new datastream_t(begin, end);
}
+void datastream_free(const datastream_t *self) { delete self; }
void datastream_assign_by_copy(datastream_t *dst, const datastream_t *src) {
*dst = *src;
@@ -44,39 +46,27 @@ size_t datastream_size(const datastream_t *self) { return self->size(); }
void datastream_put_u8(datastream_t *self, uint8_t val) { *self << val; }
void datastream_put_u16(datastream_t *self, uint16_t val) { *self << val; }
void datastream_put_u32(datastream_t *self, uint32_t val) { *self << val; }
+void datastream_put_u64(datastream_t *self, uint64_t val) { *self << val; }
void datastream_put_i8(datastream_t *self, int8_t val) { *self << val; }
void datastream_put_i16(datastream_t *self, int16_t val) { *self << val; }
void datastream_put_i32(datastream_t *self, int32_t val) { *self << val; }
+void datastream_put_i64(datastream_t *self, int64_t val) { *self << val; }
void datastream_put_data(datastream_t *self,
const uint8_t *begin, const uint8_t *end) {
self->put_data(begin, end);
}
-uint8_t datastream_get_u8(datastream_t *self) {
- uint8_t val; *self >> val; return val;
-}
-
-uint16_t datastream_get_u16(datastream_t *self) {
- uint16_t val; *self >> val; return val;
-}
+uint8_t datastream_get_u8(datastream_t *self) { uint8_t val; *self >> val; return val; }
+uint16_t datastream_get_u16(datastream_t *self) { uint16_t val; *self >> val; return val; }
+uint32_t datastream_get_u32(datastream_t *self) { uint32_t val; *self >> val; return val; }
+uint64_t datastream_get_u64(datastream_t *self) { uint64_t val; *self >> val; return val; }
-uint32_t datastream_get_u32(datastream_t *self) {
- uint32_t val; *self >> val; return val;
-}
-
-int8_t datastream_get_i8(datastream_t *self) {
- int8_t val; *self >> val; return val;
-}
-
-int16_t datastream_get_i16(datastream_t *self) {
- int16_t val; *self >> val; return val;
-}
-
-int32_t datastream_get_i32(datastream_t *self) {
- int32_t val; *self >> val; return val;
-}
+int8_t datastream_get_i8(datastream_t *self) { int8_t val; *self >> val; return val; }
+int16_t datastream_get_i16(datastream_t *self) { int16_t val; *self >> val; return val; }
+int32_t datastream_get_i32(datastream_t *self) { int32_t val; *self >> val; return val; }
+int64_t datastream_get_i64(datastream_t *self) { int64_t val; *self >> val; return val; }
const uint8_t *datastream_get_data_inplace(datastream_t *self, size_t len) {
return self->get_data_inplace(len);
@@ -86,11 +76,9 @@ uint256_t *datastream_get_hash(const datastream_t *self) {
return new uint256_t(self->get_hash());
}
-void datastream_free(const datastream_t *self) { delete self; }
-
-bytearray_t *datastream_to_bytearray(datastream_t *self) {
- auto res = new bytearray_t(std::move(*self));
- delete self;
+bytearray_t *datastream_to_bytearray(datastream_t *_moved_self) {
+ auto res = new bytearray_t(std::move(*_moved_self));
+ delete _moved_self;
return res;
}
diff --git a/src/type.cpp b/src/type.cpp
index 90d5055..397daac 100644
--- a/src/type.cpp
+++ b/src/type.cpp
@@ -6,12 +6,12 @@ extern "C" {
bytearray_t *bytearray_new() { return new bytearray_t(); }
+void bytearray_free(bytearray_t *arr) { delete arr; }
+
uint8_t *bytearray_data(bytearray_t *arr) { return &(*arr)[0]; }
size_t bytearray_size(bytearray_t *arr) { return arr->size(); }
-void bytearray_free(bytearray_t *arr) { delete arr; }
-
}
#endif
diff --git a/test/test_msgnet_c.c b/test/test_msgnet_c.c
index 04a2281..b717c3a 100644
--- a/test/test_msgnet_c.c
+++ b/test/test_msgnet_c.c
@@ -72,7 +72,10 @@ MsgHello msg_hello_unserialize(const msg_t *msg) {
return res;
}
-bytearray_t *msg_ack_serialize() { return bytearray_new(); }
+msg_t *msg_ack_serialize() {
+ msg_t *msg = msg_new(MSG_OPCODE_ACK, bytearray_new());
+ return msg;
+}
typedef struct MyNet {
msgnetwork_t *net;
@@ -85,7 +88,7 @@ void on_receive_hello(const msg_t *_msg, const msgnetwork_conn_t *conn) {
const char *name = net == alice.net ? alice.name : bob.name;
MsgHello msg = msg_hello_unserialize(_msg);
printf("[%s] %s says %s\n", name, msg.name, msg.text);
- msg_t *ack = msg_new(MSG_OPCODE_ACK, msg_ack_serialize());
+ msg_t *ack = msg_ack_serialize();
/* send acknowledgement */
msgnetwork_send_msg(net, ack, conn);
msg_free(ack);
@@ -107,8 +110,9 @@ void conn_handler(const msgnetwork_conn_t *conn, bool connected) {
{
printf("[%s] Connected, sending hello.", name);
/* send the first message through this connection */
- msgnetwork_send_msg(n->net,
- msg_hello_serialize(name, "Hello there!"), conn);
+ msg_t *hello = msg_hello_serialize(name, "Hello there!");
+ msgnetwork_send_msg(n->net, hello, conn);
+ msg_free(hello);
}
else
printf("[%s] Accepted, waiting for greetings.\n", name);
@@ -126,6 +130,7 @@ MyNet gen_mynet(const eventcontext_t *ec,
MyNet res;
const msgnetwork_config_t *netconfig = msgnetwork_config_new();
res.net = msgnetwork_new(ec, netconfig);
+ msgnetwork_config_free(netconfig);
res.name = name;
};
@@ -161,6 +166,9 @@ int main() {
msgnetwork_connect(alice.net, bob_addr);
msgnetwork_connect(bob.net, alice_addr);
+ netaddr_free(alice_addr);
+ netaddr_free(bob_addr);
+
/* the main loop can be shutdown by ctrl-c or kill */
sigev_t *ev_sigint = sigev_new(ec, on_term_signal);
sigev_t *ev_sigterm = sigev_new(ec, on_term_signal);
@@ -169,5 +177,11 @@ int main() {
/* enter the main loop */
eventcontext_dispatch(ec);
+
+ sigev_free(ev_sigint);
+ sigev_free(ev_sigterm);
+ msgnetwork_free(alice.net);
+ msgnetwork_free(bob.net);
+ eventcontext_free(ec);
return 0;
}