You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.2 KiB
Go
64 lines
1.2 KiB
Go
package nnet
|
|
|
|
import (
|
|
"git.noahlan.cn/northlan/nnet/nface"
|
|
"git.noahlan.cn/northlan/nnet/pipeline"
|
|
"git.noahlan.cn/northlan/nnet/session"
|
|
"github.com/gorilla/websocket"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
type Request struct {
|
|
session nface.ISession // Session
|
|
|
|
conn net.Conn // low-level conn fd
|
|
status Status // 连接状态
|
|
lastMid uint64 // 最近一次消息ID
|
|
lastHeartbeatAt int64 // 最近一次心跳时间
|
|
|
|
chDie chan struct{} // 停止通道
|
|
chSend chan []byte // 消息发送通道
|
|
|
|
pipeline pipeline.Pipeline // 消息管道
|
|
}
|
|
|
|
func newRequest(conn net.Conn, pipeline pipeline.Pipeline) *Request {
|
|
r := &Request{
|
|
conn: conn,
|
|
status: StatusStart,
|
|
|
|
lastHeartbeatAt: time.Now().Unix(),
|
|
|
|
chDie: make(chan struct{}),
|
|
chSend: make(chan []byte),
|
|
|
|
pipeline: pipeline,
|
|
}
|
|
|
|
// binding session
|
|
r.session = session.New()
|
|
return r
|
|
}
|
|
|
|
func newRequestWS(conn *websocket.Conn, pipeline pipeline.Pipeline) *Request {
|
|
c, err := newWSConn(conn)
|
|
if err != nil {
|
|
// TODO panic ?
|
|
panic(err)
|
|
}
|
|
return newRequest(c, pipeline)
|
|
}
|
|
|
|
func (r *Request) Status() Status {
|
|
return r.status
|
|
}
|
|
|
|
func (r *Request) ID() int64 {
|
|
return r.session.ID()
|
|
}
|
|
|
|
func (r *Request) Session() nface.ISession {
|
|
return r.session
|
|
}
|