-
Notifications
You must be signed in to change notification settings - Fork 3
/
rpcClient.go
111 lines (103 loc) · 2.45 KB
/
rpcClient.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
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"
)
const (
VERSION = 0.1
RPCCLIENT_TIMEOUT = 30
)
// 钱包连接参数
type rpcClient struct {
serverAddr string
user string
passwd string
httpClient *http.Client
}
// 请求信息
type rpcRequest struct {
Method string `json:"method"`
Params interface{} `json:"params"`
Id int64 `json:"id"`
JsonRpc string `json:"jsonrpc"`
}
type rpcResponse struct {
Id int64 `json:"id"`
Result json.RawMessage `json:"result"`
Err interface{} `json:"error"`
}
//连接配置
func newClient(host string, port int, user, passwd string, useSSL bool) (c *rpcClient, err error) {
if len(host) == 0 {
err = errors.New("Bad call missing argument host")
return
}
var serverAddr string
var httpClient *http.Client
if useSSL {
serverAddr = "https://"
t := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
httpClient = &http.Client{Transport: t}
} else {
serverAddr = "http://"
httpClient = &http.Client{}
}
c = &rpcClient{serverAddr: fmt.Sprintf("%s%s:%d", serverAddr, host, port), user: user, passwd: passwd, httpClient: httpClient}
return
}
// 超时处理
func (c *rpcClient) doTimeoutRequest(timer *time.Timer, req *http.Request) (*http.Response, error) {
type result struct {
resp *http.Response
err error
}
done := make(chan result, 1)
go func() {
resp, err := c.httpClient.Do(req)
done <- result{resp, err}
}()
select {
case r := <-done:
return r.resp, r.err
case <-timer.C:
return nil, errors.New("Timeout reading data from server")
}
}
//通信
func (c *rpcClient) send(reqJson string) (retJSON string, err error) {
connectTimer := time.NewTimer(RPCCLIENT_TIMEOUT * time.Second)
reqJsonByte := []byte(reqJson)
payloadBuffer := bytes.NewReader(reqJsonByte)
req, err := http.NewRequest("POST", c.serverAddr, payloadBuffer)
if err != nil {
return
}
req.Header.Add("Content-Type", "application/json;charset=utf-8")
req.Header.Add("Accept", "application/json")
if len(c.user) > 0 || len(c.passwd) > 0 {
req.SetBasicAuth(c.user, c.passwd)
}
resp, err := c.doTimeoutRequest(connectTimer, req)
if err != nil {
return
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
if resp.StatusCode != 200 {
err = errors.New("HTTP error: " + resp.Status)
return
}
retJSON = string(data)
return
}