binaryReadWrite.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package TcpServerLib
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. )
  6. func packetTotalSize(data []byte) int16 {
  7. totalsize := binary.LittleEndian.Uint16(data)
  8. return int16(totalsize)
  9. }
  10. // 출처: https://github.com/gonet2/agent
  11. type RawPacketData struct {
  12. pos int
  13. data []byte
  14. order binary.ByteOrder
  15. }
  16. func MakeReader(buffer []byte, isLittleEndian bool) RawPacketData {
  17. if isLittleEndian {
  18. return RawPacketData{data: buffer, order: binary.LittleEndian}
  19. }
  20. return RawPacketData{data: buffer, order: binary.BigEndian}
  21. }
  22. func MakeWriter(buffer []byte, isLittleEndian bool) RawPacketData {
  23. if isLittleEndian {
  24. return RawPacketData{data: buffer, order: binary.LittleEndian}
  25. }
  26. return RawPacketData{data: buffer, order: binary.BigEndian}
  27. }
  28. func (p *RawPacketData) Data() []byte {
  29. return p.data
  30. }
  31. func (p *RawPacketData) Length() int {
  32. return len(p.data)
  33. }
  34. // =============================================== Readers
  35. func (p *RawPacketData) ReadBool() (ret bool, err error) {
  36. b, _err := p.ReadByte()
  37. if b != byte(1) {
  38. return false, _err
  39. }
  40. return true, _err
  41. }
  42. func (p *RawPacketData) ReadS8() (ret int8, err error) {
  43. _ret, _err := p.ReadByte()
  44. ret = int8(_ret)
  45. err = _err
  46. return
  47. }
  48. func (p *RawPacketData) ReadU16() (ret uint16, err error) {
  49. if p.pos+2 > len(p.data) {
  50. err = errors.New("read uint16 failed")
  51. return
  52. }
  53. buf := p.data[p.pos : p.pos+2]
  54. ret = p.order.Uint16(buf)
  55. p.pos += 2
  56. return
  57. }
  58. func (p *RawPacketData) ReadS16() (ret int16, err error) {
  59. _ret, _err := p.ReadU16()
  60. ret = int16(_ret)
  61. err = _err
  62. return
  63. }
  64. func (p *RawPacketData) ReadU32() (ret uint32, err error) {
  65. if p.pos+4 > len(p.data) {
  66. err = errors.New("read uint32 failed")
  67. return
  68. }
  69. buf := p.data[p.pos : p.pos+4]
  70. ret = p.order.Uint32(buf)
  71. p.pos += 4
  72. return
  73. }
  74. func (p *RawPacketData) ReadS32() (ret int32, err error) {
  75. _ret, _err := p.ReadU32()
  76. ret = int32(_ret)
  77. err = _err
  78. return
  79. }
  80. func (p *RawPacketData) ReadU64() (ret uint64, err error) {
  81. if p.pos+8 > len(p.data) {
  82. err = errors.New("read uint64 failed")
  83. return
  84. }
  85. buf := p.data[p.pos : p.pos+8]
  86. ret = p.order.Uint64(buf)
  87. p.pos += 8
  88. return
  89. }
  90. func (p *RawPacketData) ReadS64() (ret int64, err error) {
  91. _ret, _err := p.ReadU64()
  92. ret = int64(_ret)
  93. err = _err
  94. return
  95. }
  96. func (p *RawPacketData) ReadByte() (ret byte, err error) {
  97. if p.pos >= len(p.data) {
  98. err = errors.New("read byte failed")
  99. return
  100. }
  101. ret = p.data[p.pos]
  102. p.pos++
  103. return
  104. }
  105. func (p *RawPacketData) ReadBytes(readSize int) (refSlice []byte) {
  106. refSlice = p.data[p.pos : p.pos+readSize]
  107. p.pos += readSize
  108. return
  109. }
  110. func (p *RawPacketData) ReadString() (ret string, err error) {
  111. if p.pos+2 > len(p.data) {
  112. err = errors.New("read string header failed")
  113. return
  114. }
  115. size, _ := p.ReadU16()
  116. if p.pos+int(size) > len(p.data) {
  117. err = errors.New("read string Data failed")
  118. return
  119. }
  120. bytes := p.data[p.pos : p.pos+int(size)]
  121. p.pos += int(size)
  122. ret = string(bytes)
  123. return
  124. }
  125. /*
  126. func (p *RawPacketData) ReadFloat32() (ret float32, err error) {
  127. bits, _err := p.ReadU32()
  128. if _err != nil {
  129. return float32(0), _err
  130. }
  131. ret = math.Float32frombits(bits)
  132. if math.IsNaN(float64(ret)) || math.IsInf(float64(ret), 0) {
  133. return 0, nil
  134. }
  135. return ret, nil
  136. }
  137. func (p *RawPacketData) ReadFloat64() (ret float64, err error) {
  138. bits, _err := p.ReadU64()
  139. if _err != nil {
  140. return float64(0), _err
  141. }
  142. ret = math.Float64frombits(bits)
  143. if math.IsNaN(ret) || math.IsInf(ret, 0) {
  144. return 0, nil
  145. }
  146. return ret, nil
  147. }
  148. */
  149. // ================================================ Writers
  150. func (p *RawPacketData) WriteS8(v int8) {
  151. p.data[p.pos] = (byte)(v)
  152. p.pos++
  153. }
  154. func (p *RawPacketData) WriteU16(v uint16) {
  155. p.order.PutUint16(p.data[p.pos:], v)
  156. p.pos += 2
  157. }
  158. func (p *RawPacketData) WriteS16(v int16) {
  159. p.WriteU16(uint16(v))
  160. }
  161. func (p *RawPacketData) WriteBytes(v []byte) {
  162. copy(p.data[p.pos:], v)
  163. p.pos += len(v)
  164. }
  165. func (p *RawPacketData) WriteU32(v uint32) {
  166. p.order.PutUint32(p.data[p.pos:], v)
  167. p.pos += 4
  168. }
  169. func (p *RawPacketData) WriteS32(v int32) {
  170. p.WriteU32(uint32(v))
  171. }
  172. func (p *RawPacketData) WriteU64(v uint64) {
  173. p.order.PutUint64(p.data[p.pos:], v)
  174. p.pos += 8
  175. }
  176. func (p *RawPacketData) WriteS64(v int64) {
  177. p.WriteU64(uint64(v))
  178. }
  179. func (p *RawPacketData) WriteString(v string) {
  180. copyLen := copy(p.data[p.pos:], v)
  181. p.pos += copyLen
  182. }
  183. /*
  184. func (p *RawPacketData) WriteZeros(n int) {
  185. for i := 0; i < n; i++ {
  186. p.Data = append(p.Data, byte(0))
  187. }
  188. }
  189. func (p *RawPacketData) WriteBool(v bool) {
  190. if v {
  191. p.Data = append(p.Data, byte(1))
  192. } else {
  193. p.Data = append(p.Data, byte(0))
  194. }
  195. }
  196. func (p *RawPacketData) WriteByte(v byte) {
  197. p.Data = append(p.Data, v)
  198. }
  199. func (p *RawPacketData) WriteBytes(v []byte) {
  200. p.WriteU16(uint16(len(v)))
  201. p.Data = append(p.Data, v...)
  202. }
  203. func (p *RawPacketData) WriteRawBytes(v []byte) {
  204. p.Data = append(p.Data, v...)
  205. }
  206. func (p *RawPacketData) WriteString(v string) {
  207. bytes := []byte(v)
  208. p.WriteU16(uint16(len(bytes)))
  209. p.Data = append(p.Data, bytes...)
  210. }
  211. func (p *RawPacketData) WriteU24(v uint32) {
  212. p.Data = append(p.Data, byte(v>>16), byte(v>>8), byte(v))
  213. }
  214. func (p *RawPacketData) WriteFloat32(f float32) {
  215. v := math.Float32bits(f)
  216. p.WriteU32(v)
  217. }
  218. func (p *RawPacketData) WriteFloat64(f float64) {
  219. v := math.Float64bits(f)
  220. p.WriteU64(v)
  221. }
  222. */