Repository navigation
Expand file tree
/
Copy pathserver.go
More file actions
95 lines (89 loc) · 1.96 KB
/
Copy pathserver.go
File metadata and controls
95 lines (89 loc) · 1.96 KB
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
package seeker
import (
"fmt"
"net"
"runtime"
"sync"
"time"
)
type Config struct {
Debug bool
// 定义服务类型(tcp)
ProtocolType string
// 服务端监听的ip和端口号
BindAddress string
// 接收超时时间
AcceptTimeout time.Duration
// 连接连续错误多少次退出
AcceptErrorLimit uint32
// 设置发送消息缓存区大小
PacketSendChanLimit uint32
// 设置发送消息缓存区大小
PacketReceiveChanLimit uint32
}
type Server struct {
// 服务配置选项
config *Config
// 服务消息事件回调处理
callback ConnCallback
// 自定义的数据包协议
protocol Protocol
// 通知所有协成退出的channel
exitChan chan struct{}
// 等待协成处理
waitGroup *sync.WaitGroup
}
// 初始化一个服务
func NewServer(config *Config, callback ConnCallback, protocol Protocol) *Server {
return &Server{
config: config,
callback: callback,
protocol: protocol,
exitChan: make(chan struct{}),
waitGroup: &sync.WaitGroup{},
}
}
// 启动服务
func (svr *Server) Start() {
// 启用多核处理
runtime.GOMAXPROCS(runtime.NumCPU())
tcpAddr, err := net.ResolveTCPAddr(svr.config.ProtocolType, svr.config.BindAddress)
if err != nil {
fmt.Println("TCP地址解析失败 : " + err.Error())
}
listener, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
fmt.Println("监听端口失败: %s" + err.Error())
}
svr.waitGroup.Add(1)
defer func() {
listener.Close()
svr.waitGroup.Done()
}()
for {
select {
case <-svr.exitChan:
fmt.Println("服务退出。")
return
default:
}
// 设置超时时间
listener.SetDeadline(time.Now().Add(svr.config.AcceptTimeout))
conn, err := listener.AcceptTCP()
if err != nil {
continue
}
svr.waitGroup.Add(1)
// 处理客户端连接
go func() {
NewConn(conn, svr).Handle()
svr.waitGroup.Done()
}()
}
}
// 停止服务
func (svr *Server) Stop() {
close(svr.exitChan)
// 等待尚未结束的协成处理完
svr.waitGroup.Wait()
}