aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDeterminant <tederminant@gmail.com>2019-06-04 18:59:00 -0400
committerDeterminant <tederminant@gmail.com>2019-06-04 18:59:00 -0400
commitda42ddf96c78a73b4cf5f8854e98e009f4f174a4 (patch)
tree2435817585507cbc2a158fd84996884263dcc8ec
parentebada91a1327da99f1827c7e4c15fc3bee527d58 (diff)
...
-rw-r--r--CMakeLists.txt1
-rw-r--r--include/salticidae/conn.h3
-rw-r--r--include/salticidae/event.h12
-rw-r--r--include/salticidae/msg.h19
-rw-r--r--include/salticidae/netaddr.h14
-rw-r--r--include/salticidae/network.h18
-rw-r--r--include/salticidae/stream.h20
-rw-r--r--include/salticidae/type.h16
-rw-r--r--include/salticidae/util.h4
-rw-r--r--src/msg.cpp2
-rw-r--r--src/network.cpp8
-rw-r--r--src/stream.cpp10
-rw-r--r--src/type.cpp4
-rw-r--r--test/CMakeLists.txt3
-rw-r--r--test/test_msgnet_c.c63
15 files changed, 139 insertions, 58 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e0bcae2..4154d66 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -23,6 +23,7 @@
cmake_minimum_required(VERSION 3.9)
project(Salticidae)
set(CMAKE_CXX_STANDARD 14)
+set(CMAKE_C_STANDARD 11)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
find_package(Libuv REQUIRED)
diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h
index 201e574..92fc5a6 100644
--- a/include/salticidae/conn.h
+++ b/include/salticidae/conn.h
@@ -25,6 +25,7 @@
#ifndef _SALTICIDAE_CONN_H
#define _SALTICIDAE_CONN_H
+#ifdef __cplusplus
#include <cassert>
#include <cstdint>
#include <arpa/inet.h>
@@ -426,3 +427,5 @@ class ConnPool {
}
#endif
+
+#endif
diff --git a/include/salticidae/event.h b/include/salticidae/event.h
index 27d081a..cc739f2 100644
--- a/include/salticidae/event.h
+++ b/include/salticidae/event.h
@@ -25,6 +25,7 @@
#ifndef _SALTICIDAE_EVENT_H
#define _SALTICIDAE_EVENT_H
+#ifdef __cplusplus
#include <condition_variable>
#include <unistd.h>
#include <uv.h>
@@ -35,7 +36,6 @@
#include "salticidae/util.h"
#include "salticidae/ref.h"
-#ifdef __cplusplus
namespace salticidae {
struct _event_context_deleter {
@@ -651,16 +651,18 @@ using sigev_t = salticidae::SigEvent;
#endif
#else
-
+#include "config.h"
#ifdef SALTICIDAE_CBINDINGS
-typedef struct eventcontext_t;
-typedef struct sigevent_t;
+typedef struct eventcontext_t eventcontext_t;
+typedef struct sigev_t sigev_t;
#endif
#endif
#ifdef SALTICIDAE_CBINDINGS
+#ifdef __cplusplus
extern "C" {
+#endif
eventcontext_t *eventcontext_new();
void eventcontext_dispatch(eventcontext_t *self);
@@ -672,7 +674,9 @@ 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);
+#ifdef __cplusplus
}
#endif
+#endif
#endif
diff --git a/include/salticidae/msg.h b/include/salticidae/msg.h
index 6964ab2..c88d70b 100644
--- a/include/salticidae/msg.h
+++ b/include/salticidae/msg.h
@@ -25,16 +25,17 @@
#ifndef _SALTICIDAE_MSG_H
#define _SALTICIDAE_MSG_H
-#include <cstdint>
-#include <cstring>
-#include <string>
-#include <vector>
-
#include "salticidae/type.h"
#include "salticidae/stream.h"
#include "salticidae/netaddr.h"
#ifdef __cplusplus
+
+#include <cstdint>
+#include <cstring>
+#include <string>
+#include <vector>
+
namespace salticidae {
template<typename OpcodeType>
@@ -280,14 +281,18 @@ typedef struct msg_t msg_t;
#endif
#ifdef SALTICIDAE_CBINDINGS
+#ifdef __cplusplus
extern "C" {
+#endif
msg_t *msg_new(_opcode_t opcode, bytearray_t *_moved_payload);
-datastream_t *msg_get_payload(msg_t *msg);
-const _opcode_t &msg_get_opcode(const msg_t *msg);
+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);
+#ifdef __cplusplus
}
#endif
+#endif
#endif
diff --git a/include/salticidae/netaddr.h b/include/salticidae/netaddr.h
index 917c498..d3c34fe 100644
--- a/include/salticidae/netaddr.h
+++ b/include/salticidae/netaddr.h
@@ -25,16 +25,16 @@
#ifndef _SALTICIDAE_NETADDR_H
#define _SALTICIDAE_NETADDR_H
+#include "salticidae/util.h"
+#include "salticidae/stream.h"
+
+#ifdef __cplusplus
#include <string>
#include <cstring>
#include <cstdint>
#include <arpa/inet.h>
#include <netdb.h>
-#include "salticidae/util.h"
-#include "salticidae/stream.h"
-
-#ifdef __cplusplus
namespace salticidae {
/* TODO: IPv6 support */
@@ -129,7 +129,10 @@ using netaddr_t = salticidae::NetAddr;
typedef struct netaddr_t netaddr_t;
#endif
+#ifdef SALTICIDAE_CBINDINGS
+#ifdef __cplusplus
extern "C" {
+#endif
netaddr_t *netaddr_new();
netaddr_t *netaddr_new_from_ip_port(uint32_t ip, uint16_t port);
@@ -138,6 +141,9 @@ netaddr_t *netaddr_new_from_sipport(const char *ip_port_addr);
bool netaddr_is_eq(const netaddr_t *a, const netaddr_t *b);
bool netaddr_is_null(const netaddr_t *self);
+#ifdef __cplusplus
}
+#endif
+#endif
#endif
diff --git a/include/salticidae/network.h b/include/salticidae/network.h
index b176b65..f6bd6fe 100644
--- a/include/salticidae/network.h
+++ b/include/salticidae/network.h
@@ -791,16 +791,22 @@ using msgnetwork_conn_t = msgnetwork_t::conn_t;
#else
#ifdef SALTICIDAE_CBINDINGS
-typedef struct msg_t;
-typedef struct msgnetwork_t;
-typedef struct msgnetwork_config_t;
-typedef struct msgnetwork_conn_t;
+typedef struct msgnetwork_t msgnetwork_t;
+typedef struct msgnetwork_config_t msgnetwork_config_t;
+typedef struct msgnetwork_conn_t msgnetwork_conn_t;
+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
void salticidae_injected_msg_callback(const msg_t *msg, msgnetwork_conn_t *conn);
@@ -825,8 +831,12 @@ 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);
+#ifdef __cplusplus
}
#endif
+#endif
#endif
diff --git a/include/salticidae/stream.h b/include/salticidae/stream.h
index e146273..3f768b3 100644
--- a/include/salticidae/stream.h
+++ b/include/salticidae/stream.h
@@ -25,11 +25,11 @@
#ifndef _SALTICIDAE_STREAM_H
#define _SALTICIDAE_STREAM_H
+#ifdef __cplusplus
#include "salticidae/type.h"
#include "salticidae/ref.h"
#include "salticidae/crypto.h"
-#ifdef __cplusplus
namespace salticidae {
template<size_t N, typename T> class Blob;
@@ -474,15 +474,23 @@ using datastream_t = salticidae::DataStream;
#else
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "config.h"
+#include "type.h"
+
#ifdef SALTICIDAE_CBINDINGS
-typedef struct datastream_t;
-typedef struct uint256_t;
+typedef struct datastream_t datastream_t;
+typedef struct uint256_t uint256_t;
#endif
#endif
#ifdef SALTICIDAE_CBINDINGS
+#ifdef __cplusplus
extern "C" {
+#endif
uint256_t *uint256_new();
uint256_t *uint256_new_from_bytes(const uint8_t *arr);
@@ -505,7 +513,7 @@ 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_data(datastream_t *self,
- uint8_t *begin, uint8_t *end);
+ const uint8_t *begin, const uint8_t *end);
uint8_t datastream_get_u8(datastream_t *self);
uint16_t datastream_get_u16(datastream_t *self);
@@ -515,8 +523,12 @@ int16_t datastream_get_i16(datastream_t *self);
int32_t datastream_get_i32(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);
+#ifdef __cplusplus
}
#endif
+#endif
#endif
diff --git a/include/salticidae/type.h b/include/salticidae/type.h
index 0ebcce7..b3da4c2 100644
--- a/include/salticidae/type.h
+++ b/include/salticidae/type.h
@@ -25,6 +25,9 @@
#ifndef _SALTICIDAE_TYPE_H
#define _SALTICIDAE_TYPE_H
+#include "config.h"
+
+#ifdef __cplusplus
#include <vector>
#include <string>
#include <cstring>
@@ -35,9 +38,6 @@
#include <functional>
#include <mutex>
-#include "config.h"
-
-#ifdef __cplusplus
namespace salticidae {
const auto _1 = std::placeholders::_1;
@@ -98,6 +98,7 @@ using bytearray_t = salticidae::bytearray_t;
#else
+#include <stdint.h>
#ifdef SALTICIDAE_CBINDINGS
typedef struct bytearray_t bytearray_t;
#endif
@@ -105,19 +106,24 @@ typedef struct bytearray_t bytearray_t;
#endif
#ifdef SALTICIDAE_CBINDINGS_STR_OP
-using _opcode_t = char *;
+typedef char * _opcode_t;
#else
-using _opcode_t = uint8_t;
+typedef uint8_t _opcode_t;
#endif
#ifdef SALTICIDAE_CBINDINGS
+#ifdef __cplusplus
extern "C" {
+#endif
+bytearray_t *bytearray_new();
uint8_t *bytearray_data(bytearray_t *arr);
size_t bytearray_size(bytearray_t *arr);
void bytearray_free();
+#ifdef __cplusplus
}
#endif
+#endif
#endif
diff --git a/include/salticidae/util.h b/include/salticidae/util.h
index a843fbd..19779b0 100644
--- a/include/salticidae/util.h
+++ b/include/salticidae/util.h
@@ -25,6 +25,8 @@
#ifndef _SALTICIDAE_COMMON_H
#define _SALTICIDAE_COMMON_H
+#ifdef __cplusplus
+
#include <string>
#include <exception>
#include <cstdarg>
@@ -326,3 +328,5 @@ class Config {
}
#endif
+
+#endif
diff --git a/src/msg.cpp b/src/msg.cpp
index e75d7d4..d395d2b 100644
--- a/src/msg.cpp
+++ b/src/msg.cpp
@@ -8,7 +8,7 @@ msg_t *msg_new(_opcode_t opcode, bytearray_t *_moved_payload) {
bytearray_free(payload);
}
-datastream_t *msg_get_payload(msg_t *msg) {
+datastream_t *msg_get_payload(const msg_t *msg) {
return new datastream_t(msg->get_payload());
}
diff --git a/src/network.cpp b/src/network.cpp
index 0b12131..d2d9615 100644
--- a/src/network.cpp
+++ b/src/network.cpp
@@ -55,6 +55,14 @@ 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();
+}
+
+netaddr_t msgnetwork_conn_get_addr(const msgnetwork_conn_t *conn) {
+ return new netaddr_t(conn->get_addr());
+}
+
}
#endif
diff --git a/src/stream.cpp b/src/stream.cpp
index f3dd322..ec1d5da 100644
--- a/src/stream.cpp
+++ b/src/stream.cpp
@@ -51,7 +51,7 @@ 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_data(datastream_t *self,
- uint8_t *begin, uint8_t *end) {
+ const uint8_t *begin, const uint8_t *end) {
self->put_data(begin, end);
}
@@ -87,6 +87,14 @@ 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;
+ return res;
+}
+
}
#endif
diff --git a/src/type.cpp b/src/type.cpp
index 8290a07..1b02933 100644
--- a/src/type.cpp
+++ b/src/type.cpp
@@ -3,6 +3,10 @@
extern "C" {
+bytearray_t *bytearray_new() {
+ return new bytearray_t();
+}
+
uint8_t *bytearray_data(bytearray_t *arr) {
return &(*arr)[0];
}
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 14201e9..2a1a8f0 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -40,3 +40,6 @@ target_link_libraries(test_queue salticidae_static pthread)
add_executable(bench_network bench_network.cpp)
target_link_libraries(bench_network salticidae_static pthread)
+
+add_executable(test_msgnet_c test_msgnet_c.c)
+target_link_libraries(test_msgnet_c salticidae_static pthread)
diff --git a/test/test_msgnet_c.c b/test/test_msgnet_c.c
index b6ba5a0..8d0c575 100644
--- a/test/test_msgnet_c.c
+++ b/test/test_msgnet_c.c
@@ -24,6 +24,7 @@
#include <stdio.h>
#include <string.h>
+#include <stdlib.h>
#include "salticidae/event.h"
#include "salticidae/network.h"
@@ -36,68 +37,76 @@ typedef struct MsgHello {
const char *text;
} MsgHello;
/** Defines how to serialize the msg. */
-datastream_t *msg_hello_serialize(const char *name, const char *text) {
+msg_t *msg_hello_serialize(const char *name, const char *text) {
datastream_t *serialized = datastream_new();
- serialized << htole((uint32_t)name.length());
- serialized << name << text;
- return serialized;
+ size_t name_len = strlen(name);
+ 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));
+ return msg;
}
/** Defines how to parse the msg. */
-MsgHello msg_hello_unserialize(datastream_t *s) {
+MsgHello msg_hello_unserialize(const msg_t *msg) {
+ datastream_t *s = msg_get_payload(msg);
MsgHello res;
uint32_t len;
len = datastream_get_u32(s);
- len = letoh(len);
+ len = le32toh(len);
- const char *name = (const char *)malloc(len + 1);
+ char *name = (char *)malloc(len + 1);
memmove(name, datastream_get_data_inplace(s, len), len);
name[len] = 0;
len = datastream_size(s);
- const char *text = (const char *)malloc(len + 1);
+ char *text = (char *)malloc(len + 1);
memmove(text, datastream_get_data_inplace(s, len), len);
text[len] = 0;
res.name = name;
res.text = text;
+ datastream_free(s);
return res;
}
-datastream_t *msg_ack_serialize() { return datastream_new(); }
+bytearray_t *msg_ack_serialize() { return bytearray_new(); }
-struct MyNet {
+typedef struct MyNet {
msgnetwork_t *net;
const char *name;
- const NetAddr peer;
-} alice, bob;
+} 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) {
msgnetwork_t *net = msgnetwork_conn_get_net(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 *msg = msg_new(0x1, msg_ack_serialize());
+ msg_t *ack = msg_new(0x1, msg_ack_serialize());
/* send acknowledgement */
- send_msg(msg, conn);
- msg_free(msg);
+ send_msg(ack, conn);
+ msg_free(ack);
}
void on_receive_ack(const msg_t *msg, const msgnetwork_conn_t *conn) {
- auto net = static_cast<MyNet *>(conn->get_net());
- printf("[%s] the peer knows\n", net->name.c_str());
+ msgnetwork_t *net = msgnetwork_conn_get_net(conn);
+ const char *name = net == alice.net ? alice.name : bob.name;
+ printf("[%s] the peer knows\n", name);
}
void conn_handler(const msgnetwork_conn_t *conn, bool connected) {
msgnetwork_t *net = msgnetwork_conn_get_net(conn);
- const char *name = net == alice.net ? alice.name : bob.name;
+ MyNet *n = net == alice.net ? &alice: &bob;
+ const char *name = n->name;
if (connected)
{
- if (conn->get_mode() == ConnPool::Conn::ACTIVE)
+ if (msgnetwork_conn_get_mode(conn) == CONN_MODE_ACTIVE)
{
- puts("[%s] Connected, sending hello.", name);
+ printf("[%s] Connected, sending hello.", name);
/* send the first message through this connection */
- msgnetwork_send_msg(alice,
- msg_hello_serialize("alice", "Hello there!"), conn);
+ msgnetwork_send_msg(n->net,
+ msg_hello_serialize(name, "Hello there!"), conn);
}
else
printf("[%s] Accepted, waiting for greetings.\n", name);
@@ -106,18 +115,16 @@ void conn_handler(const msgnetwork_conn_t *conn, bool connected) {
{
printf("[%s] Disconnected, retrying.\n", name);
/* try to reconnect to the same address */
- connect(conn->get_addr());
+ connect(msgnetwork_conn_get_addr(conn));
}
}
MyNet gen_mynet(const eventcontext_t *ec,
- const char *name,
- const netaddr_t *peer) {
+ const char *name) {
MyNet res;
const msgnetwork_config_t *netconfig = msgnetwork_config_new();
res.net = msgnetwork_new(ec, netconfig);
res.name = name;
- res.peer = peer;
};