diff --git a/README-CN.md b/README-CN.md index 57e08d3..57b6267 100644 --- a/README-CN.md +++ b/README-CN.md @@ -47,12 +47,11 @@ func main() { // 创建一个打包器 mp := tcpack.NewMsgPack(8, tcpConn) - // 打包一个消息 + // 打包一个消息并发送 msg := tcpack.NewMessage(0, uint32(len([]byte(data))), []byte(data)) - msgByte, err := mp.Pack(msg) - num, err := tcpConn.Write(msgByte) + num, err := mp.Pack(msg) - // 解包一个消息 + // 解包一个消息并接收 msg, err := mp.Unpack() } ``` @@ -75,12 +74,11 @@ data := &Person{ } dataJSON, _ := json.Marshal(data) -// 打包一个消息 +// 打包一个消息并发送 msgSend := tcpack.NewMessage(0, uint32(len(dataJSON)), dataJSON) -msgSendByte, _ := mpClient.Pack(msgSend) -num, err := tcpConn.Write(msgSendByte) +num, err := mp.Pack(msgSend) -// 解包一个消息 +// 解包一个消息并接收 msgRsv, err := mp.Unpack() // JSON UnMarshal diff --git a/README.md b/README.md index 106dfd2..52a0169 100644 --- a/README.md +++ b/README.md @@ -48,12 +48,11 @@ func main() { // Create a packager mp := tcpack.NewMsgPack(8, tcpConn) - // Pack a message + // Pack and send a message msg := tcpack.NewMessage(0, uint32(len([]byte(data))), []byte(data)) - msgByte, err := mp.Pack(msg) - num, err := tcpConn.Write(msgByte) + num, err := mp.Pack(msg) - // Unpack a message + // Unpack and receive a message msg, err := mp.Unpack() } ``` @@ -76,12 +75,11 @@ data := &Person{ } dataJSON, _ := json.Marshal(data) -// Pack a message +// Pack and send a message msgSend := tcpack.NewMessage(0, uint32(len(dataJSON)), dataJSON) -msgSendByte, _ := mpClient.Pack(msgSend) -num, err := tcpConn.Write(msgSendByte) +num, err := mp.Pack(msgSend) -// Unpack a message +// Unpack and receive a message msgRsv, err := mp.Unpack() // JSON UnMarshal diff --git a/example/client.go b/example/client.go index a7f7fbe..b8a7ce8 100644 --- a/example/client.go +++ b/example/client.go @@ -33,11 +33,7 @@ func main() { var data string fmt.Scanln(&data) msg := tcpack.NewMessage(0, uint32(len([]byte(data))), []byte(data)) - msgByte, err := mp.Pack(msg) - if err != nil { - fmt.Println("msg pack failed:", err) - } - num, err := tcpConn.Write(msgByte) + num, err := mp.Pack(msg) if err != nil { fmt.Println("msg send failed:", err) } diff --git a/example/server.go b/example/server.go index 9c2ea89..eec0724 100644 --- a/example/server.go +++ b/example/server.go @@ -40,11 +40,7 @@ func main() { var data string fmt.Scanln(&data) msg := tcpack.NewMessage(0, uint32(len([]byte(data))), []byte(data)) - msgByte, err := mp.Pack(msg) - if err != nil { - fmt.Println("msg pack failed:", err) - } - num, err := tcpConn.Write(msgByte) + num, err := mp.Pack(msg) if err != nil { fmt.Println("msg send failed:", err) } diff --git a/example/transmitJSON.go b/example/transmitJSON.go index 13cb8dd..f394c92 100644 --- a/example/transmitJSON.go +++ b/example/transmitJSON.go @@ -47,8 +47,7 @@ func main() { data, _ := json.Marshal(jack) fmt.Println("send JSON:", string(data)) msgSend := tcpack.NewMessage(0, uint32(len(data)), data) - msgSendByte, _ := mpClient.Pack(msgSend) - _, _ = tcpConnClient.Write(msgSendByte) + _, _ = mpClient.Pack(msgSend) msgRevOut := <-msgChan diff --git a/tcpack.go b/tcpack.go index ab21dc2..e983849 100644 --- a/tcpack.go +++ b/tcpack.go @@ -40,18 +40,22 @@ func (mp *MsgPack) GetHeadLen() uint32 { } // Pack packs a message to bytes stream. -func (mp *MsgPack) Pack(msg Imessage) ([]byte, error) { +func (mp *MsgPack) Pack(msg Imessage) (uint32, error) { buffer := bytes.NewBuffer([]byte{}) if err := binary.Write(buffer, binary.LittleEndian, msg.GetDataLen()); err != nil { - return nil, err + return 0, err } if err := binary.Write(buffer, binary.LittleEndian, msg.GetMsgId()); err != nil { - return nil, err + return 0, err } if err := binary.Write(buffer, binary.LittleEndian, msg.GetMsgData()); err != nil { - return nil, err + return 0, err + } + num, err := mp.conn.Write(buffer.Bytes()) + if err != nil { + return 0, err } - return buffer.Bytes(), nil + return uint32(num), nil } // Unpack unpacks a certain length bytes stream to a message. diff --git a/tcpack_test.go b/tcpack_test.go index 3dcb030..b6a2569 100644 --- a/tcpack_test.go +++ b/tcpack_test.go @@ -37,8 +37,7 @@ func TestMsgPack_Pack(t *testing.T) { mpClient := NewMsgPack(8, tcpConnClient) data := "Hello,World" msgSend := NewMessage(0, uint32(len([]byte(data))), []byte(data)) - msgSendByte, _ := mpClient.Pack(msgSend) - _, _ = tcpConnClient.Write(msgSendByte) + _, _ = mpClient.Pack(msgSend) msgRevOut := <-msgChan @@ -51,14 +50,11 @@ func TestMsgPack_Pack(t *testing.T) { data2 := "b" data3 := "c" msgSend1 := NewMessage(0, uint32(len([]byte(data1))), []byte(data1)) - msgSendByte1, _ := mpClient.Pack(msgSend1) + _, _ = mpClient.Pack(msgSend1) msgSend2 := NewMessage(0, uint32(len([]byte(data2))), []byte(data2)) - msgSendByte2, _ := mpClient.Pack(msgSend2) + _, _ = mpClient.Pack(msgSend2) msgSend3 := NewMessage(0, uint32(len([]byte(data3))), []byte(data3)) - msgSendByte3, _ := mpClient.Pack(msgSend3) - _, _ = tcpConnClient.Write(msgSendByte1) - _, _ = tcpConnClient.Write(msgSendByte2) - _, _ = tcpConnClient.Write(msgSendByte3) + _, _ = mpClient.Pack(msgSend3) msgRevOut1 := <-msgChan msgRevOut2 := <-msgChan msgRevOut3 := <-msgChan @@ -75,8 +71,7 @@ func TestMsgPack_Pack(t *testing.T) { // Send a message that exceeds the size of the socket buffer data4 := make([]byte, 65600) msgSend4 := NewMessage(0, uint32(len(data4)), data4) - msgSendByte4, _ := mpClient.Pack(msgSend4) - _, _ = tcpConnClient.Write(msgSendByte4) + _, _ = mpClient.Pack(msgSend4) msgRevOut4 := <-msgChan Convey("Send a message that exceeds the size of the socket buffer", t, func() { So(msgSend4, ShouldEqual, *msgRevOut4) @@ -113,8 +108,7 @@ func TestMsgPack_Unpack(t *testing.T) { mpClient := NewMsgPack(8, tcpConnClient) data := "" msgSend := NewMessage(0, uint32(len([]byte(data))), []byte(data)) - msgSendByte, _ := mpClient.Pack(msgSend) - _, _ = tcpConnClient.Write(msgSendByte) + _, _ = mpClient.Pack(msgSend) msgRevOut := <-msgChan