1 /*
2 * Copyright (c) 2021-2023 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_listener.h"
17
18 #include <stdbool.h>
19 #include <unistd.h>
20 #include <securec.h>
21
22 #include "client_trans_tcp_direct_callback.h"
23 #include "client_trans_tcp_direct_manager.h"
24 #include "client_trans_tcp_direct_message.h"
25 #include "softbus_adapter_thread.h"
26 #include "softbus_base_listener.h"
27 #include "softbus_errcode.h"
28 #include "softbus_socket.h"
29 #include "trans_log.h"
30
31 typedef struct {
32 SoftBusMutex lock;
33 bool lockInit;
34 } SoftBusTcpListenerLock;
35
36 static SoftBusTcpListenerLock g_lock = {
37 .lockInit = false,
38 };
39
40
TdcLockInit(void)41 static void TdcLockInit(void)
42 {
43 if (g_lock.lockInit == false) {
44 if (SoftBusMutexInit(&g_lock.lock, NULL) != SOFTBUS_OK) {
45 TRANS_LOGE(TRANS_INIT, "TDC lock init failed");
46 return;
47 }
48 g_lock.lockInit = true;
49 }
50 }
ClientTdcOnConnectEvent(ListenerModule module,int cfd,const ConnectOption * clientAddr)51 static int32_t ClientTdcOnConnectEvent(ListenerModule module, int cfd,
52 const ConnectOption *clientAddr)
53 {
54 (void)module;
55 (void)cfd;
56 (void)clientAddr;
57 return SOFTBUS_OK;
58 }
59
ClientTdcOnDataEvent(ListenerModule module,int events,int32_t fd)60 static int32_t ClientTdcOnDataEvent(ListenerModule module, int events, int32_t fd)
61 {
62 (void)module;
63 TcpDirectChannelInfo channel;
64 (void)memset_s(&channel, sizeof(TcpDirectChannelInfo), 0, sizeof(TcpDirectChannelInfo));
65 if (TransTdcGetInfoByFd(fd, &channel) == NULL) {
66 TRANS_LOGE(TRANS_SDK, "can not match fd. fd=%{public}d", fd);
67 return SOFTBUS_ERR;
68 }
69
70 if (events == SOFTBUS_SOCKET_IN) {
71 int32_t channelId = channel.channelId;
72 int32_t ret = TransTdcRecvData(channelId);
73 if (ret == SOFTBUS_DATA_NOT_ENOUGH) {
74 TRANS_LOGE(TRANS_SDK, "client process data fail, SOFTBUS_DATA_NOT_ENOUGH. channelId=%{public}d", channelId);
75 return SOFTBUS_OK;
76 }
77 if (ret != SOFTBUS_OK) {
78 TRANS_LOGE(TRANS_SDK, "client process data fail, channelId=%{public}d", channelId);
79 TransDelDataBufNode(channelId);
80 TransTdcCloseChannel(channelId);
81 ClientTransTdcOnSessionClosed(channelId, SHUTDOWN_REASON_RECV_DATA_ERR);
82 return SOFTBUS_ERR;
83 }
84 }
85 return SOFTBUS_OK;
86 }
87
TransTdcCreateListener(int32_t fd)88 int32_t TransTdcCreateListener(int32_t fd)
89 {
90 static bool isInitedFlag = false;
91 TdcLockInit();
92 if (SoftBusMutexLock(&g_lock.lock) != SOFTBUS_OK) {
93 TRANS_LOGE(TRANS_SDK, "lock failed.");
94 return SOFTBUS_ERR;
95 }
96 if (isInitedFlag == false) {
97 isInitedFlag = true;
98
99 static SoftbusBaseListener listener = {
100 .onConnectEvent = ClientTdcOnConnectEvent,
101 .onDataEvent = ClientTdcOnDataEvent,
102 };
103
104 if (StartBaseClient(DIRECT_CHANNEL_CLIENT, &listener) != SOFTBUS_OK) {
105 TRANS_LOGE(TRANS_SDK, "start sdk base listener failed.");
106 SoftBusMutexUnlock(&g_lock.lock);
107 return SOFTBUS_ERR;
108 }
109 TRANS_LOGI(TRANS_SDK, "create sdk listener success.");
110 }
111 SoftBusMutexUnlock(&g_lock.lock);
112
113 return AddTrigger(DIRECT_CHANNEL_CLIENT, fd, READ_TRIGGER);
114 }
115
TransTdcReleaseFd(int32_t fd)116 void TransTdcReleaseFd(int32_t fd)
117 {
118 if (fd < 0) {
119 TRANS_LOGI(TRANS_SDK, "fd less than zero");
120 return;
121 }
122 DelTrigger(DIRECT_CHANNEL_CLIENT, fd, READ_TRIGGER);
123 ConnShutdownSocket(fd);
124 }
125
TransTdcStopRead(int32_t fd)126 int32_t TransTdcStopRead(int32_t fd)
127 {
128 if (fd < 0) {
129 TRANS_LOGI(TRANS_SDK, "fd less than zero");
130 return SOFTBUS_OK;
131 }
132 return DelTrigger(DIRECT_CHANNEL_CLIENT, fd, READ_TRIGGER);
133 }
134