Browse Source

Merge branch 'master' into master

master
Titan Panda 4 years ago
committed by GitHub
parent
commit
6587835435
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      ziface/iconnection.go
  2. 26
      znet/connection.go

8
ziface/iconnection.go

@ -1,6 +1,9 @@
package ziface package ziface
import "net"
import (
"context"
"net"
)
//定义连接接口 //定义连接接口
type IConnection interface { type IConnection interface {
@ -8,7 +11,8 @@ type IConnection interface {
Start() Start()
//停止连接,结束当前连接状态M //停止连接,结束当前连接状态M
Stop() Stop()
//返回ctx,用于用户自定义的go程获取连接退出状态
Context() context.Context
//从当前连接获取原始的socket TCPConn //从当前连接获取原始的socket TCPConn
GetTCPConnection() *net.TCPConn GetTCPConnection() *net.TCPConn
//获取当前连接ID //获取当前连接ID

26
znet/connection.go

@ -50,7 +50,7 @@ func NewConntion(server ziface.IServer, conn *net.TCPConn, connID uint32, msgHan
MsgHandler: msgHandler, MsgHandler: msgHandler,
msgChan: make(chan []byte), msgChan: make(chan []byte),
msgBuffChan: make(chan []byte, utils.GlobalObject.MaxMsgChanLen), msgBuffChan: make(chan []byte, utils.GlobalObject.MaxMsgChanLen),
property: make(map[string]interface{}),
property: nil,
} }
//将新创建的Conn添加到链接管理中 //将新创建的Conn添加到链接管理中
@ -159,19 +159,23 @@ func (c *Connection) Start() {
//Stop 停止连接,结束当前连接状态M //Stop 停止连接,结束当前连接状态M
func (c *Connection) Stop() { func (c *Connection) Stop() {
fmt.Println("Conn Stop()...ConnID = ", c.ConnID)
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
//如果用户注册了该链接的关闭回调业务,那么在此刻应该显示调用
c.TCPServer.CallOnConnStop(c) c.TCPServer.CallOnConnStop(c)
//如果当前链接已经关闭 //如果当前链接已经关闭
if c.isClosed == true { if c.isClosed == true {
return return
} }
fmt.Println("Conn Stop()...ConnID = ", c.ConnID)
//如果用户注册了该链接的关闭回调业务,那么在此刻应该显示调用
c.TcpServer.CallOnConnStop(c)
// 关闭socket链接 // 关闭socket链接
c.Conn.Close() c.Conn.Close()
//关闭Writer //关闭Writer
@ -205,11 +209,10 @@ func (c *Connection) RemoteAddr() net.Addr {
//SendMsg 直接将Message数据发送数据给远程的TCP客户端 //SendMsg 直接将Message数据发送数据给远程的TCP客户端
func (c *Connection) SendMsg(msgID uint32, data []byte) error { func (c *Connection) SendMsg(msgID uint32, data []byte) error {
c.RLock() c.RLock()
defer c.RUnlock()
if c.isClosed == true { if c.isClosed == true {
c.RUnlock()
return errors.New("connection closed when send msg") return errors.New("connection closed when send msg")
} }
c.RUnlock()
//将data封包,并且发送 //将data封包,并且发送
dp := NewDataPack() dp := NewDataPack()
@ -228,11 +231,10 @@ func (c *Connection) SendMsg(msgID uint32, data []byte) error {
//SendBuffMsg 发生BuffMsg //SendBuffMsg 发生BuffMsg
func (c *Connection) SendBuffMsg(msgID uint32, data []byte) error { func (c *Connection) SendBuffMsg(msgID uint32, data []byte) error {
c.RLock() c.RLock()
defer c.RUnlock()
if c.isClosed == true { if c.isClosed == true {
c.RUnlock()
return errors.New("Connection closed when send buff msg") return errors.New("Connection closed when send buff msg")
} }
c.RUnlock()
//将data封包,并且发送 //将data封包,并且发送
dp := NewDataPack() dp := NewDataPack()
@ -252,6 +254,9 @@ func (c *Connection) SendBuffMsg(msgID uint32, data []byte) error {
func (c *Connection) SetProperty(key string, value interface{}) { func (c *Connection) SetProperty(key string, value interface{}) {
c.propertyLock.Lock() c.propertyLock.Lock()
defer c.propertyLock.Unlock() defer c.propertyLock.Unlock()
if c.property == nil {
c.property = make(map[string]interface{})
}
c.property[key] = value c.property[key] = value
} }
@ -275,3 +280,8 @@ func (c *Connection) RemoveProperty(key string) {
delete(c.property, key) delete(c.property, key)
} }
//返回ctx,用于用户自定义的go程获取连接退出状态
func (c *Connection) Context() context.Context {
return c.ctx
}
Loading…
Cancel
Save