/
api_event_bus.go
187 lines (155 loc) · 5.09 KB
/
api_event_bus.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
package proxy
import (
"github.com/LilyPad/GoLilyPad/packet"
"github.com/LilyPad/GoLilyPad/server/proxy/api"
)
type eventBus struct {
sessionOpen []api.EventSessionHandler
sessionLogin []api.EventSessionHandler
sessionClose []api.EventSessionHandler
sessionState []api.EventSessionHandler
sessionRedirect []api.EventSessionHandler
sessionPacket [][][][][]api.EventSessionHandler
emptySessionPacket *eventSessionPacket
}
func NewEventBus() *eventBus {
this := new(eventBus)
this.sessionOpen = make([]api.EventSessionHandler, 0)
this.sessionLogin = make([]api.EventSessionHandler, 0)
this.sessionClose = make([]api.EventSessionHandler, 0)
this.sessionState = make([]api.EventSessionHandler, 0)
this.sessionRedirect = make([]api.EventSessionHandler, 0)
this.sessionPacket = make([][][][][]api.EventSessionHandler, api.PacketStageMax)
for i := range this.sessionPacket {
this.sessionPacket[i] = make([][][][]api.EventSessionHandler, api.PacketSubjectMax)
for j := range this.sessionPacket[i] {
this.sessionPacket[i][j] = make([][][]api.EventSessionHandler, api.PacketDirectionMax)
for k := range this.sessionPacket[i][j] {
this.sessionPacket[i][j][k] = make([][]api.EventSessionHandler, api.SessionStateMax)
for l := range this.sessionPacket[i][j][k] {
this.sessionPacket[i][j][k][l] = make([]api.EventSessionHandler, 0)
}
}
}
}
this.emptySessionPacket = new(eventSessionPacket)
return this
}
func (this *eventBus) fireEventSession(handlers []api.EventSessionHandler, event api.EventSession) {
for _, handler := range handlers {
handler(event)
}
}
func (this *eventBus) fireEventSessionPacket(session *Session, packet *packet.Packet, stage api.PacketStage, subject api.PacketSubject, direction api.PacketDirection) (event *eventSessionPacket) {
handlers := this.sessionPacket[stage][subject][direction][session.state]
if len(handlers) == 0 {
event = this.emptySessionPacket
return
}
event = &eventSessionPacket{
eventSessionCancellable: eventSessionCancellable{eventSession: eventSession{session}},
packet: *packet,
packetStage: stage,
packetSubject: subject,
packetDirection: direction,
}
this.fireEventSession(handlers, event)
*packet = event.packet
return
}
func (this *eventBus) HandleSessionOpen(handler api.EventSessionHandler) {
this.sessionOpen = append(this.sessionOpen, handler)
}
func (this *eventBus) HandleSessionLogin(handler api.EventSessionHandler) {
this.sessionLogin = append(this.sessionLogin, handler)
}
func (this *eventBus) HandleSessionClose(handler api.EventSessionHandler) {
this.sessionClose = append(this.sessionClose, handler)
}
func (this *eventBus) HandleSessionState(handler api.EventSessionHandler) {
this.sessionState = append(this.sessionState, handler)
}
func (this *eventBus) HandleSessionRedirect(handler api.EventSessionHandler) {
this.sessionRedirect = append(this.sessionRedirect, handler)
}
func (this *eventBus) HandleSessionPacket(handler api.EventSessionHandler, stage api.PacketStage, subject api.PacketSubject, direction api.PacketDirection, states ...api.SessionState) {
if len(states) == 0 {
states = api.SessionStateAll
}
for _, state := range states {
this.sessionPacket[stage][subject][direction][state] = append(this.sessionPacket[stage][subject][direction][state], handler)
}
}
type eventSession struct {
session *Session
}
func (this *eventSession) Session() api.Session {
return this.session.apiSession
}
type eventSessionCancellable struct {
eventSession
cancelled bool
}
func (this *eventSessionCancellable) SetCancelled(cancelled bool) {
this.cancelled = cancelled
}
func (this *eventSessionCancellable) IsCancelled() bool {
return this.cancelled
}
type eventSessionOpen struct {
eventSessionCancellable
}
type eventSessionLogin struct {
eventSessionCancellable
reason string
}
func (this *eventSessionLogin) SetReason(reason string) {
this.reason = reason
}
func (this *eventSessionLogin) GetReason() string {
return this.reason
}
type eventSessionClose struct {
eventSession
}
type eventSessionState struct {
eventSession
state api.SessionState
}
func (this *eventSessionState) State() api.SessionState {
return this.state
}
type eventSessionRedirect struct {
eventSessionCancellable
init bool
serverName string
serverAddr string
}
func (this *eventSessionRedirect) Init() bool {
return this.init
}
func (this *eventSessionRedirect) ServerName() string {
return this.serverName
}
func (this *eventSessionRedirect) ServerAddr() string {
return this.serverAddr
}
type eventSessionPacket struct {
eventSessionCancellable
packet packet.Packet
packetStage api.PacketStage
packetSubject api.PacketSubject
packetDirection api.PacketDirection
}
func (this *eventSessionPacket) Packet() packet.Packet {
return this.packet
}
func (this *eventSessionPacket) PacketSubject() api.PacketSubject {
return this.packetSubject
}
func (this *eventSessionPacket) PacketDirection() api.PacketDirection {
return this.packetDirection
}
func (this *eventSessionPacket) SetPacket(packet packet.Packet) {
this.packet = packet
}