forked from smallnest/rpcx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_direct_creator.go
170 lines (141 loc) · 4.73 KB
/
client_direct_creator.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package rpcx
import (
"bufio"
"crypto/tls"
"errors"
"io"
"net"
"net/http"
"time"
quicconn "github.com/marten-seemann/quic-conn"
"github.com/smallnest/rpcx/core"
"github.com/smallnest/rpcx/log"
kcp "github.com/xtaci/kcp-go"
)
// NewDirectRPCClient creates a rpc client
func NewDirectRPCClient(c *Client, clientCodecFunc ClientCodecFunc, network, address string, timeout time.Duration) (*core.Client, error) {
//if network == "http" || network == "https" {
switch network {
case "http":
return NewDirectHTTPRPCClient(c, clientCodecFunc, network, address, "", timeout)
case "kcp":
return NewDirectKCPRPCClient(c, clientCodecFunc, network, address, "", timeout)
case "quic":
return NewDirectQuicRPCClient(c, clientCodecFunc, network, address, "", timeout)
default:
}
var conn net.Conn
var tlsConn *tls.Conn
var err error
if c != nil && c.TLSConfig != nil {
dialer := &net.Dialer{
Timeout: timeout,
}
tlsConn, err = tls.DialWithDialer(dialer, network, address, c.TLSConfig)
//or conn:= tls.Client(netConn, &config)
conn = net.Conn(tlsConn)
} else {
conn, err = net.DialTimeout(network, address, timeout)
}
if err != nil {
log.Errorf("failed to dial server: %v", err)
return nil, err
}
return wrapConn(c, clientCodecFunc, conn)
}
func wrapConn(c *Client, clientCodecFunc ClientCodecFunc, conn net.Conn) (*core.Client, error) {
if c == nil || c.PluginContainer == nil {
return core.NewClientWithCodec(clientCodecFunc(conn)), nil
}
var ok bool
if conn, ok = c.PluginContainer.DoPostConnected(conn); !ok {
return nil, errors.New("failed to do post connected")
}
wrapper := newClientCodecWrapper(c.PluginContainer, clientCodecFunc(conn), conn)
wrapper.ClientCodecFunc = clientCodecFunc
wrapper.Timeout = c.Timeout
wrapper.ReadTimeout = c.ReadTimeout
wrapper.WriteTimeout = c.WriteTimeout
return core.NewClientWithCodec(wrapper), nil
}
// NewDirectHTTPRPCClient creates a rpc http client
func NewDirectHTTPRPCClient(c *Client, clientCodecFunc ClientCodecFunc, network, address string, path string, timeout time.Duration) (*core.Client, error) {
if path == "" {
path = core.DefaultRPCPath
}
var conn net.Conn
var tlsConn *tls.Conn
var err error
if c != nil && c.TLSConfig != nil {
dialer := &net.Dialer{
Timeout: timeout,
}
tlsConn, err = tls.DialWithDialer(dialer, "tcp", address, c.TLSConfig)
//or conn:= tls.Client(netConn, &config)
conn = net.Conn(tlsConn)
} else {
conn, err = net.DialTimeout("tcp", address, timeout)
}
if err != nil {
log.Errorf("failed to dial server: %v", err)
return nil, err
}
io.WriteString(conn, "CONNECT "+path+" HTTP/1.0\n\n")
// Require successful HTTP response
// before switching to RPC protocol.
resp, err := http.ReadResponse(bufio.NewReader(conn), &http.Request{Method: "CONNECT"})
if err == nil && resp.Status == connected {
if c == nil || c.PluginContainer == nil {
return core.NewClientWithCodec(clientCodecFunc(conn)), nil
}
wrapper := newClientCodecWrapper(c.PluginContainer, clientCodecFunc(conn), conn)
wrapper.ClientCodecFunc = clientCodecFunc
wrapper.Timeout = c.Timeout
wrapper.ReadTimeout = c.ReadTimeout
wrapper.WriteTimeout = c.WriteTimeout
return core.NewClientWithCodec(wrapper), nil
}
if err == nil {
log.Errorf("unexpected HTTP response: %v", err)
err = errors.New("unexpected HTTP response: " + resp.Status)
}
conn.Close()
return nil, &net.OpError{
Op: "dial-http",
Net: network + " " + address,
Addr: nil,
Err: err,
}
}
// NewDirectKCPRPCClient creates a kcp client.
// kcp project: https://github.com/xtaci/kcp-go
func NewDirectKCPRPCClient(c *Client, clientCodecFunc ClientCodecFunc, network, address string, path string, timeout time.Duration) (*core.Client, error) {
var conn net.Conn
var err error
conn, err = kcp.DialWithOptions(address, c.Block, 10, 3)
if err != nil {
return nil, err
}
wrapper := newClientCodecWrapper(c.PluginContainer, clientCodecFunc(conn), conn)
wrapper.ClientCodecFunc = clientCodecFunc
wrapper.Timeout = c.Timeout
wrapper.ReadTimeout = c.ReadTimeout
wrapper.WriteTimeout = c.WriteTimeout
return wrapConn(c, clientCodecFunc, conn)
}
// NewDirectQuicRPCClient creates a quic client.
func NewDirectQuicRPCClient(c *Client, clientCodecFunc ClientCodecFunc, network, address string, path string, timeout time.Duration) (*core.Client, error) {
var conn net.Conn
var err error
tlsConf := &tls.Config{InsecureSkipVerify: true}
conn, err = quicconn.Dial(address, tlsConf)
if err != nil {
return nil, err
}
wrapper := newClientCodecWrapper(c.PluginContainer, clientCodecFunc(conn), conn)
wrapper.ClientCodecFunc = clientCodecFunc
wrapper.Timeout = c.Timeout
wrapper.ReadTimeout = c.ReadTimeout
wrapper.WriteTimeout = c.WriteTimeout
return wrapConn(c, clientCodecFunc, conn)
}