conn.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package mux
  2. import (
  3. "errors"
  4. "io"
  5. "net"
  6. "sync"
  7. "time"
  8. "github.com/cnlh/nps/lib/pool"
  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. hasWrite int
  28. mux *Mux
  29. }
  30. var connPool = sync.Pool{}
  31. func NewConn(connId int32, mux *Mux) *conn {
  32. c := &conn{
  33. readCh: make(chan struct{}),
  34. getStatusCh: make(chan struct{}),
  35. connStatusOkCh: make(chan struct{}),
  36. connStatusFailCh: make(chan struct{}),
  37. waitQueue: NewQueue(),
  38. connId: connId,
  39. mux: mux,
  40. }
  41. return c
  42. }
  43. func (s *conn) Read(buf []byte) (n int, err error) {
  44. if s.isClose || buf == nil {
  45. return 0, errors.New("the conn has closed")
  46. }
  47. if s.endRead-s.startRead == 0 { //read finish or start
  48. if s.waitQueue.Size() == 0 {
  49. s.readWait = true
  50. if t := s.readTimeOut.Sub(time.Now()); t > 0 {
  51. timer := time.NewTimer(t)
  52. defer timer.Stop()
  53. select {
  54. case <-timer.C:
  55. s.readWait = false
  56. return 0, errors.New("read timeout")
  57. case <-s.readCh:
  58. }
  59. } else {
  60. <-s.readCh
  61. }
  62. }
  63. if s.isClose { //If the connection is closed instead of continuing command
  64. return 0, errors.New("the conn has closed")
  65. }
  66. if node, err := s.waitQueue.Pop(); err != nil {
  67. s.Close()
  68. return 0, io.EOF
  69. } else {
  70. pool.PutBufPoolCopy(s.readBuffer)
  71. s.readBuffer = node.val
  72. s.endRead = node.l
  73. s.startRead = 0
  74. }
  75. }
  76. if len(buf) < s.endRead-s.startRead {
  77. n = copy(buf, s.readBuffer[s.startRead:s.startRead+len(buf)])
  78. s.startRead += n
  79. } else {
  80. n = copy(buf, s.readBuffer[s.startRead:s.endRead])
  81. s.startRead += n
  82. s.mux.sendInfo(MUX_MSG_SEND_OK, s.connId, nil)
  83. }
  84. return
  85. }
  86. func (s *conn) Write(buf []byte) (int, error) {
  87. if s.isClose {
  88. return 0, errors.New("the conn has closed")
  89. }
  90. ch := make(chan struct{})
  91. go s.write(buf, ch)
  92. if t := s.writeTimeOut.Sub(time.Now()); t > 0 {
  93. timer := time.NewTimer(t)
  94. defer timer.Stop()
  95. select {
  96. case <-timer.C:
  97. return 0, errors.New("write timeout")
  98. case <-ch:
  99. }
  100. } else {
  101. <-ch
  102. }
  103. if s.isClose {
  104. return 0, io.EOF
  105. }
  106. return len(buf), nil
  107. }
  108. func (s *conn) write(buf []byte, ch chan struct{}) {
  109. start := 0
  110. l := len(buf)
  111. for {
  112. if s.hasWrite > 50 {
  113. <-s.getStatusCh
  114. }
  115. s.hasWrite++
  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. }