From 92ad2074c4406d25ce52957890d9e48f6d769efe Mon Sep 17 00:00:00 2001 From: Determinant Date: Fri, 7 Jun 2019 22:57:05 -0400 Subject: add makefile --- .gitignore | 1 + Makefile | 14 ++++++ event.go | 24 ++++++++++ msg.go | 22 +++++++++ netaddr.go | 16 +++++++ network.go | 57 ++++++++++++++++++++++++ salticidae.go | 126 ---------------------------------------------------- test_msgnet/main.go | 6 +-- 8 files changed, 137 insertions(+), 129 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 event.go create mode 100644 msg.go create mode 100644 netaddr.go create mode 100644 network.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1ee5226 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +.PHONY: all clean + +all: build/test_msgnet + +salticidae/libsalticidae.so: + cd salticidae/ + cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED=ON -DSALTICIDAE_DEBUG_LOG=OFF -DSALTICIDAE_CBINDINGS=ON -DBUILD_TEST=OFF salticidae/ --build salticidae/ + make -C salticidae/ + +build: + mkdir -p build + +build/test_msgnet: salticidae/libsalticidae.so + go build -o $@ salticidae-go/test_msgnet diff --git a/event.go b/event.go new file mode 100644 index 0000000..93b9255 --- /dev/null +++ b/event.go @@ -0,0 +1,24 @@ +package salticidae + +// #include "salticidae/event.h" +// #include +import "C" + +type EventContext = *C.struct_eventcontext_t + +func NewEventContext() EventContext { return C.eventcontext_new() } +func (self EventContext) Free() { C.eventcontext_free(self) } +func (self EventContext) Dispatch() { C.eventcontext_dispatch(self) } +func (self EventContext) Stop() { C.eventcontext_stop(self) } + +type SigEvent = *C.sigev_t +type SigEventCallback = C.sigev_callback_t +var SIGTERM = C.SIGTERM +var SIGINT = C.SIGINT + +func NewSigEvent(ec EventContext, cb SigEventCallback) SigEvent { + return C.sigev_new(ec, cb) +} + +func (self SigEvent) Add(sig int) { C.sigev_add(self, C.int(sig)) } +func (self SigEvent) Free() { C.sigev_free(self) } diff --git a/msg.go b/msg.go new file mode 100644 index 0000000..d846399 --- /dev/null +++ b/msg.go @@ -0,0 +1,22 @@ +package salticidae + +// #include +// #include "salticidae/msg.h" +import "C" + +type Msg = *C.struct_msg_t + +func NewMsg(opcode Opcode, _moved_payload ByteArray) Msg { + return C.msg_new(C._opcode_t(opcode), _moved_payload) +} + +func (self Msg) Free() { C.msg_free(self) } + +func (self Msg) GetPayload() DataStream { + return C.msg_get_payload(self) +} + +func (self Msg) GetOpcode() Opcode { + return Opcode(C.msg_get_opcode(self)) +} + diff --git a/netaddr.go b/netaddr.go new file mode 100644 index 0000000..37ead1e --- /dev/null +++ b/netaddr.go @@ -0,0 +1,16 @@ +package salticidae + +// #include +// #include "salticidae/netaddr.h" +import "C" + +type NetAddr = *C.struct_netaddr_t + +func NewAddrFromIPPortString(addr string) (res NetAddr) { + c_str := C.CString(addr) + res = C.netaddr_new_from_sipport(c_str) + C.free(rawptr_t(c_str)) + return +} + +func (self NetAddr) Free() { C.netaddr_free(self) } diff --git a/network.go b/network.go new file mode 100644 index 0000000..572c1cc --- /dev/null +++ b/network.go @@ -0,0 +1,57 @@ +package salticidae + +// #include "salticidae/network.h" +import "C" + +type MsgNetwork = *C.struct_msgnetwork_t + +type MsgNetworkConn = *C.struct_msgnetwork_conn_t + +type MsgNetworkConnMode = C.msgnetwork_conn_mode_t + + +func (self MsgNetworkConn) GetNet() MsgNetwork { + return C.msgnetwork_conn_get_net(self) +} + +var ( + CONN_MODE_ACTIVE = MsgNetworkConnMode(C.CONN_MODE_ACTIVE) + CONN_MODE_PASSIVE = MsgNetworkConnMode(C.CONN_MODE_PASSIVE) + CONN_MODE_DEAD = MsgNetworkConnMode(C.CONN_MODE_DEAD) +) + +func (self MsgNetworkConn) GetMode() MsgNetworkConnMode { + return C.msgnetwork_conn_get_mode(self) +} + +func (self MsgNetworkConn) GetAddr() NetAddr { + return C.msgnetwork_conn_get_addr(self) +} + +type MsgNetworkConfig = *C.struct_msgnetwork_config_t + +func NewMsgNetworkConfig() MsgNetworkConfig { return C.msgnetwork_config_new() } + +func (self MsgNetworkConfig) Free() { C.msgnetwork_config_free(self) } + +func NewMsgNetwork(ec EventContext, config MsgNetworkConfig) MsgNetwork { + return C.msgnetwork_new(ec, config) +} + +func (self MsgNetwork) Free() { C.msgnetwork_free(self) } +func (self MsgNetwork) Listen(addr NetAddr) { C.msgnetwork_listen(self, addr) } +func (self MsgNetwork) Start() { C.msgnetwork_start(self) } + +func (self MsgNetwork) SendMsg(msg Msg, conn MsgNetworkConn) { C.msgnetwork_send_msg(self, msg, conn) } +func (self MsgNetwork) Connect(addr NetAddr) { C.msgnetwork_connect(self, addr) } + +type MsgNetworkMsgCallback = C.msgnetwork_msg_callback_t +type MsgNetworkConnCallback = C.msgnetwork_conn_callback_t + +func (self MsgNetwork) RegHandler(opcode Opcode, callback MsgNetworkMsgCallback) { + C.msgnetwork_reg_handler(self, C._opcode_t(opcode), callback) +} + +func (self MsgNetwork) RegConnHandler(callback MsgNetworkConnCallback) { + C.msgnetwork_reg_conn_handler(self, callback) +} diff --git a/salticidae.go b/salticidae.go index 01056a1..dba50c0 100644 --- a/salticidae.go +++ b/salticidae.go @@ -3,134 +3,8 @@ package salticidae // #cgo CFLAGS: -I${SRCDIR}/salticidae/include/ // #cgo LDFLAGS: ${SRCDIR}/salticidae/libsalticidae.so -Wl,-rpath=${SRCDIR}/salticidae/ -// #include -// #include -// #include "salticidae/netaddr.h" -// #include "salticidae/network.h" -// #include "salticidae/event.h" -// -// void salticidae_injected_msg_callback(const msg_t *msg, msgnetwork_conn_t *conn) { -// -// } import "C" import "unsafe" type rawptr_t = unsafe.Pointer - - -type NetAddr = *C.struct_netaddr_t - -func NewAddrFromIPPortString(addr string) (res NetAddr) { - c_str := C.CString(addr) - res = C.netaddr_new_from_sipport(c_str) - C.free(rawptr_t(c_str)) - return -} - -func (self NetAddr) Free() { C.netaddr_free(self) } - -type EventContext = *C.struct_eventcontext_t - -func NewEventContext() EventContext { return C.eventcontext_new() } -func (self EventContext) Free() { C.eventcontext_free(self) } -func (self EventContext) Dispatch() { C.eventcontext_dispatch(self) } -func (self EventContext) Stop() { C.eventcontext_stop(self) } - type Opcode = uint8 - -type Msg = *C.struct_msg_t - -func NewMsg(opcode Opcode, _moved_payload ByteArray) Msg { - return C.msg_new(C._opcode_t(opcode), _moved_payload) -} - -func (self Msg) Free() { C.msg_free(self) } - -func (self Msg) GetPayload() DataStream { - return C.msg_get_payload(self) -} - -func (self Msg) GetOpcode() Opcode { - return Opcode(C.msg_get_opcode(self)) -} - -type MsgNetworkInner = *C.struct_msgnetwork_t - -type MsgNetwork struct { - inner MsgNetworkInner - msgHandlers map[Opcode] func(Msg, MsgNetworkConn) -} - -type MsgNetworkConn = *C.struct_msgnetwork_conn_t - -type MsgNetworkConnMode = C.msgnetwork_conn_mode_t - - -func (self MsgNetworkConn) GetNet() MsgNetworkInner { - return C.msgnetwork_conn_get_net(self) -} - -var ( - CONN_MODE_ACTIVE = MsgNetworkConnMode(C.CONN_MODE_ACTIVE) - CONN_MODE_PASSIVE = MsgNetworkConnMode(C.CONN_MODE_PASSIVE) - CONN_MODE_DEAD = MsgNetworkConnMode(C.CONN_MODE_DEAD) -) - -func (self MsgNetworkConn) GetMode() MsgNetworkConnMode { - return C.msgnetwork_conn_get_mode(self) -} - -func (self MsgNetworkConn) GetAddr() NetAddr { - return C.msgnetwork_conn_get_addr(self) -} - -type MsgNetworkConfig = *C.struct_msgnetwork_config_t - -func NewMsgNetworkConfig() MsgNetworkConfig { return C.msgnetwork_config_new() } - -func (self MsgNetworkConfig) Free() { C.msgnetwork_config_free(self) } - -func NewMsgNetwork(ec EventContext, config MsgNetworkConfig) MsgNetwork { - return MsgNetwork { - inner: C.msgnetwork_new(ec, config), - } -} - -func (self MsgNetwork) Free() { C.msgnetwork_free(self.inner) } -func (self MsgNetwork) SendMsg(msg Msg, conn MsgNetworkConn) { self.inner.SendMsg(msg, conn) } -func (self MsgNetwork) Connect(addr NetAddr) { self.inner.Connect(addr) } -func (self MsgNetwork) Listen(addr NetAddr) { C.msgnetwork_listen(self.inner, addr) } -func (self MsgNetwork) Start() { C.msgnetwork_start(self.inner) } -func (self MsgNetwork) GetInner() MsgNetworkInner { return self.inner } - -func (self MsgNetworkInner) SendMsg(msg Msg, conn MsgNetworkConn) { - C.msgnetwork_send_msg(self, msg, conn) -} - -func (self MsgNetworkInner) Connect(addr NetAddr) { - C.msgnetwork_connect(self, addr) -} - -type MsgNetworkMsgCallback = C.msgnetwork_msg_callback_t -type MsgNetworkConnCallback = C.msgnetwork_conn_callback_t - -func (self MsgNetwork) RegHandler(opcode Opcode, callback MsgNetworkMsgCallback) { - C.msgnetwork_reg_handler(self.inner, C._opcode_t(opcode), callback) -} - -func (self MsgNetwork) RegConnHandler(callback MsgNetworkConnCallback) { - C.msgnetwork_reg_conn_handler(self.inner, callback) -} - -type SigEvent = *C.sigev_t -type SigEventCallback = C.sigev_callback_t -var SIGTERM = C.SIGTERM -var SIGINT = C.SIGINT - -func NewSigEvent(ec EventContext, cb SigEventCallback) SigEvent { - return C.sigev_new(ec, cb) -} - -func (self SigEvent) Add(sig int) { C.sigev_add(self, C.int(sig)) } -func (self SigEvent) Free() { C.sigev_free(self) } - diff --git a/test_msgnet/main.go b/test_msgnet/main.go index b768adf..f1d82dc 100644 --- a/test_msgnet/main.go +++ b/test_msgnet/main.go @@ -66,7 +66,7 @@ func onReceiveHello(__msg *C.struct_msg_t, _conn *C.struct_msgnetwork_conn_t) { conn := salticidae.MsgNetworkConn(_conn) net := conn.GetNet() name := bob.name - if net == alice.net.GetInner() { + if net == alice.net { name = alice.name } msg := msgHelloUnserialize(_msg) @@ -81,7 +81,7 @@ func onReceiveAck(_ *C.struct_msg_t, _conn *C.struct_msgnetwork_conn_t) { conn := salticidae.MsgNetworkConn(_conn) net := conn.GetNet() name := bob.name - if net == alice.net.GetInner() { + if net == alice.net { name = alice.name } fmt.Printf("[%s] the peer knows\n", name) @@ -92,7 +92,7 @@ func connHandler(_conn *C.struct_msgnetwork_conn_t, connected C.bool) { conn := salticidae.MsgNetworkConn(_conn) net := conn.GetNet() n := &bob - if net == alice.net.GetInner() { + if net == alice.net { n = &alice } name := n.name -- cgit v1.2.3-70-g09d2