From 889df45920407ff8430bc3532f2ec6843e7aceae Mon Sep 17 00:00:00 2001 From: gufeijun Date: Sun, 21 Feb 2021 15:40:01 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=A4=E4=B8=AAsendbuf?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E4=B8=AD=E5=8F=AF=E8=83=BD=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E7=9A=84panic=E5=92=8Cgo=E7=A8=8B=E6=B3=84=E9=9C=B2=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- znet/connection.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/znet/connection.go b/znet/connection.go index 5694129..c653ac7 100644 --- a/znet/connection.go +++ b/znet/connection.go @@ -206,11 +206,11 @@ func (c *Connection) RemoteAddr() net.Addr { //直接将Message数据发送数据给远程的TCP客户端 func (c *Connection) SendMsg(msgId uint32, data []byte) error { c.RLock() + defer c.RUnlock() if c.isClosed == true { - c.RUnlock() return errors.New("connection closed when send msg") } - c.RUnlock() + //将data封包,并且发送 dp := NewDataPack() @@ -228,11 +228,10 @@ func (c *Connection) SendMsg(msgId uint32, data []byte) error { func (c *Connection) SendBuffMsg(msgId uint32, data []byte) error { c.RLock() + defer c.RUnlock() if c.isClosed == true { - c.RUnlock() return errors.New("Connection closed when send buff msg") } - c.RUnlock() //将data封包,并且发送 dp := NewDataPack() From 34b9018af06e04ca8e77e1cea80d856eaf105aff Mon Sep 17 00:00:00 2001 From: gufeijun Date: Sun, 21 Feb 2021 16:00:24 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=9C=A8=E8=B7=AF=E7=94=B1=E4=B8=AD=E8=B0=83=E7=94=A8IConnecio?= =?UTF-8?q?n.Stop()=E6=97=B6=E5=AF=BC=E8=87=B4=E7=9A=84OnConnStop=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E5=A4=9A=E6=AC=A1=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- znet/connection.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/znet/connection.go b/znet/connection.go index c653ac7..d99d285 100644 --- a/znet/connection.go +++ b/znet/connection.go @@ -162,20 +162,20 @@ 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 + fmt.Println("Conn Stop()...ConnID = ", c.ConnID) + + //如果用户注册了该链接的关闭回调业务,那么在此刻应该显示调用 + c.TcpServer.CallOnConnStop(c) + // 关闭socket链接 c.Conn.Close() //关闭Writer @@ -211,7 +211,6 @@ func (c *Connection) SendMsg(msgId uint32, data []byte) error { return errors.New("connection closed when send msg") } - //将data封包,并且发送 dp := NewDataPack() msg, err := dp.Pack(NewMsgPackage(msgId, data)) From 49a163a8229265dec8063259985a9800a880619a Mon Sep 17 00:00:00 2001 From: gufeijun Date: Sun, 21 Feb 2021 16:14:33 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=80=A7=E8=83=BD=E4=BC=98=E5=8C=96?= =?UTF-8?q?=EF=BC=9ANewConnetion=E6=97=B6=E4=B8=8D=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96property=20map,=E5=88=9D=E5=A7=8B=E5=8C=96=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E7=A7=BB=E8=87=B3=E7=AC=AC=E4=B8=80=E6=AC=A1=E8=B0=83?= =?UTF-8?q?=E7=94=A8SetProperty?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- znet/connection.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/znet/connection.go b/znet/connection.go index d99d285..9f11c89 100644 --- a/znet/connection.go +++ b/znet/connection.go @@ -49,7 +49,7 @@ func NewConntion(server ziface.IServer, conn *net.TCPConn, connID uint32, msgHan MsgHandler: msgHandler, msgChan: make(chan []byte), msgBuffChan: make(chan []byte, utils.GlobalObject.MaxMsgChanLen), - property: make(map[string]interface{}), + property: nil, } //将新创建的Conn添加到链接管理中 @@ -250,6 +250,9 @@ func (c *Connection) SendBuffMsg(msgId uint32, data []byte) error { func (c *Connection) SetProperty(key string, value interface{}) { c.propertyLock.Lock() defer c.propertyLock.Unlock() + if c.property == nil { + c.property = make(map[string]interface{}) + } c.property[key] = value } From 848fda9edb867323eae8fde0e87785b381f5b53c Mon Sep 17 00:00:00 2001 From: gufeijun Date: Sun, 21 Feb 2021 16:31:28 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=A2=9E=E5=BC=BA:?= =?UTF-8?q?=E7=BB=99Connction=E6=96=B0=E5=A2=9EContext()=E6=96=B9=E6=B3=95?= =?UTF-8?q?,=E8=BF=94=E5=9B=9Econtext?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ziface/iconnection.go | 8 ++++++-- znet/connection.go | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/ziface/iconnection.go b/ziface/iconnection.go index 7fe9063..4203ab9 100644 --- a/ziface/iconnection.go +++ b/ziface/iconnection.go @@ -1,6 +1,9 @@ package ziface -import "net" +import ( + "context" + "net" +) //定义连接接口 type IConnection interface { @@ -8,7 +11,8 @@ type IConnection interface { Start() //停止连接,结束当前连接状态M Stop() - + //返回ctx,用于用户自定义的go程获取连接退出状态 + Context() context.Context //从当前连接获取原始的socket TCPConn GetTCPConnection() *net.TCPConn //获取当前连接ID diff --git a/znet/connection.go b/znet/connection.go index 9f11c89..d90e285 100644 --- a/znet/connection.go +++ b/znet/connection.go @@ -276,3 +276,8 @@ func (c *Connection) RemoveProperty(key string) { delete(c.property, key) } + +//返回ctx,用于用户自定义的go程获取连接退出状态 +func (c *Connection) Context() context.Context { + return c.ctx +}