/
session.go
54 lines (43 loc) · 1.08 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
package road
import (
"github.com/lixianmin/got/loom"
"github.com/lixianmin/logo"
"github.com/lixianmin/road/epoll"
"net"
"runtime"
"sync/atomic"
)
/********************************************************************
created: 2022-04-07
author: lixianmin
Copyright (C) - All Rights Reserved
*********************************************************************/
type Session interface {
Push(route string, v interface{}) error
Kick() error
OnHandShaken(handler func())
OnClosed(handler func())
Id() int64
RemoteAddr() net.Addr
Attachment() *Attachment
}
type sessionWrapper struct {
*sessionImpl
}
func NewSession(app *App, conn epoll.PlayerConn) Session {
var id = atomic.AddInt64(&globalIdGenerator, 1)
var my = &sessionWrapper{&sessionImpl{
app: app,
id: id,
conn: conn,
attachment: &Attachment{},
sender: app.getSender(id),
}}
logo.Info("create session(%d)", my.id)
loom.Go(my.goSessionLoop)
// 参考: https://zhuanlan.zhihu.com/p/76504936
runtime.SetFinalizer(my, func(w *sessionWrapper) {
_ = w.Close()
})
return my
}