-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient.go
96 lines (87 loc) · 1.88 KB
/
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
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
package pi_pi_net
import (
"encoding/binary"
"io"
"log"
"net"
)
type Client struct {
netWork string
Conn net.Conn
Addr string
Data chan struct {
Body []byte
Err error
}
}
func Dail(network, addr string)(*Client, error) {
c := &Client{
Data: make(chan struct{Body []byte
Err error},10),
}
conn, err := net.Dial(network, addr)
if err != nil {
return nil, err
}
c.Conn = conn
go func() {
if err := recover(); err != nil {
log.Println("[client] read data goroutine panic err is :", err)
}
c.read()
}()
return c,nil
}
func (c *Client) read() {
size := make([]byte, 2)
for {
if _, err := io.ReadFull(c.Conn, size); err != nil {
c.Data <- struct {
Body []byte
Err error
}{Err: err}
continue
}
data := make([]byte, binary.LittleEndian.Uint16(size))
if _, err := io.ReadFull(c.Conn, data); err != nil {
c.Data <- struct {
Body []byte
Err error
}{Err: err}
continue
}
c.Data <- struct {
Body []byte
Err error
}{Body: data,Err: nil}
}
}
// 客户端读取 返回值为[]byte
// client read and return []byte
func (c *Client) Read() (body []byte,err error){
data :=<-c.Data
return data.Body,data.Err
}
// 客户端读取 返回值为string
// client read and return string
func (c *Client) ReadString() (body string,err error){
data :=<-c.Data
return ByteToString(data.Body),data.Err
}
// 客户端写入
// client write
func (c *Client) Write(data []byte) (n int, err error) {
result := make([]byte, 2)
binary.LittleEndian.PutUint16(result, uint16(len(data)))
result = append(result, data...)
return c.Conn.Write(result)
}
func (c *Client) WriteString(data string) (n int, err error) {
result := make([]byte, 2)
binary.LittleEndian.PutUint16(result, uint16(len(data)))
result = append(result, StringToByte(data)...)
return c.Conn.Write(result)
}
func (c *Client)Close()error{
return c.Conn.Close()
}