• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_tcp_direct_listener.h"
17 
18 #include <stdbool.h>
19 #include <unistd.h>
20 
21 #include "client_trans_tcp_direct_callback.h"
22 #include "client_trans_tcp_direct_manager.h"
23 #include "client_trans_tcp_direct_message.h"
24 #include "softbus_adapter_thread.h"
25 #include "softbus_base_listener.h"
26 #include "softbus_errcode.h"
27 #include "softbus_log.h"
28 #include "softbus_socket.h"
29 #include "softbus_type_def.h"
30 #include "trans_pending_pkt.h"
31 
32 typedef struct {
33     SoftBusMutex lock;
34     bool lockInit;
35 } SoftBusTcpListenerLock;
36 
37 static SoftBusTcpListenerLock g_lock = {
38     .lockInit = false,
39 };
40 
41 
TdcLockInit(void)42 static void TdcLockInit(void)
43 {
44     if (g_lock.lockInit == false) {
45         if (SoftBusMutexInit(&g_lock.lock, NULL) != SOFTBUS_OK) {
46             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TDC lock init failed");
47             return;
48         }
49         g_lock.lockInit = true;
50     }
51     return;
52 }
ClientTdcOnConnectEvent(ListenerModule module,int events,int cfd,const ConnectOption * clientAddr)53 static int32_t ClientTdcOnConnectEvent(ListenerModule module, int events, int cfd, const ConnectOption *clientAddr)
54 {
55     (void)module;
56     (void)events;
57     (void)cfd;
58     (void)clientAddr;
59     return SOFTBUS_OK;
60 }
61 
ClientTdcOnDataEvent(ListenerModule module,int events,int32_t fd)62 static int32_t ClientTdcOnDataEvent(ListenerModule module, int events, int32_t fd)
63 {
64     (void)module;
65     TcpDirectChannelInfo channel;
66     if (TransTdcGetInfoByFd(fd, &channel) == NULL) {
67         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_WARN, "can not match fd.[%d]", fd);
68         return SOFTBUS_ERR;
69     }
70 
71     if (events == SOFTBUS_SOCKET_IN) {
72         int32_t channelId = channel.channelId;
73         int32_t ret = TransTdcRecvData(channelId);
74         if (ret == SOFTBUS_DATA_NOT_ENOUGH) {
75             return SOFTBUS_OK;
76         }
77         if (ret != SOFTBUS_OK) {
78             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "client cId[%d] process data fail,", channelId);
79             TransDelDataBufNode(channelId);
80             TransTdcCloseChannel(channelId);
81             ClientTransTdcOnSessionClosed(channelId);
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         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransTdcCreateListener 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 (SetSoftbusBaseListener(DIRECT_CHANNEL_CLIENT, &listener) != SOFTBUS_OK) {
105             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "start sdk base listener failed.");
106             SoftBusMutexUnlock(&g_lock.lock);
107             return SOFTBUS_ERR;
108         }
109         if (StartBaseClient(DIRECT_CHANNEL_CLIENT) < SOFTBUS_OK) {
110             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "client start base listener failed.");
111             SoftBusMutexUnlock(&g_lock.lock);
112             return SOFTBUS_ERR;
113         }
114         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "create sdk listener success.");
115     }
116     SoftBusMutexUnlock(&g_lock.lock);
117 
118     return AddTrigger(DIRECT_CHANNEL_CLIENT, fd, READ_TRIGGER);
119 }
120 
TransTdcReleaseFd(int32_t fd)121 void TransTdcReleaseFd(int32_t fd)
122 {
123     if (fd < 0) {
124         return;
125     }
126     DelTrigger(DIRECT_CHANNEL_CLIENT, fd, READ_TRIGGER);
127     ConnShutdownSocket(fd);
128 }
129 
TransTdcStopRead(int32_t fd)130 int32_t TransTdcStopRead(int32_t fd)
131 {
132     if (fd < 0) {
133         return SOFTBUS_OK;
134     }
135     return DelTrigger(DIRECT_CHANNEL_CLIENT, fd, READ_TRIGGER);
136 }
137