netpackager.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package common
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "encoding/json"
  6. "github.com/cnlh/nps/lib/pool"
  7. "io"
  8. "strings"
  9. )
  10. type NetPackager interface {
  11. Pack(writer io.Writer) (err error)
  12. UnPack(reader io.Reader) (err error)
  13. }
  14. type BasePackager struct {
  15. Length uint32
  16. Content []byte
  17. }
  18. func (Self *BasePackager) NewPac(contents ...interface{}) (err error) {
  19. Self.Content = pool.CopyBuff.Get()
  20. Self.clean()
  21. for _, content := range contents {
  22. switch content.(type) {
  23. case nil:
  24. Self.Content = Self.Content[:0]
  25. case []byte:
  26. err = Self.appendByte(content.([]byte))
  27. case string:
  28. err = Self.appendByte([]byte(content.(string)))
  29. if err != nil {
  30. return
  31. }
  32. err = Self.appendByte([]byte(CONN_DATA_SEQ))
  33. default:
  34. err = Self.marshal(content)
  35. }
  36. }
  37. Self.setLength()
  38. return
  39. }
  40. func (Self *BasePackager) appendByte(data []byte) (err error) {
  41. m := len(Self.Content)
  42. n := m + len(data)
  43. if n <= cap(Self.Content) {
  44. Self.Content = Self.Content[0:n] // grow the length for copy
  45. copy(Self.Content[m:n], data)
  46. return nil
  47. } else {
  48. return bytes.ErrTooLarge
  49. }
  50. }
  51. //似乎这里涉及到父类作用域问题,当子类调用父类的方法时,其struct仅仅为父类的
  52. func (Self *BasePackager) Pack(writer io.Writer) (err error) {
  53. err = binary.Write(writer, binary.LittleEndian, Self.Length)
  54. if err != nil {
  55. return
  56. }
  57. err = binary.Write(writer, binary.LittleEndian, Self.Content)
  58. pool.CopyBuff.Put(Self.Content)
  59. return
  60. }
  61. //Unpack 会导致传入的数字类型转化成float64!!
  62. //主要原因是json unmarshal并未传入正确的数据类型
  63. func (Self *BasePackager) UnPack(reader io.Reader) (err error) {
  64. Self.Content = pool.CopyBuff.Get()
  65. Self.clean()
  66. err = binary.Read(reader, binary.LittleEndian, &Self.Length)
  67. if err != nil {
  68. return
  69. }
  70. Self.Content = Self.Content[:Self.Length]
  71. //n, err := io.ReadFull(reader, Self.Content)
  72. //if n != int(Self.Length) {
  73. // err = io.ErrUnexpectedEOF
  74. //}
  75. err = binary.Read(reader, binary.LittleEndian, Self.Content)
  76. return
  77. }
  78. func (Self *BasePackager) marshal(content interface{}) (err error) {
  79. tmp, err := json.Marshal(content)
  80. if err != nil {
  81. return err
  82. }
  83. err = Self.appendByte(tmp)
  84. return
  85. }
  86. func (Self *BasePackager) Unmarshal(content interface{}) (err error) {
  87. err = json.Unmarshal(Self.Content, content)
  88. if err != nil {
  89. return err
  90. }
  91. return
  92. }
  93. func (Self *BasePackager) setLength() {
  94. Self.Length = uint32(len(Self.Content))
  95. return
  96. }
  97. func (Self *BasePackager) clean() {
  98. Self.Length = 0
  99. Self.Content = Self.Content[:0] // reset length
  100. }
  101. func (Self *BasePackager) Split() (strList []string) {
  102. n := bytes.IndexByte(Self.Content, 0)
  103. strList = strings.Split(string(Self.Content[:n]), CONN_DATA_SEQ)
  104. strList = strList[0 : len(strList)-1]
  105. return
  106. }
  107. type ConnPackager struct { // Todo
  108. ConnType uint8
  109. BasePackager
  110. }
  111. func (Self *ConnPackager) NewPac(connType uint8, content ...interface{}) (err error) {
  112. Self.ConnType = connType
  113. err = Self.BasePackager.NewPac(content...)
  114. return
  115. }
  116. func (Self *ConnPackager) Pack(writer io.Writer) (err error) {
  117. err = binary.Write(writer, binary.LittleEndian, Self.ConnType)
  118. if err != nil {
  119. return
  120. }
  121. err = Self.BasePackager.Pack(writer)
  122. return
  123. }
  124. func (Self *ConnPackager) UnPack(reader io.Reader) (err error) {
  125. err = binary.Read(reader, binary.LittleEndian, &Self.ConnType)
  126. if err != nil && err != io.EOF {
  127. return
  128. }
  129. err = Self.BasePackager.UnPack(reader)
  130. return
  131. }
  132. type MuxPackager struct {
  133. Flag uint8
  134. Id int32
  135. BasePackager
  136. }
  137. func (Self *MuxPackager) NewPac(flag uint8, id int32, content ...interface{}) (err error) {
  138. Self.Flag = flag
  139. Self.Id = id
  140. if flag == MUX_NEW_MSG {
  141. err = Self.BasePackager.NewPac(content...)
  142. }
  143. return
  144. }
  145. func (Self *MuxPackager) Pack(writer io.Writer) (err error) {
  146. err = binary.Write(writer, binary.LittleEndian, Self.Flag)
  147. if err != nil {
  148. return
  149. }
  150. err = binary.Write(writer, binary.LittleEndian, Self.Id)
  151. if err != nil {
  152. return
  153. }
  154. if Self.Flag == MUX_NEW_MSG {
  155. err = Self.BasePackager.Pack(writer)
  156. }
  157. return
  158. }
  159. func (Self *MuxPackager) UnPack(reader io.Reader) (err error) {
  160. err = binary.Read(reader, binary.LittleEndian, &Self.Flag)
  161. if err != nil {
  162. return
  163. }
  164. err = binary.Read(reader, binary.LittleEndian, &Self.Id)
  165. if err != nil {
  166. return
  167. }
  168. if Self.Flag == MUX_NEW_MSG {
  169. err = Self.BasePackager.UnPack(reader)
  170. }
  171. return
  172. }