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.

192 lines
4.4 KiB

  1. package znet
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net"
  7. "zinx/ziface"
  8. )
  9. type Connection struct {
  10. //当前连接的socket TCP套接字
  11. Conn *net.TCPConn
  12. //当前连接的ID 也可以称作为SessionID,ID全局唯一
  13. ConnID uint32
  14. //当前连接的关闭状态
  15. isClosed bool
  16. //该连接的处理方法router
  17. Router ziface.IRouter
  18. //告知该链接已经退出/停止的channel
  19. ExitBuffChan chan bool
  20. //给缓冲队列发送数据的channel,
  21. // 如果向缓冲队列发送数据,那么把数据发送到这个channel下
  22. // SendBuffChan chan []byte
  23. }
  24. //创建连接的方法
  25. func NewConntion(conn *net.TCPConn, connID uint32, router ziface.IRouter) *Connection{
  26. c := &Connection{
  27. Conn: conn,
  28. ConnID: connID,
  29. isClosed: false,
  30. Router: router,
  31. ExitBuffChan: make(chan bool, 1),
  32. // SendBuffChan: make(chan []byte, 512),
  33. }
  34. return c
  35. }
  36. func (c *Connection) StartReader() {
  37. fmt.Println("Reader Goroutine is running")
  38. defer fmt.Println(c.RemoteAddr().String(), " conn reader exit!")
  39. defer c.Stop()
  40. for {
  41. //读取我们最大的数据到buf中
  42. //buf := make([]byte, utils.GlobalObject.MaxPacketSize)
  43. //_, err := c.Conn.Read(buf)
  44. //if err != nil {
  45. // fmt.Println("recv buf err ", err)
  46. // c.ExitBuffChan <- true
  47. // continue
  48. //}
  49. // 创建拆包解包的对象
  50. dp := NewDataPack()
  51. //读取客户端的Msg head
  52. headData := make([]byte, dp.GetHeadLen())
  53. if _, err := io.ReadFull(c.GetTCPConnection(), headData); err != nil {
  54. fmt.Println("read msg head error ", err)
  55. c.ExitBuffChan <- true
  56. continue
  57. }
  58. //拆包,得到msgid 和 datalen 放在msg中
  59. msg , err := dp.Unpack(headData)
  60. if err != nil {
  61. fmt.Println("unpack error ", err)
  62. c.ExitBuffChan <- true
  63. continue
  64. }
  65. //根据 dataLen 读取 data,放在msg.Data中
  66. var data []byte
  67. if msg.GetDataLen() > 0 {
  68. data = make([]byte, msg.GetDataLen())
  69. if _, err := io.ReadFull(c.GetTCPConnection(), data); err != nil {
  70. fmt.Println("read msg data error ", err)
  71. c.ExitBuffChan <- true
  72. continue
  73. }
  74. }
  75. msg.SetData(data)
  76. //得到当前客户端请求的Request数据
  77. req := Request{
  78. conn:c,
  79. msg:msg,
  80. }
  81. //从路由Routers 中找到注册绑定Conn的对应Handle
  82. go func (request ziface.IRequest) {
  83. //执行注册的路由方法
  84. c.Router.PreHandle(request)
  85. c.Router.Handle(request)
  86. c.Router.PostHandle(request)
  87. }(&req)
  88. //if err := c.handleAPI(c.Conn, buf, cnt); err !=nil {
  89. // fmt.Println("connID ", c.ConnID, " handle is error")
  90. // c.ExitBuffChan <- true
  91. // return
  92. //}
  93. }
  94. }
  95. //启动连接,让当前连接开始工作
  96. func (c *Connection) Start() {
  97. //开启处理该链接读取到客户端数据之后的请求业务
  98. go c.StartReader()
  99. for {
  100. select {
  101. case <- c.ExitBuffChan:
  102. //得到退出消息,不再阻塞
  103. return
  104. }
  105. }
  106. //1 开启用于写回客户端数据流程的Goroutine
  107. //2 开启用户从客户端读取数据流程的Goroutine
  108. }
  109. //停止连接,结束当前连接状态M
  110. func (c *Connection) Stop() {
  111. //1. 如果当前链接已经关闭
  112. if c.isClosed == true {
  113. return
  114. }
  115. c.isClosed = true
  116. //TODO Connection Stop() 如果用户注册了该链接的关闭回调业务,那么在此刻应该显示调用
  117. // 关闭socket链接
  118. c.Conn.Close()
  119. //通知从缓冲队列读数据的业务,该链接已经关闭
  120. c.ExitBuffChan <- true
  121. //关闭该链接全部管道
  122. close(c.ExitBuffChan)
  123. //close(c.SendBuffChan)
  124. }
  125. //从当前连接获取原始的socket TCPConn
  126. func (c *Connection) GetTCPConnection() *net.TCPConn {
  127. return c.Conn
  128. }
  129. //获取当前连接ID
  130. func (c *Connection) GetConnID() uint32{
  131. return c.ConnID
  132. }
  133. //获取远程客户端地址信息
  134. func (c *Connection) RemoteAddr() net.Addr {
  135. return c.Conn.RemoteAddr()
  136. }
  137. //直接将Message数据发送数据给远程的TCP客户端
  138. func (c *Connection) SendMsg(msgId uint32, data []byte) error {
  139. if c.isClosed == true {
  140. return errors.New("Connection closed when send msg")
  141. }
  142. //将data封包,并且发送
  143. dp := NewDataPack()
  144. msg, err := dp.Pack(NewMsgPackage(msgId, data))
  145. if err != nil {
  146. fmt.Println("Pack error msg id = ", msgId)
  147. return errors.New("Pack error msg ")
  148. }
  149. //写回客户端
  150. if _, err := c.Conn.Write(msg); err != nil {
  151. fmt.Println("Write msg id ", msgId, " error ")
  152. c.ExitBuffChan <- true
  153. return errors.New("conn Write error")
  154. }
  155. return nil
  156. }
  157. //将数据发送给缓冲队列,通过专门从缓冲队列读数据的go写给客户端
  158. //func (c *Connection) SendBuff(data []byte) error {
  159. // return nil
  160. //}