summaryrefslogtreecommitdiff
path: root/pkg/client
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/client')
-rw-r--r--pkg/client/client.go22
1 files changed, 14 insertions, 8 deletions
diff --git a/pkg/client/client.go b/pkg/client/client.go
index 66aa745..eee397f 100644
--- a/pkg/client/client.go
+++ b/pkg/client/client.go
@@ -14,15 +14,23 @@ var errClosed = errors.New("server closed connection")
type Client struct {
conn net.Conn
+ r *netstring.Decoder
+ w *netstring.Encoder
}
func New(path string) (*Client, error) {
- conn, err := net.Dial("unixpacket", path)
+ conn, err := net.Dial("unix", path)
if err != nil {
return nil, err
}
- return &Client{conn: conn}, nil
+ c := &Client{
+ conn: conn,
+ r: netstring.NewDecoder(conn),
+ w: netstring.NewEncoder(conn),
+ }
+
+ return c, nil
}
func (c *Client) Send(args []string) (string, error) {
@@ -40,23 +48,21 @@ func (c *Client) Send(args []string) (string, error) {
enc.Encode(s)
}
- buf := make([]byte, config.BufSize)
-
- _, ew := c.conn.Write(out.Bytes())
+ ew := c.w.Encode(out.String())
if ew != nil {
return "", ew
}
- nr, er := c.conn.Read(buf)
+ resp, er := c.r.Decode()
if er != nil {
- if er == io.EOF {
+ if errors.Is(er, io.EOF) {
return "", errClosed
}
return "", er
}
- return string(buf[:nr]), nil
+ return resp, nil
}
func (c *Client) Close() {