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_session_callback.h"
17
18 #include <securec.h>
19
20 #include "client_trans_proxy_manager.h"
21 #include "client_trans_session_manager.h"
22 #include "softbus_adapter_mem.h"
23 #include "softbus_errcode.h"
24 #include "softbus_log.h"
25
26 static IClientSessionCallBack g_sessionCb;
27
AcceptSessionAsServer(const char * sessionName,const ChannelInfo * channel,uint32_t flag,int32_t * sessionId)28 static int32_t AcceptSessionAsServer(const char *sessionName, const ChannelInfo *channel, uint32_t flag,
29 int32_t *sessionId)
30 {
31 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "AcceptSessionAsServer");
32 SessionInfo *session = (SessionInfo *)SoftBusCalloc(sizeof(SessionInfo));
33 if (session == NULL) {
34 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "malloc failed");
35 return SOFTBUS_MALLOC_ERR;
36 }
37
38 session->channelId = channel->channelId;
39 session->channelType = channel->channelType;
40 session->peerPid = channel->peerPid;
41 session->peerUid = channel->peerUid;
42 session->isServer = channel->isServer;
43 session->isEnable = true;
44 session->info.flag = (int32_t)flag;
45 session->routeType = channel->routeType;
46 if (strcpy_s(session->info.peerSessionName, SESSION_NAME_SIZE_MAX, channel->peerSessionName) != EOK ||
47 strcpy_s(session->info.peerDeviceId, DEVICE_ID_SIZE_MAX, channel->peerDeviceId) != EOK ||
48 strcpy_s(session->info.groupId, GROUP_ID_SIZE_MAX, channel->groupId) != EOK) {
49 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "client add peer session name, device id, group id failed");
50 SoftBusFree(session);
51 return SOFTBUS_MEM_ERR;
52 }
53
54 int32_t ret = ClientAddNewSession(sessionName, session);
55 if (ret != SOFTBUS_OK) {
56 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "client add session failed");
57 SoftBusFree(session);
58 return SOFTBUS_ERR;
59 }
60 *sessionId = session->sessionId;
61 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "AcceptSessionAsServer ok");
62 return SOFTBUS_OK;
63 }
64
GetSessionCallbackByChannelId(int32_t channelId,int32_t channelType,int32_t * sessionId,ISessionListener * listener)65 static int32_t GetSessionCallbackByChannelId(int32_t channelId, int32_t channelType,
66 int32_t *sessionId, ISessionListener *listener)
67 {
68 if ((channelId < 0) || (sessionId == NULL) || (listener == NULL)) {
69 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Invalid param");
70 return SOFTBUS_INVALID_PARAM;
71 }
72 int32_t ret = ClientGetSessionIdByChannelId(channelId, channelType, sessionId);
73 if (ret != SOFTBUS_OK) {
74 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get sessionId failed, channelId [%d]", channelId);
75 return SOFTBUS_ERR;
76 }
77 ret = ClientGetSessionCallbackById(*sessionId, listener);
78 if (ret != SOFTBUS_OK) {
79 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get session listener failed");
80 return SOFTBUS_ERR;
81 }
82 return SOFTBUS_OK;
83 }
84
TransOnSessionOpened(const char * sessionName,const ChannelInfo * channel,uint32_t flag)85 int32_t TransOnSessionOpened(const char *sessionName, const ChannelInfo *channel, uint32_t flag)
86 {
87 if ((sessionName == NULL) || (channel == NULL)) {
88 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Invalid param");
89 return SOFTBUS_INVALID_PARAM;
90 }
91 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO,
92 "TransOnSessionOpened: sessionName=%s, flag=%d, isServer=%d, type=%d",
93 sessionName, flag, channel->isServer, channel->routeType);
94
95 ISessionListener listener = {0};
96 int32_t ret = ClientGetSessionCallbackByName(sessionName, &listener);
97 if (ret != SOFTBUS_OK) {
98 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get session listener failed");
99 return SOFTBUS_ERR;
100 }
101
102 int32_t sessionId = INVALID_SESSION_ID;
103 if (channel->isServer) {
104 ret = AcceptSessionAsServer(sessionName, channel, flag, &sessionId);
105 } else {
106 ret = ClientEnableSessionByChannelId(channel, &sessionId);
107 }
108
109 if (ret != SOFTBUS_OK) {
110 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "accept session failed");
111 return SOFTBUS_ERR;
112 }
113
114 if ((listener.OnSessionOpened == NULL) || (listener.OnSessionOpened(sessionId, SOFTBUS_OK) != SOFTBUS_OK)) {
115 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "OnSessionOpened failed");
116 (void)ClientDeleteSession(sessionId);
117 return SOFTBUS_ERR;
118 }
119 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "TransOnSessionOpened ok");
120 return SOFTBUS_OK;
121 }
122
TransOnSessionOpenFailed(int32_t channelId,int32_t channelType)123 int32_t TransOnSessionOpenFailed(int32_t channelId, int32_t channelType)
124 {
125 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "TransOnSessionOpenFailed: channelId=%d, channelType=%d",
126 channelId, channelType);
127 int32_t sessionId;
128 ISessionListener listener = {0};
129 int32_t ret = GetSessionCallbackByChannelId(channelId, channelType, &sessionId, &listener);
130 if (ret != SOFTBUS_OK) {
131 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get session callback failed");
132 return ret;
133 }
134
135 if (listener.OnSessionOpened != NULL) {
136 (void)listener.OnSessionOpened(sessionId, SOFTBUS_ERR);
137 }
138
139 (void)ClientDeleteSession(sessionId);
140 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "TransOnSessionOpenFailed ok");
141 return SOFTBUS_OK;
142 }
143
TransOnSessionClosed(int32_t channelId,int32_t channelType)144 int32_t TransOnSessionClosed(int32_t channelId, int32_t channelType)
145 {
146 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "TransOnSessionClosed: channelId=%d, channelType=%d",
147 channelId, channelType);
148 int32_t sessionId;
149 ISessionListener listener = {0};
150 int32_t ret = GetSessionCallbackByChannelId(channelId, channelType, &sessionId, &listener);
151 if (ret != SOFTBUS_OK) {
152 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get session callback failed");
153 return ret;
154 }
155
156 if (listener.OnSessionClosed != NULL) {
157 listener.OnSessionClosed(sessionId);
158 }
159
160 ret = ClientDeleteSession(sessionId);
161 if (ret != SOFTBUS_OK) {
162 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "client delete session failed");
163 return SOFTBUS_ERR;
164 }
165 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "TransOnSessionClosed ok");
166 return SOFTBUS_OK;
167 }
168
ProcessReceivedFileData(int32_t sessionId,const char * data,uint32_t len,SessionPktType type)169 static int32_t ProcessReceivedFileData(int32_t sessionId, const char *data, uint32_t len, SessionPktType type)
170 {
171 char sessionName[SESSION_NAME_SIZE_MAX] = {0};
172 if (ClientGetSessionDataById(sessionId, sessionName, SESSION_NAME_SIZE_MAX, KEY_SESSION_NAME)
173 != SOFTBUS_OK) {
174 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get session name failed");
175 return SOFTBUS_ERR;
176 }
177
178 FileListener fileListener;
179 memset_s(&fileListener, sizeof(fileListener), 0, sizeof(fileListener));
180 if (TransGetFileListener(sessionName, &fileListener) != SOFTBUS_OK) {
181 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get file listener failed");
182 return SOFTBUS_ERR;
183 }
184
185 if (type == TRANS_SESSION_FILE_ALLFILE_SENT) {
186 if (ProcessFileListData(sessionId, fileListener, data, len) != SOFTBUS_OK) {
187 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "process filelist data failed");
188 return SOFTBUS_ERR;
189 }
190 } else {
191 if (ProcessFileFrameData(sessionId, fileListener, data, len, type) != SOFTBUS_OK) {
192 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "process fileframe data failed");
193 return SOFTBUS_ERR;
194 }
195 }
196 return SOFTBUS_OK;
197 }
198
TransOnDataReceived(int32_t channelId,int32_t channelType,const void * data,uint32_t len,SessionPktType type)199 int32_t TransOnDataReceived(int32_t channelId, int32_t channelType,
200 const void *data, uint32_t len, SessionPktType type)
201 {
202 int32_t sessionId;
203 ISessionListener listener = {0};
204 int32_t ret = GetSessionCallbackByChannelId(channelId, channelType, &sessionId, &listener);
205 if (ret != SOFTBUS_OK) {
206 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get session callback failed");
207 return ret;
208 }
209
210 switch (type) {
211 case TRANS_SESSION_BYTES:
212 if (listener.OnBytesReceived != NULL) {
213 listener.OnBytesReceived(sessionId, data, len);
214 }
215 break;
216 case TRANS_SESSION_MESSAGE:
217 if (listener.OnMessageReceived != NULL) {
218 listener.OnMessageReceived(sessionId, data, len);
219 }
220 break;
221 case TRANS_SESSION_FILE_FIRST_FRAME:
222 case TRANS_SESSION_FILE_ONGOINE_FRAME:
223 case TRANS_SESSION_FILE_LAST_FRAME:
224 case TRANS_SESSION_FILE_ONLYONE_FRAME:
225 case TRANS_SESSION_FILE_ALLFILE_SENT:
226 if (channelType == CHANNEL_TYPE_PROXY) {
227 return ProcessReceivedFileData(sessionId, data, len, type);
228 }
229 break;
230 default:
231 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "unknown session type");
232 return SOFTBUS_ERR;
233 }
234
235 return SOFTBUS_OK;
236 }
237
TransOnOnStreamRecevied(int32_t channelId,int32_t channelType,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)238 int32_t TransOnOnStreamRecevied(int32_t channelId, int32_t channelType,
239 const StreamData *data, const StreamData *ext, const StreamFrameInfo *param)
240 {
241 int32_t sessionId;
242 ISessionListener listener = {0};
243 int32_t ret = GetSessionCallbackByChannelId(channelId, channelType, &sessionId, &listener);
244 if (ret != SOFTBUS_OK) {
245 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get session callback failed");
246 return ret;
247 }
248 if (listener.OnStreamReceived == NULL) {
249 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "listener OnStreamReceived is NULL");
250 return SOFTBUS_ERR;
251 }
252 listener.OnStreamReceived(sessionId, data, ext, param);
253 return SOFTBUS_OK;
254 }
255
TransOnQosEvent(int32_t channelId,int32_t channelType,int32_t eventId,int32_t tvCount,const QosTv * tvList)256 int32_t TransOnQosEvent(int32_t channelId, int32_t channelType, int32_t eventId, int32_t tvCount,
257 const QosTv *tvList)
258 {
259 int32_t sessionId;
260 ISessionListener listener = {0};
261 int32_t ret = GetSessionCallbackByChannelId(channelId, channelType, &sessionId, &listener);
262 if (ret != SOFTBUS_OK) {
263 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get session callback failed");
264 return ret;
265 }
266 if (listener.OnQosEvent == NULL) {
267 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "listener OnQosEvent is NULL");
268 return SOFTBUS_ERR;
269 }
270 listener.OnQosEvent(sessionId, eventId, tvCount, tvList);
271 return SOFTBUS_OK;
272 }
273
GetClientSessionCb(void)274 IClientSessionCallBack *GetClientSessionCb(void)
275 {
276 g_sessionCb.OnSessionOpened = TransOnSessionOpened;
277 g_sessionCb.OnSessionClosed = TransOnSessionClosed;
278 g_sessionCb.OnSessionOpenFailed = TransOnSessionOpenFailed;
279 g_sessionCb.OnDataReceived = TransOnDataReceived;
280 g_sessionCb.OnStreamReceived = TransOnOnStreamRecevied;
281 g_sessionCb.OnGetSessionId = ClientGetSessionIdByChannelId;
282 g_sessionCb.OnQosEvent = TransOnQosEvent;
283 return &g_sessionCb;
284 }
285