diff --git a/src/common/define.h b/src/common/define.h index 7b7107bb58dc02b92262d63f871e6a631ecc5246..25d8d65e7c6079ed88882613d9188e780b2e5da9 100644 --- a/src/common/define.h +++ b/src/common/define.h @@ -110,6 +110,8 @@ constexpr uint16_t MAX_DELETED_SESSION_ID_RECORD_COUNT = 10; constexpr uint16_t TCP_CONNECT_MAX_RETRY_COUNT = 6; constexpr uint16_t TCP_CONNECT_RETRY_TIME_MS = 500; +constexpr uint16_t SESSION_HANDSHAKE_REPLY_CHECK_INTERVAL = 1000; // 1 seconds + // the maximum time (in milliseconds) to drop USB channel data for new session constexpr uint16_t NEW_SESSION_DROP_USB_DATA_TIME_MAX_MS = 1000; diff --git a/src/common/define_plus.h b/src/common/define_plus.h index 51f09211bf9478be0871c7be3e3c832be52f2941..960f4bb3f6b4dca125fa720f59b1cd95855c7e7c 100644 --- a/src/common/define_plus.h +++ b/src/common/define_plus.h @@ -261,6 +261,8 @@ struct HdcSession { HdcHeartbeat heartbeat; bool supportEncrypt = false; uv_timer_t heartbeatTimer; + uv_timer_t handshakeReplyCheckTimer; + uint64_t handshakeRetryCount = 0; HdcSessionStat stat; #ifdef HDC_HOST diff --git a/src/common/session.cpp b/src/common/session.cpp old mode 100755 new mode 100644 index e3e7747aaec70bdfab42489fd7da90473158870b..156cfb34e7cdf683dbc09ab3b3a9789f5c40bf63 --- a/src/common/session.cpp +++ b/src/common/session.cpp @@ -502,6 +502,9 @@ HSession HdcSessionBase::MallocSession(bool serverOrDaemon, const ConnType connT #else Base::SetTcpOptions(&hSession->dataPipe[STREAM_MAIN]); #endif + uv_timer_init(&(hSession->childLoop), &(hSession->handshakeReplyCheckTimer)); + hSession->handshakeRetryCount = 0; + hSession->handshakeReplyCheckTimer.data = hSession; ret = MallocSessionByConnectType(hSession); if (ret) { delete hSession; @@ -694,6 +697,10 @@ void HdcSessionBase::FreeSession(const uint32_t sessionId) uint64_t(hSession->stat.dataSendBytes), uint64_t(hSession->stat.dataRecvBytes)); hSession->isDead = true; + if (uv_is_active((uv_handle_t*)&hSession->handshakeReplyCheckTimer)) { + WRITE_LOG(LOG_INFO, "FreeSession stop handshakeReplyCheckTimer, sid:%u", sessionId); + uv_timer_stop(&hSession->handshakeReplyCheckTimer); + } Base::TimerUvTask(&loopMain, hSession, FreeSessionOpeate); NotifyInstanceSessionFree(hSession, false); WRITE_LOG(LOG_INFO, "FreeSession sessionId:%u ref:%u", hSession->sessionId, uint32_t(hSession->ref)); @@ -1203,6 +1210,44 @@ void HdcSessionBase::WorkThreadInitSession(HSession hSession, SessionHandShake & hSessionBase->SetFeature(handshake); } +void HdcSessionBase::HandshakeReplyCheck(uv_timer_t *handle) +{ + HSession hSession = (HSession)handle->data; + WRITE_LOG(LOG_INFO, "HandshakeReplyCheck sid:%u count:%d", hSession->sessionId, + hSession->handshakeRetryCount); + if (Base::IsSessionDeleted(hSession->sessionId)) { + WRITE_LOG(LOG_INFO, "HandshakeReplyCheck session is deleted, sid:%u", hSession->sessionId); + uv_timer_stop(&hSession->handshakeReplyCheckTimer); + return; + } + HdcSessionBase *thisClass = (HdcSessionBase *)hSession->classInstance; + if (hSession->handshakeRetryCount > 5) { + WRITE_LOG(LOG_INFO, "HandshakeReplyCheck over retry count:%d sid:%u, free session", + hSession->handshakeRetryCount, hSession->sessionId); + uv_timer_stop(&hSession->handshakeReplyCheckTimer); + thisClass->FreeSession(hSession->sessionId); + return; + } + + // session handshake step1 + SessionHandShake handshake = {}; + thisClass->WorkThreadInitSession(hSession, handshake); + string hs = SerialStruct::SerializeToString(handshake); + WRITE_LOG(LOG_INFO, "HandshakeReplyCheck sid:%u auth %u retry %d send handshake", + hSession->sessionId, handshake.authType, hSession->handshakeRetryCount); + thisClass->Send(hSession->sessionId, 0, CMD_KERNEL_HANDSHAKE, + reinterpret_cast(const_cast(hs.c_str())), hs.size()); + + hSession->handshakeRetryCount++; +} + +void HdcSessionBase::StartSessionHandshakeReplyCheckTimer(HSession hSession) +{ + WRITE_LOG(LOG_INFO, "start handshakeReplyCheckTimer, sid:%u", hSession->sessionId); + uv_timer_start(&(hSession->handshakeReplyCheckTimer), HandshakeReplyCheck, + SESSION_HANDSHAKE_REPLY_CHECK_INTERVAL, SESSION_HANDSHAKE_REPLY_CHECK_INTERVAL); +} + bool HdcSessionBase::WorkThreadStartSession(HSession hSession) { bool regOK = false; @@ -1249,6 +1294,7 @@ bool HdcSessionBase::WorkThreadStartSession(HSession hSession) #endif Send(hSession->sessionId, 0, CMD_KERNEL_HANDSHAKE, reinterpret_cast(const_cast(hs.c_str())), hs.size()); + StartSessionHandshakeReplyCheckTimer(hSession); } return regOK; } diff --git a/src/common/session.h b/src/common/session.h old mode 100755 new mode 100644 index 9130ab3a77db2877545ef2c4a828f8256d4948a8..6082a6744d408a056491d8f21f57d5baea5f8d39 --- a/src/common/session.h +++ b/src/common/session.h @@ -220,6 +220,7 @@ private: static void AsyncMainLoopTask(uv_idle_t *handle); static void FreeSessionOpeate(uv_timer_t *handle); static void SendHeartbeatMsg(uv_timer_t *handle); + static void HandshakeReplyCheck(uv_timer_t *handle); int MallocSessionByConnectType(HSession hSession); void FreeSessionByConnectType(HSession hSession); bool WorkThreadStartSession(HSession hSession); @@ -230,6 +231,7 @@ private: void StartHeartbeatWork(HSession hSession); void SetFeature(SessionHandShake &handshake); void StopHeartbeatWork(HSession hSession); + void StartSessionHandshakeReplyCheckTimer(HSession hSession); map mapSession; uv_rwlock_t lockMapSession; diff --git a/src/host/server.cpp b/src/host/server.cpp index 8d3e45586842bc4bfeb739042871fb9ecdffccb3..9dac64d82027cf6abf38a90064314ad46c4f7307 100644 --- a/src/host/server.cpp +++ b/src/host/server.cpp @@ -663,6 +663,9 @@ bool HdcServer::ServerSessionSSLInit(HSession hSession, SessionHandShake &handsh bool HdcServer::ServerSessionHandshake(HSession hSession, uint8_t *payload, int payloadSize) { // session handshake step3 + WRITE_LOG(LOG_INFO, "ServerSessionHandshake stop handshakeReplyCheckTimer, sid:%u", + hSession->sessionId); + uv_timer_stop(&hSession->handshakeReplyCheckTimer); string s = string(reinterpret_cast(payload), payloadSize); Hdc::HdcSessionBase::SessionHandShake handshake; SerialStruct::ParseFromString(handshake, s);