/
connector.go
190 lines (128 loc) · 3.21 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package kcp
import (
"net"
"sync"
"time"
"github.com/davyxu/cellnet"
"github.com/davyxu/cellnet/peer"
"github.com/xtaci/kcp-go/v5"
)
type kcpConnector struct {
peer.SessionManager
peer.CorePeerProperty
peer.CoreContextSet
peer.CoreRunningTag
peer.CoreProcBundle
peer.CoreTCPSocketOption
defaultSes *kcpSession
tryConnTimes int // 尝试连接次数
sesEndSignal sync.WaitGroup
reconDur time.Duration
}
func (self *kcpConnector) Start() cellnet.Peer {
self.WaitStopFinished()
if self.IsRunning() {
return self
}
go self.connect(self.Address())
return self
}
func (self *kcpConnector) Session() cellnet.Session {
return self.defaultSes
}
func (self *kcpConnector) SetSessionManager(raw interface{}) {
self.SessionManager = raw.(peer.SessionManager)
}
func (self *kcpConnector) Stop() {
if !self.IsRunning() {
return
}
if self.IsStopping() {
return
}
self.StartStopping()
// 通知发送关闭
self.defaultSes.Close()
// 等待线程结束
self.WaitStopFinished()
}
func (self *kcpConnector) ReconnectDuration() time.Duration {
return self.reconDur
}
func (self *kcpConnector) SetReconnectDuration(v time.Duration) {
self.reconDur = v
}
func (self *kcpConnector) Port() int {
conn := self.defaultSes.Conn()
if conn == nil {
return 0
}
return conn.LocalAddr().(*net.UDPAddr).Port
}
const reportConnectFailedLimitTimes = 3
// 连接器,传入连接地址和发送封包次数
func (self *kcpConnector) connect(address string) {
self.SetRunning(true)
for {
self.tryConnTimes++
// 尝试用Socket连接地址
conn, err := kcp.DialWithOptions(address, blockCrypto, 10, 3)
self.defaultSes.setConn(conn)
// 发生错误时退出
if err != nil {
if self.tryConnTimes <= reportConnectFailedLimitTimes {
log.Errorf("#kcp.connect failed(%s) %v", self.Name(), err.Error())
if self.tryConnTimes == reportConnectFailedLimitTimes {
log.Errorf("(%s) continue reconnecting, but mute log", self.Name())
}
}
// 没重连就退出
if self.ReconnectDuration() == 0 || self.IsStopping() {
self.ProcEvent(&cellnet.RecvMsgEvent{
Ses: self.defaultSes,
Msg: &cellnet.SessionConnectError{},
})
break
}
// 有重连就等待
time.Sleep(self.ReconnectDuration())
// 继续连接
continue
}
self.sesEndSignal.Add(1)
self.ApplySocketOption(conn)
self.defaultSes.Start()
self.tryConnTimes = 0
self.ProcEvent(&cellnet.RecvMsgEvent{Ses: self.defaultSes, Msg: &cellnet.SessionConnected{}})
self.sesEndSignal.Wait()
self.defaultSes.setConn(nil)
// 没重连就退出/主动退出
if self.IsStopping() || self.ReconnectDuration() == 0 {
break
}
// 有重连就等待
time.Sleep(self.ReconnectDuration())
// 继续连接
continue
}
self.SetRunning(false)
self.EndStopping()
}
func (self *kcpConnector) IsReady() bool {
return self.SessionCount() != 0
}
func (self *kcpConnector) TypeName() string {
return "kcp.Connector"
}
func init() {
peer.RegisterPeerCreator(func() cellnet.Peer {
self := &kcpConnector{
SessionManager: new(peer.CoreSessionManager),
}
self.defaultSes = newSession(nil, self, func() {
self.sesEndSignal.Done()
})
self.CoreTCPSocketOption.Init()
return self
})
}