-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
148 lines (129 loc) · 3.22 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package RPC
import (
"encoding/json"
"errors"
"log"
"net"
"os"
"time"
)
type Client struct {
Timeout time.Duration
}
func NewClient(timeout time.Duration) *Client {
return &Client{
Timeout: timeout,
}
}
func (c *Client) Call(network string, addr string, method string, params ...interface{}) (interface{}, error) {
var message Message
conn, err := net.DialTimeout(network, addr, c.Timeout)
if err != nil {
log.Println("dial failure " + err.Error())
os.Exit(1)
}
defer func(conn net.Conn) {
err := conn.Close()
if err != nil {
log.Println(conn.LocalAddr().String() + "conn close error" + err.Error())
}
}(conn)
var request interface{} = Request{
Method: method,
Params: params,
}
message.Type = 4
r, _ := json.Marshal(request)
message.Length = uint16(len(r))
message.Payload = request
//设置写入超时时间
if err := conn.SetWriteDeadline(time.Now().Add(c.Timeout)); err != nil {
return nil, err
}
message = Write(conn, message)
if message.Type == 0 {
log.Printf("client write request error")
return nil, errors.New("client write request error")
}
// 设置读取超时时间
if err := conn.SetReadDeadline(time.Now().Add(c.Timeout)); err != nil {
return nil, err
}
message = Read(conn)
if message.Type == 0 {
return nil, errors.New(message.Payload.(string))
} else {
var response Response
resp := message.Payload.(map[string]interface{})
response.Error = resp["error"].(string)
response.Result = resp["result"].(interface{})
if response.Error != "" {
return nil, errors.New(response.Error)
} else {
return response.Result, nil
}
}
}
func (c *Client) IsExistService(network string, addr string, method string) (bool, error) {
exist, err := c.Call(network, addr, "CheckMethod", method)
if err != nil {
log.Printf(err.Error())
return false, err
} else {
return exist.(bool), err
}
}
func (c *Client) Find(addr string, method string, params ...interface{}) ([]string, error) {
var message Message
conn, err := net.DialTimeout("tcp", addr, c.Timeout)
if err != nil {
log.Println("dial failure " + err.Error())
os.Exit(1)
}
defer func(conn net.Conn) {
err := conn.Close()
if err != nil {
log.Println(conn.LocalAddr().String() + "conn close error" + err.Error())
}
}(conn)
var request interface{} = Request{
Method: method,
Params: params,
}
message.Type = 4
r, _ := json.Marshal(request)
message.Length = uint16(len(r))
message.Payload = request
//设置写入超时时间
if err := conn.SetWriteDeadline(time.Now().Add(c.Timeout)); err != nil {
return nil, err
}
message = Write(conn, message)
if message.Type == 0 {
log.Printf("client write request error")
return nil, errors.New("client write request error")
}
// 设置读取超时时间
if err := conn.SetReadDeadline(time.Now().Add(c.Timeout)); err != nil {
return nil, err
}
message = Read(conn)
if message.Type == 0 {
return nil, errors.New(message.Payload.(string))
} else {
resp := message.Payload
if resp == nil {
return nil, nil
} else {
var result []string
if response, ok := resp.([]interface{}); ok {
for _, item := range response {
if str, ok := item.(string); ok {
result = append(result, str)
}
}
}
return result, nil
}
}
}