aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/salticidae/event.h2
-rw-r--r--include/salticidae/network.h13
-rw-r--r--include/salticidae/type.h2
-rw-r--r--src/event.cpp7
-rw-r--r--src/msg.cpp6
-rw-r--r--src/netaddr.cpp14
-rw-r--r--src/network.cpp13
-rw-r--r--src/stream.cpp7
-rw-r--r--src/type.cpp17
-rw-r--r--test/test_msgnet_c.c35
10 files changed, 62 insertions, 54 deletions
diff --git a/include/salticidae/event.h b/include/salticidae/event.h
index cc739f2..1dcd49d 100644
--- a/include/salticidae/event.h
+++ b/include/salticidae/event.h
@@ -669,7 +669,7 @@ void eventcontext_dispatch(eventcontext_t *self);
void eventcontext_stop(eventcontext_t *self);
void eventcontext_free(eventcontext_t *self);
-typedef void (*sigev_callback_t)(int fd, int events);
+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);
diff --git a/include/salticidae/network.h b/include/salticidae/network.h
index f6bd6fe..6800632 100644
--- a/include/salticidae/network.h
+++ b/include/salticidae/network.h
@@ -794,16 +794,17 @@ using msgnetwork_conn_t = msgnetwork_t::conn_t;
typedef struct msgnetwork_t msgnetwork_t;
typedef struct msgnetwork_config_t msgnetwork_config_t;
typedef struct msgnetwork_conn_t msgnetwork_conn_t;
+#endif
+
+#endif
+
+#ifdef SALTICIDAE_CBINDINGS
typedef enum msgnetwork_conn_mode_t {
CONN_MODE_ACTIVE,
CONN_MODE_PASSIVE,
CONN_MODE_DEAD
} msgnetwork_conn_mode_t;
-#endif
-
-#endif
-#ifdef SALTICIDAE_CBINDINGS
#ifdef __cplusplus
extern "C" {
#endif
@@ -833,6 +834,10 @@ void msgnetwork_reg_conn_handler(msgnetwork_t *self, msgnetwork_conn_callback_t
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/type.h b/include/salticidae/type.h
index b3da4c2..0bc2ae0 100644
--- a/include/salticidae/type.h
+++ b/include/salticidae/type.h
@@ -119,7 +119,7 @@ extern "C" {
bytearray_t *bytearray_new();
uint8_t *bytearray_data(bytearray_t *arr);
size_t bytearray_size(bytearray_t *arr);
-void bytearray_free();
+void bytearray_free(bytearray_t *arr);
#ifdef __cplusplus
}
diff --git a/src/event.cpp b/src/event.cpp
index 050914c..0daa498 100644
--- a/src/event.cpp
+++ b/src/event.cpp
@@ -1,3 +1,4 @@
+#include "salticidae/config.h"
#ifdef SALTICIDAE_CBINDINGS
#include "salticidae/event.h"
@@ -11,13 +12,13 @@ void eventcontext_stop(eventcontext_t *self) { return self->stop(); }
void eventcontext_free(eventcontext_t *self) { delete self; }
-void sigev_new(const eventcontext_t *self, sigev_callback_t cb) {
- return new SigEvent(*self, cb);
+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_delete() { delete self; }
+void sigev_delete(sigev_t *self) { delete self; }
}
diff --git a/src/msg.cpp b/src/msg.cpp
index d395d2b..caee1c7 100644
--- a/src/msg.cpp
+++ b/src/msg.cpp
@@ -1,11 +1,13 @@
+#include "salticidae/config.h"
#ifdef SALTICIDAE_CBINDINGS
#include "salticidae/msg.h"
extern "C" {
msg_t *msg_new(_opcode_t opcode, bytearray_t *_moved_payload) {
- auto res = new msg_t(opcode, *payload);
- bytearray_free(payload);
+ auto res = new msg_t(opcode, std::move(*_moved_payload));
+ bytearray_free(_moved_payload);
+ return res;
}
datastream_t *msg_get_payload(const msg_t *msg) {
diff --git a/src/netaddr.cpp b/src/netaddr.cpp
index a3a54bf..1803629 100644
--- a/src/netaddr.cpp
+++ b/src/netaddr.cpp
@@ -1,22 +1,20 @@
+#include "salticidae/config.h"
+#ifdef SALTICIDAE_CBINDINGS
#include "salticidae/netaddr.h"
-using namespace salticidae;
-
-#ifdef __cplusplus
-
extern "C" {
-netaddr_t *netaddr_new() { return new NetAddr(); }
+netaddr_t *netaddr_new() { return new netaddr_t(); }
netaddr_t *netaddr_new_from_ip_port(uint32_t ip, uint16_t port) {
- return new NetAddr(ip, port);
+ return new netaddr_t(ip, port);
}
netaddr_t *netaddr_new_from_sip_port(const char *ip, uint16_t port) {
- return new NetAddr(ip, port);
+ return new netaddr_t(ip, port);
}
netaddr_t *netaddr_new_from_sipport(const char *ip_port_addr) {
- return new NetAddr(ip_port_addr);
+ return new netaddr_t(ip_port_addr);
}
bool netaddr_is_eq(const netaddr_t *a, const netaddr_t *b) {
diff --git a/src/network.cpp b/src/network.cpp
index d2d9615..58b3ce1 100644
--- a/src/network.cpp
+++ b/src/network.cpp
@@ -1,3 +1,4 @@
+#include "salticidae/config.h"
#ifdef SALTICIDAE_CBINDINGS
#include "salticidae/network.h"
@@ -56,13 +57,19 @@ msgnetwork_t *msgnetwork_conn_get_net(const msgnetwork_conn_t *conn) {
void msgnetwork_start(msgnetwork_t *self) { self->start(); }
msgnetwork_conn_mode_t msgnetwork_conn_get_mode(const msgnetwork_conn_t *conn) {
- return conn->get_mode();
+ return (msgnetwork_conn_mode_t)(*conn)->get_mode();
}
-netaddr_t msgnetwork_conn_get_addr(const msgnetwork_conn_t *conn) {
- return new netaddr_t(conn->get_addr());
+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 ec1d5da..2fe1a11 100644
--- a/src/stream.cpp
+++ b/src/stream.cpp
@@ -1,8 +1,7 @@
+#include "salticidae/config.h"
#ifdef SALTICIDAE_CBINDINGS
#include "salticidae/stream.h"
-using namespace salticidae;
-
extern "C" {
uint256_t *uint256_new() { return new uint256_t(); }
@@ -23,9 +22,9 @@ void uint256_unserialize(uint256_t *self, datastream_t *s) {
self->unserialize(*s);
}
-datastream_t *datastream_new() { return new DataStream(); }
+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(begin, end);
+ return new datastream_t(begin, end);
}
void datastream_assign_by_copy(datastream_t *dst, const datastream_t *src) {
diff --git a/src/type.cpp b/src/type.cpp
index 1b02933..90d5055 100644
--- a/src/type.cpp
+++ b/src/type.cpp
@@ -1,23 +1,16 @@
+#include "salticidae/config.h"
#ifdef SALTICIDAE_CBINDINGS
#include "salticidae/type.h"
extern "C" {
-bytearray_t *bytearray_new() {
- return new bytearray_t();
-}
+bytearray_t *bytearray_new() { return new bytearray_t(); }
-uint8_t *bytearray_data(bytearray_t *arr) {
- return &(*arr)[0];
-}
+uint8_t *bytearray_data(bytearray_t *arr) { return &(*arr)[0]; }
-size_t bytearray_size(bytearray_t *arr) {
- return arr->size();
-}
+size_t bytearray_size(bytearray_t *arr) { return arr->size(); }
-void bytearray_free(bytearray_t *arr) {
- delete arr;
-}
+void bytearray_free(bytearray_t *arr) { delete arr; }
}
diff --git a/test/test_msgnet_c.c b/test/test_msgnet_c.c
index 8d0c575..04a2281 100644
--- a/test/test_msgnet_c.c
+++ b/test/test_msgnet_c.c
@@ -25,13 +25,15 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#include <signal.h>
#include "salticidae/event.h"
#include "salticidae/network.h"
#include "salticidae/stream.h"
/** Hello Message. */
-const uint8_t msg_hello_opcode = 0x0;
+const uint8_t MSG_OPCODE_HELLO = 0x0;
+const uint8_t MSG_OPCODE_ACK = 0x1;
typedef struct MsgHello {
const char *name;
const char *text;
@@ -43,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_hello_opcode, datastream_to_bytearray(serialized));
+ msg_t *msg = msg_new(MSG_OPCODE_HELLO, datastream_to_bytearray(serialized));
return msg;
}
@@ -83,9 +85,9 @@ 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(0x1, msg_ack_serialize());
+ msg_t *ack = msg_new(MSG_OPCODE_ACK, msg_ack_serialize());
/* send acknowledgement */
- send_msg(ack, conn);
+ msgnetwork_send_msg(net, ack, conn);
msg_free(ack);
}
@@ -115,7 +117,7 @@ void conn_handler(const msgnetwork_conn_t *conn, bool connected) {
{
printf("[%s] Disconnected, retrying.\n", name);
/* try to reconnect to the same address */
- connect(msgnetwork_conn_get_addr(conn));
+ msgnetwork_connect(net, msgnetwork_conn_get_addr(conn));
}
}
@@ -127,36 +129,37 @@ MyNet gen_mynet(const eventcontext_t *ec,
res.name = name;
};
+static eventcontext_t *ec;
-void on_term_signal(int) {
- ec.stop();
+void on_term_signal(int sig) {
+ eventcontext_stop(ec);
}
int main() {
- eventcontext_t *ec = eventcontext_new();
+ ec = eventcontext_new();
netaddr_t *alice_addr = netaddr_new_from_sipport("127.0.0.1:12345");
netaddr_t *bob_addr = netaddr_new_from_sipport("127.0.0.1:12346");
/* test two nodes in the same main loop */
- alice = gen_mynet(ec, "Alice", bob_addr);
- bob = gen_mynet(ec, "Bob", alice_addr);
+ 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_HELLO, on_receive_hello);
- msgnetwork_reg_handler(bob.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);
/* start all threads */
msgnetwork_start(alice.net);
msgnetwork_start(bob.net);
/* accept incoming connections */
- msgnetwork_listen(alice_addr);
- msgnetwork_listen(bob_addr);
+ msgnetwork_listen(alice.net, alice_addr);
+ msgnetwork_listen(bob.net, bob_addr);
/* try to connect once */
- msgnetwork_connect(bob_addr);
- msgnetwork_connect(alice_addr);
+ msgnetwork_connect(alice.net, bob_addr);
+ msgnetwork_connect(bob.net, alice_addr);
/* the main loop can be shutdown by ctrl-c or kill */
sigev_t *ev_sigint = sigev_new(ec, on_term_signal);