You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
1.9 KiB
Go
99 lines
1.9 KiB
Go
package packet
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestPacker(t *testing.T) {
|
|
p := NewNNetPacker()
|
|
|
|
body := []byte("")
|
|
header := Header{
|
|
PacketType: Handshake,
|
|
Length: uint32(len(body)),
|
|
MessageHeader: MessageHeader{
|
|
MsgType: Request,
|
|
ID: 1,
|
|
Route: "",
|
|
compressed: false,
|
|
},
|
|
}
|
|
|
|
pack, err := p.Pack(header, body)
|
|
if err != nil {
|
|
return
|
|
}
|
|
fmt.Println(hex.EncodeToString(pack))
|
|
|
|
// handshake ack
|
|
body = []byte("")
|
|
header = Header{
|
|
PacketType: HandshakeAck,
|
|
Length: uint32(len(body)),
|
|
MessageHeader: MessageHeader{
|
|
MsgType: Response,
|
|
ID: 1,
|
|
Route: "",
|
|
compressed: false,
|
|
},
|
|
}
|
|
|
|
pack, err = p.Pack(header, body)
|
|
if err != nil {
|
|
return
|
|
}
|
|
fmt.Println(hex.EncodeToString(pack))
|
|
|
|
// data
|
|
body = []byte("123")
|
|
header = Header{
|
|
PacketType: Data,
|
|
Length: uint32(len(body)),
|
|
MessageHeader: MessageHeader{
|
|
MsgType: Request,
|
|
ID: 2,
|
|
Route: "",
|
|
compressed: false,
|
|
},
|
|
}
|
|
|
|
pack, err = p.Pack(header, body)
|
|
if err != nil {
|
|
return
|
|
}
|
|
fmt.Println(hex.EncodeToString(pack))
|
|
|
|
// data -> route: test -> handler
|
|
// data
|
|
body = []byte("ni hao")
|
|
header = Header{
|
|
PacketType: Data,
|
|
Length: uint32(len(body)),
|
|
MessageHeader: MessageHeader{
|
|
MsgType: Request,
|
|
ID: 3,
|
|
Route: "test",
|
|
compressed: false,
|
|
},
|
|
}
|
|
|
|
pack, err = p.Pack(header, body)
|
|
if err != nil {
|
|
return
|
|
}
|
|
fmt.Println(hex.EncodeToString(pack))
|
|
}
|
|
|
|
func TestUnPack(t *testing.T) {
|
|
data := []byte{0x04, 0x00, 0x00, 0x23, 0x04, 0x03, 0xE6, 0x9C, 0x8D, 0xE5, 0x8A, 0xA1, 0xE5, 0x99, 0xA8, 0xE6, 0x8E, 0xA5, 0xE6, 0x94, 0xB6, 0xE5, 0x88, 0xB0, 0xE6, 0x95, 0xB0, 0xE6, 0x8D, 0xAE, 0xE4, 0xB8, 0xBA, 0x3A, 0x20, 0x6E, 0x69, 0x20, 0x68, 0x61, 0x6F}
|
|
p := NewNNetPacker()
|
|
|
|
unPacked, err := p.Unpack(data)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println(unPacked)
|
|
}
|