You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
6.3 KiB

  1. package znet
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net"
  8. "sync"
  9. "github.com/aceld/zinx/utils"
  10. "github.com/aceld/zinx/ziface"
  11. )
  12. type Connection struct {
  13. //当前Conn属于哪个Server
  14. TcpServer ziface.IServer
  15. //当前连接的socket TCP套接字
  16. Conn *net.TCPConn
  17. //当前连接的ID 也可以称作为SessionID,ID全局唯一
  18. ConnID uint32
  19. //消息管理MsgId和对应处理方法的消息管理模块
  20. MsgHandler ziface.IMsgHandle
  21. //告知该链接已经退出/停止的channel
  22. ctx context.Context
  23. cancel context.CancelFunc
  24. //无缓冲管道,用于读、写两个goroutine之间的消息通信
  25. msgChan chan []byte
  26. //有缓冲管道,用于读、写两个goroutine之间的消息通信
  27. msgBuffChan chan []byte
  28. sync.RWMutex
  29. //链接属性
  30. property map[string]interface{}
  31. //当前连接的关闭状态
  32. isClosed bool
  33. }
  34. //创建连接的方法
  35. func NewConntion(server ziface.IServer, conn *net.TCPConn, connID uint32, msgHandler ziface.IMsgHandle) *Connection {
  36. //初始化Conn属性
  37. c := &Connection{
  38. TcpServer: server,
  39. Conn: conn,
  40. ConnID: connID,
  41. isClosed: false,
  42. MsgHandler: msgHandler,
  43. msgChan: make(chan []byte),
  44. msgBuffChan: make(chan []byte, utils.GlobalObject.MaxMsgChanLen),
  45. property: make(map[string]interface{}),
  46. }
  47. //将新创建的Conn添加到链接管理中
  48. c.TcpServer.GetConnMgr().Add(c)
  49. return c
  50. }
  51. /*
  52. 写消息Goroutine 用户将数据发送给客户端
  53. */
  54. func (c *Connection) StartWriter() {
  55. fmt.Println("[Writer Goroutine is running]")
  56. defer fmt.Println(c.RemoteAddr().String(), "[conn Writer exit!]")
  57. for {
  58. select {
  59. case data := <-c.msgChan:
  60. //有数据要写给客户端
  61. if _, err := c.Conn.Write(data); err != nil {
  62. fmt.Println("Send Data error:, ", err, " Conn Writer exit")
  63. return
  64. }
  65. //fmt.Printf("Send data succ! data = %+v\n", data)
  66. case data, ok := <-c.msgBuffChan:
  67. if ok {
  68. //有数据要写给客户端
  69. if _, err := c.Conn.Write(data); err != nil {
  70. fmt.Println("Send Buff Data error:, ", err, " Conn Writer exit")
  71. return
  72. }
  73. } else {
  74. fmt.Println("msgBuffChan is Closed")
  75. break
  76. }
  77. case <-c.ctx.Done():
  78. return
  79. }
  80. }
  81. }
  82. /*
  83. 读消息Goroutine用于从客户端中读取数据
  84. */
  85. func (c *Connection) StartReader() {
  86. fmt.Println("[Reader Goroutine is running]")
  87. defer fmt.Println(c.RemoteAddr().String(), "[conn Reader exit!]")
  88. defer c.Stop()
  89. for {
  90. select {
  91. case <-c.ctx.Done():
  92. return
  93. default:
  94. // 创建拆包解包的对象
  95. dp := NewDataPack()
  96. //读取客户端的Msg head
  97. headData := make([]byte, dp.GetHeadLen())
  98. if _, err := io.ReadFull(c.Conn, headData); err != nil {
  99. fmt.Println("read msg head error ", err)
  100. break
  101. }
  102. //fmt.Printf("read headData %+v\n", headData)
  103. //拆包,得到msgid 和 datalen 放在msg中
  104. msg, err := dp.Unpack(headData)
  105. if err != nil {
  106. fmt.Println("unpack error ", err)
  107. break
  108. }
  109. //根据 dataLen 读取 data,放在msg.Data中
  110. var data []byte
  111. if msg.GetDataLen() > 0 {
  112. data = make([]byte, msg.GetDataLen())
  113. if _, err := io.ReadFull(c.Conn, data); err != nil {
  114. fmt.Println("read msg data error ", err)
  115. break
  116. }
  117. }
  118. msg.SetData(data)
  119. //得到当前客户端请求的Request数据
  120. req := Request{
  121. conn: c,
  122. msg: msg,
  123. }
  124. if utils.GlobalObject.WorkerPoolSize > 0 {
  125. //已经启动工作池机制,将消息交给Worker处理
  126. c.MsgHandler.SendMsgToTaskQueue(&req)
  127. } else {
  128. //从绑定好的消息和对应的处理方法中执行对应的Handle方法
  129. go c.MsgHandler.DoMsgHandler(&req)
  130. }
  131. }
  132. }
  133. }
  134. //启动连接,让当前连接开始工作
  135. func (c *Connection) Start() {
  136. c.ctx, c.cancel = context.WithCancel(context.Background())
  137. //1 开启用户从客户端读取数据流程的Goroutine
  138. go c.StartReader()
  139. //2 开启用于写回客户端数据流程的Goroutine
  140. go c.StartWriter()
  141. //按照用户传递进来的创建连接时需要处理的业务,执行钩子方法
  142. c.TcpServer.CallOnConnStart(c)
  143. }
  144. //停止连接,结束当前连接状态M
  145. func (c *Connection) Stop() {
  146. fmt.Println("Conn Stop()...ConnID = ", c.ConnID)
  147. //如果当前链接已经关闭
  148. c.Lock()
  149. defer c.Unlock()
  150. if c.isClosed == true {
  151. return
  152. }
  153. c.isClosed = true
  154. //如果用户注册了该链接的关闭回调业务,那么在此刻应该显示调用
  155. c.TcpServer.CallOnConnStop(c)
  156. // 关闭socket链接
  157. c.Conn.Close()
  158. //关闭Writer
  159. c.cancel()
  160. //将链接从连接管理器中删除
  161. c.TcpServer.GetConnMgr().Remove(c)
  162. //关闭该链接全部管道
  163. close(c.msgBuffChan)
  164. }
  165. //从当前连接获取原始的socket TCPConn
  166. func (c *Connection) GetTCPConnection() *net.TCPConn {
  167. return c.Conn
  168. }
  169. //获取当前连接ID
  170. func (c *Connection) GetConnID() uint32 {
  171. return c.ConnID
  172. }
  173. //获取远程客户端地址信息
  174. func (c *Connection) RemoteAddr() net.Addr {
  175. return c.Conn.RemoteAddr()
  176. }
  177. //直接将Message数据发送数据给远程的TCP客户端
  178. func (c *Connection) SendMsg(msgId uint32, data []byte) error {
  179. c.RLock()
  180. if c.isClosed == true {
  181. c.RUnlock()
  182. return errors.New("connection closed when send msg")
  183. }
  184. c.RUnlock()
  185. //将data封包,并且发送
  186. dp := NewDataPack()
  187. msg, err := dp.Pack(NewMsgPackage(msgId, data))
  188. if err != nil {
  189. fmt.Println("Pack error msg id = ", msgId)
  190. return errors.New("Pack error msg ")
  191. }
  192. //写回客户端
  193. c.msgChan <- msg
  194. return nil
  195. }
  196. func (c *Connection) SendBuffMsg(msgId uint32, data []byte) error {
  197. c.RLock()
  198. if c.isClosed == true {
  199. c.RUnlock()
  200. return errors.New("Connection closed when send buff msg")
  201. }
  202. c.RUnlock()
  203. //将data封包,并且发送
  204. dp := NewDataPack()
  205. msg, err := dp.Pack(NewMsgPackage(msgId, data))
  206. if err != nil {
  207. fmt.Println("Pack error msg id = ", msgId)
  208. return errors.New("Pack error msg ")
  209. }
  210. //写回客户端
  211. c.msgBuffChan <- msg
  212. return nil
  213. }
  214. //设置链接属性
  215. func (c *Connection) SetProperty(key string, value interface{}) {
  216. c.Lock()
  217. defer c.Unlock()
  218. c.property[key] = value
  219. }
  220. //获取链接属性
  221. func (c *Connection) GetProperty(key string) (interface{}, error) {
  222. c.RLock()
  223. defer c.RUnlock()
  224. if value, ok := c.property[key]; ok {
  225. return value, nil
  226. } else {
  227. return nil, errors.New("no property found")
  228. }
  229. }
  230. //移除链接属性
  231. func (c *Connection) RemoveProperty(key string) {
  232. c.Lock()
  233. defer c.Unlock()
  234. delete(c.property, key)
  235. }