-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
47 lines (40 loc) · 978 Bytes
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package client
import (
"io"
"bufio"
"fmt"
log "github.com/Sirupsen/logrus"
)
const BUFFER_SIZE = 512
type Client struct {
conn io.ReadWriteCloser
in *bufio.Reader
out *bufio.Writer
}
type middlewareReadWriter struct {
conn io.ReadWriteCloser
}
func (mrw *middlewareReadWriter) Read(p []byte) (n int, err error) {
n, err = mrw.conn.Read(p)
log.WithFields(log.Fields{
"bytesRead": n,
"error": err,
"bytes": fmt.Sprintf("%s", p[:n]),
}).Debug("read data from client")
return
}
func (mrw *middlewareReadWriter) Write(p []byte) (n int, err error) {
n, err = mrw.conn.Write(p)
log.WithFields(log.Fields{
"bytesWritten": n,
"error": err,
"bytes": fmt.Sprintf("%s", p[:n]),
}).Debug("wrote data to client")
return
}
func MakeClient(c io.ReadWriteCloser) *Client {
mrw := &middlewareReadWriter{c}
return &Client{ c,
bufio.NewReaderSize(mrw, BUFFER_SIZE),
bufio.NewWriter(mrw) }
}