• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_callback.h"
17 
18 #include <stddef.h>
19 #include <securec.h>
20 
21 #include "softbus_def.h"
22 #include "softbus_error_code.h"
23 #include "client_trans_tcp_direct_manager.h"
24 #include "client_trans_tcp_direct_message.h"
25 #include "trans_log.h"
26 #include "client_trans_tcp_direct_listener.h"
27 
28 static IClientSessionCallBack g_sessionCb;
29 
ClientTransTdcSetCallBack(const IClientSessionCallBack * cb)30 int32_t ClientTransTdcSetCallBack(const IClientSessionCallBack *cb)
31 {
32     if (cb == NULL) {
33         TRANS_LOGE(TRANS_SDK, "cb null.");
34         return SOFTBUS_INVALID_PARAM;
35     }
36     g_sessionCb = *cb;
37     return SOFTBUS_OK;
38 }
39 
ClientTransTdcOnSessionOpened(const char * sessionName,const ChannelInfo * channel,SocketAccessInfo * accessInfo)40 int32_t ClientTransTdcOnSessionOpened(const char *sessionName, const ChannelInfo *channel, SocketAccessInfo *accessInfo)
41 {
42     if (sessionName == NULL || channel == NULL) {
43         TRANS_LOGE(TRANS_SDK, "invalid param");
44         return SOFTBUS_INVALID_PARAM;
45     }
46     SessionType type = TYPE_BUTT;
47     switch (channel->businessType) {
48         case BUSINESS_TYPE_MESSAGE:
49             type = TYPE_MESSAGE;
50             break;
51         default:
52             type = TYPE_BYTES;
53             break;
54     }
55     return g_sessionCb.OnSessionOpened(sessionName, channel, type, accessInfo);
56 }
57 
ClientTransTdcOnSessionClosed(int32_t channelId,ShutdownReason reason)58 int32_t ClientTransTdcOnSessionClosed(int32_t channelId, ShutdownReason reason)
59 {
60     (void)TransDelDataBufNode(channelId);
61     (void)TransTdcCloseChannel(channelId);
62     return g_sessionCb.OnSessionClosed(channelId, CHANNEL_TYPE_TCP_DIRECT, reason);
63 }
64 
ClientTransTdcOnSessionOpenFailed(int32_t channelId,int32_t errCode)65 int32_t ClientTransTdcOnSessionOpenFailed(int32_t channelId, int32_t errCode)
66 {
67     return g_sessionCb.OnSessionOpenFailed(channelId, CHANNEL_TYPE_TCP_DIRECT, errCode);
68 }
69 
ClientTransTdcOnDataReceived(int32_t channelId,const void * data,uint32_t len,SessionPktType type)70 int32_t ClientTransTdcOnDataReceived(int32_t channelId,
71     const void *data, uint32_t len, SessionPktType type)
72 {
73     return g_sessionCb.OnDataReceived(channelId, CHANNEL_TYPE_TCP_DIRECT, data, len, type);
74 }
75 
ClientTransTdcOnChannelBind(int32_t channelId,int32_t channelType)76 int32_t ClientTransTdcOnChannelBind(int32_t channelId, int32_t channelType)
77 {
78     if (g_sessionCb.OnChannelBind == NULL) {
79         TRANS_LOGW(TRANS_SDK, "OnChannelBind is null channelId=%{public}d", channelId);
80         return SOFTBUS_INVALID_PARAM;
81     }
82 
83     int32_t ret = g_sessionCb.OnChannelBind(channelId, channelType);
84     if (ret == SOFTBUS_NOT_NEED_UPDATE) {
85         return SOFTBUS_OK;
86     }
87     if (ret != SOFTBUS_OK) {
88         return ret;
89     }
90 
91     TcpDirectChannelInfo info;
92     (void)memset_s(&info, sizeof(TcpDirectChannelInfo), 0, sizeof(TcpDirectChannelInfo));
93     ret = TransTdcGetInfoById(channelId, &info);
94     if (ret != SOFTBUS_OK) {
95         TRANS_LOGE(TRANS_SDK, "TransTdcGetInfoById failed, channelId=%{public}d", channelId);
96         return SOFTBUS_NOT_FIND;
97     }
98 
99     if (info.detail.needStopListener) {
100         TRANS_LOGI(TRANS_SDK, "info.detail.needStopListener is true, channelId=%{public}d", channelId);
101         return SOFTBUS_OK;
102     }
103 
104     ret = TransTdcCreateListener(info.detail.fd);
105     if (ret != SOFTBUS_OK) {
106         TRANS_LOGE(TRANS_SDK, "TransTdcCreateListener failed, channelId=%{public}d", channelId);
107         g_sessionCb.OnSessionClosed(channelId, CHANNEL_TYPE_TCP_DIRECT, SHUTDOWN_REASON_LOCAL);
108         return ret;
109     }
110 
111     return SOFTBUS_OK;
112 }
113 
ClientTransTdcIfChannelForSocket(const char * sessionName,bool * isSocket)114 int32_t ClientTransTdcIfChannelForSocket(const char *sessionName, bool *isSocket)
115 {
116     return g_sessionCb.IfChannelForSocket(sessionName, isSocket);
117 }
118