-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
54 lines (46 loc) · 953 Bytes
/
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 gnet
import (
"github.com/flylib/gonet"
"github.com/panjf2000/gnet/v2"
"net"
"reflect"
)
// Socket会话
type session struct {
//核心会话标志
gonet.SessionCommon
//存储功能
//累计收消息总数
recvCount uint64
//raw conn
conn gnet.Conn
//缓存数据,用于解决粘包问题
cache []byte
}
// 新会话
func newSession(c *gonet.Context, conn gnet.Conn) *session {
is := c.GetIdleSession()
ns := is.(*session)
ns.conn = conn
ns.WithContext(c)
ns.UpdateID(uint64(conn.Fd()))
c.GetEventHandler().OnConnect(ns)
return ns
}
func (s *session) RemoteAddr() net.Addr {
return s.conn.RemoteAddr()
}
func (s *session) Send(msgID uint32, msg any) error {
buf, err := s.GetContext().Package(s, msgID, msg)
if err != nil {
return err
}
_, err = s.conn.Write(buf)
return err
}
func (s *session) Close() error {
return s.conn.Close()
}
func SessionType() reflect.Type {
return reflect.TypeOf(session{})
}