1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "client_trans_udp_manager.h"
17
18 #include <stdbool.h>
19 #include "client_trans_file.h"
20 #include "client_trans_file_listener.h"
21 #include "client_trans_stream.h"
22 #include "nstackx_dfile.h"
23 #include "securec.h"
24 #include "softbus_adapter_mem.h"
25 #include "softbus_errcode.h"
26 #include "softbus_log.h"
27 #include "softbus_utils.h"
28 #include "trans_server_proxy.h"
29
30 static SoftBusList *g_udpChannelMgr = NULL;
31 static IClientSessionCallBack *g_sessionCb = NULL;
32
ClientTransAddUdpChannel(UdpChannel * channel)33 static int32_t ClientTransAddUdpChannel(UdpChannel *channel)
34 {
35 if (g_udpChannelMgr == NULL) {
36 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "udp channel manager hasn't initialized.");
37 return SOFTBUS_ERR;
38 }
39
40 if (channel == NULL) {
41 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ClientTransAddUdpChannel invalid param.");
42 return SOFTBUS_INVALID_PARAM;
43 }
44 if (SoftBusMutexLock(&(g_udpChannelMgr->lock)) != 0) {
45 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock failed");
46 return SOFTBUS_LOCK_ERR;
47 }
48
49 UdpChannel *channelNode = NULL;
50 LIST_FOR_EACH_ENTRY(channelNode, &(g_udpChannelMgr->list), UdpChannel, node) {
51 if (channelNode->channelId == channel->channelId) {
52 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "udp channel has exited.channelId = %d.",
53 channel->channelId);
54 (void)SoftBusMutexUnlock(&(g_udpChannelMgr->lock));
55 return SOFTBUS_ERR;
56 }
57 }
58 ListInit(&(channel->node));
59 ListAdd(&(g_udpChannelMgr->list), &(channel->node));
60 g_udpChannelMgr->cnt++;
61
62 (void)SoftBusMutexUnlock(&(g_udpChannelMgr->lock));
63 return SOFTBUS_OK;
64 }
65
TransDeleteUdpChannel(int32_t channelId)66 int32_t TransDeleteUdpChannel(int32_t channelId)
67 {
68 if (g_udpChannelMgr == NULL) {
69 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "udp channel manager hasn't initialized.");
70 return SOFTBUS_ERR;
71 }
72 if (SoftBusMutexLock(&(g_udpChannelMgr->lock)) != 0) {
73 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock failed");
74 return SOFTBUS_LOCK_ERR;
75 }
76
77 UdpChannel *channelNode = NULL;
78 LIST_FOR_EACH_ENTRY(channelNode, &(g_udpChannelMgr->list), UdpChannel, node) {
79 if (channelNode->channelId == channelId) {
80 ListDelete(&(channelNode->node));
81 SoftBusFree(channelNode);
82 g_udpChannelMgr->cnt--;
83 (void)SoftBusMutexUnlock(&(g_udpChannelMgr->lock));
84 return SOFTBUS_OK;
85 }
86 }
87 (void)SoftBusMutexUnlock(&(g_udpChannelMgr->lock));
88 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "udp channel not found, channelId = %d.", channelId);
89 return SOFTBUS_ERR;
90 }
91
TransGetUdpChannel(int32_t channelId,UdpChannel * channel)92 int32_t TransGetUdpChannel(int32_t channelId, UdpChannel *channel)
93 {
94 if (g_udpChannelMgr == NULL) {
95 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "udp channel manager hasn't initialized.");
96 return SOFTBUS_ERR;
97 }
98
99 if (SoftBusMutexLock(&(g_udpChannelMgr->lock)) != 0) {
100 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock failed");
101 return SOFTBUS_LOCK_ERR;
102 }
103
104 UdpChannel *channelNode = NULL;
105 LIST_FOR_EACH_ENTRY(channelNode, &(g_udpChannelMgr->list), UdpChannel, node) {
106 if (channelNode->channelId == channelId) {
107 if (memcpy_s(channel, sizeof(UdpChannel), channelNode, sizeof(UdpChannel)) != EOK) {
108 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get udp channel memcpy_s failed.");
109 (void)SoftBusMutexUnlock(&(g_udpChannelMgr->lock));
110 return SOFTBUS_MEM_ERR;
111 }
112 (void)SoftBusMutexUnlock(&(g_udpChannelMgr->lock));
113 return SOFTBUS_OK;
114 }
115 }
116 (void)SoftBusMutexUnlock(&(g_udpChannelMgr->lock));
117 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "udp channel not found, channelId = %d.", channelId);
118 return SOFTBUS_ERR;
119 }
120
TransSetUdpChannelEnable(int32_t channelId,bool isEnable)121 static int32_t TransSetUdpChannelEnable(int32_t channelId, bool isEnable)
122 {
123 if (g_udpChannelMgr == NULL) {
124 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "udp channel manager hasn't initialized.");
125 return SOFTBUS_ERR;
126 }
127
128 if (SoftBusMutexLock(&(g_udpChannelMgr->lock)) != 0) {
129 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock failed");
130 return SOFTBUS_LOCK_ERR;
131 }
132
133 UdpChannel *channelNode = NULL;
134 LIST_FOR_EACH_ENTRY(channelNode, &(g_udpChannelMgr->list), UdpChannel, node) {
135 if (channelNode->channelId == channelId) {
136 channelNode->isEnable = isEnable;
137 (void)SoftBusMutexUnlock(&(g_udpChannelMgr->lock));
138 return SOFTBUS_OK;
139 }
140 }
141 (void)SoftBusMutexUnlock(&(g_udpChannelMgr->lock));
142 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "udp channel not found, channelId = %d.", channelId);
143 return SOFTBUS_ERR;
144 }
145
OnUdpChannelOpened(int32_t channelId)146 static void OnUdpChannelOpened(int32_t channelId)
147 {
148 UdpChannel channel;
149 if (memset_s(&channel, sizeof(UdpChannel), 0, sizeof(UdpChannel)) != EOK) {
150 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "on udp channel opened memset failed.");
151 return;
152 }
153 if (TransGetUdpChannel(channelId, &channel) != SOFTBUS_OK) {
154 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get udp channel[%d] failed.", channelId);
155 return;
156 }
157 if (TransSetUdpChannelEnable(channelId, true) != SOFTBUS_OK) {
158 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "set udp channel[%d] enable failed.", channelId);
159 return;
160 }
161 SessionType type = TYPE_BUTT;
162 switch (channel.businessType) {
163 case BUSINESS_TYPE_STREAM:
164 type = TYPE_STREAM;
165 break;
166 case BUSINESS_TYPE_FILE:
167 type = TYPE_FILE;
168 break;
169 default:
170 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "unsupport business type=%d.", channel.businessType);
171 return;
172 }
173 ChannelInfo info = {0};
174 info.channelId = channel.channelId;
175 info.channelType = CHANNEL_TYPE_UDP;
176 info.isServer = channel.info.isServer;
177 info.peerPid = channel.info.peerPid;
178 info.peerUid = channel.info.peerUid;
179 info.groupId = channel.info.groupId;
180 info.peerDeviceId = channel.info.peerDeviceId;
181 info.peerSessionName = channel.info.peerSessionName;
182 info.routeType = channel.routeType;
183 info.businessType = channel.businessType;
184 if ((g_sessionCb != NULL) && (g_sessionCb->OnSessionOpened != NULL)) {
185 g_sessionCb->OnSessionOpened(channel.info.mySessionName, &info, type);
186 }
187 }
188
ConvertChannelInfoToUdpChannel(const char * sessionName,const ChannelInfo * channel)189 static UdpChannel *ConvertChannelInfoToUdpChannel(const char *sessionName, const ChannelInfo *channel)
190 {
191 UdpChannel *newChannel = (UdpChannel *)SoftBusCalloc(sizeof(UdpChannel));
192 if (newChannel == NULL) {
193 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "new udp channel failed.");
194 return NULL;
195 }
196 newChannel->businessType = channel->businessType;
197 newChannel->channelId = channel->channelId;
198 newChannel->dfileId = -1;
199 newChannel->isEnable = false;
200 newChannel->info.isServer = channel->isServer;
201 newChannel->info.peerPid = channel->peerPid;
202 newChannel->info.peerUid = channel->peerUid;
203 newChannel->routeType = channel->routeType;
204 if (strcpy_s(newChannel->info.peerSessionName, SESSION_NAME_SIZE_MAX, channel->peerSessionName) != EOK ||
205 strcpy_s(newChannel->info.mySessionName, SESSION_NAME_SIZE_MAX, sessionName) != EOK ||
206 strcpy_s(newChannel->info.peerDeviceId, DEVICE_ID_SIZE_MAX, channel->peerDeviceId) != EOK ||
207 strcpy_s(newChannel->info.groupId, GROUP_ID_SIZE_MAX, channel->groupId) != EOK) {
208 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR,
209 "udp channel add peer session name, device id, group id failed");
210 SoftBusFree(newChannel);
211 return NULL;
212 }
213
214 return newChannel;
215 }
216
TransOnUdpChannelOpened(const char * sessionName,const ChannelInfo * channel,int32_t * udpPort)217 int32_t TransOnUdpChannelOpened(const char *sessionName, const ChannelInfo *channel, int32_t *udpPort)
218 {
219 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "TransOnUdpChannelOpened enter");
220 if (channel == NULL || udpPort == NULL || sessionName == NULL) {
221 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "%s:invalid param.", __func__);
222 return SOFTBUS_INVALID_PARAM;
223 }
224 UdpChannel *newChannel = ConvertChannelInfoToUdpChannel(sessionName, channel);
225 if (newChannel == NULL) {
226 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "convert channel info to udp channel failed.");
227 return SOFTBUS_MEM_ERR;
228 }
229 if (ClientTransAddUdpChannel(newChannel) != SOFTBUS_OK) {
230 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "add udp channel failed.");
231 SoftBusFree(newChannel);
232 return SOFTBUS_TRANS_UDP_CLIENT_ADD_CHANNEL_FAILED;
233 }
234 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "add new udp channel success, channelId[%d], business type[%d]",
235 newChannel->channelId, newChannel->businessType);
236
237 int32_t ret = SOFTBUS_ERR;
238 switch (channel->businessType) {
239 case BUSINESS_TYPE_STREAM:
240 ret = TransOnstreamChannelOpened(channel, udpPort);
241 if (ret != SOFTBUS_OK) {
242 (void)TransDeleteUdpChannel(newChannel->channelId);
243 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "on stream channel opened failed.");
244 }
245 break;
246 case BUSINESS_TYPE_FILE:
247 ret = TransOnFileChannelOpened(sessionName, channel, udpPort);
248 if (ret < SOFTBUS_OK) {
249 (void)TransDeleteUdpChannel(newChannel->channelId);
250 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "on file channel open failed.");
251 return SOFTBUS_ERR;
252 }
253 newChannel->dfileId = ret;
254 ret = SOFTBUS_OK;
255 break;
256 default:
257 (void)TransDeleteUdpChannel(newChannel->channelId);
258 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "unsupport businessType=%d.", channel->businessType);
259 break;
260 }
261 return ret;
262 }
263
TransDeleteBusinnessChannel(UdpChannel * channel)264 static int32_t TransDeleteBusinnessChannel(UdpChannel *channel)
265 {
266 switch (channel->businessType) {
267 case BUSINESS_TYPE_STREAM:
268 if (TransCloseStreamChannel(channel->channelId) != SOFTBUS_OK) {
269 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "trans close udp channel failed.");
270 return SOFTBUS_ERR;
271 }
272 break;
273 case BUSINESS_TYPE_FILE:
274 TransCloseFileChannel(channel->dfileId);
275 break;
276 default:
277 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "unsupport business type=%d.", channel->businessType);
278 return SOFTBUS_ERR;
279 }
280 return SOFTBUS_OK;
281 }
282
TransOnUdpChannelOpenFailed(int32_t channelId,int32_t errCode)283 int32_t TransOnUdpChannelOpenFailed(int32_t channelId, int32_t errCode)
284 {
285 UdpChannel channel;
286 bool isFind = true;
287 if (TransGetUdpChannel(channelId, &channel) != SOFTBUS_OK) {
288 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_WARN, "[%s] get channel[%d] failed.", __func__, channelId);
289 isFind = false;
290 }
291 if (TransDeleteUdpChannel(channelId) != SOFTBUS_OK) {
292 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_WARN, "[%s] del channel[%d] failed.", __func__, channelId);
293 }
294 if ((isFind) && (channel.isEnable)) {
295 if (TransDeleteBusinnessChannel(&channel) != SOFTBUS_OK) {
296 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR,
297 "TransOnUdpChannelOpenFailed del business channel[%d] failed.", channelId);
298 return SOFTBUS_ERR;
299 }
300 }
301 if ((g_sessionCb == NULL) || (g_sessionCb->OnSessionOpenFailed == NULL)) {
302 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "client trans udp manager seesion callback is null");
303 return SOFTBUS_ERR;
304 }
305
306 return g_sessionCb->OnSessionOpenFailed(channelId, CHANNEL_TYPE_UDP, errCode);
307 }
308
ClosePeerUdpChannel(int32_t channelId)309 static int32_t ClosePeerUdpChannel(int32_t channelId)
310 {
311 return ServerIpcCloseChannel(channelId, CHANNEL_TYPE_UDP);
312 }
313
CloseUdpChannel(int32_t channelId,bool isActive)314 static int32_t CloseUdpChannel(int32_t channelId, bool isActive)
315 {
316 UdpChannel channel;
317 (void)memset_s(&channel, sizeof(UdpChannel), 0, sizeof(UdpChannel));
318 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "close udp channel=%d.", channelId);
319 if (TransGetUdpChannel(channelId, &channel) != SOFTBUS_OK) {
320 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "CloseUdpChannel get channel=%d failed.", channelId);
321 return SOFTBUS_ERR;
322 }
323
324 if (TransDeleteUdpChannel(channelId) != SOFTBUS_OK) {
325 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "trans del udp channel=%d failed.", channelId);
326 }
327
328 if (isActive && (ClosePeerUdpChannel(channelId) != SOFTBUS_OK)) {
329 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "trans close peer udp channel=%d failed.", channelId);
330 }
331
332 if (TransDeleteBusinnessChannel(&channel) != SOFTBUS_OK) {
333 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "CloseUdpChannel del business channel=%d failed.", channelId);
334 return SOFTBUS_ERR;
335 }
336
337 if (!isActive && (g_sessionCb != NULL) && (g_sessionCb->OnSessionClosed != NULL)) {
338 g_sessionCb->OnSessionClosed(channelId, CHANNEL_TYPE_UDP);
339 }
340 return SOFTBUS_OK;
341 }
342
TransOnUdpChannelClosed(int32_t channelId)343 int32_t TransOnUdpChannelClosed(int32_t channelId)
344 {
345 return CloseUdpChannel(channelId, false);
346 }
347
TransOnUdpChannelQosEvent(int32_t channelId,int32_t eventId,int32_t tvCount,const QosTv * tvList)348 int32_t TransOnUdpChannelQosEvent(int32_t channelId, int32_t eventId, int32_t tvCount, const QosTv *tvList)
349 {
350 UdpChannel channel;
351 (void)memset_s(&channel, sizeof(UdpChannel), 0, sizeof(UdpChannel));
352 if (TransGetUdpChannel(channelId, &channel) != SOFTBUS_OK) {
353 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransOnUdpChannelQosEvent get channel=%d failed.", channelId);
354 return SOFTBUS_ERR;
355 }
356 if (g_sessionCb->OnQosEvent != NULL) {
357 g_sessionCb->OnQosEvent(channelId, CHANNEL_TYPE_UDP, eventId, tvCount, tvList);
358 }
359 return SOFTBUS_OK;
360 }
361
ClientTransCloseUdpChannel(int32_t channelId)362 int32_t ClientTransCloseUdpChannel(int32_t channelId)
363 {
364 return CloseUdpChannel(channelId, true);
365 }
366
TransUdpChannelSendStream(int32_t channelId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)367 int32_t TransUdpChannelSendStream(int32_t channelId, const StreamData *data, const StreamData *ext,
368 const StreamFrameInfo *param)
369 {
370 UdpChannel channel;
371 (void)memset_s(&channel, sizeof(UdpChannel), 0, sizeof(UdpChannel));
372 if (TransGetUdpChannel(channelId, &channel) != SOFTBUS_OK) {
373 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransUdpChannelSendStream get channel=%d failed.", channelId);
374 return SOFTBUS_TRANS_UDP_GET_CHANNEL_FAILED;
375 }
376 if (!channel.isEnable) {
377 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "udp channel is not enable.");
378 return SOFTBUS_TRANS_UDP_CHANNEL_DISABLE;
379 }
380 return TransSendStream(channelId, data, ext, param);
381 }
382
OnUdpChannelClosed(int32_t channelId)383 static void OnUdpChannelClosed(int32_t channelId)
384 {
385 if ((g_sessionCb == NULL) || (g_sessionCb->OnSessionClosed == NULL)) {
386 return;
387 }
388 g_sessionCb->OnSessionClosed(channelId, CHANNEL_TYPE_UDP);
389 if (TransDeleteUdpChannel(channelId) != SOFTBUS_OK) {
390 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "trans delete udp channel=%d failed.", channelId);
391 }
392 }
393
OnStreamReceived(int32_t channelId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)394 static void OnStreamReceived(int32_t channelId, const StreamData *data, const StreamData *ext,
395 const StreamFrameInfo *param)
396 {
397 if ((g_sessionCb == NULL) || (g_sessionCb->OnStreamReceived == NULL)) {
398 return;
399 }
400 g_sessionCb->OnStreamReceived(channelId, CHANNEL_TYPE_UDP, data, ext, param);
401 }
402
OnFileGetSessionId(int32_t channelId,int32_t * sessionId)403 static int32_t OnFileGetSessionId(int32_t channelId, int32_t *sessionId)
404 {
405 if ((g_sessionCb == NULL) || (g_sessionCb->OnGetSessionId == NULL)) {
406 return SOFTBUS_ERR;
407 }
408 return g_sessionCb->OnGetSessionId(channelId, CHANNEL_TYPE_UDP, sessionId);
409 }
410
OnQosEvent(int channelId,int eventId,int tvCount,const QosTv * tvList)411 static void OnQosEvent(int channelId, int eventId, int tvCount, const QosTv *tvList)
412 {
413 if ((g_sessionCb == NULL) || (g_sessionCb->OnQosEvent == NULL)) {
414 return;
415 }
416 g_sessionCb->OnQosEvent(channelId, CHANNEL_TYPE_UDP, eventId, tvCount, tvList);
417 }
418
419 static UdpChannelMgrCb g_udpChannelCb = {
420 .OnStreamReceived = OnStreamReceived,
421 .OnFileGetSessionId = OnFileGetSessionId,
422 .OnMessageReceived = NULL,
423 .OnUdpChannelOpened = OnUdpChannelOpened,
424 .OnUdpChannelClosed = OnUdpChannelClosed,
425 .OnQosEvent = OnQosEvent,
426 };
427
ClientTransUdpMgrInit(IClientSessionCallBack * callback)428 int32_t ClientTransUdpMgrInit(IClientSessionCallBack *callback)
429 {
430 if (g_udpChannelMgr != NULL) {
431 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "udp channel info manager has initialized.");
432 return SOFTBUS_OK;
433 }
434 if (callback == NULL) {
435 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "udp channel info manager init failed, calback is null.");
436 return SOFTBUS_ERR;
437 }
438 g_sessionCb = callback;
439 RegisterStreamCb(&g_udpChannelCb);
440 TransFileInit();
441 TransFileSchemaInit();
442 NSTACKX_DFileRegisterLogCallback(NstackxLog);
443 RegisterFileCb(&g_udpChannelCb);
444 g_udpChannelMgr = CreateSoftBusList();
445 if (g_udpChannelMgr == NULL) {
446 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "create udp channel manager list failed.");
447 return SOFTBUS_MALLOC_ERR;
448 }
449 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "trans udp channel manager init success.");
450 return SOFTBUS_OK;
451 }
452
ClientTransUdpMgrDeinit(void)453 void ClientTransUdpMgrDeinit(void)
454 {
455 if (g_udpChannelMgr == NULL) {
456 return;
457 }
458 UnregisterStreamCb();
459 RegisterFileCb(NULL);
460 if (SoftBusMutexLock(&g_udpChannelMgr->lock) != 0) {
461 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock failed");
462 return;
463 }
464 UdpChannel *channel = NULL;
465 UdpChannel *nextChannel = NULL;
466 LIST_FOR_EACH_ENTRY_SAFE(channel, nextChannel, &g_udpChannelMgr->list, UdpChannel, node) {
467 ListDelete(&(channel->node));
468 SoftBusFree(channel);
469 }
470 (void)SoftBusMutexUnlock(&g_udpChannelMgr->lock);
471 DestroySoftBusList(g_udpChannelMgr);
472 g_udpChannelMgr = NULL;
473 TransFileDeinit();
474 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "trans udp channel manager deinit success.");
475 }
476
TransUdpChannelSendFile(int32_t channelId,const char * sFileList[],const char * dFileList[],uint32_t fileCnt)477 int32_t TransUdpChannelSendFile(int32_t channelId, const char *sFileList[], const char *dFileList[], uint32_t fileCnt)
478 {
479 UdpChannel channel;
480 if (memset_s(&channel, sizeof(UdpChannel), 0, sizeof(UdpChannel)) != EOK) {
481 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "memset failed.");
482 return SOFTBUS_ERR;
483 }
484 if (TransGetUdpChannel(channelId, &channel) != SOFTBUS_OK) {
485 return SOFTBUS_TRANS_UDP_GET_CHANNEL_FAILED;
486 }
487 if (!channel.isEnable || channel.dfileId < 0) {
488 LOG_ERR("udp channel is not enable.");
489 return SOFTBUS_TRANS_UDP_CHANNEL_DISABLE;
490 }
491 return TransSendFile(channel.dfileId, sFileList, dFileList, fileCnt);
492 }
493
TransGetUdpChannelByFileId(int32_t dfileId,UdpChannel * udpChannel)494 int32_t TransGetUdpChannelByFileId(int32_t dfileId, UdpChannel *udpChannel)
495 {
496 if (g_udpChannelMgr == NULL) {
497 LOG_ERR("udp channel manager hasn't initialized.");
498 return SOFTBUS_ERR;
499 }
500
501 if (SoftBusMutexLock(&(g_udpChannelMgr->lock)) != 0) {
502 LOG_ERR("TransGetUdpChannelByFileId lock failed");
503 return SOFTBUS_LOCK_ERR;
504 }
505
506 UdpChannel *channelNode = NULL;
507 LIST_FOR_EACH_ENTRY(channelNode, &(g_udpChannelMgr->list), UdpChannel, node) {
508 if (channelNode->dfileId == dfileId) {
509 if (memcpy_s(udpChannel, sizeof(UdpChannel), channelNode, sizeof(UdpChannel)) != EOK) {
510 LOG_ERR("memcpy_s failed.");
511 (void)SoftBusMutexUnlock(&(g_udpChannelMgr->lock));
512 return SOFTBUS_MEM_ERR;
513 }
514 (void)SoftBusMutexUnlock(&(g_udpChannelMgr->lock));
515 return SOFTBUS_OK;
516 }
517 }
518 (void)SoftBusMutexUnlock(&(g_udpChannelMgr->lock));
519 return SOFTBUS_ERR;
520 }
521
TransUdpDeleteFileListener(const char * sessionName)522 void TransUdpDeleteFileListener(const char *sessionName)
523 {
524 return TransDeleteFileListener(sessionName);
525 }