This repository has been archived by the owner on Jul 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-ref.go
100 lines (73 loc) · 1.62 KB
/
client-ref.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package socket
import (
"bufio"
"bytes"
"net"
"sync"
"cyberpull.com/go-cyb/errors"
)
type ClientRef struct {
conn net.Conn
reader *bufio.Reader
mutex sync.Mutex
}
func (c *ClientRef) validate() error {
if c.conn == nil {
return errors.New("Connection instance not found")
}
if c.reader == nil {
return errors.New("Client reader not found")
}
return nil
}
func (c *ClientRef) Write(b []byte) (i int, err error) {
if err = c.validate(); err != nil {
return
}
i, err = c.conn.Write(b)
return
}
func (c *ClientRef) WriteString(d string) (int, error) {
return c.Write([]byte(d))
}
func (c *ClientRef) Writeln(b []byte) (int, error) {
return c.Write(append(b, '\n'))
}
func (c *ClientRef) WriteStringln(d string) (int, error) {
return c.Writeln([]byte(d))
}
func (c *ClientRef) ReadBytes(delim byte) (value []byte, err error) {
if err = c.validate(); err != nil {
return
}
value, err = c.reader.ReadBytes(delim)
return
}
func (c *ClientRef) ReadString(delim byte) (value string, err error) {
if err = c.validate(); err != nil {
return
}
value, err = c.reader.ReadString(delim)
return
}
func (c *ClientRef) checkError(data []byte) (err error) {
c.mutex.Lock()
defer c.mutex.Unlock()
if !bytes.HasPrefix(data, []byte(ErrorPrefix)) {
return
}
data = bytes.TrimPrefix(data, []byte(ErrorPrefix))
err = errors.New(string(data))
c.WriteStringln(ErrorRcpt)
return
}
func (c *ClientRef) close() error {
return c.conn.Close()
}
/**********************************************/
func newClientRef(conn net.Conn) *ClientRef {
return &ClientRef{
conn: conn,
reader: bufio.NewReader(conn),
}
}