|
|
|
@ -40,8 +40,10 @@ type (
|
|
|
|
|
session *session.Session // Session
|
|
|
|
|
ngin *engine // engine
|
|
|
|
|
|
|
|
|
|
conn net.Conn // low-level conn fd
|
|
|
|
|
status int32 // 连接状态
|
|
|
|
|
conn net.Conn // low-level conn fd
|
|
|
|
|
packer packet.Packer // 封包、拆包器
|
|
|
|
|
|
|
|
|
|
lastMid uint64 // 最近一次消息ID
|
|
|
|
|
// TODO 考虑独立出去作为一个中间件
|
|
|
|
|
lastHeartbeatAt int64 // 最近一次心跳时间
|
|
|
|
@ -59,9 +61,11 @@ type (
|
|
|
|
|
|
|
|
|
|
func newConn(server *engine, conn net.Conn) *Connection {
|
|
|
|
|
r := &Connection{
|
|
|
|
|
conn: conn,
|
|
|
|
|
ngin: server,
|
|
|
|
|
|
|
|
|
|
status: StatusStart,
|
|
|
|
|
conn: conn,
|
|
|
|
|
packer: server.packerFn(),
|
|
|
|
|
|
|
|
|
|
lastHeartbeatAt: time.Now().Unix(),
|
|
|
|
|
|
|
|
|
@ -189,7 +193,7 @@ func (r *Connection) write() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// packet pack data
|
|
|
|
|
p, err := r.ngin.packer.Pack(data.header, data.payload.([]byte))
|
|
|
|
|
p, err := r.packer.Pack(data.header, data.payload.([]byte))
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error(err.Error())
|
|
|
|
|
break
|
|
|
|
@ -222,13 +226,13 @@ func (r *Connection) read() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if r.ngin.packer == nil {
|
|
|
|
|
if r.packer == nil {
|
|
|
|
|
log.Errorf("unexpected error: packer is nil")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// warning: 为性能考虑,复用slice处理数据,buf传入后必须要copy再处理
|
|
|
|
|
packets, err := r.ngin.packer.Unpack(buf[:n])
|
|
|
|
|
packets, err := r.packer.Unpack(buf[:n])
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error(err.Error())
|
|
|
|
|
}
|
|
|
|
|