1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
|
#ifndef BASIC_KINTERACTIVEPROTOCOL_KINTERACTIVEPROTOCOL_HPP_ #define BASIC_KINTERACTIVEPROTOCOL_KINTERACTIVEPROTOCOL_HPP_
#include <vector> #include <plog/plog.h> #include "tools.h"
class kInteractiveProtocol { public:
enum BinaryMode {
high,
low }; public:
typedef int64_t (*read_callback)(unsigned char *data, unsigned int offset, unsigned int data_len, unsigned int timeout, void *arg);
typedef int64_t (*send_callback)(const unsigned char *data, unsigned int offset, unsigned int data_len, void *arg);
private:
BinaryMode binaryMode = BinaryMode::low;
bool disable_crc32 = false;
unsigned char head[4]{0xFF, 0x27, 0x71, 0xFF};
long message_id_length = 8;
int64_t message_id = 0;
bool enable_custom_param = true;
unsigned char custom_param_len = 0;
std::string custom_param;
bool enable_type = true;
unsigned char type_len = 0;
std::string type;
unsigned int data_len = 0;
std::vector<unsigned char> data;
unsigned char crc32[4]{0x00, 0x00, 0x00, 0x00};
unsigned int crc32_init = 0;
unsigned int no_date_size = 4 + 1 + 1 + sizeof(unsigned int) + 4; public:
inline int64_t get_pack_size() { return no_date_size + get_data_size() + custom_param_len + type_len; }
inline explicit kInteractiveProtocol(long _message_id_length = 8, BinaryMode binary_mode = BinaryMode::low, bool _enable_custom_param = true, bool _enable_type = true, bool _disable_crc32 = false) { this->message_id_length = _message_id_length; this->binaryMode = binary_mode; this->enable_custom_param = _enable_custom_param; this->enable_type = _enable_type; this->disable_crc32 = _disable_crc32; };
inline bool check_mini_size(int64_t size) const { return no_date_size < size; }
inline int64_t get_message_id() { return message_id; }
inline void set_message_id(int64_t id) { message_id = id; }
inline std::string get_custom_param() { return custom_param; }
inline void set_custom_param(const std::string ¶m) { custom_param = param; custom_param_len = custom_param.size(); }
inline std::string get_type() { return type; }
inline unsigned int get_data_size() { return data.size(); }
inline std::string get_data_string() { std::string str; str.insert(str.end(), data.begin(), data.end()); str.push_back(0x00); return str; }
inline const std::vector<unsigned char> &get_data() { return data; }
inline unsigned int get_crc() { unsigned int crc = 0; memcpy((unsigned char *) &crc, crc32, 4); return crc; }
inline void get_crc(unsigned char crc[4]) { memcpy(crc, crc32, 4); }
inline void set_data(unsigned char *_data, unsigned int _data_len, const std::string &_type = "binary") { this->type = _type; if (type.empty()) { type_len = 0; } else { type_len = (type[type.size() - 1] == 0x00) ? (type.size() - 1) : (type.size()); } data.clear(); data.insert(data.end(), _data, _data + _data_len); data_len = data.size(); }
inline void set_data(std::vector<unsigned char> _data, const std::string &_type = "binary") { set_data(_data.data(), _data.size(), _type); }
inline void set_data(const std::string &_data, const std::string &_type = "text") { set_data((unsigned char *) _data.data(), _data.size(), _type); }
inline bool unpack(const unsigned char *_data, unsigned int len) { unsigned int offset = 0; if (len < no_date_size) { LOG(WARNING) << "not long enough [" << len << "][" << (5 + sizeof(unsigned int) + 4) << "]"; return false; } for (int i = 0; i < 4; i++) { if (_data[i] != head[i]) { LOG(WARNING) << "head is wrong"; return false; } } offset += 4; if (message_id_length == 8) { memcpy((unsigned char *) &message_id, &_data[offset], 8); if (binaryMode == BinaryMode::high) { tools::reverse((unsigned char *) &message_id, 8); } offset += 8; } else { message_id = 0; memcpy(((unsigned char *) &message_id) + 0, &_data[offset], 4); if (binaryMode == BinaryMode::high) { tools::reverse((unsigned char *) &message_id, 4); } offset += 4; }
custom_param.clear(); custom_param_len = 0; if (enable_custom_param) { custom_param_len = _data[offset]; offset += 1; if (custom_param_len > 0) { custom_param.insert(custom_param.end(), &_data[offset], &_data[offset + custom_param_len]); } offset += custom_param_len; }
type.clear(); type_len = 0; if (enable_type) { type_len = _data[offset]; offset += 1; if (type_len > 0) { type.insert(type.end(), &_data[offset], &_data[offset + type_len]); type.push_back(0x00); } offset += type_len; } data.clear(); memcpy((unsigned char *) &data_len, &_data[offset], sizeof(unsigned int)); if (binaryMode == BinaryMode::high) { tools::reverse((unsigned char *) &data_len, sizeof(unsigned int)); } offset += sizeof(unsigned int); if (len < data_len + no_date_size + message_id_length + type_len) { LOG(WARNING) << "not long enough:[" << len << "<" << (data_len + no_date_size + message_id_length + type_len) << "]"; return false; } data.insert(data.end(), &_data[offset], &_data[data_len + offset]); offset += data_len; unsigned int crc = tools::crc32(_data, offset, crc32_init); if (binaryMode == BinaryMode::high) { tools::reverse((unsigned char *) &crc, 4); } bool crc_flag = true; for (int i = 0; i < 4; i++) { if (_data[offset + i] != ((unsigned char *) &crc)[i]) { crc_flag = false; break; } } if (!crc_flag) { LOG(WARNING) << "crc is wrong"; return false; } if (disable_crc32) { memset(crc32, 0x00, 4); } else { memcpy(crc32, (unsigned char *) &crc, 4); } offset += 4; return true; }
inline bool unpack(const std::vector<unsigned char> &_data) { return unpack(_data.data(), _data.size()); }
inline bool unpack(read_callback cb, void *arg = nullptr, int timeout = 0) { std::vector<unsigned char> crc_data; int64_t offset = 0; if (cb == nullptr) { LOG(WARNING) << "read_callback is null"; return false; } long long end = tools::get_time_tick() + timeout; unsigned char d[51]; do { int64_t ret = cb(d, 0, 1, 100, arg); if (ret < 0) { LOG(WARNING) << "read_callback return :" << ret; return false; } if (ret == 0) { continue; } if (head[offset] != d[0]) { offset = d[0] == head[0] ? 1 : 0; } else { offset++; } if (offset == 4) { break; } } while (timeout <= 0 || end > tools::get_time_tick()); int64_t ret = 0; if (message_id_length == 8) { ret = cb((unsigned char *) &message_id, 0, sizeof(int64_t), 200, arg); if (binaryMode == BinaryMode::high) { tools::reverse((unsigned char *) &message_id, 8); } } else { message_id = 0; ret = cb(((unsigned char *) &message_id), 0, 4, 200, arg); if (binaryMode == BinaryMode::high) { tools::reverse((unsigned char *) &message_id, 4); } } if (ret != message_id_length) { LOG(WARNING) << "read_callback message_id return :" << ret; return false; }
if (enable_custom_param) { ret = cb(d, 0, 1, 200, arg); if (ret != 1) { LOG(WARNING) << "read_callback custom_param_len return :" << ret; return false; } custom_param_len = d[0]; if (custom_param_len >= 45) { PLOGW.printf("custom_param_len is %d", custom_param_len); return false; } offset = 0; custom_param.clear(); if (custom_param_len > 0) { do { ret = cb(d, 0, custom_param_len - offset, 200, arg); if (ret <= 0) { LOG(WARNING) << "read_callback custom_param return :" << ret; return false; } custom_param.insert(custom_param.end(), d, d + ret); offset += ret; if (offset == custom_param_len) { break; } } while (timeout <= 0 || end > tools::get_time_tick()); } }
if (enable_type) { ret = cb(d, 0, 1, 200, arg); if (ret != 1) { LOG(WARNING) << "read_callback type_len return :" << ret; return false; } type_len = d[0]; if (type_len >= 45) { PLOGW.printf("type_len is %d", type_len); return false; } offset = 0; type.clear(); if (type_len > 0) { do { ret = cb(d, 0, type_len - offset, 200, arg); if (ret <= 0) { LOG(WARNING) << "read_callback type return :" << ret; return false; } type.insert(type.end(), d, d + ret); offset += ret; if (offset == type_len) { break; } } while (timeout <= 0 || end > tools::get_time_tick()); type.push_back(0x00); } }
ret = cb(d, 0, 4, 200, arg); if (ret != 4) { LOG(WARNING) << "read_callback data_len return :" << ret; return false; } if (binaryMode == BinaryMode::high) { tools::reverse((unsigned char *) &d[0], 4); } memcpy((unsigned char *) &data_len, d, 4); offset = 0; data.clear(); if (data_len > 0) { do { unsigned int read_len = data_len - offset; if (read_len >= 50) { read_len = 50; } ret = cb(d, 0, read_len, 200, arg); if (ret <= 0) { LOG(WARNING) << "read_callback data return :" << ret << " ," << read_len; return false; } data.insert(data.end(), d, d + ret); offset += ret; if (offset == data_len) { break; } end = tools::get_time_tick() + timeout; } while (timeout <= 0 || end > tools::get_time_tick()); } ret = cb(crc32, 0, 4, 200, arg); if (binaryMode == BinaryMode::high) { tools::reverse((unsigned char *) &crc32[0], 4); } if (ret != 4) { LOG(WARNING) << "read_callback crc32 return :" << ret; return false; } if (disable_crc32 && crc32[0] == 0x00 && crc32[1] == 0x00 && crc32[2] == 0x00 && crc32[3] == 0x00) { return true; } crc_data.insert(crc_data.end(), head, head + 4); if (message_id_length == 8) { if (binaryMode == BinaryMode::high) { for (int i = 7; i >= 0; i--) { crc_data.push_back(((unsigned char *) &message_id)[i]); } } else { crc_data.insert(crc_data.end(), (unsigned char *) &message_id, ((unsigned char *) &message_id) + 8); } } else { if (binaryMode == BinaryMode::high) { for (int i = 3; i >= 0; i--) { crc_data.push_back(((unsigned char *) &message_id)[i]); } } else { crc_data.insert(crc_data.end(), ((unsigned char *) &message_id) + 0, ((unsigned char *) &message_id) + 4); } } if (enable_custom_param) { crc_data.push_back(custom_param_len); if (custom_param_len > 0) { crc_data.insert(crc_data.end(), custom_param.begin(), custom_param.begin() + custom_param_len); } } if (enable_type) { crc_data.push_back(type_len); if (type_len > 0) { crc_data.insert(crc_data.end(), type.begin(), type.begin() + type_len); } } if (binaryMode == BinaryMode::high) { for (int i = 3; i >= 0; i--) { crc_data.push_back(((unsigned char *) &data_len)[i]); } } else { crc_data.insert(crc_data.end(), (unsigned char *) &data_len, ((unsigned char *) &data_len) + 4); } crc_data.insert(crc_data.end(), data.begin(), data.end()); unsigned int crc = 0; memcpy((unsigned char *) &crc, crc32, 4);
unsigned int _crc = tools::crc32(crc_data.data(), crc_data.size(), crc32_init); if (_crc == crc) { return true; } LOG(WARNING) << "crc32 is wrong :[" << _crc << "][" << crc << "]"; return false; }
inline void pack(std::vector<unsigned char> &_data) const { _data.insert(_data.end(), &head[0], &head[4]); if (message_id_length == 8) { if (binaryMode == BinaryMode::high) { tools::reverse((unsigned char *) &message_id, 8); } _data.insert(_data.end(), ((unsigned char *) &message_id) + 0, ((unsigned char *) &message_id) + 8); if (binaryMode == BinaryMode::high) { tools::reverse((unsigned char *) &message_id, 8); } } else { if (binaryMode == BinaryMode::high) { tools::reverse((unsigned char *) &message_id, 4); } _data.insert(_data.end(), ((unsigned char *) &message_id) + 0, ((unsigned char *) &message_id) + 4); if (binaryMode == BinaryMode::high) { tools::reverse((unsigned char *) &message_id, 4); } } if (enable_custom_param) { if (custom_param.empty()) { _data.push_back(0x00); } else { unsigned int _custom_param_len = custom_param.size(); _data.push_back(_custom_param_len); _data.insert(_data.end(), custom_param.begin(), custom_param.begin() + _custom_param_len); } } if (enable_type) { if (type.empty()) { _data.push_back(0x00); } else { unsigned char _type_len = (type[type.size() - 1] == 0x00) ? (type.size() - 1) : (type.size()); _data.push_back(_type_len); _data.insert(_data.end(), type.begin(), type.begin() + _type_len); } } unsigned int _data_len = data.size(); if (binaryMode == BinaryMode::high) { tools::reverse((unsigned char *) &_data_len, 4); } _data.insert(_data.end(), ((unsigned char *) &_data_len) + 0, ((unsigned char *) &_data_len) + 4); if (!data.empty()) { _data.insert(_data.end(), data.begin(), data.end()); } unsigned int crc = 0; if (!disable_crc32) { tools::crc32(_data.data(), _data.size(), crc32_init); if (binaryMode == BinaryMode::high) { tools::reverse((unsigned char *) &crc, 4); } } _data.insert(_data.end(), ((unsigned char *) &crc) + 0, ((unsigned char *) &crc) + 4); }
inline bool pack(send_callback cb, void *arg, unsigned int data_size = 0) const { if (cb == nullptr) { LOG(WARNING) << "read_callback is null"; return false; } std::vector<unsigned char> _data; pack(_data); if (data_size == 0) { return _data.size() == cb(_data.data(), 0, _data.size(), arg); } unsigned int offset = 0; do { int64_t send_len = _data.size() - offset; if (send_len > data_size) { send_len = data_size; } int64_t ret = cb(_data.data(), offset, send_len, arg); if (ret <= 0) { LOG(WARNING) << "send : [" << ret << "," << send_len << "]"; return false; } offset += ret; } while (offset < _data.size()); return true; }
public: inline kInteractiveProtocol &operator=(const kInteractiveProtocol &old) { if (this == &old) { return *this; } std::vector<unsigned char> _data; this->message_id_length = old.message_id_length; old.pack(_data); unpack(_data); return (*this); }
inline unsigned char &operator[](const unsigned int index) { if (data.size() <= index) { static unsigned char error_type = 0xff; error_type = 0xff; LOG(WARNING) << "out of bounds"; return error_type; } return data[index]; }
void set_message_id_len(long i) { message_id_length = i; }
void set_crc32_init(long i) { crc32_init = i; }
void set_disable_crc32(bool flag = false) { disable_crc32 = flag; }
void set_binary_mode(BinaryMode binary_mode = BinaryMode::low) { binaryMode = binary_mode; } };
#endif
|