-
Notifications
You must be signed in to change notification settings - Fork 1
/
dialer.go
67 lines (62 loc) · 2.49 KB
/
dialer.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
// Copyright (c) 2019 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package rpc
import (
"crypto/tls"
"errors"
"github.com/hslam/socket"
)
// Dial connects to an RPC server at the specified network address.
func Dial(network, address, codec string) (*Conn, error) {
if newSocket := NewSocket(network); newSocket != nil {
if newCodec := NewCodec(codec); newCodec != nil {
return NewConn().Dial(newSocket(nil), address, func(messages socket.Messages) ClientCodec {
return NewClientCodec(newCodec(), nil, messages, bufferSize)
})
}
return nil, errors.New("unsupported codec: " + codec)
}
return nil, errors.New("unsupported protocol scheme: " + network)
}
// DialTLS connects to an RPC server at the specified network address with tls.Config.
func DialTLS(network, address, codec string, config *tls.Config) (*Conn, error) {
if newSocket := NewSocket(network); newSocket != nil {
if newCodec := NewCodec(codec); newCodec != nil {
return NewConn().Dial(newSocket(config), address, func(messages socket.Messages) ClientCodec {
return NewClientCodec(newCodec(), nil, messages, bufferSize)
})
}
return nil, errors.New("unsupported codec: " + codec)
}
return nil, errors.New("unsupported protocol scheme: " + network)
}
// DialWithOptions connects to an RPC server at the specified network address with Options.
func DialWithOptions(address string, opts *Options) (*Conn, error) {
if opts.NewCodec == nil && opts.NewHeaderEncoder == nil && opts.Codec == "" {
return nil, errors.New("need opts.NewCodec, opts.NewHeaderEncoder or opts.Codec")
}
if opts.NewSocket == nil && opts.Network == "" {
return nil, errors.New("need opts.NewSocket or opts.Network")
}
var sock socket.Socket
if newSocket := NewSocket(opts.Network); newSocket != nil {
sock = newSocket(opts.TLSConfig)
} else if opts.NewSocket != nil {
sock = opts.NewSocket(opts.TLSConfig)
}
return NewConn().Dial(sock, address, func(messages socket.Messages) ClientCodec {
var bodyCodec Codec
if newCodec := NewCodec(opts.Codec); newCodec != nil {
bodyCodec = newCodec()
} else if opts.NewCodec != nil {
bodyCodec = opts.NewCodec()
}
var headerEncoder Encoder
if newEncoder := NewHeaderEncoder(opts.HeaderEncoder); newEncoder != nil {
headerEncoder = newEncoder()
} else if opts.NewHeaderEncoder != nil {
headerEncoder = opts.NewHeaderEncoder()
}
return NewClientCodec(bodyCodec, headerEncoder, messages, opts.ClientBufferSize)
})
}