update
This commit is contained in:
parent
c4c6f1c4ff
commit
f426b7b2cc
@ -8,7 +8,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
AppDataHeader = []byte{0x17, 0x3, 0x3}
|
||||
AppDataHeader = []byte("GGGGGGGG")
|
||||
HeaderLength = len(AppDataHeader)
|
||||
)
|
||||
|
||||
type PackAppData struct {
|
||||
@ -17,11 +18,11 @@ type PackAppData struct {
|
||||
|
||||
func (m PackAppData) Read(p []byte) (n int, err error) {
|
||||
buf := make([]byte, 32*1024)
|
||||
read, err := m.Conn.Read(buf[0:5])
|
||||
if bytes.Equal(buf[0:3], AppDataHeader) {
|
||||
u := int(binary.BigEndian.Uint16(buf[3:5]))
|
||||
r, err := m.Conn.Read(buf[5 : u+5])
|
||||
copy(p, buf[5:r+5])
|
||||
read, err := m.Conn.Read(buf[0 : HeaderLength+2])
|
||||
if bytes.Equal(buf[0:HeaderLength], AppDataHeader) {
|
||||
u := int(binary.BigEndian.Uint16(buf[HeaderLength : HeaderLength+2]))
|
||||
r, err := m.Conn.Read(buf[HeaderLength+2 : u+HeaderLength+2])
|
||||
copy(p, buf[HeaderLength+2:r+HeaderLength+2])
|
||||
return r, err
|
||||
} else {
|
||||
fmt.Println("Header is not present")
|
||||
@ -30,12 +31,12 @@ func (m PackAppData) Read(p []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
func (m PackAppData) Write(p []byte) (n int, err error) {
|
||||
t := make([]byte, len(p)+5)
|
||||
t := make([]byte, len(p)+HeaderLength+2)
|
||||
copy(t[0:], AppDataHeader)
|
||||
binary.BigEndian.PutUint16(t[3:], uint16(len(p)))
|
||||
copy(t[5:], p)
|
||||
binary.BigEndian.PutUint16(t[HeaderLength:], uint16(len(p)))
|
||||
copy(t[HeaderLength+2:], p)
|
||||
write, err := m.Conn.Write(t)
|
||||
write = write - 5
|
||||
write = write - HeaderLength - 2
|
||||
return write, err
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user