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