Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: encapsulate sending in Pack #1

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
```
Expand All @@ -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
Expand Down
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
```
Expand All @@ -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
Expand Down
6 changes: 1 addition & 5 deletions example/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 1 addition & 5 deletions example/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions example/transmitJSON.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 9 additions & 5 deletions tcpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 6 additions & 12 deletions tcpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down