开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
forked from OpenHarmony/developtools_hdc
关闭
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (138)
标签 (38)
master
weekly_20240422
revert-merge-1025-master
weekly_20240415
weekly_20240408
revert-merge-1004-master
weekly_20240401
weekly_20240325
revert-merge-971-master
weekly_20240318
weekly_20240311
revert-merge-921-master
weekly_20240304
revert-merge-937-master
weekly_20240226
OpenHarmony-4.1-Release
weekly_20240219
weekly_20240212
weekly_20240205
weekly_20240129
OpenHarmony-v4.1-Release
weekly_20240115-v
master-v
OpenHarmony-v4.1-Beta1
OpenHarmony-v4.0-Release
OpenHarmony-v4.0.1-Release
OpenHarmony-v4.0-Beta2
OpenHarmony-v3.2.3-Release
OpenHarmony-v3.2.4-Release
OpenHarmony-v4.0-Beta1
OpenHarmony-v3.2-Release
OpenHarmony-v3.2.1-Release
OpenHarmony-v3.2.2-Release
OpenHarmony-v3.2-Beta5
OpenHarmony-v3.1.5-Release
OpenHarmony-v3.1.6-Release
OpenHarmony-v3.1.7-Release
OpenHarmony-v3.2-Beta4
OpenHarmony-v3.1.4-Release
OpenHarmony-v3.2-Beta3
master
分支 (138)
标签 (38)
master
weekly_20240422
revert-merge-1025-master
weekly_20240415
weekly_20240408
revert-merge-1004-master
weekly_20240401
weekly_20240325
revert-merge-971-master
weekly_20240318
weekly_20240311
revert-merge-921-master
weekly_20240304
revert-merge-937-master
weekly_20240226
OpenHarmony-4.1-Release
weekly_20240219
weekly_20240212
weekly_20240205
weekly_20240129
OpenHarmony-v4.1-Release
weekly_20240115-v
master-v
OpenHarmony-v4.1-Beta1
OpenHarmony-v4.0-Release
OpenHarmony-v4.0.1-Release
OpenHarmony-v4.0-Beta2
OpenHarmony-v3.2.3-Release
OpenHarmony-v3.2.4-Release
OpenHarmony-v4.0-Beta1
OpenHarmony-v3.2-Release
OpenHarmony-v3.2.1-Release
OpenHarmony-v3.2.2-Release
OpenHarmony-v3.2-Beta5
OpenHarmony-v3.1.5-Release
OpenHarmony-v3.1.6-Release
OpenHarmony-v3.1.7-Release
OpenHarmony-v3.2-Beta4
OpenHarmony-v3.1.4-Release
OpenHarmony-v3.2-Beta3
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (138)
标签 (38)
master
weekly_20240422
revert-merge-1025-master
weekly_20240415
weekly_20240408
revert-merge-1004-master
weekly_20240401
weekly_20240325
revert-merge-971-master
weekly_20240318
weekly_20240311
revert-merge-921-master
weekly_20240304
revert-merge-937-master
weekly_20240226
OpenHarmony-4.1-Release
weekly_20240219
weekly_20240212
weekly_20240205
weekly_20240129
OpenHarmony-v4.1-Release
weekly_20240115-v
master-v
OpenHarmony-v4.1-Beta1
OpenHarmony-v4.0-Release
OpenHarmony-v4.0.1-Release
OpenHarmony-v4.0-Beta2
OpenHarmony-v3.2.3-Release
OpenHarmony-v3.2.4-Release
OpenHarmony-v4.0-Beta1
OpenHarmony-v3.2-Release
OpenHarmony-v3.2.1-Release
OpenHarmony-v3.2.2-Release
OpenHarmony-v3.2-Beta5
OpenHarmony-v3.1.5-Release
OpenHarmony-v3.1.6-Release
OpenHarmony-v3.1.7-Release
OpenHarmony-v3.2-Beta4
OpenHarmony-v3.1.4-Release
OpenHarmony-v3.2-Beta3
developtools_hdc
/
src
/
common
/
session.cpp
developtools_hdc
/
src
/
common
/
session.cpp
session.cpp 51.23 KB
一键复制 编辑 原始数据 按行查看 历史
maliang 提交于 2024年04月25日 19:29 +08:00 . feat:告警清理2
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 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "session.h"
#ifndef TEST_HASH
#include "hdc_hash_gen.h"
#endif
#include "serial_struct.h"
namespace Hdc {
HdcSessionBase::HdcSessionBase(bool serverOrDaemonIn, size_t uvThreadSize)
{
// print version pid
WRITE_LOG(LOG_INFO, "Program running. %s Pid:%u", Base::GetVersion().c_str(), getpid());
// server/daemon common initialization code
if (uvThreadSize < SIZE_THREAD_POOL_MIN) {
uvThreadSize = SIZE_THREAD_POOL_MIN;
} else if (uvThreadSize > SIZE_THREAD_POOL_MAX) {
uvThreadSize = SIZE_THREAD_POOL_MAX;
}
threadPoolCount = uvThreadSize;
WRITE_LOG(LOG_INFO, "set UV_THREADPOOL_SIZE:%zu", threadPoolCount);
string uvThreadEnv("UV_THREADPOOL_SIZE");
string uvThreadVal = std::to_string(threadPoolCount);
#ifdef _WIN32
uvThreadEnv += "=";
uvThreadEnv += uvThreadVal;
_putenv(uvThreadEnv.c_str());
#else
setenv(uvThreadEnv.c_str(), uvThreadVal.c_str(), 1);
#endif
uv_loop_init(&loopMain);
WRITE_LOG(LOG_DEBUG, "loopMain init");
uv_rwlock_init(&mainAsync);
uv_async_init(&loopMain, &asyncMainLoop, MainAsyncCallback);
uv_rwlock_init(&lockMapSession);
serverOrDaemon = serverOrDaemonIn;
ctxUSB = nullptr;
wantRestart = false;
threadSessionMain = uv_thread_self();
#ifdef HDC_HOST
if (serverOrDaemon) {
if (libusb_init((libusb_context **)&ctxUSB) != 0) {
ctxUSB = nullptr;
WRITE_LOG(LOG_FATAL, "libusb_init failed ctxUSB is nullptr");
}
}
#endif
}
HdcSessionBase::~HdcSessionBase()
{
Base::TryCloseHandle((uv_handle_t *)&asyncMainLoop);
uv_loop_close(&loopMain);
// clear base
uv_rwlock_destroy(&mainAsync);
uv_rwlock_destroy(&lockMapSession);
#ifdef HDC_HOST
if (serverOrDaemon and ctxUSB != nullptr) {
libusb_exit((libusb_context *)ctxUSB);
}
#endif
WRITE_LOG(LOG_DEBUG, "~HdcSessionBase free sessionRef:%u instance:%s", uint32_t(sessionRef),
serverOrDaemon ? "server" : "daemon");
}
// remove step2
bool HdcSessionBase::TryRemoveTask(HTaskInfo hTask)
{
if (hTask->taskFree) {
WRITE_LOG(LOG_WARN, "TryRemoveTask channelId:%u", hTask->channelId);
return true;
}
bool ret = RemoveInstanceTask(OP_REMOVE, hTask);
if (ret) {
hTask->taskFree = true;
} else {
// This is used to check that the memory cannot be cleaned up. If the memory cannot be released, break point
// here to see which task has not been released
// print task clear
}
return ret;
}
// remove step1
void HdcSessionBase::BeginRemoveTask(HTaskInfo hTask)
{
StartTraceScope("HdcSessionBase::BeginRemoveTask");
if (hTask->taskStop || hTask->taskFree) {
WRITE_LOG(LOG_WARN, "BeginRemoveTask channelId:%u taskStop:%d taskFree:%d",
hTask->channelId, hTask->taskStop, hTask->taskFree);
return;
}
WRITE_LOG(LOG_DEBUG, "BeginRemoveTask taskType:%d channelId:%u", hTask->taskType, hTask->channelId);
bool ret = RemoveInstanceTask(OP_CLEAR, hTask);
if (!ret) {
WRITE_LOG(LOG_INFO, "RemoveInstanceTask false taskType:%d channelId:%u", hTask->taskType, hTask->channelId);
}
auto taskClassDeleteRetry = [](uv_timer_t *handle) -> void {
StartTraceScope("HdcSessionBase::BeginRemoveTask taskClassDeleteRetry");
HTaskInfo hTask = (HTaskInfo)handle->data;
HdcSessionBase *thisClass = (HdcSessionBase *)hTask->ownerSessionClass;
constexpr uint32_t count = 1000;
if (hTask->closeRetryCount == 0 || hTask->closeRetryCount > count) {
WRITE_LOG(LOG_DEBUG, "TaskDelay task remove retry count %d/%d, taskType:%d channelId:%u, sessionId:%u",
hTask->closeRetryCount, GLOBAL_TIMEOUT, hTask->taskType, hTask->channelId, hTask->sessionId);
hTask->closeRetryCount = 1;
}
hTask->closeRetryCount++;
if (!thisClass->TryRemoveTask(hTask)) {
WRITE_LOG(LOG_WARN, "TaskDelay TryRemoveTask false channelId:%u", hTask->channelId);
return;
}
WRITE_LOG(LOG_DEBUG, "TaskDelay task remove finish, channelId:%u", hTask->channelId);
if (hTask != nullptr) {
delete hTask;
hTask = nullptr;
}
Base::TryCloseHandle((uv_handle_t *)handle, Base::CloseTimerCallback);
};
Base::TimerUvTask(hTask->runLoop, hTask, taskClassDeleteRetry, (GLOBAL_TIMEOUT * TIME_BASE) / UV_DEFAULT_INTERVAL);
hTask->taskStop = true;
}
// Clear all Task or a single Task, the regular situation is stopped first, and the specific class memory is cleaned up
// after the end of the LOOP.
// When ChannelIdinput == 0, at this time, all of the LOOP ends, all runs in the class end, so directly skip STOP,
// physical memory deletion class trimming
void HdcSessionBase::ClearOwnTasks(HSession hSession, const uint32_t channelIDInput)
{
// First case: normal task cleanup process (STOP Remove)
// Second: The task is cleaned up, the session ends
// Third: The task is cleaned up, and the session is directly over the session.
StartTraceScope("HdcSessionBase::ClearOwnTasks");
hSession->mapTaskMutex.lock();
map<uint32_t, HTaskInfo>::iterator iter;
for (iter = hSession->mapTask->begin(); iter != hSession->mapTask->end();) {
uint32_t channelId = iter->first;
HTaskInfo hTask = iter->second;
if (channelIDInput != 0) { // single
if (channelIDInput != channelId) {
++iter;
continue;
}
BeginRemoveTask(hTask);
WRITE_LOG(LOG_DEBUG, "ClearOwnTasks OP_CLEAR finish, sessionId:%u channelIDInput:%u",
hSession->sessionId, channelIDInput);
iter = hSession->mapTask->erase(iter);
break;
}
// multi
BeginRemoveTask(hTask);
iter = hSession->mapTask->erase(iter);
}
hSession->mapTaskMutex.unlock();
}
void HdcSessionBase::ClearSessions()
{
// no need to lock mapSession
// broadcast free signal
for (auto v : mapSession) {
HSession hSession = (HSession)v.second;
if (!hSession->isDead) {
FreeSession(hSession->sessionId);
}
}
}
void HdcSessionBase::ReMainLoopForInstanceClear()
{ // reloop
StartTraceScope("HdcSessionBase::ReMainLoopForInstanceClear");
auto clearSessionsForFinish = [](uv_idle_t *handle) -> void {
HdcSessionBase *thisClass = (HdcSessionBase *)handle->data;
if (thisClass->sessionRef > 0) {
return;
}
// all task has been free
uv_close((uv_handle_t *)handle, Base::CloseIdleCallback);
uv_stop(&thisClass->loopMain);
};
Base::IdleUvTask(&loopMain, this, clearSessionsForFinish);
uv_run(&loopMain, UV_RUN_DEFAULT);
};
#ifdef HDC_SUPPORT_UART
void HdcSessionBase::EnumUARTDeviceRegister(UartKickoutZombie kickOut)
{
uv_rwlock_rdlock(&lockMapSession);
map<uint32_t, HSession>::iterator i;
for (i = mapSession.begin(); i != mapSession.end(); ++i) {
HSession hs = i->second;
if ((hs->connType != CONN_SERIAL) or (hs->hUART == nullptr)) {
continue;
}
kickOut(hs);
break;
}
uv_rwlock_rdunlock(&lockMapSession);
}
#endif
void HdcSessionBase::EnumUSBDeviceRegister(void (*pCallBack)(HSession hSession))
{
if (!pCallBack) {
return;
}
uv_rwlock_rdlock(&lockMapSession);
map<uint32_t, HSession>::iterator i;
for (i = mapSession.begin(); i != mapSession.end(); ++i) {
HSession hs = i->second;
if (hs->connType != CONN_USB) {
continue;
}
if (hs->hUSB == nullptr) {
continue;
}
if (pCallBack) {
pCallBack(hs);
}
break;
}
uv_rwlock_rdunlock(&lockMapSession);
}
// The PC side gives the device information, determines if the USB device is registered
// PDEV and Busid Devid two choices
HSession HdcSessionBase::QueryUSBDeviceRegister(void *pDev, uint8_t busIDIn, uint8_t devIDIn)
{
#ifdef HDC_HOST
libusb_device *dev = (libusb_device *)pDev;
HSession hResult = nullptr;
if (!mapSession.size()) {
return nullptr;
}
uint8_t busId = 0;
uint8_t devId = 0;
if (pDev) {
busId = libusb_get_bus_number(dev);
devId = libusb_get_device_address(dev);
} else {
busId = busIDIn;
devId = devIDIn;
}
uv_rwlock_rdlock(&lockMapSession);
map<uint32_t, HSession>::iterator i;
for (i = mapSession.begin(); i != mapSession.end(); ++i) {
HSession hs = i->second;
if (hs->connType == CONN_USB) {
continue;
}
if (hs->hUSB == nullptr) {
continue;
}
if (hs->hUSB->devId != devId || hs->hUSB->busId != busId) {
continue;
}
hResult = hs;
break;
}
uv_rwlock_rdunlock(&lockMapSession);
return hResult;
#else
return nullptr;
#endif
}
void HdcSessionBase::AsyncMainLoopTask(uv_idle_t *handle)
{
AsyncParam *param = (AsyncParam *)handle->data;
HdcSessionBase *thisClass = (HdcSessionBase *)param->thisClass;
switch (param->method) {
case ASYNC_FREE_SESSION:
// Destruction is unified in the main thread
thisClass->FreeSession(param->sid);
break;
case ASYNC_STOP_MAINLOOP:
uv_stop(&thisClass->loopMain);
break;
default:
break;
}
if (param->data) {
delete[]((uint8_t *)param->data);
}
delete param;
param = nullptr;
Base::TryCloseHandle((uv_handle_t *)handle, Base::CloseIdleCallback);
}
void HdcSessionBase::MainAsyncCallback(uv_async_t *handle)
{
HdcSessionBase *thisClass = (HdcSessionBase *)handle->data;
list<void *>::iterator i;
list<void *> &lst = thisClass->lstMainThreadOP;
uv_rwlock_wrlock(&thisClass->mainAsync);
for (i = lst.begin(); i != lst.end();) {
AsyncParam *param = (AsyncParam *)*i;
Base::IdleUvTask(&thisClass->loopMain, param, AsyncMainLoopTask);
i = lst.erase(i);
}
uv_rwlock_wrunlock(&thisClass->mainAsync);
}
void HdcSessionBase::PushAsyncMessage(const uint32_t sessionId, const uint8_t method, const void *data,
const int dataSize)
{
AsyncParam *param = new AsyncParam();
if (!param) {
return;
}
param->sid = sessionId;
param->thisClass = this;
param->method = method;
if (dataSize > 0) {
param->dataSize = dataSize;
param->data = new uint8_t[param->dataSize]();
if (!param->data) {
delete param;
return;
}
if (memcpy_s((uint8_t *)param->data, param->dataSize, data, dataSize)) {
delete[]((uint8_t *)param->data);
delete param;
return;
}
}
asyncMainLoop.data = this;
uv_rwlock_wrlock(&mainAsync);
lstMainThreadOP.push_back(param);
uv_rwlock_wrunlock(&mainAsync);
uv_async_send(&asyncMainLoop);
}
void HdcSessionBase::WorkerPendding()
{
uv_run(&loopMain, UV_RUN_DEFAULT);
ClearInstanceResource();
}
int HdcSessionBase::MallocSessionByConnectType(HSession hSession)
{
int ret = 0;
switch (hSession->connType) {
case CONN_TCP: {
uv_tcp_init(&loopMain, &hSession->hWorkTCP);
++hSession->uvHandleRef;
hSession->hWorkTCP.data = hSession;
break;
}
case CONN_USB: {
// Some members need to be placed at the primary thread
HUSB hUSB = new HdcUSB();
if (!hUSB) {
ret = -1;
break;
}
hSession->hUSB = hUSB;
hSession->hUSB->wMaxPacketSizeSend = MAX_PACKET_SIZE_HISPEED;
break;
}
#ifdef HDC_SUPPORT_UART
case CONN_SERIAL: {
HUART hUART = new HdcUART();
if (!hUART) {
ret = -1;
break;
}
hSession->hUART = hUART;
break;
}
#endif // HDC_SUPPORT_UART
default:
ret = -1;
break;
}
return ret;
}
// Avoid unit test when client\server\daemon on the same host, maybe get the same ID value
uint32_t HdcSessionBase::GetSessionPseudoUid()
{
uint32_t uid = 0;
do {
uid = static_cast<uint32_t>(Base::GetRandom());
} while (AdminSession(OP_QUERY, uid, nullptr) != nullptr);
return uid;
}
// when client 0 to automatic generated,when daemon First place 1 followed by
HSession HdcSessionBase::MallocSession(bool serverOrDaemon, const ConnType connType, void *classModule,
uint32_t sessionId)
{
#ifdef CONFIG_USE_JEMALLOC_DFX_INIF
mallopt(M_DELAYED_FREE, M_DELAYED_FREE_DISABLE);
mallopt(M_SET_THREAD_CACHE, M_THREAD_CACHE_DISABLE);
#endif
HSession hSession = new(std::nothrow) HdcSession();
if (!hSession) {
WRITE_LOG(LOG_FATAL, "MallocSession new hSession failed");
return nullptr;
}
int ret = 0;
++sessionRef;
hSession->classInstance = this;
hSession->connType = connType;
hSession->classModule = classModule;
hSession->isDead = false;
hSession->sessionId = ((sessionId == 0) ? GetSessionPseudoUid() : sessionId);
hSession->serverOrDaemon = serverOrDaemon;
hSession->hWorkThread = uv_thread_self();
hSession->mapTask = new(std::nothrow) map<uint32_t, HTaskInfo>();
if (hSession->mapTask == nullptr) {
WRITE_LOG(LOG_FATAL, "MallocSession new hSession->mapTask failed");
delete hSession;
hSession = nullptr;
return nullptr;
}
hSession->listKey = new(std::nothrow) list<void *>;
if (hSession->listKey == nullptr) {
WRITE_LOG(LOG_FATAL, "MallocSession new hSession->listKey failed");
delete hSession;
hSession = nullptr;
return nullptr;
}
uv_loop_init(&hSession->childLoop);
hSession->uvHandleRef = 0;
// pullup child
WRITE_LOG(LOG_DEBUG, "HdcSessionBase NewSession, sessionId:%u, connType:%d.",
hSession->sessionId, hSession->connType);
++hSession->uvHandleRef;
Base::CreateSocketPair(hSession->ctrlFd);
size_t handleSize = sizeof(uv_poll_t);
hSession->pollHandle[STREAM_WORK] = (uv_poll_t *)malloc(handleSize);
hSession->pollHandle[STREAM_MAIN] = (uv_poll_t *)malloc(handleSize);
uv_poll_t *pollHandleMain = hSession->pollHandle[STREAM_MAIN];
if (pollHandleMain == nullptr || hSession->pollHandle[STREAM_WORK] == nullptr) {
WRITE_LOG(LOG_FATAL, "MallocSession malloc hSession->pollHandle failed");
delete hSession;
hSession = nullptr;
return nullptr;
}
uv_poll_init_socket(&loopMain, pollHandleMain, hSession->ctrlFd[STREAM_MAIN]);
uv_poll_start(pollHandleMain, UV_READABLE, ReadCtrlFromSession);
hSession->pollHandle[STREAM_MAIN]->data = hSession;
hSession->pollHandle[STREAM_WORK]->data = hSession;
// Activate USB DAEMON's data channel, may not for use
uv_tcp_init(&loopMain, &hSession->dataPipe[STREAM_MAIN]);
(void)memset_s(&hSession->dataPipe[STREAM_WORK], sizeof(hSession->dataPipe[STREAM_WORK]),
0, sizeof(uv_tcp_t));
++hSession->uvHandleRef;
Base::CreateSocketPair(hSession->dataFd);
uv_tcp_open(&hSession->dataPipe[STREAM_MAIN], hSession->dataFd[STREAM_MAIN]);
hSession->dataPipe[STREAM_MAIN].data = hSession;
hSession->dataPipe[STREAM_WORK].data = hSession;
#ifdef HDC_HOST
Base::SetTcpOptions(&hSession->dataPipe[STREAM_MAIN], HOST_SOCKETPAIR_SIZE);
#else
Base::SetTcpOptions(&hSession->dataPipe[STREAM_MAIN]);
#endif
ret = MallocSessionByConnectType(hSession);
if (ret) {
delete hSession;
hSession = nullptr;
} else {
AdminSession(OP_ADD, hSession->sessionId, hSession);
}
return hSession;
}
void HdcSessionBase::FreeSessionByConnectType(HSession hSession)
{
WRITE_LOG(LOG_DEBUG, "FreeSessionByConnectType %s", hSession->ToDebugString().c_str());
if (hSession->connType == CONN_USB) {
// ibusb All context is applied for sub-threaded, so it needs to be destroyed in the subline
if (!hSession->hUSB) {
return;
}
HUSB hUSB = hSession->hUSB;
if (!hUSB) {
return;
}
#ifdef HDC_HOST
if (hUSB->devHandle) {
libusb_release_interface(hUSB->devHandle, hUSB->interfaceNumber);
libusb_close(hUSB->devHandle);
hUSB->devHandle = nullptr;
}
#else
Base::CloseFd(hUSB->bulkIn);
Base::CloseFd(hUSB->bulkOut);
#endif
delete hSession->hUSB;
hSession->hUSB = nullptr;
}
#ifdef HDC_SUPPORT_UART
if (CONN_SERIAL == hSession->connType) {
if (!hSession->hUART) {
return;
}
HUART hUART = hSession->hUART;
if (!hUART) {
return;
}
HdcUARTBase *uartBase = (HdcUARTBase *)hSession->classModule;
// tell uart session will be free
uartBase->StopSession(hSession);
#ifdef HDC_HOST
#ifdef HOST_MINGW
if (hUART->devUartHandle != INVALID_HANDLE_VALUE) {
CloseHandle(hUART->devUartHandle);
hUART->devUartHandle = INVALID_HANDLE_VALUE;
}
#elif defined(HOST_LINUX)
Base::CloseFd(hUART->devUartHandle);
#endif // _WIN32
#endif
delete hSession->hUART;
hSession->hUART = nullptr;
}
#endif
}
// work when libuv-handle at struct of HdcSession has all callback finished
void HdcSessionBase::FreeSessionFinally(uv_idle_t *handle)
{
HSession hSession = (HSession)handle->data;
HdcSessionBase *thisClass = (HdcSessionBase *)hSession->classInstance;
if (hSession->uvHandleRef > 0) {
WRITE_LOG(LOG_DEBUG, "FreeSessionFinally uvHandleRef:%d sessionId:%u",
hSession->uvHandleRef, hSession->sessionId);
return;
}
// Notify Server or Daemon, just UI or display commandline
thisClass->NotifyInstanceSessionFree(hSession, true);
// all hsession uv handle has been clear
thisClass->AdminSession(OP_REMOVE, hSession->sessionId, nullptr);
WRITE_LOG(LOG_DEBUG, "!!!FreeSessionFinally sessionId:%u finish", hSession->sessionId);
HdcAuth::FreeKey(!hSession->serverOrDaemon, hSession->listKey);
delete hSession;
hSession = nullptr; // fix CodeMars SetNullAfterFree issue
Base::TryCloseHandle((const uv_handle_t *)handle, Base::CloseIdleCallback);
--thisClass->sessionRef;
}
// work when child-work thread finish
void HdcSessionBase::FreeSessionContinue(HSession hSession)
{
auto closeSessionTCPHandle = [](uv_handle_t *handle) -> void {
HSession hSession = (HSession)handle->data;
--hSession->uvHandleRef;
Base::TryCloseHandle((uv_handle_t *)handle);
if (handle == reinterpret_cast<uv_handle_t *>(hSession->pollHandle[STREAM_MAIN])) {
Base::CloseFd(hSession->ctrlFd[STREAM_MAIN]);
Base::CloseFd(hSession->ctrlFd[STREAM_WORK]);
free(hSession->pollHandle[STREAM_MAIN]);
}
};
if (hSession->connType == CONN_TCP) {
// Turn off TCP to prevent continuing writing
Base::TryCloseHandle((uv_handle_t *)&hSession->hWorkTCP, true, closeSessionTCPHandle);
}
hSession->availTailIndex = 0;
if (hSession->ioBuf) {
delete[] hSession->ioBuf;
hSession->ioBuf = nullptr;
}
Base::TryCloseHandle((uv_handle_t *)hSession->pollHandle[STREAM_MAIN], true, closeSessionTCPHandle);
Base::TryCloseHandle((uv_handle_t *)&hSession->dataPipe[STREAM_MAIN], true, closeSessionTCPHandle);
FreeSessionByConnectType(hSession);
// finish
Base::IdleUvTask(&loopMain, hSession, FreeSessionFinally);
}
void HdcSessionBase::FreeSessionOpeate(uv_timer_t *handle)
{
StartTraceScope("HdcSessionBase::FreeSessionOpeate");
HSession hSession = (HSession)handle->data;
HdcSessionBase *thisClass = (HdcSessionBase *)hSession->classInstance;
if (hSession->ref > 0) {
WRITE_LOG(LOG_WARN, "FreeSessionOpeate ref:%u > 0", uint32_t(hSession->ref));
return;
}
WRITE_LOG(LOG_DEBUG, "FreeSessionOpeate ref:%u", uint32_t(hSession->ref));
#ifdef HDC_HOST
if (hSession->hUSB != nullptr
&& (!hSession->hUSB->hostBulkIn.isShutdown || !hSession->hUSB->hostBulkOut.isShutdown)) {
HdcUSBBase *pUSB = ((HdcUSBBase *)hSession->classModule);
pUSB->CancelUsbIo(hSession);
return;
}
#endif
// wait workthread to free
if (hSession->pollHandle[STREAM_WORK]->loop) {
auto ctrl = BuildCtrlString(SP_STOP_SESSION, 0, nullptr, 0);
Base::SendToPollFd(hSession->ctrlFd[STREAM_MAIN], ctrl.data(), ctrl.size());
WRITE_LOG(LOG_DEBUG, "FreeSessionOpeate, send workthread for free. sessionId:%u", hSession->sessionId);
auto callbackCheckFreeSessionContinue = [](uv_timer_t *handle) -> void {
HSession hSession = (HSession)handle->data;
HdcSessionBase *thisClass = (HdcSessionBase *)hSession->classInstance;
if (!hSession->childCleared) {
WRITE_LOG(LOG_DEBUG, "FreeSessionOpeate childCleared:%d sessionId:%u",
hSession->childCleared, hSession->sessionId);
return;
}
Base::TryCloseHandle((uv_handle_t *)handle, Base::CloseTimerCallback);
thisClass->FreeSessionContinue(hSession);
};
Base::TimerUvTask(&thisClass->loopMain, hSession, callbackCheckFreeSessionContinue);
} else {
thisClass->FreeSessionContinue(hSession);
}
Base::TryCloseHandle((uv_handle_t *)handle, Base::CloseTimerCallback);
}
void HdcSessionBase::FreeSession(const uint32_t sessionId)
{
StartTraceScope("HdcSessionBase::FreeSession");
if (threadSessionMain != uv_thread_self()) {
PushAsyncMessage(sessionId, ASYNC_FREE_SESSION, nullptr, 0);
return;
}
HSession hSession = AdminSession(OP_QUERY, sessionId, nullptr);
WRITE_LOG(LOG_DEBUG, "Begin to free session, sessionid:%u", sessionId);
do {
if (!hSession || hSession->isDead) {
WRITE_LOG(LOG_WARN, "FreeSession hSession nullptr or isDead sessionId:%u", sessionId);
break;
}
hSession->isDead = true;
Base::TimerUvTask(&loopMain, hSession, FreeSessionOpeate);
NotifyInstanceSessionFree(hSession, false);
WRITE_LOG(LOG_DEBUG, "FreeSession sessionId:%u ref:%u", hSession->sessionId, uint32_t(hSession->ref));
} while (false);
}
HSession HdcSessionBase::AdminSession(const uint8_t op, const uint32_t sessionId, HSession hInput)
{
HSession hRet = nullptr;
switch (op) {
case OP_ADD:
uv_rwlock_wrlock(&lockMapSession);
mapSession[sessionId] = hInput;
uv_rwlock_wrunlock(&lockMapSession);
break;
case OP_REMOVE:
uv_rwlock_wrlock(&lockMapSession);
mapSession.erase(sessionId);
uv_rwlock_wrunlock(&lockMapSession);
break;
case OP_QUERY:
uv_rwlock_rdlock(&lockMapSession);
if (mapSession.count(sessionId)) {
hRet = mapSession[sessionId];
}
uv_rwlock_rdunlock(&lockMapSession);
break;
case OP_QUERY_REF:
uv_rwlock_wrlock(&lockMapSession);
if (mapSession.count(sessionId)) {
hRet = mapSession[sessionId];
++hRet->ref;
}
uv_rwlock_wrunlock(&lockMapSession);
break;
case OP_UPDATE:
uv_rwlock_wrlock(&lockMapSession);
// remove old
mapSession.erase(sessionId);
mapSession[hInput->sessionId] = hInput;
uv_rwlock_wrunlock(&lockMapSession);
break;
case OP_VOTE_RESET:
if (mapSession.count(sessionId) == 0) {
break;
}
bool needReset;
if (serverOrDaemon) {
uv_rwlock_wrlock(&lockMapSession);
hRet = mapSession[sessionId];
hRet->voteReset = true;
needReset = true;
for (auto &kv : mapSession) {
if (sessionId == kv.first) {
continue;
}
WRITE_LOG(LOG_DEBUG, "session:%u vote reset, session %u is %s",
sessionId, kv.first, kv.second->voteReset ? "YES" : "NO");
if (!kv.second->voteReset) {
needReset = false;
}
}
uv_rwlock_wrunlock(&lockMapSession);
} else {
needReset = true;
}
if (needReset) {
WRITE_LOG(LOG_FATAL, "!! session:%u vote reset, passed unanimously !!", sessionId);
abort();
}
break;
default:
break;
}
return hRet;
}
void HdcSessionBase::DumpTasksInfo(map<uint32_t, HTaskInfo> &mapTask)
{
int idx = 1;
for (auto t : mapTask) {
HTaskInfo ti = t.second;
WRITE_LOG(LOG_WARN, "%d: channelId: %lu, type: %d, closeRetry: %d\n",
idx++, ti->channelId, ti->taskType, ti->closeRetryCount);
}
}
// All in the corresponding sub-thread, no need locks
HTaskInfo HdcSessionBase::AdminTask(const uint8_t op, HSession hSession, const uint32_t channelId, HTaskInfo hInput)
{
HTaskInfo hRet = nullptr;
map<uint32_t, HTaskInfo> &mapTask = *hSession->mapTask;
switch (op) {
case OP_ADD:
hRet = mapTask[channelId];
if (hRet != nullptr) {
delete hRet;
}
mapTask[channelId] = hInput;
hRet = hInput;
WRITE_LOG(LOG_DEBUG, "AdminTask add session %u, channelId %u, mapTask size: %zu",
hSession->sessionId, channelId, mapTask.size());
break;
case OP_REMOVE:
mapTask.erase(channelId);
WRITE_LOG(LOG_DEBUG, "AdminTask rm session %u, channelId %u, mapTask size: %zu",
hSession->sessionId, channelId, mapTask.size());
break;
case OP_QUERY:
if (mapTask.count(channelId)) {
hRet = mapTask[channelId];
}
break;
case OP_VOTE_RESET:
AdminSession(op, hSession->sessionId, nullptr);
break;
default:
break;
}
return hRet;
}
int HdcSessionBase::SendByProtocol(HSession hSession, uint8_t *bufPtr, const int bufLen, bool echo)
{
StartTraceScope("HdcSessionBase::SendByProtocol");
if (hSession->isDead) {
delete[] bufPtr;
WRITE_LOG(LOG_WARN, "SendByProtocol session dead error");
return ERR_SESSION_NOFOUND;
}
int ret = 0;
switch (hSession->connType) {
case CONN_TCP: {
HdcTCPBase *pTCP = ((HdcTCPBase *)hSession->classModule);
if (echo && !hSession->serverOrDaemon) {
ret = pTCP->WriteUvTcpFd(&hSession->hChildWorkTCP, bufPtr, bufLen);
} else {
if (hSession->hWorkThread == uv_thread_self()) {
ret = pTCP->WriteUvTcpFd(&hSession->hWorkTCP, bufPtr, bufLen);
} else {
ret = pTCP->WriteUvTcpFd(&hSession->hChildWorkTCP, bufPtr, bufLen);
}
}
break;
}
case CONN_USB: {
HdcUSBBase *pUSB = ((HdcUSBBase *)hSession->classModule);
ret = pUSB->SendUSBBlock(hSession, bufPtr, bufLen);
delete[] bufPtr;
break;
}
#ifdef HDC_SUPPORT_UART
case CONN_SERIAL: {
HdcUARTBase *pUART = ((HdcUARTBase *)hSession->classModule);
ret = pUART->SendUARTData(hSession, bufPtr, bufLen);
delete[] bufPtr;
break;
}
#endif
default:
break;
}
return ret;
}
int HdcSessionBase::Send(const uint32_t sessionId, const uint32_t channelId, const uint16_t commandFlag,
const uint8_t *data, const int dataSize)
{
StartTraceScope("HdcSessionBase::Send");
HSession hSession = AdminSession(OP_QUERY, sessionId, nullptr);
if (!hSession) {
WRITE_LOG(LOG_DEBUG, "Send to offline device, drop it, sessionId:%u", sessionId);
return ERR_SESSION_NOFOUND;
}
PayloadProtect protectBuf; // noneed convert to big-endian
protectBuf.channelId = channelId;
protectBuf.commandFlag = commandFlag;
protectBuf.checkSum = (ENABLE_IO_CHECKSUM && dataSize > 0) ? Base::CalcCheckSum(data, dataSize) : 0;
protectBuf.vCode = payloadProtectStaticVcode;
string s = SerialStruct::SerializeToString(protectBuf);
// reserve for encrypt here
// xx-encrypt
PayloadHead payloadHead = {}; // need convert to big-endian
payloadHead.flag[0] = PACKET_FLAG.at(0);
payloadHead.flag[1] = PACKET_FLAG.at(1);
payloadHead.protocolVer = VER_PROTOCOL;
payloadHead.headSize = htons(s.size());
payloadHead.dataSize = htonl(dataSize);
int finalBufSize = sizeof(PayloadHead) + s.size() + dataSize;
uint8_t *finayBuf = new(std::nothrow) uint8_t[finalBufSize]();
if (finayBuf == nullptr) {
WRITE_LOG(LOG_WARN, "send allocmem err");
return ERR_BUF_ALLOC;
}
bool bufRet = false;
do {
if (memcpy_s(finayBuf, sizeof(PayloadHead), reinterpret_cast<uint8_t *>(&payloadHead), sizeof(PayloadHead))) {
WRITE_LOG(LOG_WARN, "send copyhead err for dataSize:%d", dataSize);
break;
}
if (memcpy_s(finayBuf + sizeof(PayloadHead), s.size(),
reinterpret_cast<uint8_t *>(const_cast<char *>(s.c_str())), s.size())) {
WRITE_LOG(LOG_WARN, "send copyProtbuf err for dataSize:%d", dataSize);
break;
}
if (dataSize > 0 && memcpy_s(finayBuf + sizeof(PayloadHead) + s.size(), dataSize, data, dataSize)) {
WRITE_LOG(LOG_WARN, "send copyDatabuf err for dataSize:%d", dataSize);
break;
}
bufRet = true;
} while (false);
if (!bufRet) {
delete[] finayBuf;
WRITE_LOG(LOG_WARN, "send copywholedata err for dataSize:%d", dataSize);
return ERR_BUF_COPY;
}
if (CMD_KERNEL_ECHO == commandFlag) {
return SendByProtocol(hSession, finayBuf, finalBufSize, true);
} else {
return SendByProtocol(hSession, finayBuf, finalBufSize);
}
}
int HdcSessionBase::DecryptPayload(HSession hSession, PayloadHead *payloadHeadBe, uint8_t *encBuf)
{
StartTraceScope("HdcSessionBase::DecryptPayload");
PayloadProtect protectBuf = {};
uint16_t headSize = ntohs(payloadHeadBe->headSize);
int dataSize = ntohl(payloadHeadBe->dataSize);
string encString(reinterpret_cast<char *>(encBuf), headSize);
SerialStruct::ParseFromString(protectBuf, encString);
if (protectBuf.vCode != payloadProtectStaticVcode) {
WRITE_LOG(LOG_FATAL, "Session recv static vcode failed");
return ERR_BUF_CHECK;
}
uint8_t *data = encBuf + headSize;
if (ENABLE_IO_CHECKSUM && protectBuf.checkSum != 0 && (protectBuf.checkSum != Base::CalcCheckSum(data, dataSize))) {
WRITE_LOG(LOG_FATAL, "Session recv CalcCheckSum failed");
return ERR_BUF_CHECK;
}
if (!FetchCommand(hSession, protectBuf.channelId, protectBuf.commandFlag, data, dataSize)) {
WRITE_LOG(LOG_WARN, "FetchCommand failed: channelId %x commandFlag %x",
protectBuf.channelId, protectBuf.commandFlag);
return ERR_GENERIC;
}
return RET_SUCCESS;
}
int HdcSessionBase::OnRead(HSession hSession, uint8_t *bufPtr, const int bufLen)
{
int ret = ERR_GENERIC;
StartTraceScope("HdcSessionBase::OnRead");
if (memcmp(bufPtr, PACKET_FLAG.c_str(), PACKET_FLAG.size())) {
WRITE_LOG(LOG_FATAL, "PACKET_FLAG incorrect %x %x", bufPtr[0], bufPtr[1]);
return ERR_BUF_CHECK;
}
struct PayloadHead *payloadHead = reinterpret_cast<struct PayloadHead *>(bufPtr);
// to prevent integer overflow caused by the add operation of two input num
uint64_t payloadHeadSize = static_cast<uint64_t>(ntohl(payloadHead->dataSize)) +
static_cast<uint64_t>(ntohs(payloadHead->headSize));
int packetHeadSize = sizeof(struct PayloadHead);
if (payloadHeadSize == 0 || payloadHeadSize > static_cast<uint64_t>(HDC_BUF_MAX_BYTES)) {
WRITE_LOG(LOG_FATAL, "Packet size incorrect");
return ERR_BUF_CHECK;
}
// 0 < payloadHeadSize < HDC_BUF_MAX_BYTES
int tobeReadLen = static_cast<int>(payloadHeadSize);
if (bufLen - packetHeadSize < tobeReadLen) {
return 0;
}
if (DecryptPayload(hSession, payloadHead, bufPtr + packetHeadSize)) {
WRITE_LOG(LOG_WARN, "decrypt plhead error");
return ERR_BUF_CHECK;
}
ret = packetHeadSize + tobeReadLen;
return ret;
}
// Returns <0 error;> 0 receives the number of bytes; 0 untreated
int HdcSessionBase::FetchIOBuf(HSession hSession, uint8_t *ioBuf, int read)
{
HdcSessionBase *ptrConnect = (HdcSessionBase *)hSession->classInstance;
int indexBuf = 0;
int childRet = 0;
StartTraceScope("HdcSessionBase::FetchIOBuf");
if (read < 0) {
constexpr int bufSize = 1024;
char buf[bufSize] = { 0 };
uv_strerror_r(read, buf, bufSize);
WRITE_LOG(LOG_FATAL, "FetchIOBuf read io failed,%s", buf);
return ERR_IO_FAIL;
}
hSession->availTailIndex += read;
while (!hSession->isDead && hSession->availTailIndex > static_cast<int>(sizeof(PayloadHead))) {
childRet = ptrConnect->OnRead(hSession, ioBuf + indexBuf, hSession->availTailIndex);
if (childRet > 0) {
hSession->availTailIndex -= childRet;
indexBuf += childRet;
} else if (childRet == 0) {
// Not enough a IO
break;
} else { // <0
WRITE_LOG(LOG_FATAL, "FetchIOBuf error childRet:%d sessionId:%u", childRet, hSession->sessionId);
hSession->availTailIndex = 0; // Preventing malicious data packages
indexBuf = ERR_BUF_SIZE;
break;
}
// It may be multi-time IO to merge in a BUF, need to loop processing
}
if (indexBuf > 0 && hSession->availTailIndex > 0) {
if (memmove_s(hSession->ioBuf, hSession->bufSize, hSession->ioBuf + indexBuf, hSession->availTailIndex)
!= EOK) {
return ERR_BUF_COPY;
};
uint8_t *bufToZero = reinterpret_cast<uint8_t *>(hSession->ioBuf + hSession->availTailIndex);
Base::ZeroBuf(bufToZero, hSession->bufSize - hSession->availTailIndex);
}
return indexBuf;
}
void HdcSessionBase::AllocCallback(uv_handle_t *handle, size_t sizeWanted, uv_buf_t *buf)
{
HSession context = (HSession)handle->data;
Base::ReallocBuf(&context->ioBuf, &context->bufSize, HDC_SOCKETPAIR_SIZE);
buf->base = (char *)context->ioBuf + context->availTailIndex;
int size = context->bufSize - context->availTailIndex;
buf->len = std::min(size, static_cast<int>(sizeWanted));
}
void HdcSessionBase::FinishWriteSessionTCP(uv_write_t *req, int status)
{
HSession hSession = (HSession)req->handle->data;
--hSession->ref;
HdcSessionBase *thisClass = (HdcSessionBase *)hSession->classInstance;
if (status < 0) {
WRITE_LOG(LOG_WARN, "FinishWriteSessionTCP status:%d sessionId:%u isDead:%d ref:%u",
status, hSession->sessionId, hSession->isDead, uint32_t(hSession->ref));
Base::TryCloseHandle((uv_handle_t *)req->handle);
if (!hSession->isDead && !hSession->ref) {
WRITE_LOG(LOG_DEBUG, "FinishWriteSessionTCP freesession :%u", hSession->sessionId);
thisClass->FreeSession(hSession->sessionId);
}
}
delete[]((uint8_t *)req->data);
delete req;
}
bool HdcSessionBase::DispatchSessionThreadCommand(HSession hSession, const uint8_t *baseBuf,
const int bytesIO)
{
bool ret = true;
uint8_t flag = *const_cast<uint8_t *>(baseBuf);
switch (flag) {
case SP_JDWP_NEWFD:
case SP_ARK_NEWFD: {
JdwpNewFileDescriptor(baseBuf, bytesIO);
break;
}
default:
WRITE_LOG(LOG_WARN, "Not support session command");
break;
}
return ret;
}
void HdcSessionBase::ReadCtrlFromSession(uv_poll_t *poll, int status, int events)
{
HSession hSession = (HSession)poll->data;
HdcSessionBase *hSessionBase = (HdcSessionBase *)hSession->classInstance;
const int size = Base::GetMaxBufSize();
char *buf = reinterpret_cast<char *>(new uint8_t[size]());
ssize_t nread = Base::ReadFromFd(hSession->ctrlFd[STREAM_MAIN], buf, size);
while (true) {
if (nread < 0) {
constexpr int bufSize = 1024;
char buffer[bufSize] = { 0 };
uv_strerror_r(static_cast<int>(nread), buffer, bufSize);
WRITE_LOG(LOG_DEBUG, "ReadCtrlFromSession failed,%s", buffer);
uv_poll_stop(poll);
break;
}
if (nread == 0) {
WRITE_LOG(LOG_FATAL, "ReadCtrlFromSession read data zero byte");
break;
}
// only one command, no need to split command from stream
// if add more commands, consider the format command
hSessionBase->DispatchSessionThreadCommand(hSession, reinterpret_cast<uint8_t *>(buf), nread);
break;
}
delete[] buf;
}
bool HdcSessionBase::WorkThreadStartSession(HSession hSession)
{
bool regOK = false;
int childRet = 0;
if (hSession->connType == CONN_TCP) {
HdcTCPBase *pTCPBase = (HdcTCPBase *)hSession->classModule;
hSession->hChildWorkTCP.data = hSession;
if (uv_tcp_init(&hSession->childLoop, &hSession->hChildWorkTCP) < 0) {
WRITE_LOG(LOG_DEBUG, "HdcSessionBase SessionCtrl failed 1");
return false;
}
if ((childRet = uv_tcp_open(&hSession->hChildWorkTCP, hSession->fdChildWorkTCP)) < 0) {
constexpr int bufSize = 1024;
char buf[bufSize] = { 0 };
uv_strerror_r(childRet, buf, bufSize);
WRITE_LOG(LOG_DEBUG, "SessionCtrl failed 2,fd:%d,str:%s", hSession->fdChildWorkTCP, buf);
return false;
}
Base::SetTcpOptions((uv_tcp_t *)&hSession->hChildWorkTCP);
uv_read_start((uv_stream_t *)&hSession->hChildWorkTCP, AllocCallback, pTCPBase->ReadStream);
regOK = true;
#ifdef HDC_SUPPORT_UART
} else if (hSession->connType == CONN_SERIAL) { // UART
HdcUARTBase *pUARTBase = (HdcUARTBase *)hSession->classModule;
WRITE_LOG(LOG_DEBUG, "UART ReadyForWorkThread");
regOK = pUARTBase->ReadyForWorkThread(hSession);
#endif
} else { // USB
HdcUSBBase *pUSBBase = (HdcUSBBase *)hSession->classModule;
WRITE_LOG(LOG_DEBUG, "USB ReadyForWorkThread");
regOK = pUSBBase->ReadyForWorkThread(hSession);
}
if (regOK && hSession->serverOrDaemon) {
// session handshake step1
SessionHandShake handshake = {};
handshake.banner = HANDSHAKE_MESSAGE;
handshake.sessionId = hSession->sessionId;
handshake.connectKey = hSession->connectKey;
if (!hSession->isCheck) {
handshake.version = Base::GetVersion() + HDC_MSG_HASH;
WRITE_LOG(LOG_INFO, "set version = %s", handshake.version.c_str());
}
handshake.authType = AUTH_NONE;
string hs = SerialStruct::SerializeToString(handshake);
#ifdef HDC_SUPPORT_UART
WRITE_LOG(LOG_DEBUG, "WorkThreadStartSession session %u auth %u send handshake hs: %s",
hSession->sessionId, handshake.authType, hs.c_str());
#endif
Send(hSession->sessionId, 0, CMD_KERNEL_HANDSHAKE,
reinterpret_cast<uint8_t *>(const_cast<char *>(hs.c_str())), hs.size());
}
return regOK;
}
vector<uint8_t> HdcSessionBase::BuildCtrlString(InnerCtrlCommand command, uint32_t channelId, uint8_t *data,
int dataSize)
{
vector<uint8_t> ret;
while (true) {
if (dataSize > BUF_SIZE_MICRO) {
WRITE_LOG(LOG_WARN, "BuildCtrlString dataSize:%d", dataSize);
break;
}
CtrlStruct ctrl = {};
ctrl.command = command;
ctrl.channelId = channelId;
ctrl.dataSize = dataSize;
if (dataSize > 0 && data != nullptr && memcpy_s(ctrl.data, sizeof(ctrl.data), data, dataSize) != EOK) {
break;
}
uint8_t *buf = reinterpret_cast<uint8_t *>(&ctrl);
ret.insert(ret.end(), buf, buf + sizeof(CtrlStruct));
break;
}
return ret;
}
bool HdcSessionBase::DispatchMainThreadCommand(HSession hSession, const CtrlStruct *ctrl)
{
bool ret = true;
uint32_t channelId = ctrl->channelId; // if send not set, it is zero
switch (ctrl->command) {
case SP_START_SESSION: {
WRITE_LOG(LOG_DEBUG, "Dispatch MainThreadCommand START_SESSION sessionId:%u instance:%s",
hSession->sessionId, hSession->serverOrDaemon ? "server" : "daemon");
ret = WorkThreadStartSession(hSession);
break;
}
case SP_STOP_SESSION: {
WRITE_LOG(LOG_DEBUG, "Dispatch MainThreadCommand STOP_SESSION sessionId:%u", hSession->sessionId);
auto closeSessionChildThreadTCPHandle = [](uv_handle_t *handle) -> void {
HSession hSession = (HSession)handle->data;
Base::TryCloseHandle((uv_handle_t *)handle);
if (handle == (uv_handle_t *)hSession->pollHandle[STREAM_WORK]) {
free(hSession->pollHandle[STREAM_WORK]);
}
if (--hSession->uvChildRef == 0) {
uv_stop(&hSession->childLoop);
};
};
constexpr int uvChildRefOffset = 2;
hSession->uvChildRef += uvChildRefOffset;
if (hSession->connType == CONN_TCP && hSession->hChildWorkTCP.loop) { // maybe not use it
++hSession->uvChildRef;
Base::TryCloseHandle((uv_handle_t *)&hSession->hChildWorkTCP, true, closeSessionChildThreadTCPHandle);
}
Base::TryCloseHandle((uv_handle_t *)hSession->pollHandle[STREAM_WORK], true,
closeSessionChildThreadTCPHandle);
Base::TryCloseHandle((uv_handle_t *)&hSession->dataPipe[STREAM_WORK], true,
closeSessionChildThreadTCPHandle);
break;
}
case SP_ATTACH_CHANNEL: {
if (!serverOrDaemon) {
break; // Only Server has this feature
}
AttachChannel(hSession, channelId);
break;
}
case SP_DEATCH_CHANNEL: {
if (!serverOrDaemon) {
break; // Only Server has this feature
}
DeatchChannel(hSession, channelId);
break;
}
default:
WRITE_LOG(LOG_WARN, "Not support main command");
ret = false;
break;
}
return ret;
}
// Several bytes of control instructions, generally do not stick
void HdcSessionBase::ReadCtrlFromMain(uv_poll_t *poll, int status, int events)
{
HSession hSession = (HSession)poll->data;
HdcSessionBase *hSessionBase = (HdcSessionBase *)hSession->classInstance;
int formatCommandSize = sizeof(CtrlStruct);
int index = 0;
const int size = Base::GetMaxBufSize();
char *buf = reinterpret_cast<char *>(new uint8_t[size]());
ssize_t nread = Base::ReadFromFd(hSession->ctrlFd[STREAM_WORK], buf, size);
while (true) {
if (nread < 0) {
constexpr int bufSize = 1024;
char buffer[bufSize] = { 0 };
uv_strerror_r(static_cast<int>(nread), buffer, bufSize);
WRITE_LOG(LOG_DEBUG, "SessionCtrl failed,%s", buffer);
break;
}
if (nread % formatCommandSize != 0) {
WRITE_LOG(LOG_FATAL, "ReadCtrlFromMain size failed, nread == %d", nread);
break;
}
CtrlStruct *ctrl = reinterpret_cast<CtrlStruct *>(buf + index);
if (!hSessionBase->DispatchMainThreadCommand(hSession, ctrl)) {
WRITE_LOG(LOG_FATAL, "ReadCtrlFromMain failed sessionId:%u channelId:%u command:%u",
hSession->sessionId, ctrl->channelId, ctrl->command);
break;
}
index += sizeof(CtrlStruct);
if (index >= nread) {
break;
}
}
delete[] buf;
}
void HdcSessionBase::ReChildLoopForSessionClear(HSession hSession)
{
// Restart loop close task
ClearOwnTasks(hSession, 0);
WRITE_LOG(LOG_INFO, "ReChildLoopForSessionClear sessionId:%u", hSession->sessionId);
auto clearTaskForSessionFinish = [](uv_timer_t *handle) -> void {
HSession hSession = (HSession)handle->data;
for (auto v : *hSession->mapTask) {
HTaskInfo hTask = (HTaskInfo)v.second;
uint8_t level;
if (hTask->closeRetryCount < GLOBAL_TIMEOUT / 2) {
level = LOG_DEBUG;
} else {
level = LOG_WARN;
}
WRITE_LOG(level, "wait task free retry %d/%d, channelId:%u, sessionId:%u",
hTask->closeRetryCount, GLOBAL_TIMEOUT, hTask->channelId, hTask->sessionId);
if (hTask->closeRetryCount++ >= GLOBAL_TIMEOUT) {
HdcSessionBase *thisClass = (HdcSessionBase *)hTask->ownerSessionClass;
hSession = thisClass->AdminSession(OP_QUERY, hTask->sessionId, nullptr);
thisClass->AdminTask(OP_VOTE_RESET, hSession, hTask->channelId, nullptr);
}
if (!hTask->taskFree)
return;
}
// all task has been free
uv_close((uv_handle_t *)handle, Base::CloseTimerCallback);
uv_stop(&hSession->childLoop); // stop ReChildLoopForSessionClear pendding
};
Base::TimerUvTask(
&hSession->childLoop, hSession, clearTaskForSessionFinish, (GLOBAL_TIMEOUT * TIME_BASE) / UV_DEFAULT_INTERVAL);
uv_run(&hSession->childLoop, UV_RUN_DEFAULT);
// clear
Base::TryCloseChildLoop(&hSession->childLoop, "Session childUV");
}
void HdcSessionBase::SessionWorkThread(uv_work_t *arg)
{
HSession hSession = (HSession)arg->data;
HdcSessionBase *thisClass = (HdcSessionBase *)hSession->classInstance;
hSession->hWorkChildThread = uv_thread_self();
uv_poll_t *pollHandle = hSession->pollHandle[STREAM_WORK];
pollHandle->data = hSession;
uv_poll_init_socket(&hSession->childLoop, pollHandle, hSession->ctrlFd[STREAM_WORK]);
uv_poll_start(pollHandle, UV_READABLE, ReadCtrlFromMain);
WRITE_LOG(LOG_DEBUG, "!!!Workthread run begin, sessionId:%u instance:%s", hSession->sessionId,
thisClass->serverOrDaemon ? "server" : "daemon");
uv_run(&hSession->childLoop, UV_RUN_DEFAULT); // work pendding
WRITE_LOG(LOG_DEBUG, "!!!Workthread run again, sessionId:%u", hSession->sessionId);
// main loop has exit
thisClass->ReChildLoopForSessionClear(hSession); // work pending again
hSession->childCleared = true;
WRITE_LOG(LOG_DEBUG, "!!!Workthread run finish, sessionId:%u", hSession->sessionId);
}
// clang-format off
void HdcSessionBase::LogMsg(const uint32_t sessionId, const uint32_t channelId,
MessageLevel level, const char *msg, ...)
// clang-format on
{
va_list vaArgs;
va_start(vaArgs, msg);
string log = Base::StringFormat(msg, vaArgs);
va_end(vaArgs);
vector<uint8_t> buf;
buf.push_back(level);
buf.insert(buf.end(), log.c_str(), log.c_str() + log.size());
ServerCommand(sessionId, channelId, CMD_KERNEL_ECHO, buf.data(), buf.size());
}
bool HdcSessionBase::NeedNewTaskInfo(const uint16_t command, bool &masterTask)
{
// referer from HdcServerForClient::DoCommandRemote
bool ret = false;
bool taskMasterInit = false;
masterTask = false;
switch (command) {
case CMD_FILE_INIT:
case CMD_FLASHD_FLASH_INIT:
case CMD_FLASHD_UPDATE_INIT:
case CMD_FLASHD_ERASE:
case CMD_FLASHD_FORMAT:
case CMD_FORWARD_INIT:
case CMD_APP_INIT:
case CMD_APP_UNINSTALL:
case CMD_APP_SIDELOAD:
taskMasterInit = true;
break;
default:
break;
}
if (!serverOrDaemon
&& (command == CMD_SHELL_INIT || (command > CMD_UNITY_COMMAND_HEAD && command < CMD_UNITY_COMMAND_TAIL))) {
// daemon's single side command
ret = true;
} else if (command == CMD_KERNEL_WAKEUP_SLAVETASK) {
// slave tasks
ret = true;
} else if (command == CMD_UNITY_BUGREPORT_INIT) {
ret = true;
} else if (taskMasterInit) {
// task init command
masterTask = true;
ret = true;
}
return ret;
}
// Heavy and time-consuming work was putted in the new thread to do, and does
// not occupy the main thread
bool HdcSessionBase::DispatchTaskData(HSession hSession, const uint32_t channelId, const uint16_t command,
uint8_t *payload, int payloadSize)
{
StartTraceScope("HdcSessionBase::DispatchTaskData");
bool ret = true;
HTaskInfo hTaskInfo = nullptr;
bool masterTask = false;
while (true) {
// Some basic commands do not have a local task constructor. example: Interactive shell, some uinty commands
if (NeedNewTaskInfo(command, masterTask)) {
WRITE_LOG(LOG_DEBUG, "New HTaskInfo channelId:%u command:%u", channelId, command);
hTaskInfo = new(std::nothrow) TaskInformation();
if (hTaskInfo == nullptr) {
WRITE_LOG(LOG_FATAL, "DispatchTaskData new hTaskInfo failed");
break;
}
hTaskInfo->channelId = channelId;
hTaskInfo->sessionId = hSession->sessionId;
hTaskInfo->runLoop = &hSession->childLoop;
hTaskInfo->serverOrDaemon = serverOrDaemon;
hTaskInfo->masterSlave = masterTask;
hTaskInfo->closeRetryCount = 0;
hTaskInfo->channelTask = false;
int addTaskRetry = 3; // try 3 time
while (addTaskRetry > 0) {
if (AdminTask(OP_ADD, hSession, channelId, hTaskInfo)) {
break;
}
sleep(1);
--addTaskRetry;
}
if (addTaskRetry == 0) {
#ifndef HDC_HOST
LogMsg(hTaskInfo->sessionId, hTaskInfo->channelId,
MSG_FAIL, "hdc thread pool busy, may cause reset later");
#endif
delete hTaskInfo;
hTaskInfo = nullptr;
ret = false;
break;
}
} else {
hTaskInfo = AdminTask(OP_QUERY, hSession, channelId, nullptr);
}
if (!hTaskInfo || hTaskInfo->taskStop || hTaskInfo->taskFree) {
WRITE_LOG(LOG_ALL, "Dead HTaskInfo, ignore, channelId:%u command:%u", channelId, command);
break;
}
ret = RedirectToTask(hTaskInfo, hSession, channelId, command, payload, payloadSize);
break;
}
return ret;
}
void HdcSessionBase::PostStopInstanceMessage(bool restart)
{
PushAsyncMessage(0, ASYNC_STOP_MAINLOOP, nullptr, 0);
WRITE_LOG(LOG_DEBUG, "StopDaemon has sended restart %d", restart);
wantRestart = restart;
}
} // namespace Hdc
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

Device debug connector that provides the device connection capability and a command line tool | 设备调试连接器,提供了连接设备的能力,并提供了对外的命令行工具
暂无标签
Apache-2.0
使用 Apache-2.0 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/prife/developtools_hdc.git
git@gitee.com:prife/developtools_hdc.git
prife
developtools_hdc
developtools_hdc
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /