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.

142 lines
3.2 KiB

  1. package znet
  2. import (
  3. "fmt"
  4. "net"
  5. "zinx/utils"
  6. "zinx/ziface"
  7. )
  8. type Connection struct {
  9. //当前连接的socket TCP套接字
  10. Conn *net.TCPConn
  11. //当前连接的ID 也可以称作为SessionID,ID全局唯一
  12. ConnID uint32
  13. //当前连接的关闭状态
  14. isClosed bool
  15. //该连接的处理方法router
  16. Router ziface.IRouter
  17. //告知该链接已经退出/停止的channel
  18. ExitBuffChan chan bool
  19. //给缓冲队列发送数据的channel,
  20. // 如果向缓冲队列发送数据,那么把数据发送到这个channel下
  21. // SendBuffChan chan []byte
  22. }
  23. //创建连接的方法
  24. func NewConntion(conn *net.TCPConn, connID uint32, router ziface.IRouter) *Connection{
  25. c := &Connection{
  26. Conn: conn,
  27. ConnID: connID,
  28. isClosed: false,
  29. Router: router,
  30. ExitBuffChan: make(chan bool, 1),
  31. // SendBuffChan: make(chan []byte, 512),
  32. }
  33. return c
  34. }
  35. func (c *Connection) StartReader() {
  36. fmt.Println("Reader Goroutine is running")
  37. defer fmt.Println(c.RemoteAddr().String(), " conn reader exit!")
  38. defer c.Stop()
  39. for {
  40. //读取我们最大的数据到buf中
  41. buf := make([]byte, utils.GlobalObject.MaxPacketSize)
  42. _, err := c.Conn.Read(buf)
  43. if err != nil {
  44. fmt.Println("recv buf err ", err)
  45. c.ExitBuffChan <- true
  46. continue
  47. }
  48. //得到当前客户端请求的Request数据
  49. req := Request{
  50. conn:c,
  51. data:buf,
  52. }
  53. //从路由Routers 中找到注册绑定Conn的对应Handle
  54. go func (request ziface.IRequest) {
  55. //执行注册的路由方法
  56. c.Router.PreHandle(request)
  57. c.Router.Handle(request)
  58. c.Router.PostHandle(request)
  59. }(&req)
  60. //if err := c.handleAPI(c.Conn, buf, cnt); err !=nil {
  61. // fmt.Println("connID ", c.ConnID, " handle is error")
  62. // c.ExitBuffChan <- true
  63. // return
  64. //}
  65. }
  66. }
  67. //启动连接,让当前连接开始工作
  68. func (c *Connection) Start() {
  69. //开启处理该链接读取到客户端数据之后的请求业务
  70. go c.StartReader()
  71. for {
  72. select {
  73. case <- c.ExitBuffChan:
  74. //得到退出消息,不再阻塞
  75. return
  76. }
  77. }
  78. //1 开启用于写回客户端数据流程的Goroutine
  79. //2 开启用户从客户端读取数据流程的Goroutine
  80. }
  81. //停止连接,结束当前连接状态M
  82. func (c *Connection) Stop() {
  83. //1. 如果当前链接已经关闭
  84. if c.isClosed == true {
  85. return
  86. }
  87. c.isClosed = true
  88. //TODO Connection Stop() 如果用户注册了该链接的关闭回调业务,那么在此刻应该显示调用
  89. // 关闭socket链接
  90. c.Conn.Close()
  91. //通知从缓冲队列读数据的业务,该链接已经关闭
  92. c.ExitBuffChan <- true
  93. //关闭该链接全部管道
  94. close(c.ExitBuffChan)
  95. //close(c.SendBuffChan)
  96. }
  97. //从当前连接获取原始的socket TCPConn
  98. func (c *Connection) GetTCPConnection() *net.TCPConn {
  99. return c.Conn
  100. }
  101. //获取当前连接ID
  102. func (c *Connection) GetConnID() uint32{
  103. return c.ConnID
  104. }
  105. //获取远程客户端地址信息
  106. func (c *Connection) RemoteAddr() net.Addr {
  107. return c.Conn.RemoteAddr()
  108. }
  109. //直接将数据发送数据给远程的TCP客户端
  110. func (c *Connection) Send(data []byte) error {
  111. return nil
  112. }
  113. //将数据发送给缓冲队列,通过专门从缓冲队列读数据的go写给客户端
  114. func (c *Connection) SendBuff(data []byte) error {
  115. return nil
  116. }