-
Notifications
You must be signed in to change notification settings - Fork 3
/
type_channel_base.go
196 lines (161 loc) · 3.59 KB
/
type_channel_base.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
package pushKit
import (
"github.com/richelieu-yang/chimera/v3/src/concurrency/mutexKit"
"github.com/richelieu-yang/chimera/v3/src/time/timeKit"
"time"
)
var _ Channel = &BaseChannel{}
// BaseChannel
/*
!!!:
(1) 此类型实现了部分 Channel 接口,
(2) 此类型的子类应当实现 Channel 接口(主要是: Push()、Close()、bind、unbind),不能覆盖父类的方法.
(由于unbind可能涉及Close(),因此只能在子类中实现)
*/
type BaseChannel struct {
mutexKit.RWMutex
CloseCh chan string
// ClientIP 可能是error string(获取失败的情况下).
ClientIP string
// Type Channel 的类型.
Type string
Id string
Bsid string
User string
Group string
Data interface{}
Closed bool
Listeners Listeners
PongInterval time.Duration
Interval *timeKit.Interval
}
// IsClosed (读锁)
func (channel *BaseChannel) IsClosed() (rst bool) {
/* 读锁 */
channel.RLockFunc(func() {
rst = channel.Closed
})
return
}
// SetClosed (写锁)
/*
PS: 返回值如果为true,应当调用 Listeners.OnClose().
@return true: 设置成功
false: 设置失败(因为已经被设置关闭)
*/
func (channel *BaseChannel) SetClosed() (flag bool) {
if channel.Closed {
return
}
/* 写锁 */
channel.LockFunc(func() {
if channel.Closed {
return
}
channel.Closed = true
flag = true
})
return
}
// GetCloseCh
/*
PS: 只有 调用SetClosed() && 返回值为true 的情况下,才能向通道中写数据.
*/
func (channel *BaseChannel) GetCloseCh() chan string {
return channel.CloseCh
}
func (channel *BaseChannel) GetClientIP() string {
return channel.ClientIP
}
func (channel *BaseChannel) GetType() string {
return channel.Type
}
func (channel *BaseChannel) GetId() (id string) {
// 无需加解锁,因为: id是不变的
return channel.Id
}
func (channel *BaseChannel) GetBsid() (bsid string) {
/* 读锁 */
channel.RLockFunc(func() {
bsid = channel.Bsid
})
return
}
func (channel *BaseChannel) SetBsid(bsid string) {
/* 写锁 */
channel.LockFunc(func() {
channel.Bsid = bsid
})
}
func (channel *BaseChannel) ClearBsid() {
channel.SetBsid("")
}
func (channel *BaseChannel) GetUser() (user string) {
/* 读锁 */
channel.RLockFunc(func() {
user = channel.User
})
return
}
func (channel *BaseChannel) SetUser(user string) {
/* 写锁 */
channel.LockFunc(func() {
channel.User = user
})
}
func (channel *BaseChannel) ClearUser() {
channel.SetUser("")
}
func (channel *BaseChannel) GetGroup() (group string) {
/* 读锁 */
channel.RLockFunc(func() {
group = channel.Group
})
return
}
func (channel *BaseChannel) SetGroup(group string) {
/* 写锁 */
channel.LockFunc(func() {
channel.Group = group
})
}
func (channel *BaseChannel) ClearGroup() {
channel.SetGroup("")
}
func (channel *BaseChannel) GetData() (data interface{}) {
/* 读锁 */
channel.RLockFunc(func() {
data = channel.Data
})
return
}
func (channel *BaseChannel) SetData(data interface{}) {
/* 写锁 */
channel.LockFunc(func() {
channel.Data = data
})
}
func (channel *BaseChannel) ClearData() {
channel.SetData(nil)
}
// Equals 根据 id 判断是否相等.
func (channel *BaseChannel) Equals(c Channel) bool {
if c == nil {
return false
}
return channel.GetId() == c.GetId()
}
func (channel *BaseChannel) BindGroup(group string) {
BindGroup(channel, group)
}
func (channel *BaseChannel) BindUser(user string) {
BindUser(channel, user)
}
func (channel *BaseChannel) BindBsid(bsid string) {
BindBsid(channel, bsid)
}
func (channel *BaseChannel) Dispose() {
// 停止主动pong
channel.Interval.Stop()
channel.Interval = nil
}