diff options
-rw-r--r-- | network.go | 52 | ||||
m--------- | salticidae | 0 | ||||
-rw-r--r-- | test_msgnet/main.go | 4 | ||||
-rw-r--r-- | test_p2p_stress/main.go | 4 |
4 files changed, 37 insertions, 23 deletions
@@ -140,6 +140,19 @@ func (self MsgNetwork) Listen(addr NetAddr, err *Error) { C.msgnetwork_listen(se // Stop the message network. No other methods should be called after this. func (self MsgNetwork) Stop() { C.msgnetwork_stop(self.inner) } +// Send a message through the given connection. +func (self MsgNetwork) SendMsg(msg Msg, conn MsgNetworkConn) { + C.msgnetwork_send_msg(self.inner, msg.inner, conn.inner) +} + +// Send a message through the given connection, using a worker thread to +// seralize and put data to the send buffer. The payload contained in the given +// msg will be moved and sent. Thus, no methods of msg involving the payload +// should be called afterwards. +func (self MsgNetwork) SendMsgDeferredByMove(msg Msg, conn MsgNetworkConn) { + C.msgnetwork_send_msg_deferred_by_move(self.inner, msg.inner, conn.inner) +} + // Try to connect to a remote address. The connection handle is returned. The // returned connection handle could be kept in your program. func (self MsgNetwork) Connect(addr NetAddr, err *Error) MsgNetworkConn { @@ -151,11 +164,6 @@ func (self MsgNetwork) Connect(addr NetAddr, err *Error) MsgNetworkConn { // Terminate the given connection. func (self MsgNetwork) Terminate(conn MsgNetworkConn) { C.msgnetwork_terminate(self.inner, conn.inner) } -// Send a message through the given connection. The payload contained in the -// given msg will be moved and sent. Thus, no methods of msg involving the -// payload should be called afterwards. -func (self MsgNetwork) SendMsgByMove(msg Msg, conn MsgNetworkConn) { C.msgnetwork_send_msg_by_move(self.inner, msg.inner, conn.inner) } - // The C function pointer type which takes msg_t*, msgnetwork_conn_t* and void* // (passing in the custom user data allocated by C.malloc) as parameters. type MsgNetworkMsgCallback = C.msgnetwork_msg_callback_t @@ -322,26 +330,32 @@ func (self PeerNetworkConn) Copy() PeerNetworkConn { func (self PeerNetworkConn) free() { C.peernetwork_conn_free(self.inner) } -// Send a message to the given peer. The payload contained in the given msg -// will be moved and sent. Thus, no methods of msg involving the payload should -// be called afterwards. -func (self PeerNetwork) SendMsgByMove(_moved_msg Msg, paddr NetAddr) { - C.peernetwork_send_msg_by_move(self.inner, _moved_msg.inner, paddr.inner) +// Listen to the specified network address. Notice that this method overrides +// Listen() in MsgNetwork, so you should always call this one instead of +// AsMsgNetwork().Listen(). +func (self PeerNetwork) Listen(listenAddr NetAddr, err *Error) { + C.peernetwork_listen(self.inner, listenAddr.inner, err) +} + +// Send a message to the given peer. +func (self PeerNetwork) SendMsg(msg Msg, addr NetAddr) { + C.peernetwork_send_msg(self.inner, msg.inner, addr.inner) +} + +// Send a message to the given peer, using a worker thread to seralize and put +// data to the send buffer. The payload contained in the given msg will be +// moved and sent. Thus, no methods of msg involving the payload should be +// called afterwards. +func (self PeerNetwork) SendMsgDeferredByMove(msg Msg, addr NetAddr) { + C.peernetwork_send_msg_deferred_by_move(self.inner, msg.inner, addr.inner) } // Send a message to the given list of peers. The payload contained in the // given msg will be moved and sent. Thus, no methods of msg involving the // payload should be called afterwards. -func (self PeerNetwork) MulticastMsgByMove(_moved_msg Msg, paddrs []NetAddr) { +func (self PeerNetwork) MulticastMsgByMove(msg Msg, paddrs []NetAddr) { na := NewAddrArrayFromAddrs(paddrs) - C.peernetwork_multicast_msg_by_move(self.inner, _moved_msg.inner, na.inner) -} - -// Listen to the specified network address. Notice that this method overrides -// Listen() in MsgNetwork, so you should always call this one instead of -// AsMsgNetwork().Listen(). -func (self PeerNetwork) Listen(listenAddr NetAddr, err *Error) { - C.peernetwork_listen(self.inner, listenAddr.inner, err) + C.peernetwork_multicast_msg_by_move(self.inner, msg.inner, na.inner) } // The C function pointer type which takes netaddr_t* and void* (passing in the diff --git a/salticidae b/salticidae -Subproject a154cb399a6fcbd3d4fd19ab46aa2c107128d34 +Subproject ff904b23147e72db4f7f77f8269242d9a5a0859 diff --git a/test_msgnet/main.go b/test_msgnet/main.go index 9509c2e..66d7287 100644 --- a/test_msgnet/main.go +++ b/test_msgnet/main.go @@ -79,7 +79,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.SendMsgByMove(ack, conn) + net.SendMsg(ack, conn) } //export onReceiveAck @@ -106,7 +106,7 @@ 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.\n", name) hello := msgHelloSerialize(name, "Hello there!") - n.net.SendMsgByMove(hello, conn) + n.net.SendMsg(hello, conn) } else { fmt.Printf("[%s] Accepted, waiting for greetings.\n", name) } diff --git a/test_p2p_stress/main.go b/test_p2p_stress/main.go index 5bb0dc5..9e0757f 100644 --- a/test_p2p_stress/main.go +++ b/test_p2p_stress/main.go @@ -126,7 +126,7 @@ func sendRand(size int, app *AppContext, conn salticidae.MsgNetworkConn) { msg, hash := msgRandSerialize(size) tc := app.getTC(addr2id(conn.GetAddr())) tc.hash = hash - app.net.AsMsgNetwork().SendMsgByMove(msg, conn) + app.net.AsMsgNetwork().SendMsg(msg, conn) } var apps []AppContext @@ -157,7 +157,7 @@ func onReceiveRand(_msg *C.struct_msg_t, _conn *C.struct_msgnetwork_conn_t, user conn := salticidae.MsgNetworkConnFromC(salticidae.CMsgNetworkConn(_conn)) net := conn.GetNet() ack := msgAckSerialize(msgRandUnserialize(msg).GetHash()) - net.SendMsgByMove(ack, conn) + net.SendMsg(ack, conn) } //export onReceiveAck |