From fbf8315d734b289cd124d118f4ac1ad449679cd1 Mon Sep 17 00:00:00 2001 From: breakfei1 Date: Sun, 25 Sep 2022 00:29:52 +0800 Subject: [PATCH] Add udp channel process in lnn node off line Signed-off-by: breakfei1 --- core/frame/common/src/softbus_server_frame.c | 10 ++-- .../common/include/softbus_app_info.h | 1 + .../manager/src/trans_channel_manager.c | 48 ++++++++++++------- .../include/trans_udp_channel_manager.h | 1 + .../src/trans_udp_channel_manager.c | 41 ++++++++++++++++ .../src/trans_udp_negotiation.c | 23 +++++++++ .../udp_negotiation/udp_negotiation.gni | 5 +- 7 files changed, 105 insertions(+), 24 deletions(-) diff --git a/core/frame/common/src/softbus_server_frame.c b/core/frame/common/src/softbus_server_frame.c index a389a3e8fe..b72ccfa62d 100644 --- a/core/frame/common/src/softbus_server_frame.c +++ b/core/frame/common/src/softbus_server_frame.c @@ -90,11 +90,6 @@ void InitSoftBusServer(void) goto ERR_EXIT; } - if (TransServerInit() == SOFTBUS_ERR) { - SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "softbus trans server init failed."); - goto ERR_EXIT; - } - if (AuthInit() == SOFTBUS_ERR) { SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "softbus auth init failed."); goto ERR_EXIT; @@ -110,6 +105,11 @@ void InitSoftBusServer(void) goto ERR_EXIT; } + if (TransServerInit() == SOFTBUS_ERR) { + SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "softbus trans server init failed."); + goto ERR_EXIT; + } + if (InitP2pLink() != SOFTBUS_OK) { goto ERR_EXIT; } diff --git a/core/transmission/common/include/softbus_app_info.h b/core/transmission/common/include/softbus_app_info.h index 97bb4fe104..30a49cd110 100644 --- a/core/transmission/common/include/softbus_app_info.h +++ b/core/transmission/common/include/softbus_app_info.h @@ -78,6 +78,7 @@ typedef struct { char groupId[GROUP_ID_SIZE_MAX]; char sessionKey[SESSION_KEY_LENGTH]; char reqId[REQ_ID_SIZE_MAX]; + char peerNetWorkId[DEVICE_ID_SIZE_MAX]; RouteType routeType; BusinessType businessType; StreamType streamType; diff --git a/core/transmission/trans_channel/manager/src/trans_channel_manager.c b/core/transmission/trans_channel/manager/src/trans_channel_manager.c index 07cbe0615d..0a243e0e4f 100644 --- a/core/transmission/trans_channel/manager/src/trans_channel_manager.c +++ b/core/transmission/trans_channel/manager/src/trans_channel_manager.c @@ -88,6 +88,34 @@ void TransChannelDeinit(void) TransReqLanePendingDeinit(); } +static int32_t CopyAppInfoFromSessionParam(AppInfo* appInfo, const SessionParam* param) +{ + if (TransGetUidAndPid(param->sessionName, &appInfo->myData.uid, &appInfo->myData.pid) != SOFTBUS_OK) { + return SOFTBUS_ERR; + } + if (strcpy_s(appInfo->groupId, sizeof(appInfo->groupId), param->groupId) != EOK) { + return SOFTBUS_ERR; + } + if (strcpy_s(appInfo->myData.sessionName, sizeof(appInfo->myData.sessionName), param->sessionName) != EOK) { + return SOFTBUS_ERR; + } + if (strcpy_s(appInfo->peerNetWorkId, sizeof(appInfo->peerNetWorkId), param->peerDeviceId) != EOK) { + return SOFTBUS_ERR; + } + if (TransGetPkgNameBySessionName(param->sessionName, appInfo->myData.pkgName, PKG_NAME_SIZE_MAX) != SOFTBUS_OK) { + return SOFTBUS_ERR; + } + if (strcpy_s(appInfo->peerData.sessionName, sizeof(appInfo->peerData.sessionName), param->peerSessionName) != 0) { + return SOFTBUS_ERR; + } + if (LnnGetRemoteStrInfo(param->peerDeviceId, STRING_KEY_UUID, + appInfo->peerData.deviceId, sizeof(appInfo->peerData.deviceId)) != SOFTBUS_OK) { + SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get remote node uuid err"); + return SOFTBUS_ERR; + } + return SOFTBUS_OK; +} + static AppInfo *GetAppInfo(const SessionParam *param) { SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "GetAppInfo"); @@ -103,20 +131,11 @@ static AppInfo *GetAppInfo(const SessionParam *param) } else if (param->attr->dataType == TYPE_FILE) { appInfo->businessType = BUSINESS_TYPE_FILE; } - if (TransGetUidAndPid(param->sessionName, &appInfo->myData.uid, &appInfo->myData.pid) != SOFTBUS_OK) { - goto EXIT_ERR; - } if (LnnGetLocalStrInfo(STRING_KEY_UUID, appInfo->myData.deviceId, sizeof(appInfo->myData.deviceId)) != SOFTBUS_OK) { goto EXIT_ERR; } - if (strcpy_s(appInfo->groupId, sizeof(appInfo->groupId), param->groupId) != EOK) { - goto EXIT_ERR; - } - if (strcpy_s(appInfo->myData.sessionName, sizeof(appInfo->myData.sessionName), param->sessionName) != EOK) { - goto EXIT_ERR; - } - if (TransGetPkgNameBySessionName(param->sessionName, appInfo->myData.pkgName, PKG_NAME_SIZE_MAX) != SOFTBUS_OK) { + if (CopyAppInfoFromSessionParam(appInfo, param) != SOFTBUS_OK) { goto EXIT_ERR; } @@ -124,14 +143,7 @@ static AppInfo *GetAppInfo(const SessionParam *param) appInfo->encrypt = APP_INFO_FILE_FEATURES_SUPPORT; appInfo->algorithm = APP_INFO_ALGORITHM_AES_GCM_256; appInfo->crc = APP_INFO_FILE_FEATURES_SUPPORT; - if (strcpy_s(appInfo->peerData.sessionName, sizeof(appInfo->peerData.sessionName), param->peerSessionName) != 0) { - goto EXIT_ERR; - } - if (LnnGetRemoteStrInfo(param->peerDeviceId, STRING_KEY_UUID, - appInfo->peerData.deviceId, sizeof(appInfo->peerData.deviceId)) != SOFTBUS_OK) { - SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get remote node uuid err"); - goto EXIT_ERR; - } + SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "GetAppInfo ok"); return appInfo; EXIT_ERR: diff --git a/core/transmission/trans_channel/udp_negotiation/include/trans_udp_channel_manager.h b/core/transmission/trans_channel/udp_negotiation/include/trans_udp_channel_manager.h index 065d7d4540..45d1a8b79f 100644 --- a/core/transmission/trans_channel/udp_negotiation/include/trans_udp_channel_manager.h +++ b/core/transmission/trans_channel/udp_negotiation/include/trans_udp_channel_manager.h @@ -45,6 +45,7 @@ void ReleaseUdpChannelLock(void); int32_t TransAddUdpChannel(UdpChannelInfo *channel); int32_t TransDelUdpChannel(int32_t channelId); +void TransCloseUdpChannelByNetWorkId(const char* netWorkId); int32_t TransGetUdpChannelBySeq(int64_t seq, UdpChannelInfo *channel); int32_t TransGetUdpChannelById(int32_t channelId, UdpChannelInfo *channel); diff --git a/core/transmission/trans_channel/udp_negotiation/src/trans_udp_channel_manager.c b/core/transmission/trans_channel/udp_negotiation/src/trans_udp_channel_manager.c index 9429674035..7d01c5f32f 100644 --- a/core/transmission/trans_channel/udp_negotiation/src/trans_udp_channel_manager.c +++ b/core/transmission/trans_channel/udp_negotiation/src/trans_udp_channel_manager.c @@ -205,6 +205,47 @@ int32_t TransDelUdpChannel(int32_t channelId) return SOFTBUS_ERR; } +static void NotifyUdpChannelCloseInList(ListNode *udpChannelList) +{ + UdpChannelInfo *udpChannel = NULL; + UdpChannelInfo *udpChannelNext = NULL; + LIST_FOR_EACH_ENTRY_SAFE(udpChannel, udpChannelNext, udpChannelList, UdpChannelInfo, node) { + (void)NotifyUdpChannelClosed(&udpChannel->info); + + ListDelete(&(udpChannel->node)); + SoftBusFree(udpChannel); + } +} + +void TransCloseUdpChannelByNetWorkId(const char* netWorkId) +{ + if ((g_udpChannelMgr == NULL) || (netWorkId == NULL)) { + return; + } + if (SoftBusMutexLock(&g_udpChannelMgr->lock) != 0) { + SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransCloseUdpChannelByAuId lock failed"); + return; + } + + ListNode udpDeleteChannelList; + ListInit(&udpDeleteChannelList); + + UdpChannelInfo *udpChannel = NULL; + UdpChannelInfo *udpChannelNext = NULL; + LIST_FOR_EACH_ENTRY_SAFE(udpChannel, udpChannelNext, &g_udpChannelMgr->list, UdpChannelInfo, node) { + if (strcmp(udpChannel->info.peerNetWorkId, netWorkId) == 0) { + ReleaseUdpChannelId((int32_t)(udpChannel->info.myData.channelId)); + ListDelete(&(udpChannel->node)); + g_udpChannelMgr->cnt--; + + ListAdd(&udpDeleteChannelList, &(udpChannel->node)); + } + } + (void)SoftBusMutexUnlock(&g_udpChannelMgr->lock); + + NotifyUdpChannelCloseInList(&udpDeleteChannelList); +} + int32_t TransGetUdpChannelBySeq(int64_t seq, UdpChannelInfo *channel) { if (g_udpChannelMgr == NULL) { diff --git a/core/transmission/trans_channel/udp_negotiation/src/trans_udp_negotiation.c b/core/transmission/trans_channel/udp_negotiation/src/trans_udp_negotiation.c index bbdbe897f9..43137fca99 100644 --- a/core/transmission/trans_channel/udp_negotiation/src/trans_udp_negotiation.c +++ b/core/transmission/trans_channel/udp_negotiation/src/trans_udp_negotiation.c @@ -16,6 +16,7 @@ #include "trans_udp_negotiation.h" #include "auth_interface.h" +#include "bus_center_event.h" #include "bus_center_info_key.h" #include "bus_center_manager.h" #include "p2plink_interface.h" @@ -750,6 +751,20 @@ static void UdpModuleCb(int64_t authId, const AuthTransData *data) } } +void TransUdpNodeOffLineProc(const LnnEventBasicInfo *info) +{ + if ((info == NULL) || (info->event != LNN_EVENT_NODE_ONLINE_STATE_CHANGED)) { + return; + } + + LnnOnlineStateEventInfo *onlineStateInfo = (LnnOnlineStateEventInfo*)info; + if (onlineStateInfo->isOnline == true) { + return; + } + + TransCloseUdpChannelByNetWorkId(onlineStateInfo->networkId); +} + int32_t TransUdpChannelInit(IServerChannelCallBack *callback) { g_channelCb = callback; @@ -770,6 +785,12 @@ int32_t TransUdpChannelInit(IServerChannelCallBack *callback) SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "register udp callback to auth failed."); return SOFTBUS_ERR; } + + if (LnnRegisterEventHandler(LNN_EVENT_NODE_ONLINE_STATE_CHANGED, TransUdpNodeOffLineProc) != SOFTBUS_OK) { + SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "register udp Node offLine Proc failed."); + return SOFTBUS_ERR; + } + SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "server trans udp channel init success."); return SOFTBUS_OK; } @@ -778,6 +799,8 @@ void TransUdpChannelDeinit(void) { TransUdpChannelMgrDeinit(); UnregAuthTransListener(MODULE_UDP_INFO); + LnnUnregisterEventHandler(LNN_EVENT_NODE_ONLINE_STATE_CHANGED, TransUdpNodeOffLineProc); + g_channelCb = NULL; SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "server trans udp channel deinit success."); } diff --git a/core/transmission/trans_channel/udp_negotiation/udp_negotiation.gni b/core/transmission/trans_channel/udp_negotiation/udp_negotiation.gni index e05cf05fa4..afabb683dd 100644 --- a/core/transmission/trans_channel/udp_negotiation/udp_negotiation.gni +++ b/core/transmission/trans_channel/udp_negotiation/udp_negotiation.gni @@ -27,4 +27,7 @@ if (dsoftbus_feature_trans_udp == true) { "$udp_channel_path/src/trans_udp_channel_manager_virtual.c", ] } -trans_udp_channel_inc = [ "$udp_channel_path/include" ] +trans_udp_channel_inc = [ + "$udp_channel_path/include", + "$dsoftbus_root_path/core/bus_center/service/include", +]