From 5e524639b2280de2b1061a41def84647c2723aaf Mon Sep 17 00:00:00 2001 From: aceld Date: Tue, 27 Oct 2020 17:27:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20953c254e=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=9B=A0=E5=A2=9E=E5=8A=A0connection=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E5=AE=89=E5=85=A8=20=E5=9C=A8close=E5=8A=A0=E9=94=81?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=20property=E8=8E=B7=E5=8F=96=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E6=AD=BB=E9=94=81=E9=97=AE=E9=A2=98,=E5=B0=86?= =?UTF-8?q?=E4=BA=92=E6=96=A5=E9=94=81=E4=B8=8Eproperty=E9=94=81=E5=88=86?= =?UTF-8?q?=E5=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- znet/connection.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/znet/connection.go b/znet/connection.go index cea50e1..5694129 100644 --- a/znet/connection.go +++ b/znet/connection.go @@ -32,6 +32,8 @@ type Connection struct { sync.RWMutex //链接属性 property map[string]interface{} + ////保护当前property的锁 + propertyLock sync.Mutex //当前连接的关闭状态 isClosed bool } @@ -161,18 +163,19 @@ func (c *Connection) Start() { //停止连接,结束当前连接状态M func (c *Connection) Stop() { fmt.Println("Conn Stop()...ConnID = ", c.ConnID) - //如果当前链接已经关闭 + c.Lock() defer c.Unlock() + //如果用户注册了该链接的关闭回调业务,那么在此刻应该显示调用 + c.TcpServer.CallOnConnStop(c) + + //如果当前链接已经关闭 if c.isClosed == true { return } c.isClosed = true - //如果用户注册了该链接的关闭回调业务,那么在此刻应该显示调用 - c.TcpServer.CallOnConnStop(c) - // 关闭socket链接 c.Conn.Close() //关闭Writer @@ -247,16 +250,16 @@ func (c *Connection) SendBuffMsg(msgId uint32, data []byte) error { //设置链接属性 func (c *Connection) SetProperty(key string, value interface{}) { - c.Lock() - defer c.Unlock() + c.propertyLock.Lock() + defer c.propertyLock.Unlock() c.property[key] = value } //获取链接属性 func (c *Connection) GetProperty(key string) (interface{}, error) { - c.RLock() - defer c.RUnlock() + c.propertyLock.Lock() + defer c.propertyLock.Unlock() if value, ok := c.property[key]; ok { return value, nil @@ -267,8 +270,8 @@ func (c *Connection) GetProperty(key string) (interface{}, error) { //移除链接属性 func (c *Connection) RemoveProperty(key string) { - c.Lock() - defer c.Unlock() + c.propertyLock.Lock() + defer c.propertyLock.Unlock() delete(c.property, key) }