-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_server.go
188 lines (157 loc) · 4.08 KB
/
io_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
package io
import (
"context"
"fmt"
"github.com/injoyai/base/maps"
"sync/atomic"
"time"
)
func RunServer(newListen ListenFunc, options ...OptionServer) error {
s, err := NewServer(newListen, options...)
if err != nil {
return err
}
return s.Run()
}
func NewServer(newListen ListenFunc, options ...OptionServer) (*Server, error) {
return NewServerWithContext(context.Background(), newListen, options...)
}
func NewServerWithContext(ctx context.Context, newListen func() (Listener, error), options ...OptionServer) (*Server, error) {
//连接listener
listener, err := newListen()
if err != nil {
return nil, err
}
key := fmt.Sprintf("%p", listener)
//新建实例
s := &Server{
Key: &Key{key: key},
Logger: defaultLogger(),
ICloser: NewICloserWithContext(ctx, listener),
ClientManage: NewClientManage(ctx, key),
tag: maps.NewSafe(),
listener: listener,
}
s.ICloser.Logger = s.Logger
s.ClientManage.Logger = s.Logger
//开启基础信息打印
s.Logger.Debug()
//设置关闭函数
s.ICloser.SetCloseFunc(func(ctx context.Context, msg Message) {
//关闭listener
s.listener.Close()
//关闭已连接的客户端,关闭listener后,客户端还能正常通讯
s.ClientManage.CloseClientAll()
})
//预设服务处理
s.SetOptions(options...)
return s, nil
}
// Server 服务端
type Server struct {
*Key
*ICloser
*ClientManage
Logger *logger
tag *maps.Safe //tag
listener Listener //listener
running uint32 //是否在运行
startTime time.Time //运行时间
closeTime time.Time //关闭时间
}
//================================Nature================================
func (this *Server) Debug(b ...bool) {
this.Logger.Debug(b...)
this.ClientManage.Logger.Debug(b...)
}
func (this *Server) Tag() *maps.Safe {
return this.tag
}
func (this *Server) SetTag(key, value interface{}) *Server {
this.Tag().Set(key, value)
return this
}
func (this *Server) GetTag(key interface{}) (interface{}, bool) {
return this.Tag().Get(key)
}
func (this *Server) StartTime() time.Time {
return this.startTime
}
func (this *Server) CloseTime() time.Time {
return this.closeTime
}
//================================SetFunc================================
// SetOptions 设置选项
func (this *Server) SetOptions(options ...OptionServer) *Server {
for _, v := range options {
v(this)
}
return this
}
func (this *Server) Listener() Listener {
return this.listener
}
// Timer 定时执行
func (this *Server) Timer(interval time.Duration, do OptionServer) {
go this.ICloser.Timer(interval, func() error {
do(this)
return nil
})
}
func (this *Server) Close() error {
return this.ICloser.Close()
}
func (this *Server) SetCloseFunc(fn func(c *Client, msg Message)) *Server {
this.ClientManage.SetCloseFunc(fn)
return this
}
// Swap 和一个IO交换数据
func (this *Server) Swap(i ReadWriteCloser) *Server {
c := NewClient(i)
c.SetReadWithAll()
return this.SwapClient(c)
}
// SwapClient 和一个客户端交换数据
func (this *Server) SwapClient(c *Client) *Server {
this.SetDealWithWriter(c)
c.SetDealWithWriter(this)
go c.Run()
return this
}
//================================RunTime================================
// Running 是否在运行
func (this *Server) Running() bool {
return atomic.LoadUint32(&this.running) == 1
}
// Run 运行(监听)
func (this *Server) Run() error {
//判断是否在运行,防止重复运行
if atomic.SwapUint32(&this.running, 1) == 1 {
return nil
}
//结束执行,修改运行状态和时间
defer func() {
atomic.StoreUint32(&this.running, 0)
this.closeTime = time.Now()
}()
this.startTime = time.Now()
this.Logger.Infof("[%s] 开启服务成功...", this.GetKey())
//执行监听连接
for {
select {
case <-this.Done():
return this.Err()
default:
}
c, key, err := this.listener.Accept()
if err != nil {
this.CloseWithErr(err)
return err
}
//新建客户端,并配置
x := NewClientWithContext(this.Ctx(), c).SetKey(key)
x.Tag().Set("address", key)
this.Logger.Infof("[%s] 新的客户端连接...", key)
this.ClientManage.SetClient(x)
}
}