diff options
-rw-r--r-- | netaddr.go | 16 | ||||
-rw-r--r-- | network.go | 35 | ||||
m--------- | salticidae | 0 | ||||
-rw-r--r-- | test_msgnet/main.go | 5 | ||||
-rw-r--r-- | test_p2p_stress/main.go | 10 |
5 files changed, 39 insertions, 27 deletions
@@ -5,12 +5,14 @@ package salticidae import "C" import "runtime" -type netAddr struct { - inner *C.netaddr_t -} - +type CNetAddr = *C.netaddr_t +type netAddr struct { inner CNetAddr } type NetAddr = *netAddr +func NetAddrFromC(ptr CNetAddr) NetAddr { + return &netAddr{ inner: ptr } +} + type netAddrArray struct { inner *C.netaddr_array_t } @@ -49,4 +51,10 @@ func (self NetAddr) GetIP() uint32 { return uint32(C.netaddr_get_ip(self.inner)) func (self NetAddr) GetPort() uint16 { return uint16(C.netaddr_get_port(self.inner)) } +func (self NetAddr) Copy() NetAddr { + res := &netAddr{ inner: C.netaddr_copy(self.inner) } + runtime.SetFinalizer(res, func(self NetAddr) { self.free() }) + return res +} + func (self NetAddrArray) free() { C.netaddr_array_free(self.inner) } @@ -25,7 +25,7 @@ type MsgNetworkConnMode = C.msgnetwork_conn_mode_t func (self MsgNetworkConn) free() { C.msgnetwork_conn_free(self.inner) } func (self MsgNetworkConn) GetNet() MsgNetwork { - return &msgNetwork{ inner: C.msgnetwork_conn_get_net(self.inner) } + return MsgNetworkFromC(C.msgnetwork_conn_get_net(self.inner)) } var ( @@ -39,9 +39,7 @@ func (self MsgNetworkConn) GetMode() MsgNetworkConnMode { } func (self MsgNetworkConn) GetAddr() NetAddr { - res := &netAddr{ inner: C.msgnetwork_conn_get_addr(self.inner) } - runtime.SetFinalizer(res, func(self NetAddr) { self.free() }) - return res + return NetAddrFromC(C.msgnetwork_conn_get_addr(self.inner)) } type CMsgNetworkConfig = *C.msgnetwork_config_t @@ -53,7 +51,7 @@ func MsgNetworkConfigFromC(ptr CMsgNetworkConfig) MsgNetworkConfig { } func NewMsgNetworkConfig() MsgNetworkConfig { - res := &msgNetworkConfig{ inner: C.msgnetwork_config_new() } + res := MsgNetworkConfigFromC(C.msgnetwork_config_new()) runtime.SetFinalizer(res, func(self MsgNetworkConfig) { self.free() }) return res } @@ -85,7 +83,7 @@ func (self MsgNetworkConfig) QueueCapacity(capacity int) { } func NewMsgNetwork(ec EventContext, config MsgNetworkConfig) MsgNetwork { - res := &msgNetwork { inner: C.msgnetwork_new(ec.inner, config.inner) } + res := MsgNetworkFromC(C.msgnetwork_new(ec.inner, config.inner)) ec.attach(rawptr_t(res.inner), res) runtime.SetFinalizer(res, func(self MsgNetwork) { self.free() }) return res @@ -94,10 +92,11 @@ func NewMsgNetwork(ec EventContext, config MsgNetworkConfig) MsgNetwork { func (self MsgNetwork) free() { C.msgnetwork_free(self.inner) } func (self MsgNetwork) Listen(addr NetAddr, err *Error) { C.msgnetwork_listen(self.inner, addr.inner, err) } func (self MsgNetwork) Start() { C.msgnetwork_start(self.inner) } +func (self MsgNetwork) Stop() { C.msgnetwork_stop(self.inner) } func (self MsgNetwork) SendMsgByMove(msg Msg, conn MsgNetworkConn) { C.msgnetwork_send_msg_by_move(self.inner, msg.inner, conn.inner) } func (self MsgNetwork) Connect(addr NetAddr, err *Error) MsgNetworkConn { - res := &msgNetworkConn { inner: C.msgnetwork_connect(self.inner, addr.inner, err) } + res := MsgNetworkConnFromC(C.msgnetwork_connect(self.inner, addr.inner, err)) runtime.SetFinalizer(res, func(self MsgNetworkConn) { self.free() }) return res } @@ -118,7 +117,7 @@ func (self MsgNetwork) RegErrorHandler(callback MsgNetworkErrorCallback, userdat func (self MsgNetworkConn) Copy() MsgNetworkConn { - res := &msgNetworkConn { inner: C.msgnetwork_conn_copy(self.inner) } + res := MsgNetworkConnFromC(C.msgnetwork_conn_copy(self.inner)) runtime.SetFinalizer(res, func(self MsgNetworkConn) { self.free() }) return res } @@ -161,7 +160,7 @@ func PeerNetworkConfigFromC(ptr CPeerNetworkConfig) PeerNetworkConfig { } func NewPeerNetworkConfig() PeerNetworkConfig { - res := &peerNetworkConfig { inner: C.peernetwork_config_new() } + res := PeerNetworkConfigFromC(C.peernetwork_config_new()) runtime.SetFinalizer(res, func(self PeerNetworkConfig) { self.free() }) return res } @@ -185,11 +184,11 @@ func (self PeerNetworkConfig) IdMode(mode PeerNetworkIdMode) { } func (self PeerNetworkConfig) AsMsgNetworkConfig() MsgNetworkConfig { - return &msgNetworkConfig { inner: C.peernetwork_config_as_msgnetwork_config(self.inner) } + return MsgNetworkConfigFromC(C.peernetwork_config_as_msgnetwork_config(self.inner)) } func NewPeerNetwork(ec EventContext, config PeerNetworkConfig) PeerNetwork { - res := &peerNetwork { inner: C.peernetwork_new(ec.inner, config.inner) } + res := PeerNetworkFromC(C.peernetwork_new(ec.inner, config.inner)) ec.attach(rawptr_t(res.inner), res) runtime.SetFinalizer(res, func(self PeerNetwork) { self.free() }) return res @@ -202,21 +201,27 @@ func (self PeerNetwork) AddPeer(paddr NetAddr) { C.peernetwork_add_peer(self.inn func (self PeerNetwork) HasPeer(paddr NetAddr) bool { return bool(C.peernetwork_has_peer(self.inner, paddr.inner)) } func (self PeerNetwork) GetPeerConn(paddr NetAddr, err *Error) PeerNetworkConn { - res := &peerNetworkConn{ inner: C.peernetwork_get_peer_conn(self.inner, paddr.inner, err) } + res := PeerNetworkConnFromC(C.peernetwork_get_peer_conn(self.inner, paddr.inner, err)) runtime.SetFinalizer(res, func(self PeerNetworkConn) { self.free() }) return res } -func (self PeerNetwork) AsMsgNetwork() MsgNetwork { return &msgNetwork{ inner: C.peernetwork_as_msgnetwork(self.inner) } } +func (self PeerNetwork) AsMsgNetwork() MsgNetwork { + return MsgNetworkFromC(C.peernetwork_as_msgnetwork(self.inner)) +} + +func (self MsgNetwork) AsPeerNetworkUnsafe() PeerNetwork { + return PeerNetworkFromC(C.msgnetwork_as_peernetwork_unsafe(self.inner)) +} func NewMsgNetworkConnFromPeerNetWorkConn(conn PeerNetworkConn) MsgNetworkConn { - res := &msgNetworkConn{ inner: C.msgnetwork_conn_new_from_peernetwork_conn(conn.inner) } + res := MsgNetworkConnFromC(C.msgnetwork_conn_new_from_peernetwork_conn(conn.inner)) runtime.SetFinalizer(res, func(self MsgNetworkConn) { self.free() }) return res } func (self PeerNetworkConn) Copy() PeerNetworkConn { - res := &peerNetworkConn { inner: C.peernetwork_conn_copy(self.inner) } + res := PeerNetworkConnFromC(C.peernetwork_conn_copy(self.inner)) runtime.SetFinalizer(res, func(self PeerNetworkConn) { self.free() }) return res } diff --git a/salticidae b/salticidae -Subproject 74e4246565ba5814a92ed9f84b13b17226f219d +Subproject 4dc7feb8b15e0a76cc95e5a80fce363cca63785 diff --git a/test_msgnet/main.go b/test_msgnet/main.go index fd561f7..c2ae70d 100644 --- a/test_msgnet/main.go +++ b/test_msgnet/main.go @@ -114,9 +114,8 @@ func connHandler(_conn *C.struct_msgnetwork_conn_t, connected C.bool, _ unsafe.P } } else { fmt.Printf("[%s] Disconnected, retrying.\n", name) - addr := conn.GetAddr() err := salticidae.NewError() - net.Connect(addr, &err) + net.Connect(conn.GetAddr(), &err) } } @@ -170,4 +169,6 @@ func main() { ev_term.Add(salticidae.SIGTERM) ec.Dispatch() + alice.net.Stop() + bob.net.Stop() } diff --git a/test_p2p_stress/main.go b/test_p2p_stress/main.go index 404956f..d6f3b71 100644 --- a/test_p2p_stress/main.go +++ b/test_p2p_stress/main.go @@ -127,8 +127,7 @@ func (self AppContext) getTC(addr_id uint64) (_tc *TestContext) { func sendRand(size int, app *AppContext, conn salticidae.MsgNetworkConn) { msg, hash := msgRandSerialize(size) - addr := conn.GetAddr() - tc := app.getTC(addr2id(addr)) + tc := app.getTC(addr2id(conn.GetAddr())) tc.hash = hash app.net.AsMsgNetwork().SendMsgByMove(msg, conn) } @@ -172,8 +171,7 @@ func onReceiveAck(_msg *C.struct_msg_t, _conn *C.struct_msgnetwork_conn_t, userd id := *(* int)(userdata) app := &apps[id] conn := salticidae.MsgNetworkConnFromC(salticidae.CMsgNetworkConn(_conn)) - _addr := conn.GetAddr() - addr := addr2id(_addr) + addr := addr2id(conn.GetAddr()) tc := app.getTC(addr) if !hash.IsEq(tc.hash) { @@ -212,8 +210,7 @@ func connHandler(_conn *C.struct_msgnetwork_conn_t, connected C.bool, userdata u app := &apps[id] if connected { if conn.GetMode() == salticidae.CONN_MODE_ACTIVE { - addr := conn.GetAddr() - tc := app.getTC(addr2id(addr)) + tc := app.getTC(addr2id(conn.GetAddr())) tc.state = 1 fmt.Printf("INFO: increasing phase\n") sendRand(tc.state, app, conn) @@ -296,6 +293,7 @@ func main() { } } a.ec.Dispatch() + a.net.AsMsgNetwork().Stop() a.Free() C.free(unsafe.Pointer(ids[app_id])) threads.Done() |