-
Notifications
You must be signed in to change notification settings - Fork 28
/
session.go
405 lines (354 loc) · 8.79 KB
/
session.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package session
import (
//"github.com/daniel840829/gameServer2/entity"
. "github.com/daniel840829/gameServer/msg"
"github.com/daniel840829/gameServer/user"
//. "github.com/daniel840829/gameServer/uuid"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/metadata"
"strconv"
"sync"
"time"
)
type MsgChannel struct {
DataCh chan (interface{})
StopSignal chan (struct{})
}
func (m *MsgChannel) Close() {
select {
case <-m.StopSignal:
return
default:
close(m.StopSignal)
}
}
func NewMsgChannel(bufferNumber int32) *MsgChannel {
return &MsgChannel{
DataCh: make(chan (interface{}), bufferNumber),
StopSignal: make(chan (struct{}), 1),
}
}
func NewMsgChannelManager() *MsgChannelManager {
return &MsgChannelManager{
make(map[string]*MsgChannel),
}
}
type MsgChannelManager struct {
c map[string]*MsgChannel
}
func (m *MsgChannelManager) AddMsgChan(name string, bufferNumber int32) bool {
if _, ok := m.c[name]; ok {
return false
}
m.c[name] = NewMsgChannel(bufferNumber)
return true
}
func (m *MsgChannelManager) GetMsgChan(name string) *MsgChannel {
return m.c[name]
}
func (m *MsgChannelManager) CloseMsgChan(name string) {
if ch, ok := m.c[name]; ok {
ch.Close()
delete(m.c, name)
}
}
type sessionManager struct {
Sessions map[int64]*Session
sync.RWMutex
}
func (sm *sessionManager) MakeSession(info *SessionInfo) int64 {
s := NewSession(info)
sm.Lock()
sm.Sessions[s.Info.Uuid] = s
sm.Unlock()
return s.Info.Uuid
}
func (sm *sessionManager) CreateSessionFromAgent(sessionInfo *SessionInfo) *Session {
characterInfo := sessionInfo.UserInfo.OwnCharacter[sessionInfo.UserInfo.UsedCharacter]
log.Debug(characterInfo)
s := sm.Sessions[sm.MakeSession(sessionInfo)]
return s
}
func (sm *sessionManager) GetSession(md metadata.MD) *Session {
mdid := md.Get("session-id")
if len(mdid) == 0 {
return nil
}
id, err := strconv.ParseInt(mdid[0], 10, 64)
if err != nil {
return nil
}
s, ok := sm.Sessions[id]
if !ok {
return s
}
s.RLock()
if s.User != nil {
uname := md.Get("uname")
if len(uname) == 0 {
s.RUnlock()
return nil
} else if s.User.UserInfo.UserName != uname[0] {
s.RUnlock()
return nil
}
}
s.RUnlock()
return s
}
func NewSession(info *SessionInfo) *Session {
s := &Session{
Info: info,
MsgChannelManager: NewMsgChannelManager(),
PlayerInfo: &PlayerInfo{},
}
for i := int32(SessionInfo_NoSession); i <= int32(SessionInfo_GameServerWaitReconnect); i++ {
ss := SessionStateFactory.makeSessionState(s, SessionInfo_SessionState(i))
s.States = append(s.States, ss)
}
s.SetState(int32(SessionInfo_OnStart))
s.State.CreateSession()
s.AddMsgChan("GameFrame", 5)
return s
}
type Session struct {
Info *SessionInfo
State SessionState
SessionKey int64
User *user.User
States []SessionState
Room *Room
sync.RWMutex
PlayerInfo *PlayerInfo
TeamNo int32
InputPool *MsgChannel
InputStamp int64
*MsgChannelManager
}
func (s *Session) GetPlayerInfo() *PlayerInfo {
if s.User == nil {
return nil
}
s.PlayerInfo.UserName = s.User.UserInfo.UserName
s.PlayerInfo.UserId = s.User.UserInfo.Uuid
if s.User.UserInfo.UsedCharacter == int64(0) {
for id, c := range s.User.UserInfo.OwnCharacter {
s.User.UserInfo.UsedCharacter = id
s.PlayerInfo.Character = c
break
}
} else {
s.PlayerInfo.Character = s.User.UserInfo.OwnCharacter[s.User.UserInfo.UsedCharacter]
}
s.PlayerInfo.TeamNo = s.TeamNo
return s.PlayerInfo
}
func (s *Session) SetState(state_index int32) {
s.State = s.States[state_index]
}
type SessionState interface {
SetSession(s *Session) bool
SetStateCode(SessionInfo_SessionState)
GetStateCode() SessionInfo_SessionState
CreateSession() int64
Login(uname string, pswd string) *user.User
Logout() bool
Regist(uname string, pswd string, info ...string) bool
CreateRoom(setting *RoomSetting) bool
EnterRoom(roomId int64) bool
DeleteRoom() bool
ReadyRoom() bool
LeaveRoom() bool
StartRoom() bool
SettingCharacter(*CharacterSetting) bool
SettingRoom() bool
CancelReady() bool
EndRoom() bool
String() string
Lock()
HandleInput(input *Input)
Unlock()
StartGame()
Reconnect()
EndConnection()
WaitReconnect()
End()
}
func (sb *SessionStateBase) SetSession(s *Session) bool {
if sb.Session != nil {
return false
}
sb.Session = s
return true
}
func (sb *SessionStateBase) End() {
log.Debug("origin")
}
func (sb *SessionStateBase) String() string {
return SessionInfo_SessionState_name[int32(sb.StateCode)]
}
func (sb *SessionStateBase) StartGame() {
//TODO
}
func (sb *SessionStateBase) Reconnect() {
}
func (sb *SessionStateBase) WaitReconnect() {
}
func (sb *SessionStateBase) EndConnection() {
}
func (sb *SessionStateBase) SetStateCode(code SessionInfo_SessionState) {
sb.StateCode = code
}
func (sb *SessionStateBase) GetStateCode() SessionInfo_SessionState {
return sb.StateCode
}
func (sb *SessionStateBase) CreateSession() int64 {
return 0
}
func (sb *SessionStateBase) HandleInput(input *Input) {
}
func (sb *SessionStateBase) Login(uname string, pswd string) *user.User {
return nil
}
func (sb *SessionStateBase) Logout() bool {
return false
}
func (sb *SessionStateBase) Regist(uname string, pswd string, info ...string) bool {
return false
}
func (sb *SessionStateBase) CreateRoom(setting *RoomSetting) bool {
return false
}
func (sb *SessionStateBase) EnterRoom(roomId int64) bool {
return false
}
func (sb *SessionStateBase) DeleteRoom() bool {
return false
}
func (sb *SessionStateBase) ReadyRoom() bool {
return false
}
func (sb *SessionStateBase) LeaveRoom() bool {
return false
}
func (sb *SessionStateBase) StartRoom() bool {
return false
}
func (sb *SessionStateBase) SettingCharacter(*CharacterSetting) bool {
return false
}
func (sb *SessionStateBase) SettingRoom() bool {
return false
}
func (sb *SessionStateBase) EndRoom() bool {
return false
}
func (sb *SessionStateBase) CancelReady() bool {
return false
}
type SessionStateBase struct {
StateCode SessionInfo_SessionState
Session *Session
sync.RWMutex
}
type SessionStateGameOnStart struct {
SessionStateBase
}
func (ssgos *SessionStateGameOnStart) StartGame() {
//firsttimeGetGameframe
ssgos.Session.Room.GameStart <- struct{}{}
ssgos.Session.SetState(int32(SessionInfo_Playing))
log.Debug("state code :", int32(ssgos.Session.State.GetStateCode()))
}
type SessionStatePlaying struct {
SessionStateBase
}
func (sb *SessionStatePlaying) HandleInput(input *Input) {
if sb.Session.InputStamp > input.TimeStamp {
log.Debug("input sequence is disorder")
}
sb.Session.InputPool.DataCh <- input
}
func (sb *SessionStatePlaying) WaitReconnect() {
sb.Session.SetState(int32(SessionInfo_GameServerWaitReconnect))
}
func (sb *SessionStatePlaying) EndConnection() {
sb.Session.SetState(int32(SessionInfo_GameOver))
log.Debug("state code :", int32(sb.Session.State.GetStateCode()))
sb.Session.State.End()
}
type SessionStateWaitingReconnection struct {
SessionStateBase
}
func (sswr *SessionStateWaitingReconnection) Waiting() {
go func() {
c := time.After(10 * time.Second)
END:
for {
select {
case <-c:
break END
}
}
log.Warn("Exceed waiting time, end connection!")
sswr.EndConnection()
}()
}
func (sswr *SessionStateWaitingReconnection) EndConnection() {
sswr.Session.SetState(int32(SessionInfo_GameOver))
log.Debug("state code :", int32(sswr.Session.State.GetStateCode()))
sswr.Session.State.End()
}
func (sswr *SessionStateWaitingReconnection) StartGame() {
sswr.Reconnect()
log.Debug("state code :", int32(sswr.Session.State.GetStateCode()))
}
func (sswr *SessionStateWaitingReconnection) Reconnect() {
sswr.Session.SetState(int32(SessionInfo_Playing))
log.Debug("state code :", int32(sswr.Session.State.GetStateCode()))
sswr.Session.Room.Client[sswr.Session] = struct{}{}
}
type SessionStateGameOver struct {
SessionStateBase
}
func (ssgo *SessionStateGameOver) End() {
ssgo.Session.Lock()
if ssgo.Session.Room == nil {
ssgo.Session.Unlock()
return
}
ssgo.Session.Room.PlayerLeave(ssgo.Session)
log.Debug("ssgo.Session.Room", ssgo.Session.Room)
ssgo.Session.Room = nil
ssgo.Session.Unlock()
}
type sessionStateFactory struct {
}
func (sf *sessionStateFactory) makeSessionState(session *Session, state_code SessionInfo_SessionState) SessionState {
var s SessionState
switch state_code {
case SessionInfo_OnStart:
s = &SessionStateGameOnStart{}
case SessionInfo_Playing:
s = &SessionStatePlaying{}
case SessionInfo_GameOver:
s = &SessionStateGameOver{}
case SessionInfo_GameServerWaitReconnect:
s = &SessionStateWaitingReconnection{}
default:
s = &SessionStateBase{}
}
s.Lock()
s.SetSession(session)
s.SetStateCode(state_code)
s.Unlock()
return s
}
var Manager *sessionManager
var SessionStateFactory *sessionStateFactory
func init() {
Manager = &sessionManager{
Sessions: make(map[int64]*Session),
}
SessionStateFactory = &sessionStateFactory{}
}