From 1b2215ae2c792f96765a15c7ffcc6f20e3046cc7 Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 11 Jun 2019 01:59:04 -0400 Subject: fix slice bugs --- network.go | 8 ++++++-- stream.go | 30 +++++++++++++++--------------- test_msgnet/main.go | 6 +++--- test_p2p_stress/main.go | 10 ++++++++-- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/network.go b/network.go index a4cac02..78d798a 100644 --- a/network.go +++ b/network.go @@ -84,8 +84,12 @@ func NewMsgNetworkConnFromPeerNetWorkConn(conn PeerNetworkConn) MsgNetworkConn { func (self PeerNetwork) SendMsg(_moved_msg Msg, paddr NetAddr) { C.peernetwork_send_msg(self, _moved_msg, paddr) } func (self PeerNetwork) MulticastMsg(_moved_msg Msg, paddrs []NetAddr) { - base := uintptr(rawptr_t(&paddrs[0])) - C.peernetwork_multicast_msg(self, _moved_msg, (*C.struct_netaddr_t)(rawptr_t(base)), C.size_t(len(paddrs))) + size := len(paddrs) + if size > 0 { + base := (C.netaddr_t *)(&paddrs[0]) + C.peernetwork_multicast_msg( + self, _moved_msg, base, C.size_t(size)) + } } func (self PeerNetwork) Listen(listenAddr NetAddr) { C.peernetwork_listen(self, listenAddr) } diff --git a/stream.go b/stream.go index c4019b8..ebfaaf4 100644 --- a/stream.go +++ b/stream.go @@ -17,10 +17,13 @@ type DataStream = *C.struct_datastream_t func NewDataStream() DataStream { return C.datastream_new() } func NewDataStreamFromBytes(bytes []byte) DataStream { - base := uintptr(rawptr_t(&bytes[0])) - return C.datastream_new_from_bytes( - (*C.uint8_t)(rawptr_t(base)), - (*C.uint8_t)(rawptr_t(base + uintptr(len(bytes))))) + size := len(bytes) + if size > 0 { + base := (*C.uint8_t)(&bytes[0]) + return C.datastream_new_from_bytes(base, base + uintptr(size)) + } else { + return C.datastream_new() + } } func (self DataStream) Free() { C.datastream_free(self) } @@ -50,11 +53,11 @@ 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) PutData(bytes []byte) { - base := uintptr(rawptr_t(&bytes[0])) - C.datastream_put_data(self, - (*C.uint8_t)(rawptr_t(base)), - (*C.uint8_t)(rawptr_t(base + uintptr(len(bytes))))) - + size := len(bytes) + if size > 0 { + base := (*C.uint8_t)(&bytes[0]) + C.datastream_put_data(self, base, base + uintptr(size)) + } } func (self DataStream) GetU8() uint8 { return uint8(C.datastream_get_u8(self)) } @@ -68,12 +71,9 @@ 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) GetDataInPlace(length int) *C.uint8_t { - return C.datastream_get_data_inplace(self, C.size_t(length)) -} - -func (self DataStream) GetData(length int) []byte { - return C.GoBytes(rawptr_t(self.GetDataInPlace(length)), C.int(length)) +func (self DataStream) GetDataInPlace(length int) []byte { + base := C.datastream_get_data_inplace(self, C.size_t(length)) + return C.GoBytes(base, C.int(length)) } type UInt256 = *C.uint256_t diff --git a/test_msgnet/main.go b/test_msgnet/main.go index 0da5af7..230bd83 100644 --- a/test_msgnet/main.go +++ b/test_msgnet/main.go @@ -45,9 +45,9 @@ func msgHelloSerialize(name string, text string) salticidae.Msg { func msgHelloUnserialize(msg salticidae.Msg) MsgHello { p := msg.GetPayload() - length := binary.LittleEndian.Uint32(p.GetData(4)) - name := string(p.GetData(int(length))) - text := string(p.GetData(p.Size())) + length := binary.LittleEndian.Uint32(p.GetDataInPlace(4)) + name := string(p.GetDataInPlace(int(length))) + text := string(p.GetDataInPlace(p.Size())) p.Free() return MsgHello { name: name, text: text } } diff --git a/test_p2p_stress/main.go b/test_p2p_stress/main.go index 79b28b0..4534c4f 100644 --- a/test_p2p_stress/main.go +++ b/test_p2p_stress/main.go @@ -14,7 +14,13 @@ package main // int app_id; // uint64_t addr_id; // msgnetwork_conn_t *conn; -// } timerout_callback_context_t; +// } timeout_callback_context_t; +// timeout_callback_context_t *timeout_callback_context_new() { +// timeout_callback_context_t *ctx = malloc(sizeof(timeout_callback_context_t); +// ctx->conn = NULL; +// return ctx; +// } +// import "C" import ( @@ -151,7 +157,7 @@ func onReceiveAck(_msg *C.struct_msg_t, _conn *C.struct_msgnetwork_conn_t, userd if tc.state == seg_buff_size * 2 { sendRand(tc.state, app, conn) tc.state = -1 - ctx := (*C.struct_timeout_callback_context_t)(C.malloc(C.sizeof_struct_timeout_callback_context_t)) + ctx := C.timeout_callback_context_new() ctx.app_id = C.int(id) ctx.addr_id = C.uint64_t(addr) ctx.conn = (*C.struct_msgnetwork_conn_t)(conn) -- cgit v1.2.3