From 1a30d41e215626c4c1fa294fcc94dc59f42f5396 Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Fri, 5 May 2023 00:33:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=A4=9A=E5=AE=9E?= =?UTF-8?q?=E4=BE=8B=E4=B8=8BconnId=E5=85=B1=E4=BA=AB=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E4=BF=AE=E5=A4=8D=E6=B6=88=E6=81=AF=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA=E6=97=B6=E8=BF=9E=E6=8E=A5=E5=B9=B6=E6=9C=AA=E4=B8=BB?= =?UTF-8?q?=E5=8A=A8=E5=85=B3=E9=97=AD=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/connection.go | 11 ++++++++--- core/engine.go | 2 ++ core/session.go | 6 ++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/core/connection.go b/core/connection.go index 00e48aa..8007251 100644 --- a/core/connection.go +++ b/core/connection.go @@ -8,7 +8,6 @@ import ( "git.noahlan.cn/noahlan/nnet/scheduler" "git.noahlan.cn/noahlan/ntools-go/core/nlog" "git.noahlan.cn/noahlan/ntools-go/core/pool" - "io" "net" "sync/atomic" ) @@ -71,7 +70,7 @@ func newConnection(server *engine, conn net.Conn) *connection { _, r.isWS = conn.(*WSConn) // binding session - r.session = newSession(r) + r.session = newSession(r, server.sessIdMgr.SessionID()) return r } @@ -204,12 +203,18 @@ func (r *connection) read() { for { n, err := r.conn.Read(buf) //nlog.Debugf("receive data %v", buf[:n]) - if err != nil && err != io.EOF { + if err != nil { nlog.Errorf("%s [readLoop] Read message error: %s, session will be closed immediately", r.ngin.logPrefix(), err.Error()) return } + if n == 0 { + nlog.Errorf("%s [readLoop] Read empty message, session will be closed immediately", + r.ngin.logPrefix()) + return + } + if r.packer == nil { nlog.Errorf("%s [readLoop] unexpected error: packer is nil", r.ngin.logPrefix()) return diff --git a/core/engine.go b/core/engine.go index e2f79d5..aadf1c0 100644 --- a/core/engine.go +++ b/core/engine.go @@ -50,6 +50,7 @@ type ( wsOpt wsOptions // websocket connManager *conn2.Manager + sessIdMgr *sessionIDMgr } wsOptions struct { @@ -70,6 +71,7 @@ func newEngine(conf config.EngineConf) *engine { routes: make([]Route, 0), taskTimerPrecision: conf.TaskTimerPrecision, connManager: conn2.NewManager(), + sessIdMgr: newSessionIDMgr(), } pool.InitPool(conf.Pool) diff --git a/core/session.go b/core/session.go index 9293fd0..c6c84bf 100644 --- a/core/session.go +++ b/core/session.go @@ -17,9 +17,9 @@ type session struct { data map[string]interface{} // session数据存储(内存) } -func newSession(entity entity.NetworkEntity) *session { +func newSession(entity entity.NetworkEntity, id int64) *session { return &session{ - id: sessionIDMgrInstance.SessionID(), + id: id, entity: entity, uid: "", data: make(map[string]interface{}), @@ -110,8 +110,6 @@ func (s *session) Close() { s.Invalidate() } -var sessionIDMgrInstance = newSessionIDMgr() - type sessionIDMgr struct { count int64 sid int64