/
socket_client.go
69 lines (61 loc) · 2.18 KB
/
socket_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
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* rpc/socket_client.go *
* *
* hprose socket client for Go. *
* *
* LastModified: Jan 7, 2017 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
package rpc
import (
"crypto/tls"
"net"
"time"
)
type socketTransport interface {
IdleTimeout() time.Duration
SetIdleTimeout(timeout time.Duration)
MaxPoolSize() int
SetMaxPoolSize(size int)
setCreateConn(createConn func() (net.Conn, error))
sendAndReceive(data []byte, context *ClientContext) ([]byte, error)
close()
}
// SocketClient is base struct for TCPClient and UnixClient
type SocketClient struct {
BaseClient
socketTransport
ReadBuffer int
WriteBuffer int
TLSConfig *tls.Config
}
func (client *SocketClient) initSocketClient() {
client.InitBaseClient()
client.socketTransport = newHalfDuplexSocketTransport()
client.ReadBuffer = 0
client.WriteBuffer = 0
client.TLSConfig = nil
client.SendAndReceive = client.sendAndReceive
}
// TLSClientConfig returns the tls.Config in hprose client
func (client *SocketClient) TLSClientConfig() *tls.Config {
return client.TLSConfig
}
// SetTLSClientConfig sets the tls.Config
func (client *SocketClient) SetTLSClientConfig(config *tls.Config) {
client.TLSConfig = config
}
// Close the client
func (client *SocketClient) Close() {
client.socketTransport.close()
}