connection.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package connect
  2. import (
  3. "errors"
  4. "time"
  5. "github.com/gorilla/websocket"
  6. "git.bvbej.com/bvbej/base-golang/pkg/websocket/peer"
  7. )
  8. const (
  9. writeWait = 20 * time.Second
  10. pongWait = 60 * time.Second
  11. pingPeriod = (pongWait * 9) / 10
  12. maxFrameMessageLen = 16 * 1024 //4 * 4096
  13. maxSendBuffer = 16
  14. )
  15. var (
  16. ErrBrokenPipe = errors.New("send to broken pipe")
  17. ErrBufferPoolExceed = errors.New("send buffer exceed")
  18. ErrSendBufferLimit = errors.New("send buffer limit")
  19. )
  20. type wsConnection struct {
  21. peer.ConnectionIdentify
  22. p *peer.SessionManager
  23. conn *websocket.Conn
  24. send chan []byte
  25. running bool
  26. }
  27. func (ws *wsConnection) Peer() *peer.SessionManager {
  28. return ws.p
  29. }
  30. func (ws *wsConnection) Raw() interface{} {
  31. if ws.conn == nil {
  32. return nil
  33. }
  34. return ws.conn
  35. }
  36. func (ws *wsConnection) RemoteAddr() string {
  37. if ws.conn == nil {
  38. return ""
  39. }
  40. return ws.conn.RemoteAddr().String()
  41. }
  42. func (ws *wsConnection) Close() {
  43. ws.conn.Close()
  44. ws.running = false
  45. }
  46. func (ws *wsConnection) Send(msg []byte) (err error) {
  47. defer func() {
  48. if e := recover(); e != nil {
  49. err = ErrBrokenPipe
  50. }
  51. }()
  52. if !ws.running {
  53. return ErrBrokenPipe
  54. }
  55. if len(ws.send) >= maxSendBuffer {
  56. return ErrBufferPoolExceed
  57. }
  58. if len(msg) > maxFrameMessageLen {
  59. return
  60. }
  61. ws.send <- msg
  62. return nil
  63. }
  64. func (ws *wsConnection) recvLoop() {
  65. defer func() {
  66. ws.p.Unregister <- ws.ID()
  67. ws.conn.Close()
  68. ws.running = false
  69. }()
  70. ws.conn.SetReadDeadline(time.Now().Add(pongWait))
  71. ws.conn.SetPongHandler(func(string) error { ws.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
  72. for ws.conn != nil {
  73. _, data, err := ws.conn.ReadMessage()
  74. if err != nil {
  75. break
  76. }
  77. ws.p.ProcessMessage(ws.ID(), data)
  78. }
  79. }
  80. func (ws *wsConnection) sendLoop() {
  81. ticker := time.NewTicker(pingPeriod)
  82. defer func() {
  83. ticker.Stop()
  84. ws.conn.Close()
  85. ws.running = false
  86. close(ws.send)
  87. }()
  88. for {
  89. select {
  90. case msg := <-ws.send:
  91. ws.conn.SetWriteDeadline(time.Now().Add(writeWait))
  92. if err := ws.conn.WriteMessage(websocket.BinaryMessage, msg); err != nil {
  93. return
  94. }
  95. case <-ticker.C:
  96. ws.conn.SetWriteDeadline(time.Now().Add(writeWait))
  97. if err := ws.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
  98. return
  99. }
  100. }
  101. }
  102. }
  103. func (ws *wsConnection) IsClosed() bool {
  104. return !ws.running
  105. }
  106. func (ws *wsConnection) init() {
  107. go ws.recvLoop()
  108. go ws.sendLoop()
  109. ws.running = true
  110. }
  111. func newConnection(conn *websocket.Conn,
  112. p *peer.SessionManager) *wsConnection {
  113. wsc := &wsConnection{
  114. conn: conn,
  115. p: p,
  116. send: make(chan []byte, maxSendBuffer),
  117. }
  118. wsc.init()
  119. return wsc
  120. }