From cb133a379998112600ea1504b556484a417cf48a Mon Sep 17 00:00:00 2001 From: Determinant Date: Fri, 14 Jun 2019 17:49:28 -0400 Subject: update the library; fix bugs --- network.go | 10 +++++----- salticidae | 2 +- salticidae.go | 12 ++++++++++++ stream.go | 38 +++++++++++++++++++------------------- test_msgnet/main.go | 31 ++++++++++++++++++++++--------- test_p2p_stress/main.go | 17 ++++++++++++----- 6 files changed, 71 insertions(+), 39 deletions(-) diff --git a/network.go b/network.go index b15c3ea..5f42fb0 100644 --- a/network.go +++ b/network.go @@ -63,11 +63,11 @@ func NewMsgNetwork(ec EventContext, config MsgNetworkConfig) MsgNetwork { } func (self MsgNetwork) Free() { C.msgnetwork_free(self) } -func (self MsgNetwork) Listen(addr NetAddr) { C.msgnetwork_listen(self, addr) } +func (self MsgNetwork) Listen(addr NetAddr, err *Error) { C.msgnetwork_listen(self, addr, err) } 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) MsgNetworkConn { return C.msgnetwork_connect(self, addr) } +func (self MsgNetwork) SendMsgByMove(msg Msg, conn MsgNetworkConn) { C.msgnetwork_send_msg_by_move(self, msg, conn) } +func (self MsgNetwork) Connect(addr NetAddr, err *Error) MsgNetworkConn { return C.msgnetwork_connect(self, addr, err) } func (self MsgNetwork) Terminate(conn MsgNetworkConn) { C.msgnetwork_terminate(self, conn) } func (self MsgNetworkConn) Copy() MsgNetworkConn { return C.msgnetwork_conn_copy(self) } @@ -131,7 +131,7 @@ func (self PeerNetwork) AddPeer(paddr NetAddr) { C.peernetwork_add_peer(self, pa func (self PeerNetwork) HasPeer(paddr NetAddr) bool { return bool(C.peernetwork_has_peer(self, paddr)) } -func (self PeerNetwork) GetPeerConn(paddr NetAddr) PeerNetworkConn { return C.peernetwork_get_peer_conn(self, paddr) } +func (self PeerNetwork) GetPeerConn(paddr NetAddr, err *Error) PeerNetworkConn { return C.peernetwork_get_peer_conn(self, paddr, err) } func (self PeerNetwork) AsMsgNetwork() MsgNetwork { return C.peernetwork_as_msgnetwork(self) } @@ -148,4 +148,4 @@ func (self PeerNetwork) MulticastMsgByMove(_moved_msg Msg, paddrs []NetAddr) { C.peernetwork_multicast_msg_by_move(self, _moved_msg, na) } -func (self PeerNetwork) Listen(listenAddr NetAddr) { C.peernetwork_listen(self, listenAddr) } +func (self PeerNetwork) Listen(listenAddr NetAddr, err *Error) { C.peernetwork_listen(self, listenAddr, err) } diff --git a/salticidae b/salticidae index 9f6460c..deb4788 160000 --- a/salticidae +++ b/salticidae @@ -1 +1 @@ -Subproject commit 9f6460c7ab774d900f391345bbf3fac8617a3aa3 +Subproject commit deb478812b259cdf586ab5105284ae227fe42680 diff --git a/salticidae.go b/salticidae.go index dba50c0..0698f39 100644 --- a/salticidae.go +++ b/salticidae.go @@ -3,8 +3,20 @@ package salticidae // #cgo CFLAGS: -I${SRCDIR}/salticidae/include/ // #cgo LDFLAGS: ${SRCDIR}/salticidae/libsalticidae.so -Wl,-rpath=${SRCDIR}/salticidae/ +// #include "salticidae/util.h" import "C" import "unsafe" type rawptr_t = unsafe.Pointer type Opcode = uint8 +type Error = C.struct_SalticidaeCError + +func (self *Error) GetCode() int { return int((*C.struct_SalticidaeCError)(self).code) } + +func NewError() Error { + return C.struct_SalticidaeCError {} +} + +func StrError(code int) string { + return C.GoString(C.salticidae_strerror(C.int(code))) +} diff --git a/stream.go b/stream.go index 94b9281..d66d085 100644 --- a/stream.go +++ b/stream.go @@ -42,33 +42,33 @@ func (self DataStream) Clear() { C.datastream_clear(self) } func (self DataStream) Size() int { return int(C.datastream_size(self)) } -func (self DataStream) PutU8(v uint8) { C.datastream_put_u8(self, C.uint8_t(v)) } -func (self DataStream) PutU16(v uint16) { C.datastream_put_u16(self, C.uint16_t(v)) } -func (self DataStream) PutU32(v uint32) { C.datastream_put_u32(self, C.uint32_t(v)) } -func (self DataStream) PutU64(v uint32) { C.datastream_put_u64(self, C.uint64_t(v)) } +func (self DataStream) PutU8(v uint8) bool { return bool(C.datastream_put_u8(self, C.uint8_t(v))) } +func (self DataStream) PutU16(v uint16) bool { return bool(C.datastream_put_u16(self, C.uint16_t(v))) } +func (self DataStream) PutU32(v uint32) bool { return bool(C.datastream_put_u32(self, C.uint32_t(v))) } +func (self DataStream) PutU64(v uint32) bool { return bool(C.datastream_put_u64(self, C.uint64_t(v))) } -func (self DataStream) PutI8(v int8) { C.datastream_put_i8(self, C.int8_t(v)) } -func (self DataStream) PutI16(v int16) { C.datastream_put_i16(self, C.int16_t(v)) } -func (self DataStream) PutI32(v int32) { C.datastream_put_i32(self, C.int32_t(v)) } -func (self DataStream) PutI64(v int32) { C.datastream_put_i64(self, C.int64_t(v)) } +func (self DataStream) PutI8(v int8) bool { return bool(C.datastream_put_i8(self, C.int8_t(v))) } +func (self DataStream) PutI16(v int16) bool { return bool(C.datastream_put_i16(self, C.int16_t(v))) } +func (self DataStream) PutI32(v int32) bool { return bool(C.datastream_put_i32(self, C.int32_t(v))) } +func (self DataStream) PutI64(v int32) bool { return bool(C.datastream_put_i64(self, C.int64_t(v))) } -func (self DataStream) PutData(bytes []byte) { +func (self DataStream) PutData(bytes []byte) bool { size := len(bytes) if size > 0 { base := (*C.uint8_t)(&bytes[0]) - C.datastream_put_data(self, base, C.size_t(size)) - } + return bool(C.datastream_put_data(self, base, C.size_t(size))) + } else { return true } } -func (self DataStream) GetU8() uint8 { return uint8(C.datastream_get_u8(self)) } -func (self DataStream) GetU16() uint16 { return uint16(C.datastream_get_u16(self)) } -func (self DataStream) GetU32() uint32 { return uint32(C.datastream_get_u32(self)) } -func (self DataStream) GetU64() uint64 { return uint64(C.datastream_get_u64(self)) } +func (self DataStream) GetU8(succ *bool) uint8 { return uint8(C.datastream_get_u8(self, (*C.bool)(succ))) } +func (self DataStream) GetU16(succ *bool) uint16 { return uint16(C.datastream_get_u16(self, (*C.bool)(succ))) } +func (self DataStream) GetU32(succ *bool) uint32 { return uint32(C.datastream_get_u32(self, (*C.bool)(succ))) } +func (self DataStream) GetU64(succ *bool) uint64 { return uint64(C.datastream_get_u64(self, (*C.bool)(succ))) } -func (self DataStream) GetI8() int8 { return int8(C.datastream_get_i8(self)) } -func (self DataStream) GetI16() int16 { return int16(C.datastream_get_i16(self)) } -func (self DataStream) GetI32() int32 { return int32(C.datastream_get_i32(self)) } -func (self DataStream) GetI64() int64 { return int64(C.datastream_get_i64(self)) } +func (self DataStream) GetI8(succ *bool) int8 { return int8(C.datastream_get_i8(self, (*C.bool)(succ))) } +func (self DataStream) GetI16(succ *bool) int16 { return int16(C.datastream_get_i16(self, (*C.bool)(succ))) } +func (self DataStream) GetI32(succ *bool) int32 { return int32(C.datastream_get_i32(self, (*C.bool)(succ))) } +func (self DataStream) GetI64(succ *bool) int64 { return int64(C.datastream_get_i64(self, (*C.bool)(succ))) } func (self DataStream) GetDataInPlace(length int) []byte { diff --git a/test_msgnet/main.go b/test_msgnet/main.go index 6ed649b..ee262b7 100644 --- a/test_msgnet/main.go +++ b/test_msgnet/main.go @@ -10,6 +10,7 @@ import "C" import ( "encoding/binary" + "os" "fmt" "unsafe" "github.com/Determinant/salticidae-go" @@ -45,6 +46,13 @@ func msgAckSerialize() salticidae.Msg { return salticidae.NewMsgMovedFromByteArray(MSG_OPCODE_ACK, salticidae.NewByteArray()) } +func checkError(err *salticidae.Error) { + if err.GetCode() != 0 { + fmt.Printf("error during a sync call: %s\n", salticidae.StrError(err.GetCode())) + os.Exit(1) + } +} + type MyNet struct { net salticidae.MsgNetwork name string @@ -72,8 +80,7 @@ func onReceiveHello(_msg *C.struct_msg_t, _conn *C.struct_msgnetwork_conn_t, _ u name, text := msgHelloUnserialize(msg) fmt.Printf("[%s] %s says %s\n", myName, name, text) ack := msgAckSerialize() - net.SendMsg(ack, conn) - ack.Free() + net.SendMsgByMove(ack, conn) } //export onReceiveAck @@ -100,15 +107,15 @@ func connHandler(_conn *C.struct_msgnetwork_conn_t, connected C.bool, _ unsafe.P if conn.GetMode() == salticidae.CONN_MODE_ACTIVE { fmt.Printf("[%s] Connected, sending hello.", name) hello := msgHelloSerialize(name, "Hello there!") - n.net.SendMsg(hello, conn) - hello.Free() + n.net.SendMsgByMove(hello, conn) } else { fmt.Printf("[%s] Accepted, waiting for greetings.\n", name) } } else { fmt.Printf("[%s] Disconnected, retrying.\n", name) addr := conn.GetAddr() - net.Connect(addr).Free() + err := salticidae.NewError() + net.Connect(addr, &err).Free() addr.Free() } } @@ -128,6 +135,8 @@ func genMyNet(ec salticidae.EventContext, name string) MyNet { func main() { ec = salticidae.NewEventContext() + err := salticidae.NewError() + alice_addr := salticidae.NewAddrFromIPPortString("127.0.0.1:12345") bob_addr := salticidae.NewAddrFromIPPortString("127.0.0.1:12346") @@ -137,11 +146,15 @@ func main() { alice.net.Start() bob.net.Start() - alice.net.Listen(alice_addr) - bob.net.Listen(bob_addr) + alice.net.Listen(alice_addr, &err) + checkError(&err) + bob.net.Listen(bob_addr, &err) + checkError(&err) - alice.net.Connect(bob_addr).Free() - bob.net.Connect(alice_addr).Free() + alice.net.Connect(bob_addr, &err).Free() + checkError(&err) + bob.net.Connect(alice_addr, &err).Free() + checkError(&err) alice_addr.Free() bob_addr.Free() diff --git a/test_p2p_stress/main.go b/test_p2p_stress/main.go index 1051440..6670f03 100644 --- a/test_p2p_stress/main.go +++ b/test_p2p_stress/main.go @@ -26,6 +26,7 @@ import "C" import ( "github.com/Determinant/salticidae-go" "math/rand" + "os" "fmt" "sync" "unsafe" @@ -70,6 +71,13 @@ func msgAckUnserialize(msg salticidae.Msg) salticidae.UInt256 { return hash } +func checkError(err *salticidae.Error) { + if err.GetCode() != 0 { + fmt.Printf("error during a sync call: %s\n", salticidae.StrError(err.GetCode())) + os.Exit(1) + } +} + type TestContext struct { timer salticidae.TimerEvent timer_ctx *C.struct_timeout_callback_context_t @@ -133,8 +141,7 @@ func sendRand(size int, app *AppContext, conn salticidae.MsgNetworkConn) { salticidae.UInt256(tc.hash).Free() } tc.hash = hash - app.net.AsMsgNetwork().SendMsg(msg, conn) - msg.Free() + app.net.AsMsgNetwork().SendMsgByMove(msg, conn) } var apps []AppContext @@ -165,9 +172,8 @@ func onReceiveRand(_msg *C.struct_msg_t, _conn *C.struct_msgnetwork_conn_t, user conn := salticidae.MsgNetworkConn(_conn) net := conn.GetNet() ack := msgAckSerialize(hash) - net.SendMsg(ack, conn) hash.Free() - ack.Free() + net.SendMsgByMove(ack, conn) } //export onReceiveAck @@ -287,8 +293,9 @@ func main() { for i, _ := range apps { app_id := i go func() { + err := salticidae.NewError() a := &apps[app_id] - a.net.Listen(a.addr) + a.net.Listen(a.addr, &err) for _, addr := range addrs { if !addr.IsEq(a.addr) { a.net.AddPeer(addr) -- cgit v1.2.3-70-g09d2