diff --git a/prebuilt/linux-x86/hdc_std b/prebuilt/linux-x86/hdc_std index c575156469fe4db1c61600331f095c4db94d9996..1e714dcd9e37bd8a7f7400cd65a4ff3c81f365b5 100755 Binary files a/prebuilt/linux-x86/hdc_std and b/prebuilt/linux-x86/hdc_std differ diff --git a/prebuilt/windows/hdc_std.exe b/prebuilt/windows/hdc_std.exe index c5bb4b586f5491596f183317e4e97f2a77856eb3..7243ddf1d74c528d756b22b320032fa01609bf57 100755 --- a/prebuilt/windows/hdc_std.exe +++ b/prebuilt/windows/hdc_std.exe @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:39bd4e091032b88181b581e9827ac1dbd00054523ea67407871e2936cc5d7ebd -size 2037248 +oid sha256:36372d8eab32bfcd7e52975011dac360cf9e4e74e041d9caf50f7b5903646266 +size 2039296 diff --git a/src/common/base.cpp b/src/common/base.cpp index 43fb09cc49786b306be06377b6b798c8a21822b5..4a5ee0ab65b4545fb5d6af76c44275349ec032e5 100755 --- a/src/common/base.cpp +++ b/src/common/base.cpp @@ -957,7 +957,7 @@ namespace Base { } timer->data = data; uv_timer_init(loop, timer); - constexpr int repeatTimeout = 250; // ms + constexpr int repeatTimeout = 250; // ms uv_timer_start(timer, cb, 0, repeatTimeout); // delete by callback return true; @@ -973,7 +973,7 @@ namespace Base { return str; } - string CanonicalizeSpecPath(string& src) + string CanonicalizeSpecPath(string &src) { char resolvedPath[PATH_MAX] = { 0 }; #if defined(_WIN32) diff --git a/src/common/base.h b/src/common/base.h index 1c4ae86fedbb8afd12ed2ca7d656ca1c873d7ad8..12e8daaaebd22b242b8e59144beb386ca8ed49cd 100755 --- a/src/common/base.h +++ b/src/common/base.h @@ -75,7 +75,7 @@ namespace Base { int Base64DecodeBuf(const uint8_t *input, const int length, uint8_t *bufOut); string Base64Decode(const uint8_t *input, const int length); void ReverseBytes(void *start, int size); - string CanonicalizeSpecPath(string& src); + string CanonicalizeSpecPath(string &src); // Just zero a POD type, such as a structure or union template int ZeroStruct(T &structBuf) { diff --git a/src/common/channel.cpp b/src/common/channel.cpp index d6fedaff1d67098492fb971610d032833871a19a..b49eeed431cf9b248538baaa7d044d75f4edc12a 100755 --- a/src/common/channel.cpp +++ b/src/common/channel.cpp @@ -41,7 +41,9 @@ HdcChannelBase::~HdcChannelBase() vector HdcChannelBase::GetChannelHandshake(string &connectKey) const { vector ret; + // clang-format off struct ChannelHandShake handshake = {{0}}; + // clang-format on Base::ZeroStruct(handshake); if (strcpy_s(handshake.banner, sizeof(handshake.banner), HANDSHAKE_MESSAGE.c_str()) != EOK) { return ret; @@ -317,13 +319,9 @@ void HdcChannelBase::FreeChannelContinue(HChannel hChannel) // Call from main thread only NotifyInstanceChannelFree(hChannel); if (hChannel->hChildWorkTCP.loop) { - uint8_t abyteFlag[5] = { 0 }; - abyteFlag[0] = SP_DEATCH_CHANNEL; - if (memcpy_s(abyteFlag + 1, sizeof(abyteFlag) - 1, &hChannel->channelId, 4)) { - } - Base::SendToStream((uv_stream_t *)&hChannel->targetSession->ctrlPipe[STREAM_MAIN], (uint8_t *)&abyteFlag, 5); - // If there is blocking problem in the later stage, we can consider changing the whole release process to - // asynchronous implementation like FreeSession + auto ctrl = HdcSessionBase::BuildCtrlString(SP_DEATCH_CHANNEL, hChannel->channelId, nullptr, 0); + Base::SendToStream(reinterpret_cast(&hChannel->targetSession->ctrlPipe[STREAM_MAIN]), + ctrl.data(), ctrl.size()); while (!hChannel->childCleared) { usleep(1000); } diff --git a/src/common/debug.cpp b/src/common/debug.cpp index 1513f2d1dde177b19bf4fe286394b5aa92595dc2..5a96307965d1fcc19cc2dc877a749af09d48c401 100755 --- a/src/common/debug.cpp +++ b/src/common/debug.cpp @@ -16,85 +16,87 @@ #include "base.h" namespace Hdc { -int WriteHexToDebugFile(const char *fileName, const uint8_t *buf, const int bufLen) -{ - char pathName[BUF_SIZE_DEFAULT]; - if (snprintf_s(pathName, sizeof(pathName), sizeof(pathName) - 1, "/mnt/hgfs/vtmp/%s", fileName) < 0) { - return ERR_BUF_OVERFLOW; - } - string srcPath = pathName; - string resolvedPath = Base::CanonicalizeSpecPath(srcPath); - FILE *fp = fopen(resolvedPath.c_str(), "a+"); - if (fp == nullptr) { - if (snprintf_s(pathName, sizeof(pathName), sizeof(pathName) - 1, "/tmp/%s", fileName) < 0) { - WRITE_LOG(LOG_DEBUG, "Write hex to %s failed!", pathName); - return ERR_FILE_OPEN; +namespace Debug { + int WriteHexToDebugFile(const char *fileName, const uint8_t *buf, const int bufLen) + { + char pathName[BUF_SIZE_DEFAULT]; + if (snprintf_s(pathName, sizeof(pathName), sizeof(pathName) - 1, "/mnt/hgfs/vtmp/%s", fileName) < 0) { + return ERR_BUF_OVERFLOW; } + string srcPath = pathName; + string resolvedPath = Base::CanonicalizeSpecPath(srcPath); + FILE *fp = fopen(resolvedPath.c_str(), "a+"); + if (fp == nullptr) { + if (snprintf_s(pathName, sizeof(pathName), sizeof(pathName) - 1, "/tmp/%s", fileName) < 0) { + WRITE_LOG(LOG_DEBUG, "Write hex to %s failed!", pathName); + return ERR_FILE_OPEN; + } - srcPath = pathName; - resolvedPath = Base::CanonicalizeSpecPath(srcPath); - if ((fp = fopen(resolvedPath.c_str(), "a+")) == nullptr) { - WRITE_LOG(LOG_DEBUG, "Write hex to %s failed!", pathName); - return ERR_FILE_OPEN; + srcPath = pathName; + resolvedPath = Base::CanonicalizeSpecPath(srcPath); + if ((fp = fopen(resolvedPath.c_str(), "a+")) == nullptr) { + WRITE_LOG(LOG_DEBUG, "Write hex to %s failed!", pathName); + return ERR_FILE_OPEN; + } } + fwrite(buf, 1, bufLen, fp); + fflush(fp); + fclose(fp); + return ERR_SUCCESS; } - fwrite(buf, 1, bufLen, fp); - fflush(fp); - fclose(fp); - return ERR_SUCCESS; -} -int ReadHexFromDebugFile(const char *fileName, uint8_t *buf, const int bufLen) -{ - char pathName[BUF_SIZE_DEFAULT]; - if (snprintf_s(pathName, sizeof(pathName), sizeof(pathName) - 1, "/mnt/hgfs/VTMP/%s", fileName) < 0) { - return ERR_BUF_OVERFLOW; - } - FILE *fp = fopen(pathName, "r"); - if (fp == nullptr) { - if (snprintf_s(pathName, sizeof(pathName), sizeof(pathName) - 1, "/tmp/%s", fileName) < 0 - || (fp = fopen(pathName, "r")) == nullptr) { - WRITE_LOG(LOG_DEBUG, "Write hex to %s failed!", pathName); - return ERR_FILE_WRITE; + int ReadHexFromDebugFile(const char *fileName, uint8_t *buf, const int bufLen) + { + char pathName[BUF_SIZE_DEFAULT]; + if (snprintf_s(pathName, sizeof(pathName), sizeof(pathName) - 1, "/mnt/hgfs/vtmp/%s", fileName) < 0) { + return ERR_BUF_OVERFLOW; + } + FILE *fp = fopen(pathName, "r"); + if (fp == nullptr) { + if (snprintf_s(pathName, sizeof(pathName), sizeof(pathName) - 1, "/tmp/%s", fileName) < 0 + || (fp = fopen(pathName, "r")) == nullptr) { + WRITE_LOG(LOG_DEBUG, "Write hex to %s failed!", pathName); + return ERR_FILE_WRITE; + } + } + struct stat statbuf; + stat(pathName, &statbuf); + int size = statbuf.st_size; + if (size > bufLen) { + fclose(fp); + return ERR_BUF_SIZE; } - } - struct stat statbuf; - stat(pathName, &statbuf); - int size = statbuf.st_size; - if (size > bufLen) { - fclose(fp); - return ERR_BUF_SIZE; - } - int ret = fread(buf, 1, size, fp); - fflush(fp); - fclose(fp); - if (ret != size) { - return ERR_FILE_READ; + int ret = fread(buf, 1, size, fp); + fflush(fp); + fclose(fp); + if (ret != size) { + return ERR_FILE_READ; + } + return size; } - return size; -} -void DetermineThread(HSession hSession) -{ - if (uv_thread_self() == hSession->hWorkThread) { - WRITE_LOG(LOG_WARN, "At main workthread"); - } else if (uv_thread_self() == hSession->hWorkChildThread) { - WRITE_LOG(LOG_WARN, "At child workthread"); - } else { - WRITE_LOG(LOG_WARN, "At unknow workthread"); + void DetermineThread(HSession hSession) + { + if (uv_thread_self() == hSession->hWorkThread) { + WRITE_LOG(LOG_WARN, "At main workthread"); + } else if (uv_thread_self() == hSession->hWorkChildThread) { + WRITE_LOG(LOG_WARN, "At child workthread"); + } else { + WRITE_LOG(LOG_WARN, "At unknow workthread"); + } } -} -int PrintfHexBuf(const uint8_t *buf, int bufLen) -{ - int i = 0; - for (i = 0; i < bufLen; i++) { - printf("0x%02x, ", buf[i]); + int PrintfHexBuf(const uint8_t *buf, int bufLen) + { + int i = 0; + for (i = 0; i < bufLen; i++) { + printf("0x%02x, ", buf[i]); + fflush(stdout); + } + printf("\r\n"); fflush(stdout); + return 0; } - printf("\r\n"); - fflush(stdout); - return 0; } } // namespace Hdc \ No newline at end of file diff --git a/src/common/debug.h b/src/common/debug.h index acf9623b640361c0f353434e25048a23f6c9c32c..b14d1e10d155c7f64ee46285d5e7a026d2c5ffb3 100755 --- a/src/common/debug.h +++ b/src/common/debug.h @@ -17,10 +17,12 @@ #include "common.h" namespace Hdc { -int WriteHexToDebugFile(const char *fileName, const uint8_t *buf, const int bufLen); -int ReadHexFromDebugFile(const char *fileName, uint8_t *buf, const int bufLen); -void DetermineThread(HSession hSession); -int PrintfHexBuf(const uint8_t *buf, int bufLen); +namespace Debug { + int WriteHexToDebugFile(const char *fileName, const uint8_t *buf, const int bufLen); + int ReadHexFromDebugFile(const char *fileName, uint8_t *buf, const int bufLen); + void DetermineThread(HSession hSession); + int PrintfHexBuf(const uint8_t *buf, int bufLen); +} } // namespace Hdc #endif \ No newline at end of file diff --git a/src/common/define.h b/src/common/define.h index ebff032a0446422ffbf318c8b136aa0adea3c477..3b9d16f01daad36b96ceb60ab8f5fbb910f522f9 100755 --- a/src/common/define.h +++ b/src/common/define.h @@ -33,6 +33,7 @@ const string STRING_EMPTY = ""; const string DEFAULT_SERVER_ADDR = "127.0.0.1:8710"; // ################################ macro define ################################### +constexpr uint16_t BUF_SIZE_MICRO = 16; constexpr uint16_t BUF_SIZE_TINY = 64; constexpr uint16_t BUF_SIZE_SMALL = 256; constexpr uint16_t BUF_SIZE_MEDIUM = 512; @@ -41,8 +42,8 @@ constexpr uint16_t BUF_SIZE_DEFAULT2 = BUF_SIZE_DEFAULT * 2; constexpr uint8_t DWORD_SERIALIZE_SIZE = 4; constexpr uint32_t HDC_BUF_MAX_BYTES = 1024000000; constexpr uint16_t MAX_IP_PORT = 65535; -constexpr uint8_t STREAM_MAIN = 0; // work at main thread -constexpr uint8_t STREAM_WORK = 1; // work at work thread +constexpr uint8_t STREAM_MAIN = 0; // work at main thread +constexpr uint8_t STREAM_WORK = 1; // work at work thread constexpr uint16_t MAX_CONNECTKEY_SIZE = 32; // usb sn/tcp ipport constexpr uint8_t MAX_IO_OVERLAP = 128; constexpr auto TIME_BASE = 1000; // time unit conversion base value @@ -50,7 +51,7 @@ constexpr auto TIME_BASE = 1000; // time unit conversion base value // general one argument command argc constexpr int CMD_ARG1_COUNT = 2; // The first child versions must match, otherwise server and daemon must be upgraded -const string VERSION_NUMBER = "1.1.0a"; // same with openssl version, 1.1.2==VERNUMBER 0x10102000 +const string VERSION_NUMBER = "1.1.0b"; // same with openssl version, 1.1.2==VERNUMBER 0x10102000 const string HANDSHAKE_MESSAGE = "OHOS HDC"; // sep not char '-', not more than 11 bytes const string PACKET_FLAG = "HW"; // must 2bytes const string EMPTY_ECHO = "[Empty]"; @@ -93,7 +94,7 @@ enum LogLevel { LOG_WARN, LOG_DEBUG, LOG_FULL, - LOG_LAST = LOG_FULL, + LOG_LAST = LOG_FULL, // tail, not use }; #define WRITE_LOG(x, y...) Base::PrintLogEx(__FILE__, __LINE__, x, y) @@ -147,7 +148,7 @@ enum AsyncEvent { ASYNC_STOP_MAINLOOP = 0, ASYNC_FREE_SESSION, }; -enum SocketpairEvent { +enum InnerCtrlCommand { SP_START_SESSION = 0, SP_STOP_SESSION, SP_REGISTER_CHANNEL, diff --git a/src/common/serial_struct.h b/src/common/serial_struct.h index b50c8d1221a61b62466c49b415b29f8ea8e31866..26a2bb338024daf8211b90143d55bb7f9e81a56a 100755 --- a/src/common/serial_struct.h +++ b/src/common/serial_struct.h @@ -1214,7 +1214,7 @@ namespace SerialStruct { size_t _pos; }; - // mycode + // mytype template<> struct Serializer { static void Serialize(uint32_t tag, uint8_t value, FlagsType<>, Writer &out, bool force = false) { @@ -1246,7 +1246,7 @@ namespace SerialStruct { return false; } }; - // mycode finish + // mytype finish template std::string SerializeToString(const T &value) { std::string out; diff --git a/src/common/session.cpp b/src/common/session.cpp index 5e6da5f3c2dfc50d7ed5a0534887ec668835eeda..d024e90b5e539bb6535b6da2ec6ee041bc95c15b 100755 --- a/src/common/session.cpp +++ b/src/common/session.cpp @@ -357,7 +357,7 @@ HSession HdcSessionBase::MallocSession(bool serverOrDaemon, const ConnType connT hSession->listKey = new list; hSession->uvRef = 0; // pullup child - WRITE_LOG(LOG_DEBUG, "HdcSessionBase::New Session, ptr:%p", hSession); + WRITE_LOG(LOG_DEBUG, "HdcSessionBase NewSession, sessionId:%d", hSession->sessionId); uv_tcp_init(&loopMain, &hSession->ctrlPipe[STREAM_MAIN]); hSession->uvRef++; @@ -438,7 +438,7 @@ void HdcSessionBase::FreeSessionFinally(uv_idle_t *handle) // all hsession uv handle has been clear thisClass->AdminSession(OP_REMOVE, hSession->sessionId, nullptr); hSession->mainCleared = true; - WRITE_LOG(LOG_DEBUG, "FreeSessionFinally hSession: %p finish", hSession); + WRITE_LOG(LOG_DEBUG, "FreeSessionFinally sessionId:%d finish", hSession->sessionId); delete hSession; hSession = nullptr; // fix CodeMars SetNullAfterFree issue Base::TryCloseHandle((const uv_handle_t *)handle, Base::CloseIdleCallback); @@ -487,9 +487,9 @@ void HdcSessionBase::FreeSession(const uint32_t sessionId) PushAsyncMessage(hSession->sessionId, ASYNC_FREE_SESSION, nullptr, 0); return; } - WRITE_LOG(LOG_DEBUG, "FreeSession hSession:%p sendref:%u", hSession, uint16_t(hSession->sendRef)); + WRITE_LOG(LOG_DEBUG, "FreeSession sessionid:%d sendref:%u", hSession->sessionId, uint16_t(hSession->sendRef)); while (true) { - if (!hSession || hSession->sendRef || hSession->mainCleared || hSession->isDead) { + if (hSession->sendRef || hSession->isDead) { break; } bNotTodo = false; @@ -502,9 +502,9 @@ void HdcSessionBase::FreeSession(const uint32_t sessionId) NotifyInstanceSessionFree(hSession); // Notify Server or Daemon, just UI or display commandline // wait workthread to free if (hSession->ctrlPipe[STREAM_WORK].loop) { - // If the sub-thread has been bound and run - uint8_t flag = SP_STOP_SESSION; - Base::SendToStream((uv_stream_t *)&hSession->ctrlPipe[STREAM_MAIN], (uint8_t *)&flag, 1); + auto ctrl = BuildCtrlString(SP_STOP_SESSION, 0, nullptr, 0); + Base::SendToStream((uv_stream_t *)&hSession->ctrlPipe[STREAM_MAIN], ctrl.data(), ctrl.size()); + WRITE_LOG(LOG_DEBUG, "FreeSession, send workthread fo free. sessionId:%d", hSession->sessionId); auto callbackCheckFreeSessionContinue = [](uv_timer_t *handle) -> void { HSession hSession = (HSession)handle->data; HdcSessionBase *thisClass = (HdcSessionBase *)hSession->classInstance; @@ -585,9 +585,9 @@ int HdcSessionBase::SendByProtocol(HSession hSession, uint8_t *bufPtr, const int if (hSession->isDead) { return ERR_SESSION_NOFOUND; } + int ret = 0; uv_mutex_lock(&hSession->sendMutex); hSession->sendRef++; - int ret = 0; switch (hSession->connType) { case CONN_TCP: { if (hSession->hWorkThread == uv_thread_self()) { @@ -796,6 +796,8 @@ void HdcSessionBase::ReadCtrlFromSession(uv_stream_t *uvpipe, ssize_t nread, con WRITE_LOG(LOG_WARN, "HdcSessionBase read overlap data"); break; } + // only one command, no need to split command from stream + // if add more commands, consider the format command hSessionBase->DispatchSessionThreadCommand(uvpipe, hSession, (uint8_t *)buf->base, nread); break; } @@ -839,18 +841,41 @@ bool HdcSessionBase::WorkThreadStartSession(HSession hSession) return true; } -bool HdcSessionBase::DispatchMainThreadCommand(uv_stream_t *uvpipe, HSession hSession, const uint8_t *baseBuf, - const int bytesIO) +vector HdcSessionBase::BuildCtrlString(InnerCtrlCommand command, uint32_t channelId, + const uint8_t *data, int dataSize) { - bool ret = true; - uint8_t flag = *(uint8_t *)baseBuf; + vector ret; + while (true) { + if (dataSize > BUF_SIZE_MICRO) { + break; + } + CtrlStruct ctrl; + Base::ZeroStruct(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(&ctrl); + ret.insert(ret.end(), buf, buf + sizeof(CtrlStruct)); + break; + } + return ret; +} - switch (flag) { +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"); ret = WorkThreadStartSession(hSession); break; } case SP_STOP_SESSION: { + WRITE_LOG(LOG_DEBUG, "Dispatch MainThreadCommand STOP_SESSION"); Base::TryCloseHandle((uv_handle_t *)&hSession->hChildWorkTCP); Base::TryCloseHandle((uv_handle_t *)&hSession->ctrlPipe[STREAM_WORK]); Base::TryCloseHandle((uv_handle_t *)&hSession->dataPipe[STREAM_WORK]); @@ -861,7 +886,6 @@ bool HdcSessionBase::DispatchMainThreadCommand(uv_stream_t *uvpipe, HSession hSe if (!serverOrDaemon) { break; // Only Server has this feature } - uint32_t channelId = *(uint32_t *)(baseBuf + 1); RegisterChannel(hSession, channelId); break; } @@ -869,15 +893,13 @@ bool HdcSessionBase::DispatchMainThreadCommand(uv_stream_t *uvpipe, HSession hSe if (!serverOrDaemon) { break; // Only Server has this feature } - uint32_t channelId = *(uint32_t *)(baseBuf + 1); - AttachChannel(uvpipe, hSession, channelId); + AttachChannel(hSession, channelId); break; } case SP_DEATCH_CHANNEL: { if (!serverOrDaemon) { break; // Only Server has this feature } - uint32_t channelId = *(uint32_t *)(baseBuf + 1); DeatchChannel(channelId); break; } @@ -894,20 +916,20 @@ void HdcSessionBase::ReadCtrlFromMain(uv_stream_t *uvpipe, ssize_t nread, const { HSession hSession = (HSession)uvpipe->data; HdcSessionBase *hSessionBase = (HdcSessionBase *)hSession->classInstance; - - while (true) { - if (nread < 0) { - WRITE_LOG(LOG_DEBUG, "SessionCtrl failed,%s", uv_strerror(nread)); + int formatCommandSize = sizeof(CtrlStruct); + int index = 0; + while (index < nread) { + if (nread % formatCommandSize != 0) { + WRITE_LOG(LOG_FATAL, "ReadCtrlFromMain size failed, nread == %d", nread); break; } - // In addition to the special transmission of the control instruction, the number of bytes will not be lower - // than the Packethead length, and the control command is one byte. - if (nread > 64) { - WRITE_LOG(LOG_WARN, "HdcSessionBase read overlap data"); + if (nread < 0) { + WRITE_LOG(LOG_DEBUG, "SessionCtrl failed,%s", uv_strerror(nread)); break; } - hSessionBase->DispatchMainThreadCommand(uvpipe, hSession, (uint8_t *)buf->base, nread); - break; + CtrlStruct *ctrl = reinterpret_cast(buf->base + index); + index += sizeof(CtrlStruct); + hSessionBase->DispatchMainThreadCommand(hSession, ctrl); } delete[] buf->base; } @@ -947,12 +969,13 @@ void HdcSessionBase::SessionWorkThread(uv_work_t *arg) WRITE_LOG(LOG_DEBUG, "SessionCtrl err2, %s fd:%d", uv_strerror(childRet), hSession->ctrlFd[STREAM_WORK]); } uv_read_start((uv_stream_t *)&hSession->ctrlPipe[STREAM_WORK], Base::AllocBufferCallback, ReadCtrlFromMain); - WRITE_LOG(LOG_DEBUG, "!!!BeginThread:ChildSession, hSession:%p childLoop:%p", hSession, &hSession->childLoop); + WRITE_LOG(LOG_DEBUG, "!!!Workthread run begin, sessionId:%d", hSession->sessionId); uv_run(&hSession->childLoop, UV_RUN_DEFAULT); // work pendding + WRITE_LOG(LOG_DEBUG, "!!!Workthread run again, sessionId:%d", hSession->sessionId); // main loop has exit - thisClass->ReChildLoopForSessionClear(hSession); // work pendding + thisClass->ReChildLoopForSessionClear(hSession); // work pending again hSession->childCleared = true; - WRITE_LOG(LOG_DEBUG, "Session childUV finish, hSession:%p childLoop:%p", hSession, &hSession->childLoop); + WRITE_LOG(LOG_DEBUG, "!!!Workthread run finish, sessionId:%d", hSession->sessionId); } // clang-format off diff --git a/src/common/session.h b/src/common/session.h index e4a67962b61c8ea7900ff59e4e9a0d057e524259..6279f842ee3d5da1ac944afcc1f872928e3dbbe3 100755 --- a/src/common/session.h +++ b/src/common/session.h @@ -30,11 +30,16 @@ public: string connectKey; string buf; }; - + struct CtrlStruct { + InnerCtrlCommand command; + uint32_t channelId; + uint8_t dataSize; + uint8_t data[BUF_SIZE_MICRO]; + }; HdcSessionBase(bool serverOrDaemonIn); virtual ~HdcSessionBase(); virtual void RegisterChannel(HSession hSession, const uint32_t channelId) {}; - virtual void AttachChannel(uv_stream_t *uvpipe, HSession hSession, const uint32_t channelId) {}; + virtual void AttachChannel(HSession hSession, const uint32_t channelId) {}; virtual void DeatchChannel(const uint32_t channelId) {}; virtual bool RedirectToTask(HTaskInfo hTaskInfo, HSession hSession, const uint32_t channelId, const uint16_t command, uint8_t *payload, const int payloadSize) @@ -87,6 +92,8 @@ public: { return wantRestart; } + static vector BuildCtrlString(InnerCtrlCommand command, uint32_t channelId, + const uint8_t *data, int dataSize); uv_loop_t loopMain; bool serverOrDaemon; uv_async_t asyncMainLoop; @@ -144,7 +151,7 @@ private: { } int DecryptPayload(HSession hSession, uint8_t *pEncryptBuf, const int bufLen); - bool DispatchMainThreadCommand(uv_stream_t *uvpipe, HSession hSession, const uint8_t *baseBuf, const int bytesIO); + bool DispatchMainThreadCommand(HSession hSession, const CtrlStruct *ctrl); bool DispatchSessionThreadCommand(uv_stream_t *uvpipe, HSession hSession, const uint8_t *baseBuf, const int bytesIO); bool BeginRemoveTask(HTaskInfo hTask); diff --git a/src/common/usb.cpp b/src/common/usb.cpp index 6bc83e35cabf3cff579bc19a4a0047b41ad5d573..9bf5bdf58bd8ff7806b472d9df81a4783c8abb95 100755 --- a/src/common/usb.cpp +++ b/src/common/usb.cpp @@ -101,10 +101,9 @@ int HdcUSBBase::SendUSBBlock(HSession hSession, uint8_t *data, const int length) return offset; } -// hSession->dataPipe[STREAM_MAIN] -bool HdcUSBBase::SendToHdcStream(uv_stream_t *stream, HUSB usb, uint8_t *appendData, int dataSize) +bool HdcUSBBase::SendToHdcStream(HSession hSession, uv_stream_t *stream, uint8_t *appendData, int dataSize) { - vector &bufRecv = usb->bufRecv; + vector &bufRecv = hSession->hUSB->bufRecv; bufRecv.insert(bufRecv.end(), appendData, appendData + dataSize); int ret = ERR_SUCCESS; while (bufRecv.size() > sizeof(USBHead)) { @@ -116,17 +115,23 @@ bool HdcUSBBase::SendToHdcStream(uv_stream_t *stream, HUSB usb, uint8_t *appendD } if (bufRecv.size() < sizeof(USBHead) + usbHeader->dataSize) { WRITE_LOG(LOG_DEBUG, "SendToHdcStream not enough"); - break; // successful, but not enough + break; // successful , but not enough } - // usb data to logic - if (Base::SendToStream(stream, bufRecv.data() + sizeof(USBHead), usbHeader->dataSize) < 0) { - ret = ERR_IO_FAIL; - WRITE_LOG(LOG_FATAL, "Error usb send to stream"); + if (usbHeader->sessionId != hSession->sessionId) { + SendUsbReset(hSession->hUSB, usbHeader->sessionId); + ret = ERR_SESSION_NOFOUND; + WRITE_LOG(LOG_FATAL, "SendToHdcStream sessionId not matched"); break; + } else { + // usb data to logic + if (Base::SendToStream(stream, bufRecv.data() + sizeof(USBHead), usbHeader->dataSize) < 0) { + ret = ERR_IO_FAIL; + WRITE_LOG(LOG_FATAL, "Error usb send to stream"); + break; + } } bufRecv.erase(bufRecv.begin(), bufRecv.begin() + sizeof(USBHead) + usbHeader->dataSize); } - return ret == ERR_SUCCESS; } } \ No newline at end of file diff --git a/src/common/usb.h b/src/common/usb.h index ce81ae645d79a5976c1f64f00b20de569ff6f86e..a693528cbe1eadb3b8a94ccd998edb1d0149cf5c 100755 --- a/src/common/usb.h +++ b/src/common/usb.h @@ -28,9 +28,10 @@ public: virtual bool ReadyForWorkThread(HSession hSession); int SendUSBBlock(HSession hSession, uint8_t *data, const int length); static void ReadUSB(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf); - bool SendToHdcStream(uv_stream_t *stream, HUSB usb, uint8_t *appendData, int dataSize); + bool SendToHdcStream(HSession hSession, uv_stream_t *stream, uint8_t *appendData, int dataSize); protected: + virtual void SendUsbReset(HUSB hUSB, uint32_t sessionId) {}; void *clsMainBase; bool serverOrDaemon; bool modRunning; diff --git a/src/daemon/daemon_tcp.cpp b/src/daemon/daemon_tcp.cpp index 5d079c8976e0f71adc54f8ccaa830b7265644472..8b3de8e3993abb5fc351903bc48f72fc69c2d291 100755 --- a/src/daemon/daemon_tcp.cpp +++ b/src/daemon/daemon_tcp.cpp @@ -62,11 +62,12 @@ void HdcDaemonTCP::TransmitConfig(const sockaddr *addrSrc, uv_udp_t *handle) void HdcDaemonTCP::AcceptClient(uv_stream_t *server, int status) { - uint8_t *byteFlag = nullptr; uv_loop_t *ptrLoop = server->loop; uv_tcp_t *pServTCP = (uv_tcp_t *)server; HdcDaemonTCP *thisClass = (HdcDaemonTCP *)pServTCP->data; HdcSessionBase *ptrConnect = (HdcSessionBase *)thisClass->clsMainBase; + HdcSessionBase *daemon = reinterpret_cast(thisClass->clsMainBase); + auto ctrl = daemon->BuildCtrlString(SP_START_SESSION, 0, nullptr, 0); HSession hSession = ptrConnect->MallocSession(false, CONN_TCP, thisClass); if (!hSession) { return; @@ -86,13 +87,8 @@ void HdcDaemonTCP::AcceptClient(uv_stream_t *server, int status) #ifdef UNIT_TEST hSession->fdChildWorkTCP = dup(hSession->fdChildWorkTCP); #endif - uv_read_stop((uv_stream_t *)&hSession->hWorkTCP); - // Send a HWORKTCP handle via PIPE[0] - byteFlag = new uint8_t[1]; - *byteFlag = SP_START_SESSION; - Base::SendToStreamEx((uv_stream_t *)&hSession->ctrlPipe[STREAM_MAIN], (uint8_t *)byteFlag, 1, nullptr, - (void *)Base::SendCallback, byteFlag); + Base::SendToStream((uv_stream_t *)&hSession->ctrlPipe[STREAM_MAIN], ctrl.data(), ctrl.size()); return; Finish: ptrConnect->FreeSession(hSession->sessionId); diff --git a/src/daemon/daemon_unity.cpp b/src/daemon/daemon_unity.cpp index 32015e3fd1dc7d4548b8eb75a30f6576917b40c5..434a6d1bf48a48a296dafd1694cbce4b7274146d 100755 --- a/src/daemon/daemon_unity.cpp +++ b/src/daemon/daemon_unity.cpp @@ -233,18 +233,12 @@ bool HdcDaemonUnity::RebootDevice(const uint8_t *cmd, const int cmdSize) { sync(); string propertyVal; - int ret = -1; if (!cmdSize) { propertyVal = "reboot"; } else { propertyVal = Base::StringFormat("reboot,%s", cmd); } - if (ret > 0) { - Base::SetHdcProperty(rebootProperty.c_str(), propertyVal.c_str()); - return true; - } else { - return false; - } + return Base::SetHdcProperty(rebootProperty.c_str(), propertyVal.c_str()); } bool HdcDaemonUnity::SetDeviceRunMode(void *daemonIn, const char *cmd) diff --git a/src/daemon/daemon_usb.cpp b/src/daemon/daemon_usb.cpp index f023606b34c158239e0b34fe0226f176843e0af2..21ab130a6f33e3e248190bb00a51b733d48f29eb 100755 --- a/src/daemon/daemon_usb.cpp +++ b/src/daemon/daemon_usb.cpp @@ -139,7 +139,6 @@ void HdcDaemonUSB::CloseEndpoint(HUSB hUSB) hUSB->control = 0; } isAlive = false; - currentSessionId = 0; WRITE_LOG(LOG_FATAL, "DaemonUSB CloseEndpoint"); } @@ -157,12 +156,10 @@ bool HdcDaemonUSB::AvailablePacket(uint8_t *ioBuf, uint32_t *sessionId) break; } if ((usbPayloadHeader->option & USB_OPTION_RESET)) { - if (currentSessionId != 0) { - HdcDaemon *daemon = reinterpret_cast(clsMainBase); - // The Host end program is restarted, but the USB cable is still connected - WRITE_LOG(LOG_WARN, "Hostside want restart daemon, restart old sessionid:%d", currentSessionId); - daemon->PushAsyncMessage(currentSessionId, ASYNC_FREE_SESSION, nullptr, 0); - } + HdcDaemon *daemon = reinterpret_cast(clsMainBase); + // The Host end program is restarted, but the USB cable is still connected + WRITE_LOG(LOG_WARN, "Hostside want restart daemon, restart old sessionid:%d", usbPayloadHeader->sessionId); + daemon->PushAsyncMessage(usbPayloadHeader->sessionId, ASYNC_FREE_SESSION, nullptr, 0); break; } *sessionId = usbPayloadHeader->sessionId; @@ -193,7 +190,7 @@ int HdcDaemonUSB::SendUSBIOSync(HSession hSession, HUSB hMainUSB, uint8_t *data, if (!modRunning) { goto Finish; } - while (modRunning) { + while (modRunning && !hSession->isDead) { childRet = write(bulkIn, (uint8_t *)data + offset, length - offset); if (childRet <= 0) { int err = errno; @@ -270,16 +267,18 @@ HSession HdcDaemonUSB::PrepareNewSession(uint32_t sessionId, uint8_t *pRecvBuf, Base::StartWorkThread(&daemon->loopMain, daemon->SessionWorkThread, Base::FinishWorkThread, hChildSession); auto funcNewSessionUp = [](uv_timer_t *handle) -> void { HSession hChildSession = reinterpret_cast(handle->data); + HdcDaemon *daemon = reinterpret_cast(hChildSession->classInstance); if (hChildSession->childLoop.active_handles == 0) { return; } - uint8_t flag = SP_START_SESSION; - Base::SendToStream((uv_stream_t *)&hChildSession->ctrlPipe[STREAM_MAIN], &flag, 1); - WRITE_LOG(LOG_DEBUG, "Main thread usbio mirgate finish"); - Base::TryCloseHandle(reinterpret_cast(handle), Base::CloseTimerCallback); + if (!hChildSession->isDead) { + auto ctrl = daemon->BuildCtrlString(SP_START_SESSION, 0, nullptr, 0); + Base::SendToStream((uv_stream_t *)&hChildSession->ctrlPipe[STREAM_MAIN], ctrl.data(), ctrl.size()); + WRITE_LOG(LOG_DEBUG, "Main thread usbio mirgate finish"); + } + Base::TryCloseHandle(reinterpret_cast(handle), Base::CloseTimerCallback); }; Base::TimerUvTask(&daemon->loopMain, hChildSession, funcNewSessionUp); - return hChildSession; } @@ -296,7 +295,7 @@ int HdcDaemonUSB::DispatchToWorkThread(HSession hSession, const uint32_t session return ERR_SESSION_NOFOUND; } } - if (!SendToHdcStream(reinterpret_cast(&hChildSession->dataPipe[STREAM_MAIN]), hChildSession->hUSB, + if (!SendToHdcStream(hChildSession, reinterpret_cast(&hChildSession->dataPipe[STREAM_MAIN]), readBuf, readBytes)) { return ERR_IO_FAIL; } diff --git a/src/daemon/daemon_usb.h b/src/daemon/daemon_usb.h index c6294613dd409827b2a98f346d441ce685b7427e..e249b23985bf26b1da8ce2f11e19249b7a55486c 100755 --- a/src/daemon/daemon_usb.h +++ b/src/daemon/daemon_usb.h @@ -49,7 +49,7 @@ private: HSession usbMain; uint32_t currentSessionId = 0; // USB mode,limit only one session std::atomic ref = 0; - uv_timer_t checkEP; // server-use + uv_timer_t checkEP; // server-use uv_mutex_t sendEP; bool isAlive = false; }; diff --git a/src/daemon/usb_ffs.h b/src/daemon/usb_ffs.h index 044f4cb58b4529acf089d5cf47b12d962409f622..66305e646b72e5b8662cf58e807a239e3f485c3c 100755 --- a/src/daemon/usb_ffs.h +++ b/src/daemon/usb_ffs.h @@ -14,8 +14,10 @@ */ #ifndef HDC_USBFFS_H #define HDC_USBFFS_H +// clang-format off #include #include "daemon_common.h" +// clang-format on namespace Hdc { constexpr auto HDC_USBDR_SND = 0x0; @@ -199,16 +201,16 @@ static const struct { .hs_descs = hs_descriptors, .ss_descs = ss_descriptors, .os_header = { - .interface = CPU_TO_LE32(0), + .interface = 0, .dwLength = CPU_TO_LE32(sizeof(USB_FFS_DESC.os_header) + sizeof(USB_FFS_DESC.os_desc)), - .bcdVersion = CPU_TO_LE32(1), - .wIndex = CPU_TO_LE32(4), - .bCount = CPU_TO_LE32(1), - .Reserved = CPU_TO_LE32(0), + .bcdVersion = CPU_TO_LE16(1), + .wIndex = CPU_TO_LE16(4), + .bCount = 1, + .Reserved = 0, }, .os_desc = { .bFirstInterfaceNumber = 0, - .Reserved1 = CPU_TO_LE32(1), + .Reserved1 = 1, .CompatibleID = { 'W', 'I', 'N', 'U', 'S', 'B', '\0', '\0'}, .SubCompatibleID = {0}, .Reserved2 = {0}, diff --git a/src/host/client.cpp b/src/host/client.cpp index befcec6b77a056a980e4ea98d1e05a72be155d88..1060de263983de72f91c6f30bebf9a0bbce074c8 100755 --- a/src/host/client.cpp +++ b/src/host/client.cpp @@ -316,8 +316,10 @@ int HdcClient::PreHandshake(HChannel hChannel, const uint8_t *buf) // send config // channel handshake step2 Base::ZeroBuf(handShakePacket->connectKey, sizeof(handShakePacket->connectKey)); + // clang-format off if (memcpy_s(handShakePacket->connectKey, sizeof(handShakePacket->connectKey), connectKey.c_str(), connectKey.size())) { + // clang-format on hChannel->availTailIndex = 0; WRITE_LOG(LOG_DEBUG, "Channel Hello failed"); return ERR_BUF_COPY; diff --git a/src/host/host_tcp.cpp b/src/host/host_tcp.cpp index 9180ecfa06a0d39aa46b4db290101e186a909d9e..cef01f37487e1a031c81940f460ade0e1e218685 100755 --- a/src/host/host_tcp.cpp +++ b/src/host/host_tcp.cpp @@ -98,7 +98,7 @@ void HdcHostTCP::Connect(uv_connect_t *connection, int status) HSession hSession = (HSession)connection->data; delete connection; HdcSessionBase *ptrConnect = (HdcSessionBase *)hSession->classInstance; - uint8_t *byteFlag = nullptr; + auto ctrl = ptrConnect->BuildCtrlString(SP_START_SESSION, 0, nullptr, 0); if (status < 0) { goto Finish; } @@ -118,10 +118,7 @@ void HdcHostTCP::Connect(uv_connect_t *connection, int status) #endif // The main thread is no longer read, handed over to the Child thread uv_read_stop((uv_stream_t *)&hSession->hWorkTCP); - byteFlag = new uint8_t[1]; // free by SendCallback - *byteFlag = SP_START_SESSION; - Base::SendToStreamEx((uv_stream_t *)&hSession->ctrlPipe[STREAM_MAIN], (uint8_t *)byteFlag, 1, nullptr, - (void *)Base::SendCallback, byteFlag); + Base::SendToStream((uv_stream_t *)&hSession->ctrlPipe[STREAM_MAIN], ctrl.data(), ctrl.size()); return; Finish: WRITE_LOG(LOG_DEBUG, "Connect failed"); diff --git a/src/host/host_usb.cpp b/src/host/host_usb.cpp index 345da108ee2588919e0095c743e7d52ce932d9f8..995c2d2797fc11c571c472f641c1451984debbef 100755 --- a/src/host/host_usb.cpp +++ b/src/host/host_usb.cpp @@ -63,23 +63,38 @@ int HdcHostUSB::Initial() return 0; } -bool HdcHostUSB::SendUsbReset(HUSB hUSB) +void HdcHostUSB::SendUsbReset(HUSB hUSB, uint32_t sessionId) { - USBHead usbPayloadHeader; - Base::ZeroStruct(usbPayloadHeader); - usbPayloadHeader.option |= USB_OPTION_RESET; - if (memcpy_s(usbPayloadHeader.flag, sizeof(usbPayloadHeader.flag), + USBHead *usbPayloadHeader = new USBHead(); + usbPayloadHeader->option |= USB_OPTION_RESET; + usbPayloadHeader->sessionId = sessionId; + if (memcpy_s(usbPayloadHeader->flag, sizeof(usbPayloadHeader->flag), PACKET_FLAG.c_str(), PACKET_FLAG.size()) != EOK) { - return false; + delete usbPayloadHeader; + return; } - // if daemon failed, here willbe block USB_TIMEOUT - int transferred = 0; - libusb_bulk_transfer(hUSB->devHandle, hUSB->epHost, (uint8_t *)&usbPayloadHeader, sizeof(USBHead), - &transferred, TIME_BASE); - if (transferred != sizeof(USBHead)) { - return false; + auto resetUsbCallback = [](struct libusb_transfer *transfer) -> void LIBUSB_CALL { + USBHead *usbHead = (USBHead *)transfer->user_data; + if (LIBUSB_TRANSFER_COMPLETED != transfer->status) { + WRITE_LOG(LOG_FATAL, "SendUSBRaw status:%d", transfer->status); + } + delete usbHead; + libusb_free_transfer(transfer); + // has send soft reset, next reset daemon's send + libusb_reset_device(transfer->dev_handle); + WRITE_LOG(LOG_DEBUG, "Device reset singal send"); + }; + libusb_transfer *transferUsb = libusb_alloc_transfer(0); + // clang-format off + libusb_fill_bulk_transfer(transferUsb, hUSB->devHandle, hUSB->epHost, (uint8_t *)usbPayloadHeader, sizeof(USBHead), + resetUsbCallback, usbPayloadHeader, GLOBAL_TIMEOUT * TIME_BASE); + // clang-format on + int err = libusb_submit_transfer(transferUsb); + if (err < 0) { + WRITE_LOG(LOG_FATAL, "libusb_submit_transfer failed, err:%d", err); + delete usbPayloadHeader; + return; } - return true; } bool HdcHostUSB::DetectMyNeed(libusb_device *device, string &sn) @@ -92,22 +107,19 @@ bool HdcHostUSB::DetectMyNeed(libusb_device *device, string &sn) delete hUSB; return false; } - while (true) { - WRITE_LOG(LOG_INFO, "Needed device found, busid:%d devid:%d connectkey:%s", hUSB->busId, hUSB->devId, - hUSB->serialNumber.c_str()); - // USB device is automatically connected after recognition, auto connect USB - UpdateUSBDaemonInfo(hUSB, nullptr, STATUS_READY); - HdcServer *hdcServer = (HdcServer *)clsMainBase; - HSession hSession = hdcServer->MallocSession(true, CONN_USB, this); - hSession->connectKey = hUSB->serialNumber; - uv_timer_t *waitTimeDoCmd = new uv_timer_t; - uv_timer_init(&hdcServer->loopMain, waitTimeDoCmd); - waitTimeDoCmd->data = hSession; - uv_timer_start(waitTimeDoCmd, hdcServer->UsbPreConnect, 10, 100); - mapIgnoreDevice[sn] = HOST_USB_REGISTER; - ret = true; - break; - } + WRITE_LOG(LOG_INFO, "Needed device found, busid:%d devid:%d connectkey:%s", hUSB->busId, hUSB->devId, + hUSB->serialNumber.c_str()); + // USB device is automatically connected after recognition, auto connect USB + UpdateUSBDaemonInfo(hUSB, nullptr, STATUS_READY); + HdcServer *hdcServer = (HdcServer *)clsMainBase; + HSession hSession = hdcServer->MallocSession(true, CONN_USB, this); + hSession->connectKey = hUSB->serialNumber; + uv_timer_t *waitTimeDoCmd = new uv_timer_t; + uv_timer_init(&hdcServer->loopMain, waitTimeDoCmd); + waitTimeDoCmd->data = hSession; + uv_timer_start(waitTimeDoCmd, hdcServer->UsbPreConnect, 10, 100); + mapIgnoreDevice[sn] = HOST_USB_REGISTER; + ret = true; libusb_release_interface(hUSB->devHandle, hUSB->interfaceNumber); libusb_close(hUSB->devHandle); hUSB->devHandle = nullptr; @@ -131,7 +143,7 @@ void HdcHostUSB::KickoutZombie(HSession hSession) { HdcServer *ptrConnect = (HdcServer *)hSession->classInstance; HUSB hUSB = hSession->hUSB; - if (!hUSB->devHandle) { + if (!hUSB->devHandle || hSession->isDead) { return; } if (LIBUSB_ERROR_NO_DEVICE != libusb_kernel_driver_active(hUSB->devHandle, hUSB->interfaceNumber)) { @@ -309,7 +321,7 @@ void LIBUSB_CALL HdcHostUSB::ReadUSBBulkCallback(struct libusb_transfer *transfe WRITE_LOG(LOG_FATAL, "Host usb not LIBUSB_TRANSFER_COMPLETED, status:%d", transfer->status); break; } - if (!thisClass->SendToHdcStream(reinterpret_cast(&hSession->dataPipe[STREAM_MAIN]), hUSB, + if (!thisClass->SendToHdcStream(hSession, reinterpret_cast(&hSession->dataPipe[STREAM_MAIN]), hUSB->bufDevice, transfer->actual_length)) { break; } @@ -505,12 +517,8 @@ HSession HdcHostUSB::ConnectDetectDaemon(const HSession hSession, const HDaemonI while (hSession->childLoop.active_handles == 0) { uv_sleep(1); } - // junk data to pullup acceptchild - uint8_t *byteFlag = new uint8_t[1]; - *byteFlag = SP_START_SESSION; - Base::SendToStreamEx((uv_stream_t *)&hSession->ctrlPipe[STREAM_MAIN], (uint8_t *)byteFlag, 1, nullptr, - (void *)Base::SendCallback, byteFlag); - + auto ctrl = pServer->BuildCtrlString(SP_START_SESSION, 0, nullptr, 0); + Base::SendToStream((uv_stream_t *)&hSession->ctrlPipe[STREAM_MAIN], ctrl.data(), ctrl.size()); return hSession; } } // namespace Hdc diff --git a/src/host/host_usb.h b/src/host/host_usb.h index d7b578bee28479289f9aa0689f07eb3f4e67bef4..feef4a69e40503e9122f4467d1f3b8d92f977a66 100755 --- a/src/host/host_usb.h +++ b/src/host/host_usb.h @@ -49,7 +49,7 @@ private: bool FindDeviceByID(HUSB hUSB, const char *usbMountPoint, libusb_context *ctxUSB); void UpdateUSBDaemonInfo(HUSB hUSB, HSession hSession, uint8_t connStatus); bool DetectMyNeed(libusb_device *device, string &sn); - bool SendUsbReset(HUSB hUSB); + void SendUsbReset(HUSB usb, uint32_t sessionId); void RestoreHdcProtocol(HUSB hUsb, const uint8_t *buf, int bufSize); uv_idle_t usbWork; diff --git a/src/host/server.cpp b/src/host/server.cpp index 51a4fcba876bbbcb63dc3f276d925eafc60298f5..36d04d4afc30d64191e9b998cfe7c0b291611d02 100755 --- a/src/host/server.cpp +++ b/src/host/server.cpp @@ -602,7 +602,7 @@ void HdcServer::RegisterChannel(HSession hSession, const uint32_t channelId) hChannel->targetSession = hSession; } -void HdcServer::AttachChannel(uv_stream_t *uvpipe, HSession hSession, const uint32_t channelId) +void HdcServer::AttachChannel(HSession hSession, const uint32_t channelId) { HdcServerForClient *hSfc = static_cast(clsServerForClient); HChannel hChannel = hSfc->AdminChannel(OP_QUERY, channelId, nullptr); diff --git a/src/host/server.h b/src/host/server.h index ab7eaa8b29c6ccb363f187f40920c5c5a262d4bf..ecfff0a2ace3c08c7285b830285fbae83999fe12 100755 --- a/src/host/server.h +++ b/src/host/server.h @@ -27,7 +27,7 @@ public: string AdminForwardMap(uint8_t opType, const string &taskString, HForwardInfo &hForwardInfoInOut); int CreateConnect(const string &connectKey); bool Initial(const char *listenString); - void AttachChannel(uv_stream_t *uvpipe, HSession hSession, const uint32_t channelId); + void AttachChannel(HSession hSession, const uint32_t channelId); void RegisterChannel(HSession hSession, const uint32_t channelId); void DeatchChannel(const uint32_t channelId); void StopInstance(); diff --git a/src/host/server_for_client.cpp b/src/host/server_for_client.cpp index 2c7d5ada35c322f57ed5004c6b97f54c86334f74..a89f45a4855ff5d2b6af9d49f0cc14cf5c628665 100755 --- a/src/host/server_for_client.cpp +++ b/src/host/server_for_client.cpp @@ -502,11 +502,8 @@ int HdcServerForClient::BindChannelToSession(HChannel hChannel, uint8_t *bufPtr, return -2; } - uint8_t flag[5]; - flag[0] = SP_REGISTER_CHANNEL; - if (memcpy_s(flag + 1, sizeof(flag) - 1, &hChannel->channelId, 4)) { - } - Base::SendToStream((uv_stream_t *)&hSession->ctrlPipe[STREAM_MAIN], flag, 5); + auto ctrl = HdcSessionBase::BuildCtrlString(SP_REGISTER_CHANNEL, hChannel->channelId, nullptr, 0); + Base::SendToStream((uv_stream_t *)&hSession->ctrlPipe[STREAM_MAIN], ctrl.data(), ctrl.size()); while (!hChannel->hChildWorkTCP.loop) { uv_sleep(1); } @@ -517,9 +514,8 @@ int HdcServerForClient::BindChannelToSession(HChannel hChannel, uint8_t *bufPtr, hChannel->fdChildWorkTCP = dup(hChannel->fdChildWorkTCP); #endif uv_read_stop((uv_stream_t *)&hChannel->hWorkTCP); // disable parent - // Send work thread enabled listening - flag[0] = SP_ATTACH_CHANNEL; - Base::SendToStream((uv_stream_t *)&hSession->ctrlPipe[STREAM_MAIN], flag, 5); + auto ctrlAttach = HdcSessionBase::BuildCtrlString(SP_ATTACH_CHANNEL, hChannel->channelId, nullptr, 0); + Base::SendToStream((uv_stream_t *)&hSession->ctrlPipe[STREAM_MAIN], ctrlAttach.data(), ctrlAttach.size()); return 0; }