Initial commit

This commit is contained in:
2024-07-23 00:59:32 +03:00
commit ce74e0c0b0
7 changed files with 167 additions and 0 deletions

38
protocol/main.go Normal file
View 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
View File

@ -0,0 +1,6 @@
package protocol
type Package struct {
buffer []byte
Message string
}