-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
378 lines (329 loc) · 9.49 KB
/
server.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package util
import (
"crypto/md5"
"errors"
"fmt"
"io"
"net"
"os"
"runtime/debug"
"strings"
"sync"
"time"
utilString "github.com/linuxkungfu/go-util/string"
logger "github.com/sirupsen/logrus"
"github.com/twmb/murmur3"
)
var (
ConnId int = 0
serverId string = ""
serverName string = ""
timezoneOffsetStr string = ""
timezoneOffset int = 0
)
// 需要重新设计
type NetworkConn struct {
id string
ConnProto string
LocalIp string
LocalPort int
LocalAddr net.Addr
RemoteIp string
RemotePort int
RemoteAddr net.Addr
ReadBytes int64
WriteBytes int64
FailedCount int64
IsBind bool
conn interface{}
heartbeatTS time.Time
heartbeatInterval time.Duration
owner interface{}
data interface{}
}
type NetworkConnOwner interface {
OnConnDead(conn *NetworkConn)
}
type ServerListener interface {
OnAcceptConn(conn *NetworkConn)
OnReceiveData(data []byte, conn *NetworkConn)
OnClosed(ls interface{})
}
type NetworkServer struct {
id string
running bool
listenAddr NetworkAddr
addrs sync.Map
Listener ServerListener
listenServer interface{}
}
// CreateServerId 生成服务器id hostname+ip+port
func CreateServerId(idString string) string {
hostname, _ := os.Hostname()
serverId = fmt.Sprintf("%d", murmur3.Sum64([]byte(fmt.Sprintf("%s_%s", hostname, idString))))
return serverId
}
func generateId() int {
ConnId++
return ConnId
}
func generateStrId() string {
return fmt.Sprintf("%d", generateId())
}
func (conn NetworkConn) Id() string {
return conn.id
}
func (conn *NetworkConn) UpdateHeatbeat(ts time.Time) {
conn.heartbeatTS = ts
}
func (conn *NetworkConn) SetHeatbeatInterval(dur time.Duration) {
conn.heartbeatInterval = dur
}
func (conn *NetworkConn) IsDead(checkTime time.Time) bool {
// logger.Debugf("[networkConn][%s]NetworkConn IsDead tm:%s interval:%f", conn.id, conn.heartbeatTS.String(), conn.heartbeatInterval.Seconds())
return conn.heartbeatTS.Add(conn.heartbeatInterval).Before(checkTime)
}
func (conn *NetworkConn) HeartbeatInterval() time.Duration {
return time.Since(conn.heartbeatTS)
}
func (conn *NetworkConn) OnSendData(data []byte) {
conn.Write(data)
}
func (conn *NetworkConn) OnClosed(interface{}) {
logger.Infof("[networkConn][%s]OnClosed", conn.id)
conn.Close()
}
func (conn *NetworkConn) Close() {
logger.Infof("[networkConn][%s]local addr %s:%d remote addr %s:%d close read bytes:%s write bytes:%s, failed count:%d", conn.id, conn.LocalIp, conn.LocalPort, conn.RemoteIp, conn.RemotePort, utilString.FormatFileSize(conn.ReadBytes), utilString.FormatFileSize(conn.WriteBytes), conn.FailedCount)
if strings.ToLower(conn.ConnProto) == "udp" {
conn.closeUdpConn()
} else if strings.ToLower(conn.ConnProto) == "tcp" {
conn.closeTcpConn()
}
conn.LocalAddr = nil
conn.RemoteAddr = nil
conn.conn = nil
}
func (conn *NetworkConn) RegisterConnOwner(owner interface{}) {
conn.owner = owner
}
func (conn *NetworkConn) closeUdpConn() {
if conn.conn != nil {
conn.conn = nil
}
}
func (conn *NetworkConn) closeTcpConn() {
conn.conn.(*net.TCPConn).Close()
}
func (conn *NetworkConn) LocalAddrStr() string {
return fmt.Sprintf("%s:%d", conn.LocalIp, conn.LocalPort)
}
func (conn *NetworkConn) RemoteAddrStr() string {
return fmt.Sprintf("%s:%d", conn.RemoteIp, conn.RemotePort)
}
func (conn *NetworkConn) Write(data []byte) bool {
switch conn.ConnProto {
case "udp":
return conn.WriteUdp(data)
case "tcp":
return conn.WriteTcp(data)
default:
logger.Warnf("[networkConn][%s]NetworkConn Write unknown proto:%v", conn.id, conn.ConnProto)
return false
}
}
func (conn *NetworkConn) WriteUdp(data []byte) bool {
if conn.conn != nil {
bytes, err := conn.conn.(*net.UDPConn).WriteTo(data, conn.RemoteAddr)
if err != nil {
logger.Warnf("[networkConn][%s]WriteUdp addr:%s, error:%s", conn.id, conn.RemoteAddr.String(), err.Error())
conn.FailedCount++
return false
}
conn.WriteBytes += int64(bytes)
return err == nil
}
return false
}
func (conn *NetworkConn) WriteTcp(data []byte) bool {
if conn.conn != nil {
bytes, err := conn.conn.(*net.TCPConn).Write(data)
if err != nil {
logger.Warnf("[networkConn][%s]WriteTcp addr:%s, error:%s", conn.id, conn.RemoteAddr.String(), err.Error())
conn.FailedCount++
return false
}
conn.WriteBytes += int64(bytes)
return err == nil
}
return false
}
func (conn *NetworkConn) Dead() {
if conn.owner != nil {
conn.owner.(NetworkConnOwner).OnConnDead(conn)
}
}
func (conn *NetworkConn) SetData(intf interface{}) {
conn.data = intf
}
func (conn *NetworkConn) GetData() interface{} {
return conn.data
}
func (srv *NetworkServer) Start(addr NetworkAddr) {
srv.listenAddr = addr
switch strings.ToLower(addr.Protocol) {
case "udp":
srv.startUdp()
case "":
srv.startTcp()
default:
logger.Warnf("[networkServer][%s]NetworkServer Start unknown protocol:%v", srv.id, addr.Protocol)
}
}
func (srv *NetworkServer) Id() string {
return srv.id
}
func (srv *NetworkServer) IsRunning() bool {
return srv.running
}
func (srv *NetworkServer) SetId(id string) {
srv.id = id
}
func (srv *NetworkServer) startUdp() {
srv.running = true
ip := "0.0.0.0"
addr, ex := net.ResolveUDPAddr("udp", fmt.Sprintf("%v:%v", ip, srv.listenAddr.Port))
if ex != nil {
// panic(fmt.Sprintf("解析IP地址异常:%s", e))
logger.Warnf("[networkServer][%s]NetworkServer resolve raw udp address:%s error:%v", srv.id, fmt.Sprintf("%s:%d", ip, srv.listenAddr.Port), ex)
return
}
conn, err := net.ListenUDP("udp", addr)
if err != nil {
logger.Warnf("[networkServer][%s]NetworkServer listen udp:%s failed:%v", srv.id, fmt.Sprintf("%s:%d", ip, srv.listenAddr.Port), err)
return
}
logger.Infof("[networkServer][%s]NetworkServer is started at:%s", srv.id, fmt.Sprintf("%s://%s:%d", srv.listenAddr.Protocol, ip, srv.listenAddr.Port))
srv.listenServer = conn
go srv.handleConn(conn)
}
func (srv *NetworkServer) startTcp() {
logger.Warnf("[networkServer][%s]NetworkServer startTcp not implemented", srv.id)
}
func (srv *NetworkServer) Stop() {
srv.running = false
switch strings.ToLower(srv.listenAddr.Protocol) {
case "udp":
srv.stopUdp()
case "":
srv.stopTcp()
default:
logger.Warnf("[networkServer][%s]NetworkServer Start unknown protocol:%v", srv.id, srv.listenAddr.Protocol)
}
srv.addrs = sync.Map{}
srv.Listener = nil
}
func (srv *NetworkServer) stopUdp() {
if srv.listenServer != nil {
srv.listenServer.(*net.UDPConn).Close()
srv.listenServer = nil
}
}
func (srv *NetworkServer) stopTcp() {
}
func (srv *NetworkServer) handleConn(conn *net.UDPConn) {
defer func() {
conn.Close()
if r := recover(); r != nil {
logger.Errorf("[networkServer][%s]NetworkServer handleConn panic:%v", srv.id, r)
debug.PrintStack()
srv.Start(srv.listenAddr)
}
}()
// srv.conn = conn
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
for srv.running {
data := make([]byte, 1500)
pos, addr, err := conn.ReadFromUDP(data)
if !srv.running {
break
}
if err != nil {
if e, ok := err.(net.Error); ok && e.Timeout() {
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
continue
}
if srv.running {
if errors.Is(err, net.ErrClosed) {
logger.Infof("[networkServer][%s]NetworkServer handleConn addr:%s has been closed", srv.id, srv.listenAddr.String())
} else {
logger.Warnf("[networkServer][%s]NetworkServer handleConn addr:%s err:%s", srv.id, srv.listenAddr.String(), err.Error())
}
}
break
}
connIf, ok := srv.addrs.Load(addr.String())
var networkConn *NetworkConn
if !ok {
remoteAddr := &NetworkAddr{}
remoteAddr.ParseString(fmt.Sprintf("udp://%s", conn.LocalAddr().String()))
networkConn = &NetworkConn{
id: generateStrId(),
ConnProto: "udp",
LocalIp: srv.listenAddr.Ip,
LocalPort: srv.listenAddr.Port,
LocalAddr: conn.LocalAddr(),
RemoteIp: addr.IP.String(),
RemotePort: addr.Port,
RemoteAddr: addr,
ReadBytes: int64(pos),
IsBind: false,
conn: conn,
}
logger.Infof("[networkServer][%s]accept a new connect, id: %s, LocalAddr: %s, RemoteAddr: %s", srv.id, networkConn.id, networkConn.LocalAddr.String(), networkConn.RemoteAddr.String())
if srv.Listener != nil {
srv.Listener.OnAcceptConn(networkConn)
}
srv.addrs.Store(addr.String(), networkConn)
} else {
networkConn = connIf.(*NetworkConn)
}
networkConn.ReadBytes += int64(pos)
if srv.Listener != nil {
srv.Listener.OnReceiveData(data[:pos], networkConn)
}
}
logger.Infof("[networkServer][%s]NetworkServer:%s exit", srv.id, srv.listenAddr.String())
}
func (srv *NetworkServer) CloseConn(networkConn *NetworkConn) {
srv.RemoveConn(networkConn)
networkConn.Close()
}
func (srv *NetworkServer) RemoveConn(networkConn *NetworkConn) bool {
if networkConn.RemoteAddr != nil {
srv.addrs.Delete(networkConn.RemoteAddr.String())
return true
}
return false
}
func SetServerInfo(name string) {
serverName = name
cur := time.Now()
_, timezoneOffset = cur.Local().Zone()
timezoneOffset = timezoneOffset / 3600
if timezoneOffset > 0 {
timezoneOffsetStr = fmt.Sprintf("T+%d", timezoneOffset)
} else {
timezoneOffsetStr = fmt.Sprintf("T%d", timezoneOffset)
}
}
func GetServerId() string {
return serverId
}
func PasswordPlainToMd5(userId uint64, password string) string {
h := md5.New()
io.WriteString(h, "kungapp")
io.WriteString(h, password)
io.WriteString(h, fmt.Sprintf("%d", userId))
return fmt.Sprintf("%x", h.Sum(nil))
}