package.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. import { encrypt50, decrypt50 } from './crypto/index';
  2. /**
  3. * 4.0 行情报文协议(Len >= 15 byte,大端字节序)
  4. * Header byte 1 包头 0xFF
  5. * Length UInt32 4 长度
  6. * mainClassNumber byte 1 大类号
  7. * subClassNumber UInt16 2 小类号
  8. * SerialNumber UInt32 4 流水号
  9. * Mode byte 1 内容类型(0:ProtoBuff; 1:Json; 2:Zip+ProtoBuff)
  10. * Version byte 1 版本号
  11. * Content byte[] n 数据体 心跳数据体长度为0,目前不对4.0(行情)报文进行加解密
  12. * End byte 1 包尾 0x0
  13. */
  14. export class Package40 {
  15. /** 包头 */
  16. header = 0xff;
  17. /** 包长度 */
  18. packageLength = 0;
  19. /** 大类(服务端会自动变成回复的大类号) */
  20. mainClassNumber = 0;
  21. /** 小类 */
  22. subClassNumber = 0;
  23. /** 流水号 */
  24. serialNumber = 0;
  25. /** 内容类型 */
  26. mode = 0;
  27. /** 版本号 */
  28. version = 0;
  29. /** 数据体 */
  30. content?: Uint8Array;
  31. /** 包尾 */
  32. end = 0x0;
  33. /**
  34. * 构造函数,把报文字节流装箱成报文对象
  35. * @param bytes 报文字节流
  36. */
  37. constructor(sendData?: [mainClassNumber?: number, content?: Uint8Array], rspData?: Uint8Array) {
  38. if (sendData) {
  39. // 发送信息装箱
  40. this.mainClassNumber = sendData![0] as number;
  41. this.content = sendData![1];
  42. } else {
  43. // 接收信息装箱
  44. if (rspData!.length < 15) return;
  45. this.header = rspData![0];
  46. this.packageLength = new DataView(new Uint8Array(rspData!.subarray(1, 5)).buffer).getUint32(0, false);
  47. this.mainClassNumber = rspData![5];
  48. this.subClassNumber = new DataView(new Uint8Array(rspData!.subarray(6, 8)).buffer).getUint16(0, false);
  49. this.serialNumber = new DataView(new Uint8Array(rspData!.subarray(8, 12)).buffer).getUint32(0, false);
  50. this.mode = rspData![12];
  51. this.version = rspData![13];
  52. if (rspData!.length > 15) {
  53. this.content = rspData!.subarray(14, rspData!.length - 1);
  54. }
  55. this.end = rspData![rspData!.length - 1];
  56. }
  57. }
  58. /**
  59. * 获取可发送的包数据流
  60. */
  61. data(): Uint8Array {
  62. if (this.content) {
  63. this.packageLength = this.content.length + 15;
  64. } else {
  65. this.packageLength = 15;
  66. }
  67. let rst = new Uint8Array(this.packageLength);
  68. let pos = 0;
  69. // 包头
  70. rst[pos] = this.header;
  71. pos++;
  72. // 包长度
  73. let dataView = new DataView(new ArrayBuffer(4));
  74. dataView.setUint32(0, this.packageLength, false);
  75. rst.set(new Uint8Array(dataView.buffer), pos);
  76. pos += 4;
  77. // 大类号
  78. rst[pos] = this.mainClassNumber;
  79. pos++;
  80. // 小类号
  81. dataView = new DataView(new ArrayBuffer(2));
  82. dataView.setUint16(0, this.subClassNumber, false);
  83. rst.set(new Uint8Array(dataView.buffer), pos);
  84. pos += 2;
  85. // 流水号
  86. dataView = new DataView(new ArrayBuffer(4));
  87. dataView.setUint32(0, this.serialNumber, false);
  88. rst.set(new Uint8Array(dataView.buffer), pos);
  89. pos += 4;
  90. // 内容类型
  91. rst[pos] = this.mode;
  92. pos++;
  93. // 版本号
  94. rst[pos] = this.version;
  95. pos++;
  96. // 数据体
  97. if (this.content) rst.set(this.content, pos);
  98. // 包尾
  99. rst[this.packageLength - 1] = this.end;
  100. return rst;
  101. }
  102. }
  103. /**
  104. * 5.0 交易报文协议(Len >= 24 byte,小端字节序)
  105. * Header byte 1 包头 0xFF
  106. * Length UInt32 4 长度
  107. * FunCode UInt32 4 功能码(0代表心跳)
  108. * SessionID UInt32 4 会话ID
  109. * Mode byte 1 内容类型(0:ProtoBuff;1:Json;2:Zip+ProtoBuff)
  110. * Version byte 1 版本号
  111. * SerialNumber UInt32 4 流水号
  112. * Content byte[] n 数据内容(心跳数据体长度为0)
  113. * CRC UInt32 4 检验和 头19个字节检验和
  114. * End byte 1 包尾 0x0
  115. */
  116. export class Package50 {
  117. /** 包头 */
  118. header = 0xff;
  119. /** 长度 */
  120. packageLength = 0;
  121. /** 功能码(0代表心跳) */
  122. funCode = 0;
  123. /** 会话ID */
  124. sessionID = 0;
  125. /** 内容类型(0:ProtoBuff;1:Json;2:Zip+ProtoBuff) */
  126. mode = 0;
  127. /** 版本号 */
  128. version = 0;
  129. /** 流水号 */
  130. serialNumber = 0;
  131. /** 数据内容(心跳数据体长度为0) */
  132. content?: Uint8Array;
  133. /** 检验和 */
  134. crc = 0;
  135. /** 包尾 */
  136. end = 0;
  137. constructor(sendData?: [funCode: number, content?: Uint8Array], rspData?: Uint8Array) {
  138. if (sendData) {
  139. // 发送信息装箱
  140. this.funCode = sendData![0];
  141. this.content = sendData![1];
  142. } else if (rspData) {
  143. // 接收信息装箱
  144. if (rspData!.length < 24) return;
  145. // 先尝试对数据体进行解密
  146. let decryptData: Uint8Array | null = null;
  147. if (rspData!.length > 24) {
  148. const content = rspData!.subarray(19, rspData!.length - 4 - 1);
  149. const encryptData = content.subarray(4, content.length - 8);
  150. const size = new DataView(new Uint8Array(content.subarray(0, 4)).buffer).getUint32(0, true);
  151. decryptData = decrypt50(encryptData, size);
  152. }
  153. // 长度
  154. this.packageLength = new DataView(new Uint8Array(rspData!.subarray(1, 5)).buffer).getUint32(0, true);
  155. // 功能码
  156. this.funCode = new DataView(new Uint8Array(rspData!.subarray(5, 9)).buffer).getUint32(0, true);
  157. // 流水号
  158. this.serialNumber = new DataView(new Uint8Array(rspData!.subarray(15, 19)).buffer).getUint32(0, true);
  159. // 内容
  160. if (decryptData) {
  161. this.content = decryptData;
  162. }
  163. }
  164. }
  165. data(): Uint8Array {
  166. // 先尝试加密内容
  167. let encryptData: Uint8Array | null = null;
  168. if (this.content) {
  169. encryptData = encrypt50(this.content);
  170. }
  171. // 重新计算整包长度
  172. this.packageLength = 24;
  173. if (encryptData) {
  174. this.packageLength = 19 + encryptData.length + 5;
  175. }
  176. let rst = new Uint8Array(this.packageLength);
  177. let pos = 0;
  178. // 报文头
  179. rst[pos] = this.header;
  180. pos++;
  181. // 长度
  182. let dataView = new DataView(new ArrayBuffer(4));
  183. dataView.setUint32(0, this.packageLength, true);
  184. rst.set(new Uint8Array(dataView.buffer), pos);
  185. pos += 4;
  186. // 功能码
  187. dataView = new DataView(new ArrayBuffer(4));
  188. dataView.setUint32(0, this.funCode, true);
  189. rst.set(new Uint8Array(dataView.buffer), pos);
  190. pos += 4;
  191. // 会话ID
  192. dataView = new DataView(new ArrayBuffer(4));
  193. dataView.setUint32(0, this.sessionID, true);
  194. rst.set(new Uint8Array(dataView.buffer), pos);
  195. pos += 4;
  196. // 内容类型
  197. rst[pos] = 0x0;
  198. pos++;
  199. // 版本号
  200. rst[pos] = 0x0;
  201. pos++;
  202. // 流水号
  203. dataView = new DataView(new ArrayBuffer(4));
  204. dataView.setUint32(0, this.serialNumber, true);
  205. rst.set(new Uint8Array(dataView.buffer), pos);
  206. pos += 4;
  207. // 报文内容
  208. if (encryptData) {
  209. rst.set(encryptData, pos);
  210. pos += encryptData.length;
  211. }
  212. // 校验和
  213. let crcByte = rst.subarray(0, 19); // 复制报文头,以便于校验和计算
  214. dataView = new DataView(new ArrayBuffer(4));
  215. dataView.setUint32(0, this.getCRC32(crcByte), true);
  216. rst.set(new Uint8Array(dataView.buffer), pos);
  217. // 包尾
  218. rst[this.packageLength - 1] = this.end;
  219. return rst;
  220. }
  221. /**
  222. * 计算检验和
  223. * @param crcByte 目标数据
  224. */
  225. getCRC32(crcByte: Uint8Array) {
  226. let table = [
  227. 0x00000000,
  228. 0x77073096,
  229. 0xee0e612c,
  230. 0x990951ba,
  231. 0x076dc419,
  232. 0x706af48f,
  233. 0xe963a535,
  234. 0x9e6495a3,
  235. 0x0edb8832,
  236. 0x79dcb8a4,
  237. 0xe0d5e91e,
  238. 0x97d2d988,
  239. 0x09b64c2b,
  240. 0x7eb17cbd,
  241. 0xe7b82d07,
  242. 0x90bf1d91,
  243. 0x1db71064,
  244. 0x6ab020f2,
  245. 0xf3b97148,
  246. 0x84be41de,
  247. 0x1adad47d,
  248. 0x6ddde4eb,
  249. 0xf4d4b551,
  250. 0x83d385c7,
  251. 0x136c9856,
  252. 0x646ba8c0,
  253. 0xfd62f97a,
  254. 0x8a65c9ec,
  255. 0x14015c4f,
  256. 0x63066cd9,
  257. 0xfa0f3d63,
  258. 0x8d080df5,
  259. 0x3b6e20c8,
  260. 0x4c69105e,
  261. 0xd56041e4,
  262. 0xa2677172,
  263. 0x3c03e4d1,
  264. 0x4b04d447,
  265. 0xd20d85fd,
  266. 0xa50ab56b,
  267. 0x35b5a8fa,
  268. 0x42b2986c,
  269. 0xdbbbc9d6,
  270. 0xacbcf940,
  271. 0x32d86ce3,
  272. 0x45df5c75,
  273. 0xdcd60dcf,
  274. 0xabd13d59,
  275. 0x26d930ac,
  276. 0x51de003a,
  277. 0xc8d75180,
  278. 0xbfd06116,
  279. 0x21b4f4b5,
  280. 0x56b3c423,
  281. 0xcfba9599,
  282. 0xb8bda50f,
  283. 0x2802b89e,
  284. 0x5f058808,
  285. 0xc60cd9b2,
  286. 0xb10be924,
  287. 0x2f6f7c87,
  288. 0x58684c11,
  289. 0xc1611dab,
  290. 0xb6662d3d,
  291. 0x76dc4190,
  292. 0x01db7106,
  293. 0x98d220bc,
  294. 0xefd5102a,
  295. 0x71b18589,
  296. 0x06b6b51f,
  297. 0x9fbfe4a5,
  298. 0xe8b8d433,
  299. 0x7807c9a2,
  300. 0x0f00f934,
  301. 0x9609a88e,
  302. 0xe10e9818,
  303. 0x7f6a0dbb,
  304. 0x086d3d2d,
  305. 0x91646c97,
  306. 0xe6635c01,
  307. 0x6b6b51f4,
  308. 0x1c6c6162,
  309. 0x856530d8,
  310. 0xf262004e,
  311. 0x6c0695ed,
  312. 0x1b01a57b,
  313. 0x8208f4c1,
  314. 0xf50fc457,
  315. 0x65b0d9c6,
  316. 0x12b7e950,
  317. 0x8bbeb8ea,
  318. 0xfcb9887c,
  319. 0x62dd1ddf,
  320. 0x15da2d49,
  321. 0x8cd37cf3,
  322. 0xfbd44c65,
  323. 0x4db26158,
  324. 0x3ab551ce,
  325. 0xa3bc0074,
  326. 0xd4bb30e2,
  327. 0x4adfa541,
  328. 0x3dd895d7,
  329. 0xa4d1c46d,
  330. 0xd3d6f4fb,
  331. 0x4369e96a,
  332. 0x346ed9fc,
  333. 0xad678846,
  334. 0xda60b8d0,
  335. 0x44042d73,
  336. 0x33031de5,
  337. 0xaa0a4c5f,
  338. 0xdd0d7cc9,
  339. 0x5005713c,
  340. 0x270241aa,
  341. 0xbe0b1010,
  342. 0xc90c2086,
  343. 0x5768b525,
  344. 0x206f85b3,
  345. 0xb966d409,
  346. 0xce61e49f,
  347. 0x5edef90e,
  348. 0x29d9c998,
  349. 0xb0d09822,
  350. 0xc7d7a8b4,
  351. 0x59b33d17,
  352. 0x2eb40d81,
  353. 0xb7bd5c3b,
  354. 0xc0ba6cad,
  355. 0xedb88320,
  356. 0x9abfb3b6,
  357. 0x03b6e20c,
  358. 0x74b1d29a,
  359. 0xead54739,
  360. 0x9dd277af,
  361. 0x04db2615,
  362. 0x73dc1683,
  363. 0xe3630b12,
  364. 0x94643b84,
  365. 0x0d6d6a3e,
  366. 0x7a6a5aa8,
  367. 0xe40ecf0b,
  368. 0x9309ff9d,
  369. 0x0a00ae27,
  370. 0x7d079eb1,
  371. 0xf00f9344,
  372. 0x8708a3d2,
  373. 0x1e01f268,
  374. 0x6906c2fe,
  375. 0xf762575d,
  376. 0x806567cb,
  377. 0x196c3671,
  378. 0x6e6b06e7,
  379. 0xfed41b76,
  380. 0x89d32be0,
  381. 0x10da7a5a,
  382. 0x67dd4acc,
  383. 0xf9b9df6f,
  384. 0x8ebeeff9,
  385. 0x17b7be43,
  386. 0x60b08ed5,
  387. 0xd6d6a3e8,
  388. 0xa1d1937e,
  389. 0x38d8c2c4,
  390. 0x4fdff252,
  391. 0xd1bb67f1,
  392. 0xa6bc5767,
  393. 0x3fb506dd,
  394. 0x48b2364b,
  395. 0xd80d2bda,
  396. 0xaf0a1b4c,
  397. 0x36034af6,
  398. 0x41047a60,
  399. 0xdf60efc3,
  400. 0xa867df55,
  401. 0x316e8eef,
  402. 0x4669be79,
  403. 0xcb61b38c,
  404. 0xbc66831a,
  405. 0x256fd2a0,
  406. 0x5268e236,
  407. 0xcc0c7795,
  408. 0xbb0b4703,
  409. 0x220216b9,
  410. 0x5505262f,
  411. 0xc5ba3bbe,
  412. 0xb2bd0b28,
  413. 0x2bb45a92,
  414. 0x5cb36a04,
  415. 0xc2d7ffa7,
  416. 0xb5d0cf31,
  417. 0x2cd99e8b,
  418. 0x5bdeae1d,
  419. 0x9b64c2b0,
  420. 0xec63f226,
  421. 0x756aa39c,
  422. 0x026d930a,
  423. 0x9c0906a9,
  424. 0xeb0e363f,
  425. 0x72076785,
  426. 0x05005713,
  427. 0x95bf4a82,
  428. 0xe2b87a14,
  429. 0x7bb12bae,
  430. 0x0cb61b38,
  431. 0x92d28e9b,
  432. 0xe5d5be0d,
  433. 0x7cdcefb7,
  434. 0x0bdbdf21,
  435. 0x86d3d2d4,
  436. 0xf1d4e242,
  437. 0x68ddb3f8,
  438. 0x1fda836e,
  439. 0x81be16cd,
  440. 0xf6b9265b,
  441. 0x6fb077e1,
  442. 0x18b74777,
  443. 0x88085ae6,
  444. 0xff0f6a70,
  445. 0x66063bca,
  446. 0x11010b5c,
  447. 0x8f659eff,
  448. 0xf862ae69,
  449. 0x616bffd3,
  450. 0x166ccf45,
  451. 0xa00ae278,
  452. 0xd70dd2ee,
  453. 0x4e048354,
  454. 0x3903b3c2,
  455. 0xa7672661,
  456. 0xd06016f7,
  457. 0x4969474d,
  458. 0x3e6e77db,
  459. 0xaed16a4a,
  460. 0xd9d65adc,
  461. 0x40df0b66,
  462. 0x37d83bf0,
  463. 0xa9bcae53,
  464. 0xdebb9ec5,
  465. 0x47b2cf7f,
  466. 0x30b5ffe9,
  467. 0xbdbdf21c,
  468. 0xcabac28a,
  469. 0x53b39330,
  470. 0x24b4a3a6,
  471. 0xbad03605,
  472. 0xcdd70693,
  473. 0x54de5729,
  474. 0x23d967bf,
  475. 0xb3667a2e,
  476. 0xc4614ab8,
  477. 0x5d681b02,
  478. 0x2a6f2b94,
  479. 0xb40bbe37,
  480. 0xc30c8ea1,
  481. 0x5a05df1b,
  482. 0x2d02ef8d,
  483. ];
  484. let crc = 0xffffffff;
  485. for (let i = 0; i < crcByte.length; i++) {
  486. crc = (crc >>> 8) ^ table[(crc ^ crcByte[i]) & 0xff];
  487. }
  488. crc = crc ^ 0xffffffff;
  489. return crc;
  490. }
  491. }