erlang自定义二进制协议
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

251 satır
6.4 KiB

  1. function ByteArray(endian)
  2. local mRadix = {[8]="%03o", [10]="%03u", [16]="%02X"} -- 进制
  3. local mBuf = {} -- 二进制字节流
  4. local mPos = 1 -- 读写位置
  5. -- 验证读写位置
  6. local function checkAvailable()
  7. assert(#mBuf >= mPos, string.format("End of file was encountered. pos: %d, length: %d.", mPos, #mBuf))
  8. end
  9. -- 获取字符码
  10. local function getLetterCode(fmt)
  11. fmt = fmt or ""
  12. return ">"..fmt
  13. end
  14. -- 读单个字节
  15. local function readRawByte()
  16. checkAvailable()
  17. local rawByte = mBuf[mPos]
  18. mPos = mPos + 1
  19. return rawByte
  20. end
  21. -- 写单个字节
  22. local function writeRawByte(rawByte)
  23. if mPos > #mBuf + 1 then
  24. for i=#mBuf + 1, mPos - 1 do
  25. mBuf[i] = string.char(0)
  26. end
  27. end
  28. mBuf[mPos] = rawByte
  29. mPos = mPos + 1
  30. end
  31. -- 读字节流
  32. local function readBuf(length)
  33. checkAvailable()
  34. local buf = table.concat(mBuf, "", mPos, mPos + length - 1)
  35. mPos = mPos + length
  36. return buf
  37. end
  38. -- 写字节流
  39. local function writeBuf(buf)
  40. for i=1, #buf do
  41. writeRawByte(buf:sub(i, i))
  42. end
  43. end
  44. -- 读字符串
  45. local function read_string_bytes(length)
  46. if 0 == length then
  47. return ""
  48. end
  49. local tmp, value = string.unpack(readBuf(length), getLetterCode("A"..length))
  50. return value
  51. end
  52. -- 写字符串
  53. local function write_string_bytes(value)
  54. local buf = string.pack(getLetterCode("A"), value)
  55. writeBuf(buf)
  56. end
  57. ----------------------------------------------------------------------
  58. -- public method
  59. ----------------------------------------------------------------------
  60. local ba = {}
  61. -- 设置字节流
  62. ba.setBytes = function(buf)
  63. if #mBuf > 0 then
  64. return
  65. end
  66. writeBuf(buf)
  67. mPos = 1 -- 这里必须重置读写位置为1,方能保证接下去的读操作正确
  68. end
  69. -- 获取字节流
  70. ba.getBytes = function()
  71. local bytes = {}
  72. for i=1, #mBuf do
  73. bytes[#bytes+1] = string.byte(mBuf[i])
  74. end
  75. local packRes = string.pack(getLetterCode("b"..#bytes), unpack(bytes))
  76. return packRes
  77. end
  78. -- 获取字节流长度
  79. ba.getLength = function()
  80. return #mBuf
  81. end
  82. -- 字节流转为字符串,radix-8,10,16
  83. ba.toString = function(radix, separator)
  84. radix = radix or 16
  85. radix = mRadix[radix] or "%02X"
  86. separator = separator or " "
  87. local bytes = {}
  88. for i=1, #mBuf do
  89. bytes[i] = string.format(radix..separator, string.byte(mBuf[i]))
  90. end
  91. return table.concat(bytes)
  92. end
  93. ----------------------------------------------------------------------
  94. -- 读16位整型
  95. ba.read_int16 = function()
  96. local tmp, value = string.unpack(readBuf(2), getLetterCode("h"))
  97. return value
  98. end
  99. -- 写16位整型
  100. ba.write_int16 = function(value)
  101. local buf = string.pack(getLetterCode("h"), value)
  102. writeBuf(buf)
  103. end
  104. -- 读16位无符号整型
  105. ba.read_uint16 = function()
  106. local tmp, value = string.unpack(readBuf(2), getLetterCode("H"))
  107. return value
  108. end
  109. -- 写16位无符号整型
  110. ba.write_uint16 = function(value)
  111. local sstr = getLetterCode("H")
  112. local buf = string.pack(sstr, value)
  113. writeBuf(buf)
  114. end
  115. -- 读32位整型
  116. ba.read_int32 = function()
  117. local tmp, value = string.unpack(readBuf(4), getLetterCode("i"))
  118. return value
  119. end
  120. -- 写32位整型
  121. ba.write_int32 = function(value)
  122. local buf = string.pack(getLetterCode("i"), value)
  123. writeBuf(buf)
  124. end
  125. -- 读32位无符号整型
  126. ba.read_uint32 = function()
  127. local tmp, value = string.unpack(readBuf(4), getLetterCode("I"))
  128. return value
  129. end
  130. -- 写32位无符号整型
  131. ba.write_uint32 = function(value)
  132. local buf = string.pack(getLetterCode("I"), value)
  133. writeBuf(buf)
  134. end
  135. -- 读长整型
  136. ba.read_long = function()
  137. local tmp, value = string.unpack(readBuf(4), getLetterCode("l"))
  138. return value
  139. end
  140. -- 写长整型
  141. ba.write_long = function(value)
  142. local buf = string.pack(getLetterCode("l"), value)
  143. writeBuf(buf)
  144. end
  145. -- 读无符号长整型
  146. ba.read_ulong = function()
  147. local tmp, value = string.unpack(readBuf(4), getLetterCode("L"))
  148. return value
  149. end
  150. -- 写无符号长整型
  151. ba.write_ulong = function(value)
  152. local buf = string.pack(getLetterCode("L"), value)
  153. writeBuf(buf)
  154. end
  155. -- 读64位整型
  156. ba.read_int64 = function()
  157. -- local tmp, value = string.unpack(readBuf(8), getLetterCode("m"))
  158. -- return value
  159. return read_string_bytes(8)
  160. end
  161. -- 写64位整型
  162. ba.write_int64 = function(value)
  163. -- local buf = string.pack(getLetterCode("m"), value)
  164. -- writeBuf(buf)
  165. local buf = string.pack(getLetterCode("A"), value)
  166. writeBuf(buf)
  167. end
  168. -- 读64位无符号整型
  169. ba.read_uint64 = function()
  170. -- local tmp, value = string.unpack(readBuf(8), getLetterCode("M"))
  171. -- return value
  172. return read_string_bytes(8)
  173. end
  174. -- 写64位无符号整型
  175. ba.write_uint64 = function(value)
  176. -- local buf = string.pack(getLetterCode("M"), value)
  177. -- writeBuf(buf)
  178. local buf = string.pack(getLetterCode("A"), value)
  179. writeBuf(buf)
  180. end
  181. -- 读单精度浮点型
  182. ba.read_float = function()
  183. local tmp, value = string.unpack(readBuf(4), getLetterCode("f"))
  184. return value
  185. end
  186. -- 写单精度浮点型
  187. ba.write_float = function(value)
  188. local buf = string.pack(getLetterCode("f"), value)
  189. writeBuf(buf)
  190. end
  191. -- 读双精度浮点型
  192. ba.read_double = function()
  193. local tmp, value = string.unpack(readBuf(8), getLetterCode("d"))
  194. return value
  195. end
  196. -- 写双精度浮点型
  197. ba.write_double = function(value)
  198. local buf = string.pack(getLetterCode("d"), value)
  199. writeBuf(buf)
  200. end
  201. -- 读布尔型
  202. ba.read_bool = function()
  203. return 1 == read_char()
  204. end
  205. -- 写布尔型
  206. ba.write_bool = function(value)
  207. if value then
  208. ba.write_char(1)
  209. else
  210. ba.write_char(0)
  211. end
  212. end
  213. -- 读字符型
  214. ba.read_int8 = function()
  215. local tmp, value = string.unpack(readRawByte(), "c")
  216. return value
  217. end
  218. -- 写字符型
  219. ba.write_int8 = function(value)
  220. writeRawByte(string.pack("c", value))
  221. end
  222. -- 读单字节
  223. ba.read_uint8 = function()
  224. -- 方法1
  225. -- return string.byte(readRawByte())
  226. -- 方法2
  227. local tmp, value = string.unpack(readRawByte(), "b")
  228. return value
  229. end
  230. -- 写单字节
  231. ba.write_uint8 = function(value)
  232. -- 方法1
  233. -- writeRawByte(string.char(value))
  234. -- 方法2
  235. writeRawByte(string.pack("b", value))
  236. end
  237. -- 读字符串
  238. ba.read_string = function()
  239. local length = ba.read_uint16()
  240. return read_string_bytes(length)
  241. end
  242. -- 写字符串
  243. ba.write_string = function(value)
  244. local buf = string.pack(getLetterCode("A"), value)
  245. ba.write_uint16(#buf)
  246. writeBuf(buf)
  247. end
  248. ----------------------------------------------------------------------
  249. return ba
  250. end