39 lines
658 B
Go
39 lines
658 B
Go
package protocol
|
|
|
|
func (p *Package) AddToBuffer(data []byte) {
|
|
p.buffer = append(p.buffer, data...)
|
|
}
|
|
|
|
func (p *Package) Read() bool {
|
|
var l int32
|
|
|
|
if len(p.buffer) < 4 {
|
|
return false
|
|
}
|
|
|
|
// Read the length of the message
|
|
l = int32(p.buffer[0]) | int32(p.buffer[1])<<8 | int32(p.buffer[2])<<16 | int32(p.buffer[3])<<24
|
|
|
|
if len(p.buffer) < int(l)+4 {
|
|
return false
|
|
}
|
|
|
|
p.Message = string(p.buffer[4 : l+4])
|
|
p.buffer = p.buffer[l+4:]
|
|
|
|
return true
|
|
}
|
|
|
|
func (p *Package) Pack() []byte {
|
|
var l = int32(len([]byte(p.Message)))
|
|
|
|
var b = []byte{
|
|
byte(l),
|
|
byte(l >> 8),
|
|
byte(l >> 16),
|
|
byte(l >> 24),
|
|
}
|
|
|
|
return append(b, []byte(p.Message)...)
|
|
}
|