From a82d97a11da234fdf077880262667f2f4872c5de Mon Sep 17 00:00:00 2001 From: Sky Date: Mon, 16 Oct 2017 21:22:39 -0500 Subject: [PATCH 1/3] Update connection.go --- connection.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/connection.go b/connection.go index 23480b2..c96faf1 100644 --- a/connection.go +++ b/connection.go @@ -19,7 +19,8 @@ var uniqueId int32 = 0 func NewConnection(addr, pass string) (*Connection, error) { conn, err := net.Dial("tcp", addr) if err != nil { - log.Fatal(err) +// log.Fatal(err) + return nil, err } c := &Connection{conn: conn, pass: pass, addr: addr} if err := c.auth(); err != nil { From f8c81edbb69603eea8d03e9723e3567876aec8e8 Mon Sep 17 00:00:00 2001 From: Sky Date: Mon, 16 Oct 2017 21:25:40 -0500 Subject: [PATCH 2/3] Update connection.go --- connection.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/connection.go b/connection.go index c96faf1..d7a3e31 100644 --- a/connection.go +++ b/connection.go @@ -19,7 +19,7 @@ var uniqueId int32 = 0 func NewConnection(addr, pass string) (*Connection, error) { conn, err := net.Dial("tcp", addr) if err != nil { -// log.Fatal(err) + log.Println(err) return nil, err } c := &Connection{conn: conn, pass: pass, addr: addr} @@ -56,7 +56,7 @@ func (c *Connection) sendCommand(typ int32, body []byte) { wtr.Write(body) wtr.Write([]byte{0x0, 0x0}) if wtr.err != nil { - log.Fatal(wtr.err) + log.Println(wtr.err) } c.conn.Write(wtr.buf.Bytes()) @@ -69,7 +69,8 @@ func (c *Connection) readPkg() Pkg { // Doesn't handle split messages correctly. read, err := c.conn.Read(b) if err != nil { - log.Fatal(err) + log.Println(err) + } p := Pkg{} @@ -81,7 +82,7 @@ func (c *Connection) readPkg() Pkg { body := [bufSize - 12]byte{} rdr.Read(&body) if rdr.err != nil { - log.Fatal(rdr.err) + log.Println(rdr.err) } p.Body = body[:read-12] return p From a78205163c6e61efaf21741d7a2dc2d15d5c10be Mon Sep 17 00:00:00 2001 From: Sky Date: Fri, 22 Dec 2017 11:29:50 +0800 Subject: [PATCH 3/3] Update connection.go --- connection.go | 55 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/connection.go b/connection.go index d7a3e31..623f647 100644 --- a/connection.go +++ b/connection.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/binary" "errors" - "log" "net" ) @@ -14,12 +13,11 @@ type Connection struct { addr string } -var uniqueId int32 = 0 +var uniqueID int32 = 0 func NewConnection(addr, pass string) (*Connection, error) { conn, err := net.Dial("tcp", addr) if err != nil { - log.Println(err) return nil, err } c := &Connection{conn: conn, pass: pass, addr: addr} @@ -29,25 +27,36 @@ func NewConnection(addr, pass string) (*Connection, error) { return c, nil } -func (c *Connection) SendCommand(cmd string) string { - c.sendCommand(2, []byte(cmd)) - pkg := c.readPkg() - return string(pkg.Body) +func (c *Connection) SendCommand(cmd string) (string, error) { + err := c.sendCommand(2, []byte(cmd)) + if err != nil { + return "", err + } + pkg, err := c.readPkg() + if err != nil { + return "", err + } + return string(pkg.Body), err } func (c *Connection) auth() error { c.sendCommand(3, []byte(c.pass)) - pkg := c.readPkg() - if pkg.Type != 2 || pkg.Id != uniqueId { - return errors.New("Incorrect password.") + pkg, err := c.readPkg() + if err != nil { + return err } + + if pkg.Type != 2 || pkg.ID != uniqueID { + return errors.New("incorrect password") + } + 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) - uniqueId += 1 - id := uniqueId + uniqueID += 1 + id := uniqueID wtr := binaryReadWriter{ByteOrder: binary.LittleEndian} wtr.Write(size) @@ -56,41 +65,41 @@ func (c *Connection) sendCommand(typ int32, body []byte) { wtr.Write(body) wtr.Write([]byte{0x0, 0x0}) if wtr.err != nil { - log.Println(wtr.err) + return wtr.err } c.conn.Write(wtr.buf.Bytes()) + return nil } -func (c *Connection) readPkg() Pkg { +func (c *Connection) readPkg() (pkg, error) { const bufSize = 4096 b := make([]byte, bufSize) // Doesn't handle split messages correctly. read, err := c.conn.Read(b) if err != nil { - log.Println(err) - + return pkg{}, err } - p := Pkg{} + p := pkg{} rdr := binaryReadWriter{ByteOrder: binary.LittleEndian, buf: bytes.NewBuffer(b)} rdr.Read(&p.Size) - rdr.Read(&p.Id) + rdr.Read(&p.ID) rdr.Read(&p.Type) body := [bufSize - 12]byte{} rdr.Read(&body) if rdr.err != nil { - log.Println(rdr.err) + return p, rdr.err } p.Body = body[:read-12] - return p + return p, nil } -type Pkg struct { +type pkg struct { Size int32 - Id int32 + ID int32 Type int32 Body []byte }