conn.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package mux
  2. import (
  3. "errors"
  4. "github.com/cnlh/nps/lib/pool"
  5. "io"
  6. "net"
  7. "sync"
  8. "time"
  9. )
  10. type conn struct {
  11. net.Conn
  12. getStatusCh chan struct{}
  13. connStatusOkCh chan struct{}
  14. connStatusFailCh chan struct{}
  15. readTimeOut time.Time
  16. writeTimeOut time.Time
  17. readBuffer []byte
  18. startRead int //now read position
  19. endRead int //now end read
  20. readFlag bool
  21. readCh chan struct{}
  22. waitQueue *sliceEntry
  23. stopWrite bool
  24. connId int32
  25. isClose bool
  26. readWait bool
  27. mux *Mux
  28. }
  29. var connPool = sync.Pool{}
  30. func NewConn(connId int32, mux *Mux) *conn {
  31. c := &conn{
  32. readCh: make(chan struct{}),
  33. getStatusCh: make(chan struct{}),
  34. connStatusOkCh: make(chan struct{}),
  35. connStatusFailCh: make(chan struct{}),
  36. waitQueue: NewQueue(),
  37. connId: connId,
  38. mux: mux,
  39. }
  40. return c
  41. }
  42. func (s *conn) Read(buf []byte) (n int, err error) {
  43. if s.isClose || buf == nil {
  44. return 0, errors.New("the conn has closed")
  45. }
  46. if s.endRead-s.startRead == 0 { //read finish or start
  47. if s.waitQueue.Size() == 0 {
  48. s.readWait = true
  49. if t := s.readTimeOut.Sub(time.Now()); t > 0 {
  50. timer := time.NewTimer(t)
  51. defer timer.Stop()
  52. select {
  53. case <-timer.C:
  54. s.readWait = false
  55. return 0, errors.New("read timeout")
  56. case <-s.readCh:
  57. }
  58. } else {
  59. <-s.readCh
  60. }
  61. }
  62. if s.isClose { //If the connection is closed instead of continuing command
  63. return 0, errors.New("the conn has closed")
  64. }
  65. if node, err := s.waitQueue.Pop(); err != nil {
  66. s.Close()
  67. return 0, io.EOF
  68. } else {
  69. pool.PutBufPoolCopy(s.readBuffer)
  70. s.readBuffer = node.val
  71. s.endRead = node.l
  72. s.startRead = 0
  73. }
  74. }
  75. if len(buf) < s.endRead-s.startRead {
  76. n = copy(buf, s.readBuffer[s.startRead:s.startRead+len(buf)])
  77. s.startRead += n
  78. } else {
  79. n = copy(buf, s.readBuffer[s.startRead:s.endRead])
  80. s.startRead += n
  81. if s.waitQueue.Size() < s.mux.waitQueueSize/2 {
  82. s.mux.sendInfo(MUX_MSG_SEND_OK, s.connId, nil)
  83. }
  84. }
  85. return
  86. }
  87. func (s *conn) Write(buf []byte) (int, error) {
  88. if s.isClose {
  89. return 0, errors.New("the conn has closed")
  90. }
  91. ch := make(chan struct{})
  92. go s.write(buf, ch)
  93. if t := s.writeTimeOut.Sub(time.Now()); t > 0 {
  94. timer := time.NewTimer(t)
  95. defer timer.Stop()
  96. select {
  97. case <-timer.C:
  98. return 0, errors.New("write timeout")
  99. case <-ch:
  100. }
  101. } else {
  102. <-ch
  103. }
  104. if s.isClose {
  105. return 0, io.EOF
  106. }
  107. return len(buf), nil
  108. }
  109. func (s *conn) write(buf []byte, ch chan struct{}) {
  110. start := 0
  111. l := len(buf)
  112. for {
  113. if s.stopWrite {
  114. <-s.getStatusCh
  115. }
  116. if l-start > pool.PoolSizeCopy {
  117. s.mux.sendInfo(MUX_NEW_MSG, s.connId, buf[start:start+pool.PoolSizeCopy])
  118. start += pool.PoolSizeCopy
  119. } else {
  120. s.mux.sendInfo(MUX_NEW_MSG, s.connId, buf[start:l])
  121. break
  122. }
  123. }
  124. ch <- struct{}{}
  125. }
  126. func (s *conn) Close() error {
  127. if s.isClose {
  128. return errors.New("the conn has closed")
  129. }
  130. times := 0
  131. retry:
  132. if s.waitQueue.Size() > 0 && times < 600 {
  133. time.Sleep(time.Millisecond * 100)
  134. times++
  135. goto retry
  136. }
  137. if s.isClose {
  138. return errors.New("the conn has closed")
  139. }
  140. s.isClose = true
  141. pool.PutBufPoolCopy(s.readBuffer)
  142. if s.readWait {
  143. s.readCh <- struct{}{}
  144. }
  145. s.waitQueue.Clear()
  146. s.mux.connMap.Delete(s.connId)
  147. if !s.mux.IsClose {
  148. s.mux.sendInfo(MUX_CONN_CLOSE, s.connId, nil)
  149. }
  150. connPool.Put(s)
  151. return nil
  152. }
  153. func (s *conn) LocalAddr() net.Addr {
  154. return s.mux.conn.LocalAddr()
  155. }
  156. func (s *conn) RemoteAddr() net.Addr {
  157. return s.mux.conn.RemoteAddr()
  158. }
  159. func (s *conn) SetDeadline(t time.Time) error {
  160. s.readTimeOut = t
  161. s.writeTimeOut = t
  162. return nil
  163. }
  164. func (s *conn) SetReadDeadline(t time.Time) error {
  165. s.readTimeOut = t
  166. return nil
  167. }
  168. func (s *conn) SetWriteDeadline(t time.Time) error {
  169. s.writeTimeOut = t
  170. return nil
  171. }