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