conn.go 4.2 KB

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