1 /*
2 * Copyright (c) 2021-2024 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_tcp_direct_manager.h"
17
18 #include <securec.h>
19
20 #include "client_trans_tcp_direct_callback.h"
21 #include "client_trans_tcp_direct_listener.h"
22 #include "softbus_adapter_mem.h"
23 #include "softbus_base_listener.h"
24 #include "softbus_def.h"
25 #include "softbus_error_code.h"
26 #include "softbus_socket.h"
27 #include "softbus_utils.h"
28 #include "trans_log.h"
29 #include "trans_pending_pkt.h"
30 #include "trans_server_proxy.h"
31
32 #define HEART_TIME 300
33 #define TCP_KEEPALIVE_INTERVAL 4
34 #define TCP_KEEPALIVE_COUNT 5
35 #define USER_TIME_OUT (320 * 1000)
36
37 static SoftBusList *g_tcpDirectChannelInfoList = NULL;
38
CheckInfoAndMutexLock(TcpDirectChannelInfo * info)39 static bool CheckInfoAndMutexLock(TcpDirectChannelInfo *info)
40 {
41 if (info == NULL) {
42 TRANS_LOGE(TRANS_SDK, "param invalid.");
43 return false;
44 }
45 if (SoftBusMutexLock(&g_tcpDirectChannelInfoList->lock) != SOFTBUS_OK) {
46 TRANS_LOGE(TRANS_SDK, "lock failed");
47 return false;
48 }
49 return true;
50 }
51
TransTdcGetInfoById(int32_t channelId,TcpDirectChannelInfo * info)52 int32_t TransTdcGetInfoById(int32_t channelId, TcpDirectChannelInfo *info)
53 {
54 if (!CheckInfoAndMutexLock(info)) {
55 return SOFTBUS_LOCK_ERR;
56 }
57
58 TcpDirectChannelInfo *item = NULL;
59 LIST_FOR_EACH_ENTRY(item, &(g_tcpDirectChannelInfoList->list), TcpDirectChannelInfo, node) {
60 if (item->channelId == channelId) {
61 (void)memcpy_s(info, sizeof(TcpDirectChannelInfo), item, sizeof(TcpDirectChannelInfo));
62 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
63 return SOFTBUS_OK;
64 }
65 }
66
67 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
68 return SOFTBUS_NOT_FIND;
69 }
70
TransTdcSetListenerStateById(int32_t channelId,bool needStopListener)71 int32_t TransTdcSetListenerStateById(int32_t channelId, bool needStopListener)
72 {
73 if (g_tcpDirectChannelInfoList == NULL) {
74 TRANS_LOGE(TRANS_SDK, "g_tcpDirectChannelInfoList is NULL, channelId=%{public}d", channelId);
75 return SOFTBUS_INVALID_PARAM;
76 }
77 if (SoftBusMutexLock(&g_tcpDirectChannelInfoList->lock) != SOFTBUS_OK) {
78 TRANS_LOGE(TRANS_SDK, "lock failed, channelId=%{public}d", channelId);
79 return SOFTBUS_LOCK_ERR;
80 }
81
82 TcpDirectChannelInfo *item = NULL;
83 LIST_FOR_EACH_ENTRY(item, &(g_tcpDirectChannelInfoList->list), TcpDirectChannelInfo, node) {
84 if (item->channelId == channelId) {
85 item->detail.needStopListener = needStopListener;
86 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
87 TRANS_LOGI(TRANS_SDK, "succ, channelId=%{public}d, needStopListener=%{public}d", channelId,
88 needStopListener);
89 return SOFTBUS_OK;
90 }
91 }
92
93 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
94 TRANS_LOGE(TRANS_SDK, "channel not found, channelId=%{public}d", channelId);
95 return SOFTBUS_NOT_FIND;
96 }
97
TransTdcGetInfoIncFdRefById(int32_t channelId,TcpDirectChannelInfo * info,bool withSeq)98 TcpDirectChannelInfo *TransTdcGetInfoIncFdRefById(int32_t channelId, TcpDirectChannelInfo *info, bool withSeq)
99 {
100 if (!CheckInfoAndMutexLock(info)) {
101 return NULL;
102 }
103
104 TcpDirectChannelInfo *item = NULL;
105 LIST_FOR_EACH_ENTRY(item, &(g_tcpDirectChannelInfoList->list), TcpDirectChannelInfo, node) {
106 if (item->channelId == channelId) {
107 (void)memcpy_s(info, sizeof(TcpDirectChannelInfo), item, sizeof(TcpDirectChannelInfo));
108 item->detail.sequence = withSeq ? (item->detail.sequence + 1) : (item->detail.sequence);
109 item->detail.fdRefCnt++;
110 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
111 return item;
112 }
113 }
114
115 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
116 return NULL;
117 }
118
TransTdcGetInfoByFd(int32_t fd,TcpDirectChannelInfo * info)119 int32_t TransTdcGetInfoByFd(int32_t fd, TcpDirectChannelInfo *info)
120 {
121 if (!CheckInfoAndMutexLock(info)) {
122 return SOFTBUS_LOCK_ERR;
123 }
124
125 TcpDirectChannelInfo *item = NULL;
126 LIST_FOR_EACH_ENTRY(item, &(g_tcpDirectChannelInfoList->list), TcpDirectChannelInfo, node) {
127 if (item->detail.fd == fd) {
128 (void)memcpy_s(info, sizeof(TcpDirectChannelInfo), item, sizeof(TcpDirectChannelInfo));
129 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
130 return SOFTBUS_OK;
131 }
132 }
133
134 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
135 return SOFTBUS_NOT_FIND;
136 }
137
TransTdcCloseChannel(int32_t channelId)138 void TransTdcCloseChannel(int32_t channelId)
139 {
140 TRANS_LOGI(TRANS_SDK, "Close tdc Channel, channelId=%{public}d.", channelId);
141 if (ServerIpcCloseChannel(NULL, channelId, CHANNEL_TYPE_TCP_DIRECT) != SOFTBUS_OK) {
142 TRANS_LOGE(TRANS_SDK, "close server tdc channelId=%{public}d err.", channelId);
143 }
144
145 TcpDirectChannelInfo *item = NULL;
146 if (SoftBusMutexLock(&g_tcpDirectChannelInfoList->lock) != SOFTBUS_OK) {
147 TRANS_LOGE(TRANS_SDK, "lock failed");
148 return;
149 }
150
151 LIST_FOR_EACH_ENTRY(item, &(g_tcpDirectChannelInfoList->list), TcpDirectChannelInfo, node) {
152 if (item->channelId != channelId) {
153 continue;
154 }
155
156 item->detail.needRelease = true;
157 if (item->detail.fdRefCnt <= 0) {
158 TransTdcReleaseFd(item->detail.fd);
159 (void)SoftBusMutexDestroy(&(item->detail.fdLock));
160 ListDelete(&item->node);
161 SoftBusFree(item);
162 item = NULL;
163 }
164 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
165 DelPendingPacket(channelId, PENDING_TYPE_DIRECT);
166 TRANS_LOGI(TRANS_SDK, "Delete tdc item success. channelId=%{public}d", channelId);
167 return;
168 }
169
170 TRANS_LOGE(TRANS_SDK, "Target item not exist. channelId=%{public}d", channelId);
171 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
172 }
173
TransGetNewTcpChannel(const ChannelInfo * channel)174 static TcpDirectChannelInfo *TransGetNewTcpChannel(const ChannelInfo *channel)
175 {
176 if (channel == NULL) {
177 TRANS_LOGE(TRANS_SDK, "param invalid");
178 return NULL;
179 }
180 TcpDirectChannelInfo *item = (TcpDirectChannelInfo *)SoftBusCalloc(sizeof(TcpDirectChannelInfo));
181 if (item == NULL) {
182 TRANS_LOGE(TRANS_SDK, "calloc failed");
183 return NULL;
184 }
185 item->channelId = channel->channelId;
186 item->detail.fd = channel->fd;
187 item->detail.channelType = channel->channelType;
188 if (SoftBusMutexInit(&(item->detail.fdLock), NULL) != SOFTBUS_OK) {
189 SoftBusFree(item);
190 TRANS_LOGE(TRANS_SDK, "init fd lock failed");
191 return NULL;
192 }
193 if (memcpy_s(item->detail.sessionKey, SESSION_KEY_LENGTH, channel->sessionKey, SESSION_KEY_LENGTH) != EOK) {
194 (void)SoftBusMutexDestroy(&(item->detail.fdLock));
195 SoftBusFree(item);
196 TRANS_LOGE(TRANS_SDK, "sessionKey copy failed");
197 return NULL;
198 }
199 if (strcpy_s(item->detail.myIp, IP_LEN, channel->myIp) != EOK) {
200 (void)SoftBusMutexDestroy(&(item->detail.fdLock));
201 SoftBusFree(item);
202 TRANS_LOGE(TRANS_SDK, "myIp copy failed");
203 return NULL;
204 }
205 return item;
206 }
207
ClientTransCheckTdcChannelExist(int32_t channelId)208 static int32_t ClientTransCheckTdcChannelExist(int32_t channelId)
209 {
210 if (SoftBusMutexLock(&g_tcpDirectChannelInfoList->lock) != SOFTBUS_OK) {
211 TRANS_LOGE(TRANS_SDK, "lock failed.");
212 return SOFTBUS_LOCK_ERR;
213 }
214 TcpDirectChannelInfo *item = NULL;
215 LIST_FOR_EACH_ENTRY(item, &(g_tcpDirectChannelInfoList->list), TcpDirectChannelInfo, node) {
216 if (item->channelId == channelId) {
217 TRANS_LOGE(TRANS_SDK, "tcp direct already exist. channelId=%{public}d", channelId);
218 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
219 return SOFTBUS_TRANS_TDC_CHANNEL_ALREADY_EXIST;
220 }
221 }
222 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
223 return SOFTBUS_OK;
224 }
225
TransTdcReleaseFdResources(int32_t fd,int32_t errCode)226 static void TransTdcReleaseFdResources(int32_t fd, int32_t errCode)
227 {
228 if (errCode == SOFTBUS_TRANS_NEGOTIATE_REJECTED) {
229 TransTdcCloseFd(fd);
230 TRANS_LOGI(
231 TRANS_SDK, "Server reject conn, fd=%{public}d", fd);
232 } else {
233 TransTdcReleaseFd(fd);
234 }
235 }
236
TransTdcDelChannelInfo(int32_t channelId,int32_t errCode)237 static void TransTdcDelChannelInfo(int32_t channelId, int32_t errCode)
238 {
239 TRANS_LOGI(TRANS_SDK, "Delete tdc channelId=%{public}d.", channelId);
240
241 TcpDirectChannelInfo *item = NULL;
242 TcpDirectChannelInfo *nextNode = NULL;
243 if (g_tcpDirectChannelInfoList == NULL) {
244 return;
245 }
246 if (SoftBusMutexLock(&g_tcpDirectChannelInfoList->lock) != SOFTBUS_OK) {
247 TRANS_LOGE(TRANS_SDK, "lock failed");
248 return;
249 }
250
251 LIST_FOR_EACH_ENTRY_SAFE(item, nextNode, &(g_tcpDirectChannelInfoList->list), TcpDirectChannelInfo, node) {
252 if (item->channelId == channelId) {
253 item->detail.needRelease = true;
254 if (item->detail.fdRefCnt <= 0) {
255 TransTdcReleaseFdResources(item->detail.fd, errCode);
256 (void)SoftBusMutexDestroy(&(item->detail.fdLock));
257 ListDelete(&item->node);
258 SoftBusFree(item);
259 item = NULL;
260 }
261 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
262 TRANS_LOGI(TRANS_SDK, "Delete tdc item success. channelId=%{public}d", channelId);
263 return;
264 }
265 }
266
267 TRANS_LOGE(TRANS_SDK, "Target item not exist. channelId=%{public}d", channelId);
268 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
269 }
270
ClientTransTdcHandleListener(const char * sessionName,const ChannelInfo * channel)271 static int32_t ClientTransTdcHandleListener(const char *sessionName, const ChannelInfo *channel)
272 {
273 bool isSocket = false;
274 int32_t ret = ClientTransTdcIfChannelForSocket(sessionName, &isSocket);
275 if (ret != SOFTBUS_OK) {
276 TRANS_LOGE(TRANS_SDK, "get channel socket fail, channelId=%{public}d", channel->channelId);
277 return ret;
278 }
279
280 if (channel->isServer && isSocket) {
281 TRANS_LOGI(TRANS_SDK, "no need listen here, channelId=%{public}d", channel->channelId);
282 return SOFTBUS_OK;
283 }
284
285 ret = TransTdcCreateListenerWithoutAddTrigger(channel->fd);
286 if (ret != SOFTBUS_OK) {
287 TRANS_LOGE(TRANS_SDK, "create listener fail, channelId=%{public}d", channel->channelId);
288 return ret;
289 }
290 if (g_tcpDirectChannelInfoList == NULL) {
291 TRANS_LOGE(TRANS_SDK, "g_tcpDirectChannelInfoList is NULL, channelId=%{public}d", channel->channelId);
292 return SOFTBUS_INVALID_PARAM;
293 }
294 if (SoftBusMutexLock(&g_tcpDirectChannelInfoList->lock) != SOFTBUS_OK) {
295 TRANS_LOGE(TRANS_SDK, "lock failed, channelId=%{public}d", channel->channelId);
296 return SOFTBUS_LOCK_ERR;
297 }
298
299 TcpDirectChannelInfo info;
300 (void)memset_s(&info, sizeof(TcpDirectChannelInfo), 0, sizeof(TcpDirectChannelInfo));
301 ret = TransTdcGetInfoById(channel->channelId, &info);
302 if (ret != SOFTBUS_OK) {
303 DelTrigger(DIRECT_CHANNEL_CLIENT, channel->fd, READ_TRIGGER);
304 TRANS_LOGE(TRANS_SDK, "TransTdcGetInfoById failed, channelId=%{public}d", channel->channelId);
305 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
306 return SOFTBUS_NOT_FIND;
307 }
308
309 if (!info.detail.needStopListener) {
310 TRANS_LOGI(TRANS_SDK, "info.detail.needStopListener false, channelId=%{public}d", channel->channelId);
311 AddTrigger(DIRECT_CHANNEL_CLIENT, channel->fd, READ_TRIGGER);
312 }
313 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
314 return SOFTBUS_OK;
315 }
316
ClientTransSetTcpOption(int32_t fd)317 static int32_t ClientTransSetTcpOption(int32_t fd)
318 {
319 int32_t ret = ConnSetTcpKeepalive(fd, HEART_TIME, TCP_KEEPALIVE_INTERVAL, TCP_KEEPALIVE_COUNT);
320 if (ret != SOFTBUS_OK) {
321 TRANS_LOGE(TRANS_SDK, "ConnSetTcpKeepalive failed, fd=%{public}d.", fd);
322 return ret;
323 }
324 ret = ConnSetTcpUserTimeOut(fd, USER_TIME_OUT);
325 if (ret != SOFTBUS_OK) {
326 TRANS_LOGE(TRANS_SDK, "ConnSetTcpUserTimeOut failed, fd=%{public}d.", fd);
327 return ret;
328 }
329 return SOFTBUS_OK;
330 }
331
ClientTransTdcOnChannelOpened(const char * sessionName,const ChannelInfo * channel)332 int32_t ClientTransTdcOnChannelOpened(const char *sessionName, const ChannelInfo *channel)
333 {
334 TRANS_CHECK_AND_RETURN_RET_LOGE(sessionName != NULL && channel != NULL,
335 SOFTBUS_INVALID_PARAM, TRANS_SDK, "param invalid");
336
337 int32_t ret = ClientTransCheckTdcChannelExist(channel->channelId);
338 TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_FILE, "check tdc channel fail!");
339
340 TcpDirectChannelInfo *item = TransGetNewTcpChannel(channel);
341 TRANS_CHECK_AND_RETURN_RET_LOGE(item != NULL, SOFTBUS_MEM_ERR,
342 TRANS_SDK, "get new tcp channel err. channelId=%{public}d", channel->channelId);
343 ret = TransAddDataBufNode(channel->channelId, channel->fd);
344 if (ret != SOFTBUS_OK) {
345 TRANS_LOGE(TRANS_SDK, "add node fail. channelId=%{public}d, fd=%{public}d", channel->channelId, channel->fd);
346 SoftBusFree(item);
347 return ret;
348 }
349
350 ret = ClientTransSetTcpOption(channel->fd);
351 if (ret != SOFTBUS_OK) {
352 goto EXIT_ERR;
353 }
354 ret = SoftBusMutexLock(&g_tcpDirectChannelInfoList->lock);
355 if (ret != SOFTBUS_OK) {
356 TRANS_LOGE(TRANS_SDK, "lock failed.");
357 goto EXIT_ERR;
358 }
359 ListAdd(&g_tcpDirectChannelInfoList->list, &item->node);
360 TRANS_LOGI(TRANS_SDK, "add channelId=%{public}d, fd=%{public}d", item->channelId, channel->fd);
361 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
362
363 ret = ClientTransTdcOnSessionOpened(sessionName, channel);
364 if (ret != SOFTBUS_OK) {
365 TransDelDataBufNode(channel->channelId);
366 TransTdcDelChannelInfo(channel->channelId, ret);
367 TRANS_LOGE(TRANS_SDK, "notify on session opened err.");
368 return ret;
369 }
370
371 ret = ClientTransTdcHandleListener(sessionName, channel);
372 if (ret != SOFTBUS_OK) {
373 ClientTransTdcOnSessionClosed(channel->channelId, SHUTDOWN_REASON_LOCAL);
374 TransDelDataBufNode(channel->channelId);
375 TransTdcDelChannelInfo(channel->channelId, ret);
376 return ret;
377 }
378
379 return SOFTBUS_OK;
380 EXIT_ERR:
381 TransDelDataBufNode(channel->channelId);
382 SoftBusFree(item);
383 return ret;
384 }
385
TransTdcManagerInit(const IClientSessionCallBack * callback)386 int32_t TransTdcManagerInit(const IClientSessionCallBack *callback)
387 {
388 g_tcpDirectChannelInfoList = CreateSoftBusList();
389 if (g_tcpDirectChannelInfoList == NULL || TransDataListInit() != SOFTBUS_OK) {
390 TRANS_LOGE(TRANS_INIT, "init tcp direct channel fail.");
391 return SOFTBUS_NO_INIT;
392 }
393 int32_t ret = ClientTransTdcSetCallBack(callback);
394 if (ret != SOFTBUS_OK) {
395 TRANS_LOGE(TRANS_INIT, "ClientTransTdcSetCallBack fail, ret=%{public}d", ret);
396 return ret;
397 }
398 ret = PendingInit(PENDING_TYPE_DIRECT);
399 if (ret != SOFTBUS_OK) {
400 TRANS_LOGE(TRANS_INIT, "trans direct pending init failed, ret=%{public}d", ret);
401 return SOFTBUS_NO_INIT;
402 }
403 TRANS_LOGE(TRANS_INIT, "init tcp direct channel success.");
404 return SOFTBUS_OK;
405 }
406
TransTdcManagerDeinit(void)407 void TransTdcManagerDeinit(void)
408 {
409 if (g_tcpDirectChannelInfoList == NULL) {
410 return;
411 }
412
413 TransDataListDeinit();
414 DestroySoftBusList(g_tcpDirectChannelInfoList);
415 g_tcpDirectChannelInfoList = NULL;
416 PendingDeinit(PENDING_TYPE_DIRECT);
417 TdcLockDeinit();
418 }
419
ClientTransTdcOnChannelOpenFailed(int32_t channelId,int32_t errCode)420 int32_t ClientTransTdcOnChannelOpenFailed(int32_t channelId, int32_t errCode)
421 {
422 return ClientTransTdcOnSessionOpenFailed(channelId, errCode);
423 }
424
TransTdcGetSessionKey(int32_t channelId,char * key,unsigned int len)425 int32_t TransTdcGetSessionKey(int32_t channelId, char *key, unsigned int len)
426 {
427 if (key == NULL) {
428 TRANS_LOGW(TRANS_SDK, "invalid param.");
429 return SOFTBUS_INVALID_PARAM;
430 }
431 TcpDirectChannelInfo channel;
432 if (TransTdcGetInfoById(channelId, &channel) != SOFTBUS_OK) {
433 TRANS_LOGE(TRANS_SDK, "get tdc info failed. channelId=%{public}d", channelId);
434 return SOFTBUS_TRANS_TDC_CHANNEL_NOT_FOUND;
435 }
436 if (memcpy_s(key, len, channel.detail.sessionKey, SESSION_KEY_LENGTH) != EOK) {
437 TRANS_LOGE(TRANS_SDK, "copy session key failed.");
438 return SOFTBUS_MEM_ERR;
439 }
440 return SOFTBUS_OK;
441 }
442
TransTdcGetHandle(int32_t channelId,int * handle)443 int32_t TransTdcGetHandle(int32_t channelId, int *handle)
444 {
445 if (handle == NULL) {
446 TRANS_LOGW(TRANS_SDK, "invalid param.");
447 return SOFTBUS_INVALID_PARAM;
448 }
449 TcpDirectChannelInfo channel;
450 if (TransTdcGetInfoById(channelId, &channel) != SOFTBUS_OK) {
451 TRANS_LOGE(TRANS_SDK, "get tdc info failed. channelId=%{public}d", channelId);
452 return SOFTBUS_TRANS_TDC_CHANNEL_NOT_FOUND;
453 }
454 *handle = channel.detail.fd;
455 return SOFTBUS_OK;
456 }
457
TransDisableSessionListener(int32_t channelId)458 int32_t TransDisableSessionListener(int32_t channelId)
459 {
460 TcpDirectChannelInfo channel;
461 if (TransTdcGetInfoById(channelId, &channel) != SOFTBUS_OK) {
462 TRANS_LOGE(TRANS_SDK, "get tdc info failed. channelId=%{public}d", channelId);
463 return SOFTBUS_TRANS_TDC_CHANNEL_NOT_FOUND;
464 }
465 if (channel.detail.fd < 0) {
466 TRANS_LOGE(TRANS_SDK, "invalid handle.");
467 return SOFTBUS_INVALID_FD;
468 }
469 if (g_tcpDirectChannelInfoList == NULL) {
470 TRANS_LOGE(TRANS_SDK, "g_tcpDirectChannelInfoList is NULL, channelId=%{public}d", channelId);
471 return SOFTBUS_NO_INIT;
472 }
473 if (SoftBusMutexLock(&g_tcpDirectChannelInfoList->lock) != SOFTBUS_OK) {
474 TRANS_LOGE(TRANS_SDK, "lock failed, channelId=%{public}d", channelId);
475 return SOFTBUS_LOCK_ERR;
476 }
477
478 (void)TransTdcSetListenerStateById(channelId, true);
479 int32_t ret = TransTdcStopRead(channel.detail.fd);
480 if (ret != SOFTBUS_OK) {
481 TRANS_LOGW(TRANS_SDK, "stop read failed. channelId=%{public}d, ret=%{public}d", channelId, ret);
482 }
483 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
484 return SOFTBUS_OK;
485 }
486
TransUpdateFdState(int32_t channelId)487 void TransUpdateFdState(int32_t channelId)
488 {
489 if (g_tcpDirectChannelInfoList == NULL) {
490 TRANS_LOGE(TRANS_SDK, "g_tcpDirectChannelInfoList is NULL, channelId=%{public}d", channelId);
491 return;
492 }
493 if (SoftBusMutexLock(&g_tcpDirectChannelInfoList->lock) != SOFTBUS_OK) {
494 TRANS_LOGE(TRANS_SDK, "lock failed, channelId=%{public}d", channelId);
495 return;
496 }
497
498 TcpDirectChannelInfo *item = NULL;
499 LIST_FOR_EACH_ENTRY(item, &(g_tcpDirectChannelInfoList->list), TcpDirectChannelInfo, node) {
500 if (item->channelId == channelId) {
501 item->detail.fdRefCnt--;
502 if (item->detail.needRelease && item->detail.fdRefCnt <= 0) {
503 TransTdcReleaseFd(item->detail.fd);
504 (void)SoftBusMutexDestroy(&(item->detail.fdLock));
505 ListDelete(&item->node);
506 SoftBusFree(item);
507 item = NULL;
508 TRANS_LOGI(TRANS_SDK, "Delete tdc item success. channelId=%{public}d", channelId);
509 }
510 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
511 return;
512 }
513 }
514
515 (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
516 TRANS_LOGE(TRANS_SDK, "channel not found, channelId=%{public}d", channelId);
517 return;
518 }
519