Lines Matching refs:hSession

154 void HdcSessionBase::ClearOwnTasks(HSession hSession, const uint32_t channelIDInput)
160 hSession->mapTaskMutex.lock();
162 for (iter = hSession->mapTask->begin(); iter != hSession->mapTask->end();) {
172 hSession->sessionId, channelIDInput);
173 iter = hSession->mapTask->erase(iter);
178 iter = hSession->mapTask->erase(iter);
180 hSession->mapTaskMutex.unlock();
188 HSession hSession = (HSession)v.second;
189 if (!hSession->isDead) {
190 FreeSession(hSession->sessionId);
228 void HdcSessionBase::EnumUSBDeviceRegister(void (*pCallBack)(HSession hSession))
367 int HdcSessionBase::MallocSessionByConnectType(HSession hSession)
370 switch (hSession->connType) {
372 uv_tcp_init(&loopMain, &hSession->hWorkTCP);
373 ++hSession->uvHandleRef;
374 hSession->hWorkTCP.data = hSession;
384 hSession->hUSB = hUSB;
385 hSession->hUSB->wMaxPacketSizeSend = MAX_PACKET_SIZE_HISPEED;
395 hSession->hUART = hUART;
424 HSession hSession = new(std::nothrow) HdcSession();
425 if (!hSession) {
426 WRITE_LOG(LOG_FATAL, "MallocSession new hSession failed");
431 hSession->classInstance = this;
432 hSession->connType = connType;
433 hSession->classModule = classModule;
434 hSession->isDead = false;
435 hSession->sessionId = ((sessionId == 0) ? GetSessionPseudoUid() : sessionId);
436 hSession->serverOrDaemon = serverOrDaemon;
437 hSession->hWorkThread = uv_thread_self();
438 hSession->mapTask = new(std::nothrow) map<uint32_t, HTaskInfo>();
439 if (hSession->mapTask == nullptr) {
440 WRITE_LOG(LOG_FATAL, "MallocSession new hSession->mapTask failed");
441 delete hSession;
442 hSession = nullptr;
445 hSession->listKey = new(std::nothrow) list<void *>;
446 if (hSession->listKey == nullptr) {
447 WRITE_LOG(LOG_FATAL, "MallocSession new hSession->listKey failed");
448 delete hSession;
449 hSession = nullptr;
452 uv_loop_init(&hSession->childLoop);
453 hSession->uvHandleRef = 0;
456 hSession->sessionId, hSession->connType);
457 ++hSession->uvHandleRef;
458 Base::CreateSocketPair(hSession->ctrlFd);
460 hSession->pollHandle[STREAM_WORK] = (uv_poll_t *)malloc(handleSize);
461 hSession->pollHandle[STREAM_MAIN] = (uv_poll_t *)malloc(handleSize);
462 uv_poll_t *pollHandleMain = hSession->pollHandle[STREAM_MAIN];
463 if (pollHandleMain == nullptr || hSession->pollHandle[STREAM_WORK] == nullptr) {
464 WRITE_LOG(LOG_FATAL, "MallocSession malloc hSession->pollHandle failed");
465 delete hSession;
466 hSession = nullptr;
469 uv_poll_init_socket(&loopMain, pollHandleMain, hSession->ctrlFd[STREAM_MAIN]);
471 hSession->pollHandle[STREAM_MAIN]->data = hSession;
472 hSession->pollHandle[STREAM_WORK]->data = hSession;
474 uv_tcp_init(&loopMain, &hSession->dataPipe[STREAM_MAIN]);
475 (void)memset_s(&hSession->dataPipe[STREAM_WORK], sizeof(hSession->dataPipe[STREAM_WORK]),
477 ++hSession->uvHandleRef;
478 Base::CreateSocketPair(hSession->dataFd);
479 uv_tcp_open(&hSession->dataPipe[STREAM_MAIN], hSession->dataFd[STREAM_MAIN]);
480 hSession->dataPipe[STREAM_MAIN].data = hSession;
481 hSession->dataPipe[STREAM_WORK].data = hSession;
483 Base::SetTcpOptions(&hSession->dataPipe[STREAM_MAIN], HOST_SOCKETPAIR_SIZE);
485 Base::SetTcpOptions(&hSession->dataPipe[STREAM_MAIN]);
487 ret = MallocSessionByConnectType(hSession);
489 delete hSession;
490 hSession = nullptr;
492 AdminSession(OP_ADD, hSession->sessionId, hSession);
494 return hSession;
497 void HdcSessionBase::FreeSessionByConnectType(HSession hSession)
499 WRITE_LOG(LOG_DEBUG, "FreeSessionByConnectType %s", hSession->ToDebugString().c_str());
501 if (hSession->connType == CONN_USB) {
503 if (!hSession->hUSB) {
506 HUSB hUSB = hSession->hUSB;
520 delete hSession->hUSB;
521 hSession->hUSB = nullptr;
524 if (CONN_SERIAL == hSession->connType) {
525 if (!hSession->hUART) {
528 HUART hUART = hSession->hUART;
532 HdcUARTBase *uartBase = (HdcUARTBase *)hSession->classModule;
534 uartBase->StopSession(hSession);
545 delete hSession->hUART;
546 hSession->hUART = nullptr;
554 HSession hSession = (HSession)handle->data;
555 HdcSessionBase *thisClass = (HdcSessionBase *)hSession->classInstance;
556 if (hSession->uvHandleRef > 0) {
558 hSession->uvHandleRef, hSession->sessionId);
562 thisClass->NotifyInstanceSessionFree(hSession, true);
564 thisClass->AdminSession(OP_REMOVE, hSession->sessionId, nullptr);
565 WRITE_LOG(LOG_INFO, "!!!FreeSessionFinally sessionId:%u finish", hSession->sessionId);
566 HdcAuth::FreeKey(!hSession->serverOrDaemon, hSession->listKey);
567 delete hSession;
568 hSession = nullptr; // fix CodeMars SetNullAfterFree issue
574 void HdcSessionBase::FreeSessionContinue(HSession hSession)
577 HSession hSession = (HSession)handle->data;
578 --hSession->uvHandleRef;
580 if (handle == reinterpret_cast<uv_handle_t *>(hSession->pollHandle[STREAM_MAIN])) {
581 Base::CloseFd(hSession->ctrlFd[STREAM_MAIN]);
582 Base::CloseFd(hSession->ctrlFd[STREAM_WORK]);
583 free(hSession->pollHandle[STREAM_MAIN]);
586 if (hSession->connType == CONN_TCP) {
588 Base::TryCloseHandle((uv_handle_t *)&hSession->hWorkTCP, true, closeSessionTCPHandle);
589 Base::CloseFd(hSession->dataFd[STREAM_WORK]);
591 hSession->availTailIndex = 0;
592 if (hSession->ioBuf) {
593 delete[] hSession->ioBuf;
594 hSession->ioBuf = nullptr;
596 Base::TryCloseHandle((uv_handle_t *)hSession->pollHandle[STREAM_MAIN], true, closeSessionTCPHandle);
597 Base::TryCloseHandle((uv_handle_t *)&hSession->dataPipe[STREAM_MAIN], true, closeSessionTCPHandle);
598 FreeSessionByConnectType(hSession);
600 Base::IdleUvTask(&loopMain, hSession, FreeSessionFinally);
606 HSession hSession = (HSession)handle->data;
608 if (hSession->hUSB != nullptr
609 && (!hSession->hUSB->hostBulkIn.isShutdown || !hSession->hUSB->hostBulkOut.isShutdown)) {
610 HdcUSBBase *pUSB = ((HdcUSBBase *)hSession->classModule);
611 pUSB->CancelUsbIo(hSession);
615 HdcSessionBase *thisClass = (HdcSessionBase *)hSession->classInstance;
616 if (hSession->ref > 0) {
617 WRITE_LOG(LOG_WARN, "FreeSessionOpeate sid:%u ref:%u > 0", hSession->sessionId, uint32_t(hSession->ref));
620 WRITE_LOG(LOG_INFO, "FreeSessionOpeate sid:%u ref:%u", hSession->sessionId, uint32_t(hSession->ref));
622 if (hSession->pollHandle[STREAM_WORK]->loop) {
624 Base::SendToPollFd(hSession->ctrlFd[STREAM_MAIN], ctrl.data(), ctrl.size());
625 WRITE_LOG(LOG_INFO, "FreeSessionOpeate, send workthread for free. sessionId:%u", hSession->sessionId);
627 HSession hSession = (HSession)handle->data;
628 HdcSessionBase *thisClass = (HdcSessionBase *)hSession->classInstance;
629 if (!hSession->childCleared) {
631 hSession->childCleared, hSession->sessionId);
635 thisClass->FreeSessionContinue(hSession);
637 Base::TimerUvTask(&thisClass->loopMain, hSession, callbackCheckFreeSessionContinue);
639 thisClass->FreeSessionContinue(hSession);
652 HSession hSession = AdminSession(OP_QUERY, sessionId, nullptr);
655 if (!hSession || hSession->isDead) {
656 WRITE_LOG(LOG_WARN, "FreeSession hSession nullptr or isDead sessionId:%u", sessionId);
660 uint64_t(hSession->stat.dataSendBytes),
661 uint64_t(hSession->stat.dataRecvBytes));
662 hSession->isDead = true;
663 Base::TimerUvTask(&loopMain, hSession, FreeSessionOpeate);
664 NotifyInstanceSessionFree(hSession, false);
665 WRITE_LOG(LOG_INFO, "FreeSession sessionId:%u ref:%u", hSession->sessionId, uint32_t(hSession->ref));
751 HTaskInfo HdcSessionBase::AdminTask(const uint8_t op, HSession hSession, const uint32_t channelId, HTaskInfo hInput)
754 map<uint32_t, HTaskInfo> &mapTask = *hSession->mapTask;
766 hSession->sessionId, channelId, mapTask.size());
772 hSession->sessionId, channelId, mapTask.size());
780 AdminSession(op, hSession->sessionId, nullptr);
788 int HdcSessionBase::SendByProtocol(HSession hSession, uint8_t *bufPtr, const int bufLen, bool echo)
791 if (hSession->isDead) {
797 switch (hSession->connType) {
799 HdcTCPBase *pTCP = ((HdcTCPBase *)hSession->classModule);
800 if (echo && !hSession->serverOrDaemon) {
801 ret = pTCP->WriteUvTcpFd(&hSession->hChildWorkTCP, bufPtr, bufLen);
803 if (hSession->hWorkThread == uv_thread_self()) {
804 ret = pTCP->WriteUvTcpFd(&hSession->hWorkTCP, bufPtr, bufLen);
806 ret = pTCP->WriteUvTcpFd(&hSession->hChildWorkTCP, bufPtr, bufLen);
812 HdcUSBBase *pUSB = ((HdcUSBBase *)hSession->classModule);
813 ret = pUSB->SendUSBBlock(hSession, bufPtr, bufLen);
819 HdcUARTBase *pUART = ((HdcUARTBase *)hSession->classModule);
820 ret = pUART->SendUARTData(hSession, bufPtr, bufLen);
835 HSession hSession = AdminSession(OP_QUERY, sessionId, nullptr);
836 if (!hSession) {
884 return SendByProtocol(hSession, finayBuf, finalBufSize, true);
886 return SendByProtocol(hSession, finayBuf, finalBufSize);
890 int HdcSessionBase::DecryptPayload(HSession hSession, PayloadHead *payloadHeadBe, uint8_t *encBuf)
907 if (!FetchCommand(hSession, protectBuf.channelId, protectBuf.commandFlag, data, dataSize)) {
915 int HdcSessionBase::OnRead(HSession hSession, uint8_t *bufPtr, const int bufLen)
938 if (DecryptPayload(hSession, payloadHead, bufPtr + packetHeadSize)) {
947 int HdcSessionBase::FetchIOBuf(HSession hSession, uint8_t *ioBuf, int read)
949 HdcSessionBase *ptrConnect = (HdcSessionBase *)hSession->classInstance;
960 hSession->stat.dataRecvBytes += read;
961 hSession->availTailIndex += read;
962 while (!hSession->isDead && hSession->availTailIndex > static_cast<int>(sizeof(PayloadHead))) {
963 childRet = ptrConnect->OnRead(hSession, ioBuf + indexBuf, hSession->availTailIndex);
965 hSession->availTailIndex -= childRet;
971 WRITE_LOG(LOG_FATAL, "FetchIOBuf error childRet:%d sessionId:%u", childRet, hSession->sessionId);
972 hSession->availTailIndex = 0; // Preventing malicious data packages
978 if (indexBuf > 0 && hSession->availTailIndex > 0) {
979 if (memmove_s(hSession->ioBuf, hSession->bufSize, hSession->ioBuf + indexBuf, hSession->availTailIndex)
983 uint8_t *bufToZero = reinterpret_cast<uint8_t *>(hSession->ioBuf + hSession->availTailIndex);
984 Base::ZeroBuf(bufToZero, hSession->bufSize - hSession->availTailIndex);
1000 HSession hSession = (HSession)req->handle->data;
1001 --hSession->ref;
1002 HdcSessionBase *thisClass = (HdcSessionBase *)hSession->classInstance;
1005 status, hSession->sessionId, hSession->isDead, uint32_t(hSession->ref));
1007 if (!hSession->isDead && !hSession->ref) {
1008 WRITE_LOG(LOG_DEBUG, "FinishWriteSessionTCP freesession :%u", hSession->sessionId);
1009 thisClass->FreeSession(hSession->sessionId);
1016 bool HdcSessionBase::DispatchSessionThreadCommand(HSession hSession, const uint8_t *baseBuf,
1037 HSession hSession = (HSession)poll->data;
1038 HdcSessionBase *hSessionBase = (HdcSessionBase *)hSession->classInstance;
1041 ssize_t nread = Base::ReadFromFd(hSession->ctrlFd[STREAM_MAIN], buf, size);
1057 hSessionBase->DispatchSessionThreadCommand(hSession, reinterpret_cast<uint8_t *>(buf), nread);
1063 void HdcSessionBase::WorkThreadInitSession(HSession hSession, SessionHandShake &handshake)
1066 handshake.sessionId = hSession->sessionId;
1067 handshake.connectKey = hSession->connectKey;
1068 if (!hSession->isCheck) {
1077 bool HdcSessionBase::WorkThreadStartSession(HSession hSession)
1081 if (hSession->connType == CONN_TCP) {
1082 HdcTCPBase *pTCPBase = (HdcTCPBase *)hSession->classModule;
1083 hSession->hChildWorkTCP.data = hSession;
1084 if (uv_tcp_init(&hSession->childLoop, &hSession->hChildWorkTCP) < 0) {
1088 if ((childRet = uv_tcp_open(&hSession->hChildWorkTCP, hSession->fdChildWorkTCP)) < 0) {
1092 WRITE_LOG(LOG_WARN, "SessionCtrl failed 2,fd:%d,str:%s", hSession->fdChildWorkTCP, buf);
1095 Base::SetTcpOptions((uv_tcp_t *)&hSession->hChildWorkTCP);
1096 uv_read_start((uv_stream_t *)&hSession->hChildWorkTCP, AllocCallback, pTCPBase->ReadStream);
1099 } else if (hSession->connType == CONN_SERIAL) { // UART
1100 HdcUARTBase *pUARTBase = (HdcUARTBase *)hSession->classModule;
1102 regOK = pUARTBase->ReadyForWorkThread(hSession);
1105 HdcUSBBase *pUSBBase = (HdcUSBBase *)hSession->classModule;
1107 regOK = pUSBBase->ReadyForWorkThread(hSession);
1110 if (regOK && hSession->serverOrDaemon) {
1113 WorkThreadInitSession(hSession, handshake);
1117 hSession->sessionId, handshake.authType, hs.c_str());
1119 Send(hSession->sessionId, 0, CMD_KERNEL_HANDSHAKE,
1148 bool HdcSessionBase::DispatchMainThreadCommand(HSession hSession, const CtrlStruct *ctrl)
1155 hSession->sessionId, hSession->serverOrDaemon ? "server" : "daemon");
1156 ret = WorkThreadStartSession(hSession);
1160 WRITE_LOG(LOG_WARN, "Dispatch MainThreadCommand STOP_SESSION sessionId:%u", hSession->sessionId);
1162 HSession hSession = (HSession)handle->data;
1164 if (handle == (uv_handle_t *)hSession->pollHandle[STREAM_WORK]) {
1165 free(hSession->pollHandle[STREAM_WORK]);
1167 if (--hSession->uvChildRef == 0) {
1168 uv_stop(&hSession->childLoop);
1172 hSession->uvChildRef += uvChildRefOffset;
1173 if (hSession->connType == CONN_TCP && hSession->hChildWorkTCP.loop) { // maybe not use it
1174 ++hSession->uvChildRef;
1175 Base::TryCloseHandle((uv_handle_t *)&hSession->hChildWorkTCP, true, closeSessionChildThreadTCPHandle);
1177 Base::TryCloseHandle((uv_handle_t *)hSession->pollHandle[STREAM_WORK], true,
1179 Base::TryCloseHandle((uv_handle_t *)&hSession->dataPipe[STREAM_WORK], true,
1187 AttachChannel(hSession, channelId);
1194 DeatchChannel(hSession, channelId);
1208 HSession hSession = (HSession)poll->data;
1209 HdcSessionBase *hSessionBase = (HdcSessionBase *)hSession->classInstance;
1214 ssize_t nread = Base::ReadFromFd(hSession->ctrlFd[STREAM_WORK], buf, size);
1228 if (!hSessionBase->DispatchMainThreadCommand(hSession, ctrl)) {
1230 hSession->sessionId, ctrl->channelId, ctrl->command);
1241 void HdcSessionBase::ReChildLoopForSessionClear(HSession hSession)
1244 ClearOwnTasks(hSession, 0);
1245 WRITE_LOG(LOG_INFO, "ReChildLoopForSessionClear sessionId:%u", hSession->sessionId);
1247 HSession hSession = (HSession)handle->data;
1248 for (auto v : *hSession->mapTask) {
1260 hSession = thisClass->AdminSession(OP_QUERY, hTask->sessionId, nullptr);
1261 thisClass->AdminTask(OP_VOTE_RESET, hSession, hTask->channelId, nullptr);
1268 uv_stop(&hSession->childLoop); // stop ReChildLoopForSessionClear pendding
1271 &hSession->childLoop, hSession, clearTaskForSessionFinish, (GLOBAL_TIMEOUT * TIME_BASE) / UV_DEFAULT_INTERVAL);
1272 uv_run(&hSession->childLoop, UV_RUN_DEFAULT);
1274 Base::TryCloseChildLoop(&hSession->childLoop, "Session childUV");
1279 HSession hSession = (HSession)arg->data;
1280 HdcSessionBase *thisClass = (HdcSessionBase *)hSession->classInstance;
1281 hSession->hWorkChildThread = uv_thread_self();
1283 uv_poll_t *pollHandle = hSession->pollHandle[STREAM_WORK];
1284 pollHandle->data = hSession;
1285 uv_poll_init_socket(&hSession->childLoop, pollHandle, hSession->ctrlFd[STREAM_WORK]);
1287 WRITE_LOG(LOG_DEBUG, "!!!Workthread run begin, sessionId:%u instance:%s", hSession->sessionId,
1289 uv_run(&hSession->childLoop, UV_RUN_DEFAULT); // work pendding
1290 WRITE_LOG(LOG_DEBUG, "!!!Workthread run again, sessionId:%u", hSession->sessionId);
1292 thisClass->ReChildLoopForSessionClear(hSession); // work pending again
1293 hSession->childCleared = true;
1294 WRITE_LOG(LOG_WARN, "!!!Workthread run finish, sessionId:%u", hSession->sessionId);
1351 bool HdcSessionBase::DispatchTaskData(HSession hSession, const uint32_t channelId, const uint16_t command,
1368 hTaskInfo->sessionId = hSession->sessionId;
1369 hTaskInfo->runLoop = &hSession->childLoop;
1378 if (AdminTask(OP_ADD, hSession, channelId, hTaskInfo)) {
1396 hTaskInfo = AdminTask(OP_QUERY, hSession, channelId, nullptr);
1402 ret = RedirectToTask(hTaskInfo, hSession, channelId, command, payload, payloadSize);