read until 4 bytes

This commit is contained in:
Zack Scholl 2019-04-30 07:07:07 -07:00
parent 3c051f4283
commit e54045ef5a
1 changed files with 14 additions and 11 deletions

View File

@ -65,18 +65,21 @@ func (c *Comm) Write(b []byte) (int, error) {
} }
func (c *Comm) Read() (buf []byte, numBytes int, bs []byte, err error) { func (c *Comm) Read() (buf []byte, numBytes int, bs []byte, err error) {
// read until we get 5 bytes // read until we get 4 bytes for the header
header := make([]byte, 4) var header []byte
n, err := c.connection.Read(header) numBytes = 4
if err != nil { for {
return tmp := make([]byte,numBytes-len(header))
n, errRead := c.connection.Read(tmp)
if errRead != nil {
err = errRead
return
}
header = append(header, tmp[:n]...)
if numBytes == len(header) {
break
}
} }
if n < 4 {
err = fmt.Errorf("not enough bytes: %d", n)
return
}
// make it so it won't change
header = append([]byte(nil), header...)
var numBytesUint32 uint32 var numBytesUint32 uint32
rbuf := bytes.NewReader(header) rbuf := bytes.NewReader(header)