• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "soft_bus_client_socket.h"
17 
18 #include "relative_timer.h"
19 #include "remote_connect_listener_manager.h"
20 #include "remote_connect_manager.h"
21 #include "remote_message.h"
22 #include "thread_handler_manager.h"
23 
24 #define LOG_TAG "USER_AUTH_SA"
25 namespace OHOS {
26 namespace UserIam {
27 namespace UserAuth {
28 namespace {
29 const uint32_t KEEP_ALIVE_INTERVAL = 1000; // 1s
30 }
ClientSocket(const int32_t socketId)31 ClientSocket::ClientSocket(const int32_t socketId)
32     : BaseSocket(socketId)
33 {
34     IAM_LOGI("client socket id is %{public}d.", socketId);
35 }
36 
~ClientSocket()37 ClientSocket::~ClientSocket()
38 {
39     if (keepAliveTimerId_.has_value()) {
40         RelativeTimer::GetInstance().Unregister(keepAliveTimerId_.value());
41     }
42 }
43 
SendMessage(const std::string & connectionName,const std::string & srcEndPoint,const std::string & destEndPoint,const std::shared_ptr<Attributes> & attributes,MsgCallback & callback)44 ResultCode ClientSocket::SendMessage(const std::string &connectionName, const std::string &srcEndPoint,
45     const std::string &destEndPoint, const std::shared_ptr<Attributes> &attributes, MsgCallback &callback)
46 {
47     IAM_LOGD("start.");
48     int32_t socketId = GetSocketId();
49     if (socketId == INVALID_SOCKET_ID) {
50         IAM_LOGE("socket id is invalid");
51         return GENERAL_ERROR;
52     }
53 
54     RefreshKeepAliveTimer();
55     const ConnectionInfo connectionInfo = {
56         .socketId = socketId,
57         .connectionName = connectionName,
58         .srcEndPoint = srcEndPoint,
59         .destEndPoint = destEndPoint,
60         .attributes = attributes,
61         .callback = callback
62     };
63     return SendRequest(connectionInfo);
64 }
65 
OnQos(int32_t socketId,QoSEvent eventId,const QosTV * qos,uint32_t qosCount)66 void ClientSocket::OnQos(int32_t socketId, QoSEvent eventId, const QosTV *qos, uint32_t qosCount)
67 {
68     IAM_LOGI("start, socket id is %{public}d", socketId);
69 }
70 
OnShutdown(int32_t socketId,ShutdownReason reason)71 void ClientSocket::OnShutdown(int32_t socketId, ShutdownReason reason)
72 {
73     IAM_LOGI("start, socket id is %{public}d", socketId);
74     std::string connectionName = GetConnectionName();
75     if (!connectionName.empty()) {
76         RemoteConnectListenerManager::GetInstance().OnConnectionDown(connectionName);
77     }
78 }
79 
OnBytes(int32_t socketId,const void * data,uint32_t dataLen)80 void ClientSocket::OnBytes(int32_t socketId, const void *data, uint32_t dataLen)
81 {
82     IAM_LOGD("start, socket id is %{public}d", socketId);
83     IF_FALSE_LOGE_AND_RETURN(data != nullptr);
84     IF_FALSE_LOGE_AND_RETURN(dataLen != 0);
85 
86     std::string networkId = GetNetworkId();
87     if (networkId.empty()) {
88         IAM_LOGE("networkId id is null, socketId:%{public}d.", socketId);
89         return;
90     }
91 
92     std::shared_ptr<SoftBusMessage> softBusMessage = ParseMessage(networkId, const_cast<void *>(data), dataLen);
93     if (softBusMessage == nullptr) {
94         IAM_LOGE("serverSocket parse message fail.");
95         return;
96     }
97 
98     ResultCode ret = ProcDataReceive(socketId, softBusMessage);
99     if (ret != SUCCESS) {
100         IAM_LOGE("HandleDataReceive fail, socketId:%{public}d.", socketId);
101         return;
102     }
103 }
104 
OnBind(int32_t socketId,PeerSocketInfo info)105 void ClientSocket::OnBind(int32_t socketId, PeerSocketInfo info)
106 {
107     IAM_LOGI("start, socket id is %{public}d", socketId);
108 }
109 
GetConnectionName()110 std::string ClientSocket::GetConnectionName()
111 {
112     return connectionName_;
113 }
114 
GetNetworkId()115 std::string ClientSocket::GetNetworkId()
116 {
117     return networkId_;
118 }
119 
SetConnectionName(const std::string & connectionName)120 void ClientSocket::SetConnectionName(const std::string &connectionName)
121 {
122     connectionName_ = connectionName;
123 }
124 
SetNetworkId(const std::string & networkId)125 void ClientSocket::SetNetworkId(const std::string &networkId)
126 {
127     networkId_ = networkId;
128 }
129 
RefreshKeepAliveTimer()130 void ClientSocket::RefreshKeepAliveTimer()
131 {
132     if (keepAliveTimerId_.has_value()) {
133         RelativeTimer::GetInstance().Unregister(keepAliveTimerId_.value());
134     }
135     keepAliveTimerId_ = RelativeTimer::GetInstance().Register([weakThis = weak_from_this(), this]() {
136             auto sharedThis = weakThis.lock();
137             IF_FALSE_LOGE_AND_RETURN(sharedThis != nullptr);
138             SendKeepAliveMessage();
139         }, KEEP_ALIVE_INTERVAL);
140     IAM_LOGI("ConnectionName: %{public}s, keep alive timer is refreshed", connectionName_.c_str());
141 }
142 
SendKeepAliveMessage()143 void ClientSocket::SendKeepAliveMessage()
144 {
145     IAM_LOGI("ConnectionName: %{public}s, send keep alive message begin", connectionName_.c_str());
146     std::shared_ptr<Attributes> request = Common::MakeShared<Attributes>();
147     IF_FALSE_LOGE_AND_RETURN(request != nullptr);
148 
149     bool setMsgTypeRet = request->SetInt32Value(Attributes::ATTR_MSG_TYPE, MessageType::KEEP_ALIVE);
150     IF_FALSE_LOGE_AND_RETURN(setMsgTypeRet);
151 
152     MsgCallback sendKeepAliveCallback = [connectionName = connectionName_](const std::shared_ptr<Attributes> &) {
153         IAM_LOGI("ConnectionName: %{public}s, receive keep alive message ack", connectionName.c_str());
154     };
155     ResultCode ret = RemoteConnectionManager::GetInstance().SendMessage(connectionName_, CLIENT_SOCKET_ENDPOINT_NAME,
156         REMOTE_SERVICE_ENDPOINT_NAME, request, sendKeepAliveCallback);
157     if (ret != SUCCESS) {
158         IAM_LOGE("ConnectionName: %{public}s, send keep alive message failed, connection down",
159             connectionName_.c_str());
160         auto threadHandler = ThreadHandlerManager::GetInstance().GetThreadHandler(SINGLETON_THREAD_NAME);
161         if (threadHandler == nullptr) {
162             IAM_LOGE("ConnectionName: %{public}s, threadHandler is nullptr", connectionName_.c_str());
163             return;
164         }
165         threadHandler->PostTask(
166             [connectionName = connectionName_]() {
167                 RemoteConnectListenerManager::GetInstance().OnConnectionDown(connectionName);
168                 IAM_LOGE("ConnectionName: %{public}s, set connection down", connectionName.c_str());
169             });
170         return;
171     }
172     IAM_LOGI("ConnectionName: %{public}s, send keep alive message success", connectionName_.c_str());
173 }
174 } // namespace UserAuth
175 } // namespace UserIam
176 } // namespace OHOS