croc/src/comm/comm.go

92 lines
1.9 KiB
Go
Raw Normal View History

2018-09-23 21:34:29 +02:00
package comm
import (
2018-09-23 21:50:03 +02:00
"bytes"
2018-09-23 23:00:31 +02:00
"fmt"
2018-09-23 21:34:29 +02:00
"net"
2018-09-23 23:15:23 +02:00
"strconv"
"strings"
2018-09-23 22:32:46 +02:00
"time"
2018-09-23 21:34:29 +02:00
)
// Comm is some basic TCP communication
type Comm struct {
connection net.Conn
}
// New returns a new comm
func New(c net.Conn) Comm {
2018-09-23 22:32:46 +02:00
c.SetReadDeadline(time.Now().Add(3 * time.Hour))
c.SetDeadline(time.Now().Add(3 * time.Hour))
c.SetWriteDeadline(time.Now().Add(3 * time.Hour))
2018-09-23 21:34:29 +02:00
return Comm{c}
}
// Connection returns the net.Conn connection
func (c Comm) Connection() net.Conn {
return c.connection
}
func (c Comm) Write(b []byte) (int, error) {
2018-09-23 23:15:23 +02:00
c.connection.Write([]byte(fmt.Sprintf("%0.5d", len(b))))
2018-09-23 21:50:03 +02:00
n, err := c.connection.Write(b)
2018-09-23 23:00:31 +02:00
if n != len(b) {
err = fmt.Errorf("wanted to write %d but wrote %d", n, len(b))
}
2018-09-23 23:12:45 +02:00
// log.Printf("wanted to write %d but wrote %d", n, len(b))
2018-09-23 21:50:03 +02:00
return n, err
2018-09-23 21:34:29 +02:00
}
2018-09-23 23:12:45 +02:00
func (c Comm) Read() (buf []byte, numBytes int, bs []byte, err error) {
2018-09-23 23:15:23 +02:00
// read until we get 5 bytes
bs = make([]byte, 5)
2018-09-23 21:50:03 +02:00
_, err = c.connection.Read(bs)
if err != nil {
return
}
2018-09-23 23:12:45 +02:00
for {
bs = bytes.Trim(bytes.Trim(bs, "\x00"), "\x05")
2018-09-23 23:15:23 +02:00
if len(bs) == 5 {
2018-09-23 23:12:45 +02:00
break
}
tmp := make([]byte, 1)
c.connection.Read(tmp)
bs = append(bs, tmp...)
2018-09-23 23:15:23 +02:00
}
numBytes, err = strconv.Atoi(strings.TrimLeft(string(bs), "0"))
if err != nil {
return nil, 0, nil, err
2018-09-23 23:12:45 +02:00
}
2018-09-23 21:50:03 +02:00
buf = []byte{}
tmp := make([]byte, numBytes)
for {
2018-09-23 23:00:31 +02:00
_, err = c.connection.Read(tmp)
2018-09-23 21:50:03 +02:00
if err != nil {
2018-09-23 23:12:45 +02:00
return nil, numBytes, bs, err
2018-09-23 21:50:03 +02:00
}
2018-09-23 23:15:23 +02:00
tmp = bytes.TrimRight(tmp, "\x00")
tmp = bytes.TrimRight(tmp, "\x05")
2018-09-23 21:50:03 +02:00
buf = append(buf, tmp...)
2018-09-23 23:00:31 +02:00
if len(buf) < numBytes {
tmp = make([]byte, numBytes-len(buf))
2018-09-23 21:50:03 +02:00
} else {
break
}
}
2018-09-23 23:00:31 +02:00
// log.Printf("wanted %d and got %d", numBytes, len(buf))
2018-09-23 21:34:29 +02:00
return
}
// Send a message
func (c Comm) Send(message string) (err error) {
2018-09-23 21:50:03 +02:00
_, err = c.Write([]byte(message))
2018-09-23 21:34:29 +02:00
return
}
// Receive a message
func (c Comm) Receive() (s string, err error) {
2018-09-23 23:12:45 +02:00
b, _, _, err := c.Read()
2018-09-23 21:50:03 +02:00
s = string(b)
2018-09-23 21:34:29 +02:00
return
}