Remove log.Fatal handling of errors.

This commit is contained in:
Michael Bang 2017-10-22 18:41:09 +02:00
parent 2ee5d44039
commit 864881499f
2 changed files with 41 additions and 23 deletions

View File

@ -40,7 +40,14 @@ func main() {
prompt() prompt()
continue continue
} }
r := conn.SendCommand(input)
r, err := conn.SendCommand(input)
if err != nil {
fmt.Printf("Error: %s\n", err)
prompt()
continue
}
fmt.Printf("Server:\n%s\n", r) fmt.Printf("Server:\n%s\n", r)
prompt() prompt()
} }

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"errors" "errors"
"log"
"net" "net"
) )
@ -14,12 +13,12 @@ type Connection struct {
addr string addr string
} }
var uniqueId int32 = 0 var uniqueID int32 = 0
func NewConnection(addr, pass string) (*Connection, error) { func NewConnection(addr, pass string) (*Connection, error) {
conn, err := net.Dial("tcp", addr) conn, err := net.Dial("tcp", addr)
if err != nil { if err != nil {
log.Fatal(err) return nil, err
} }
c := &Connection{conn: conn, pass: pass, addr: addr} c := &Connection{conn: conn, pass: pass, addr: addr}
if err := c.auth(); err != nil { if err := c.auth(); err != nil {
@ -28,25 +27,36 @@ func NewConnection(addr, pass string) (*Connection, error) {
return c, nil return c, nil
} }
func (c *Connection) SendCommand(cmd string) string { func (c *Connection) SendCommand(cmd string) (string, error) {
c.sendCommand(2, []byte(cmd)) err := c.sendCommand(2, []byte(cmd))
pkg := c.readPkg() if err != nil {
return string(pkg.Body) return "", err
}
pkg, err := c.readPkg()
if err != nil {
return "", err
}
return string(pkg.Body), err
} }
func (c *Connection) auth() error { func (c *Connection) auth() error {
c.sendCommand(3, []byte(c.pass)) c.sendCommand(3, []byte(c.pass))
pkg := c.readPkg() pkg, err := c.readPkg()
if pkg.Type != 2 || pkg.Id != uniqueId { if err != nil {
return errors.New("Incorrect password.") return err
} }
if pkg.Type != 2 || pkg.ID != uniqueID {
return errors.New("incorrect password")
}
return nil return nil
} }
func (c *Connection) sendCommand(typ int32, body []byte) { func (c *Connection) sendCommand(typ int32, body []byte) error {
size := int32(4 + 4 + len(body) + 2) size := int32(4 + 4 + len(body) + 2)
uniqueId += 1 uniqueID += 1
id := uniqueId id := uniqueID
wtr := binaryReadWriter{ByteOrder: binary.LittleEndian} wtr := binaryReadWriter{ByteOrder: binary.LittleEndian}
wtr.Write(size) wtr.Write(size)
@ -55,40 +65,41 @@ func (c *Connection) sendCommand(typ int32, body []byte) {
wtr.Write(body) wtr.Write(body)
wtr.Write([]byte{0x0, 0x0}) wtr.Write([]byte{0x0, 0x0})
if wtr.err != nil { if wtr.err != nil {
log.Fatal(wtr.err) return wtr.err
} }
c.conn.Write(wtr.buf.Bytes()) c.conn.Write(wtr.buf.Bytes())
return nil
} }
func (c *Connection) readPkg() Pkg { func (c *Connection) readPkg() (pkg, error) {
const bufSize = 4096 const bufSize = 4096
b := make([]byte, bufSize) b := make([]byte, bufSize)
// Doesn't handle split messages correctly. // Doesn't handle split messages correctly.
read, err := c.conn.Read(b) read, err := c.conn.Read(b)
if err != nil { if err != nil {
log.Fatal(err) return pkg{}, err
} }
p := Pkg{} p := pkg{}
rdr := binaryReadWriter{ByteOrder: binary.LittleEndian, rdr := binaryReadWriter{ByteOrder: binary.LittleEndian,
buf: bytes.NewBuffer(b)} buf: bytes.NewBuffer(b)}
rdr.Read(&p.Size) rdr.Read(&p.Size)
rdr.Read(&p.Id) rdr.Read(&p.ID)
rdr.Read(&p.Type) rdr.Read(&p.Type)
body := [bufSize - 12]byte{} body := [bufSize - 12]byte{}
rdr.Read(&body) rdr.Read(&body)
if rdr.err != nil { if rdr.err != nil {
log.Fatal(rdr.err) return p, rdr.err
} }
p.Body = body[:read-12] p.Body = body[:read-12]
return p return p, nil
} }
type Pkg struct { type pkg struct {
Size int32 Size int32
Id int32 ID int32
Type int32 Type int32
Body []byte Body []byte
} }