-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnector.go
145 lines (121 loc) · 3.1 KB
/
connector.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
// File TcpConnector
// @Author: yandaren1220@126.com
// @Date: 2016-08-23
package gonetio
import (
"net"
"sync"
)
type ConnectorConfig struct {
sendQueueSize int // send queue size
keepAliveMinTime int // in seconds, the min time between two package read from remote, valid only when the value is positive
filterChain *IoFilterChain // filter chain
}
type TcpConnector struct {
conn *Tcpcon // raw connection
connName string // connection name
url string // connection url
waitGroup *sync.WaitGroup // wait group
config *ConnectorConfig // config
filterChain *IoFilterChain // filter chain
}
// new a connctor instance
func NewConnector(name string, maxSendQueueSize int, keepAliveTimeDuration int) *TcpConnector {
wg := &sync.WaitGroup{}
conf := &ConnectorConfig{
sendQueueSize: maxSendQueueSize,
keepAliveMinTime: keepAliveTimeDuration,
filterChain: NewIoFilterChain(nil),
}
return &TcpConnector{
conn: nil,
connName: name,
url: "",
waitGroup: wg,
config: conf,
filterChain: conf.filterChain,
}
}
// get tcp
func (this *TcpConnector) GetCon() *Tcpcon {
return this.conn
}
// write data
func (this *TcpConnector) Write(obj BaseObject) bool {
if this.conn != nil && !this.conn.IsShutdown() {
this.conn.Write(obj)
return true
}
return false
}
// get iofilter chain
func (this *TcpConnector) GetIoFilterChain() *IoFilterChain {
return this.filterChain
}
// try connect
func (this *TcpConnector) AsyncConnect(url string) {
if this.IsShutdown() {
return
}
this.waitGroup.Add(1)
go this.doConnectTask(url)
}
// do connect task
func (this *TcpConnector) doConnectTask(url string) {
defer func() {
this.waitGroup.Done()
}()
if this.tryConnect(url) {
this.start()
} else {
if !this.IsShutdown() {
this.conn.ioFilterChain.FireConnClosed()
}
}
}
// try connect to the server
func (this *TcpConnector) tryConnect(url string) bool {
LogInfo("try connect to url[%s].", url)
this.url = url
this.conn = NewConn(nil, this.config.sendQueueSize, this.waitGroup, this.config.keepAliveMinTime)
this.conn.SetIoFilterChain(this.config.filterChain)
addr, err := net.ResolveTCPAddr("tcp", url)
if err != nil {
LogError("Connection[%s] resolve tcpaddr[%s] failed, error:%s.", this.connName, url, err.Error())
return false
}
this.conn.rawConn, err = net.DialTCP("tcp", nil, addr)
if err != nil {
LogError("Connection[%s] connect to url[%s] failed, error:%s.", this.connName, url, err.Error())
return false
}
return true
}
// start the connector
func (this *TcpConnector) start() bool {
if this.conn != nil {
this.conn.Start()
return true
}
return false
}
// stop the connector
func (this *TcpConnector) Stop() {
if this.conn != nil {
this.conn.ShutDown()
}
}
// is the connector connected
func (this *TcpConnector) IsConnected() bool {
if this.conn != nil {
return this.conn.IsConnected()
}
return false
}
// is the connector shudown
func (this *TcpConnector) IsShutdown() bool {
if this.conn != nil {
return this.conn.IsShutdown()
}
return false
}