|
@ -5,6 +5,7 @@ import ( |
|
|
"fmt" |
|
|
"fmt" |
|
|
"io" |
|
|
"io" |
|
|
"net" |
|
|
"net" |
|
|
|
|
|
"sync" |
|
|
"zinx/utils" |
|
|
"zinx/utils" |
|
|
"zinx/ziface" |
|
|
"zinx/ziface" |
|
|
) |
|
|
) |
|
@ -26,8 +27,12 @@ type Connection struct { |
|
|
msgChan chan []byte |
|
|
msgChan chan []byte |
|
|
//有关冲管道,用于读、写两个goroutine之间的消息通信
|
|
|
//有关冲管道,用于读、写两个goroutine之间的消息通信
|
|
|
msgBuffChan chan []byte |
|
|
msgBuffChan chan []byte |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//链接属性
|
|
|
|
|
|
property map[string]interface{} |
|
|
|
|
|
//保护链接属性修改的锁
|
|
|
|
|
|
propertyLock sync.RWMutex |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//创建连接的方法
|
|
|
//创建连接的方法
|
|
|
func NewConntion(server ziface.IServer, conn *net.TCPConn, connID uint32, msgHandler ziface.IMsgHandle) *Connection { |
|
|
func NewConntion(server ziface.IServer, conn *net.TCPConn, connID uint32, msgHandler ziface.IMsgHandle) *Connection { |
|
@ -41,6 +46,7 @@ func NewConntion(server ziface.IServer, conn *net.TCPConn, connID uint32, msgHan |
|
|
ExitBuffChan: make(chan bool, 1), |
|
|
ExitBuffChan: make(chan bool, 1), |
|
|
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{}), |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//将新创建的Conn添加到链接管理中
|
|
|
//将新创建的Conn添加到链接管理中
|
|
@ -80,7 +86,6 @@ func NewConntion(server ziface.IServer, conn *net.TCPConn, connID uint32, msgHan |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
/* |
|
|
读消息Goroutine,用于从客户端中读取数据 |
|
|
读消息Goroutine,用于从客户端中读取数据 |
|
|
*/ |
|
|
*/ |
|
@ -220,3 +225,31 @@ func (c *Connection) SendBuffMsg(msgId uint32, data []byte) error { |
|
|
|
|
|
|
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//设置链接属性
|
|
|
|
|
|
func (c *Connection) SetProperty(key string, value interface{}) { |
|
|
|
|
|
c.propertyLock.Lock() |
|
|
|
|
|
defer c.propertyLock.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
c.property[key] = value |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//获取链接属性
|
|
|
|
|
|
func (c *Connection) GetProperty(key string) (interface{}, error) { |
|
|
|
|
|
c.propertyLock.RLock() |
|
|
|
|
|
defer c.propertyLock.RUnlock() |
|
|
|
|
|
|
|
|
|
|
|
if value, ok := c.property[key]; ok { |
|
|
|
|
|
return value, nil |
|
|
|
|
|
} else { |
|
|
|
|
|
return nil, errors.New("no property found") |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//移除链接属性
|
|
|
|
|
|
func (c *Connection) RemoveProperty(key string) { |
|
|
|
|
|
c.propertyLock.Lock() |
|
|
|
|
|
defer c.propertyLock.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
delete(c.property, key) |
|
|
|
|
|
} |