forked from smallnest/rpcx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_direct_creator.go
134 lines (111 loc) · 3.43 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
package rpcx
import (
"bufio"
"crypto/tls"
"errors"
"io"
"net"
"net/http"
"net/rpc"
"time"
kcp "github.com/xtaci/kcp-go"
)
// NewDirectRPCClient creates a rpc client
func NewDirectRPCClient(c *Client, clientCodecFunc ClientCodecFunc, network, address string, timeout time.Duration) (*rpc.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)
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 {
return nil, err
}
return wrapConn(c, clientCodecFunc, conn)
}
func wrapConn(c *Client, clientCodecFunc ClientCodecFunc, conn net.Conn) (*rpc.Client, error) {
var ok bool
if conn, ok = c.PluginContainer.DoPostConnected(conn); !ok {
return nil, errors.New("failed to do post connected")
}
if c == nil || c.PluginContainer == nil {
return rpc.NewClientWithCodec(clientCodecFunc(conn)), nil
}
wrapper := newClientCodecWrapper(c.PluginContainer, clientCodecFunc(conn), conn)
wrapper.Timeout = c.Timeout
wrapper.ReadTimeout = c.ReadTimeout
wrapper.WriteTimeout = c.WriteTimeout
return rpc.NewClientWithCodec(wrapper), nil
}
// NewDirectHTTPRPCClient creates a rpc http client
func NewDirectHTTPRPCClient(c *Client, clientCodecFunc ClientCodecFunc, network, address string, path string, timeout time.Duration) (*rpc.Client, error) {
if path == "" {
path = rpc.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 {
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 rpc.NewClientWithCodec(clientCodecFunc(conn)), nil
}
wrapper := newClientCodecWrapper(c.PluginContainer, clientCodecFunc(conn), conn)
wrapper.Timeout = c.Timeout
wrapper.ReadTimeout = c.ReadTimeout
wrapper.WriteTimeout = c.WriteTimeout
return rpc.NewClientWithCodec(wrapper), nil
}
if err == nil {
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) (*rpc.Client, error) {
var conn net.Conn
var err error
conn, err = kcp.DialWithOptions(address, c.Block, 10, 3)
if err != nil {
return nil, err
}
return wrapConn(c, clientCodecFunc, conn)
}