Initial commit
This commit is contained in:
38
protocol/main.go
Normal file
38
protocol/main.go
Normal file
@ -0,0 +1,38 @@
|
||||
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)...)
|
||||
}
|
6
protocol/scheme.go
Normal file
6
protocol/scheme.go
Normal file
@ -0,0 +1,6 @@
|
||||
package protocol
|
||||
|
||||
type Package struct {
|
||||
buffer []byte
|
||||
Message string
|
||||
}
|
Reference in New Issue
Block a user