Files
jpillora-chisel/simple_protocol.go
Jaime Pillora 9432ac005b added docs
2015-03-14 21:27:56 +11:00

28 lines
500 B
Go

package chisel
import (
"encoding/binary"
"net"
)
//simple protocol [2 bytes of size, size bytes of data]
//TODO add length and error checks
func SizeRead(c net.Conn) []byte {
sizeb := make([]byte, 2)
c.Read(sizeb)
size := binary.BigEndian.Uint16(sizeb)
datab := make([]byte, size)
c.Read(datab)
return datab
}
func SizeWrite(c net.Conn, data []byte) {
size := len(data)
sizeb := make([]byte, 2)
binary.BigEndian.PutUint16(sizeb, uint16(size))
c.Write(sizeb)
c.Write(data)
return
}