From 316b61a15ff334a27462715ed4c99cf244a25701 Mon Sep 17 00:00:00 2001 From: Determinant Date: Fri, 7 Jun 2019 16:00:27 -0400 Subject: use SigEvent callback in the example --- demo/main.go | 31 ++++++++++++++++++++++++++----- demo/main_.go | 7 +++++++ salticidae.go | 16 ++++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 demo/main_.go diff --git a/demo/main.go b/demo/main.go index e156f45..8dcbe98 100644 --- a/demo/main.go +++ b/demo/main.go @@ -1,10 +1,20 @@ package main +// void onTerm_cgo(int sig); +import "C" + import "salticidae-go" +import "unsafe" + +var ec salticidae.EventContext + +//export onTerm +func onTerm(_ int) { + ec.Stop() +} -func run(my_addr string, other_addr string) { +func run(ec salticidae.EventContext, my_addr string, other_addr string) salticidae.MsgNetwork { netconfig := salticidae.NewMsgNetworkConfig() - ec := salticidae.NewEventContext() net := salticidae.NewMsgNetwork(ec, netconfig) listen_addr := salticidae.NewAddrFromIPPortString(my_addr) connect_addr := salticidae.NewAddrFromIPPortString(other_addr) @@ -12,12 +22,23 @@ func run(my_addr string, other_addr string) { net.Start() net.Listen(listen_addr) net.Connect(connect_addr) - ec.Dispatch() + return net } func main() { + ec = salticidae.NewEventContext() + ev_int := salticidae.NewSigEvent(ec, salticidae.SigEventCallback(unsafe.Pointer(C.onTerm_cgo))) + ev_int.Add(salticidae.SIGINT) + ev_term := salticidae.NewSigEvent(ec, salticidae.SigEventCallback(unsafe.Pointer(C.onTerm_cgo))) + ev_term.Add(salticidae.SIGTERM) + alice := "127.0.0.1:10000" bob := "127.0.0.1:10001" - go run(alice, bob) - run(bob, alice) + alice_net := run(ec, alice, bob) + bob_net := run(ec, bob, alice) + ec.Dispatch() + ev_int.Free() + ev_term.Free() + alice_net.Free() + bob_net.Free() } diff --git a/demo/main_.go b/demo/main_.go new file mode 100644 index 0000000..2f53d93 --- /dev/null +++ b/demo/main_.go @@ -0,0 +1,7 @@ +package main + +// void onTerm_cgo(int sig) { +// void onTerm(int); +// onTerm(sig); +// } +import "C" diff --git a/salticidae.go b/salticidae.go index 4a8712a..1dce21e 100644 --- a/salticidae.go +++ b/salticidae.go @@ -4,8 +4,10 @@ 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" import "C" import "unsafe" @@ -26,6 +28,7 @@ type EventContext = *C.struct_eventcontext_t func NewEventContext() EventContext { return C.eventcontext_new() } func (self EventContext) Dispatch() { C.eventcontext_dispatch(self) } +func (self EventContext) Stop() { C.eventcontext_stop(self) } type MsgNetworkConfig = *C.struct_msgnetwork_config_t @@ -38,3 +41,16 @@ func NewMsgNetwork(ec EventContext, config MsgNetworkConfig) MsgNetwork { func (self MsgNetwork) Start() { C.msgnetwork_start(self) } func (self MsgNetwork) Listen(addr NetAddr) { C.msgnetwork_listen(self, addr) } func (self MsgNetwork) Connect(addr NetAddr) { C.msgnetwork_connect(self, addr) } +func (self MsgNetwork) Free() { C.msgnetwork_free(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) } -- cgit v1.2.3