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 #include "network/softbus/softbus_agent.h"
16
17 #include <sstream>
18
19 #include "dfsu_exception.h"
20 #include "ipc/i_daemon.h"
21 #include "network/softbus/softbus_session.h"
22 #include "network/softbus/softbus_session_dispatcher.h"
23 #include "network/softbus/softbus_session_name.h"
24 #include "session.h"
25 #include "utils_log.h"
26
27 namespace OHOS {
28 namespace Storage {
29 namespace DistributedFile {
30 namespace {
31 constexpr int MAX_RETRY_COUNT = 7;
32 }
33 using namespace std;
SoftbusAgent(weak_ptr<MountPoint> mountPoint)34 SoftbusAgent::SoftbusAgent(weak_ptr<MountPoint> mountPoint) : NetworkAgentTemplate(mountPoint)
35 {
36 auto spt = mountPoint.lock();
37 if (spt == nullptr) {
38 LOGE("mountPoint is not exist! bad weak_ptr");
39 sessionName_ = "";
40 return;
41 }
42
43 string path = spt->GetMountArgument().GetFullDst();
44 SoftbusSessionName sessionName(path);
45 sessionName_ = sessionName.ToString();
46 }
47
JoinDomain()48 void SoftbusAgent::JoinDomain()
49 {
50 ISessionListener sessionListener = {
51 .OnSessionOpened = SoftbusSessionDispatcher::OnSessionOpened,
52 .OnSessionClosed = SoftbusSessionDispatcher::OnSessionClosed,
53 .OnBytesReceived = nullptr,
54 .OnMessageReceived = nullptr,
55 .OnStreamReceived = nullptr,
56 };
57
58 SoftbusSessionDispatcher::RegisterSessionListener(sessionName_, shared_from_this());
59 int ret = ::CreateSessionServer(IDaemon::SERVICE_NAME.c_str(), sessionName_.c_str(), &sessionListener);
60 if (ret != 0) { // ! 错误码
61 stringstream ss;
62 ss << "Failed to CreateSessionServer, errno:" << ret;
63 LOGE("%{public}s, sessionName:%{public}s", ss.str().c_str(), sessionName_.c_str());
64 throw runtime_error(ss.str());
65 }
66 LOGD("Succeed to JoinDomain, busName:%{public}s", sessionName_.c_str());
67 }
68
QuitDomain()69 void SoftbusAgent::QuitDomain()
70 {
71 int ret = ::RemoveSessionServer(IDaemon::SERVICE_NAME.c_str(), sessionName_.c_str());
72 if (ret != 0) { // ! 错误码
73 stringstream ss;
74 ss << "Failed to RemoveSessionServer, errno:" << ret;
75 LOGE("%{public}s", ss.str().c_str());
76 throw runtime_error(ss.str());
77 }
78
79 SoftbusSessionDispatcher::UnregisterSessionListener(sessionName_.c_str());
80 LOGD("Succeed to QuitDomain, busName:%{public}s", sessionName_.c_str());
81 }
StopTopHalf()82 void SoftbusAgent::StopTopHalf()
83 {
84 QuitDomain();
85 }
StopBottomHalf()86 void SoftbusAgent::StopBottomHalf() {}
OpenSession(const DeviceInfo & info)87 void SoftbusAgent::OpenSession(const DeviceInfo &info)
88 {
89 SessionAttribute attr;
90 attr.dataType = TYPE_BYTES;
91
92 LOGD("Start to Open Session, cid:%{public}s", info.GetCid().c_str());
93
94 int sessionId =
95 ::OpenSession(sessionName_.c_str(), sessionName_.c_str(), info.GetCid().c_str(), "hmdfs_wifiGroup", &attr);
96 if (sessionId < 0) {
97 LOGE("Failed to open session, cid:%{public}s, sessionId:%{public}d", info.GetCid().c_str(), sessionId);
98 ThrowException(ERR_SOFTBUS_AGENT_ON_SESSION_OPENED_FAIL, "Open Session failed");
99 }
100 LOGD("Open Session SUCCESS, cid:%{public}s", info.GetCid().c_str());
101 return;
102 }
103
CloseSession(shared_ptr<BaseSession> session)104 void SoftbusAgent::CloseSession(shared_ptr<BaseSession> session)
105 {
106 if (session == nullptr) {
107 LOGE("Failed to close session, error:invalid session");
108 return;
109 }
110 session->Release();
111 }
112
IsContinueRetry(const string & cid)113 bool SoftbusAgent::IsContinueRetry(const string &cid)
114 {
115 auto retriedTimesMap = OpenSessionRetriedTimesMap_.find(cid);
116 if (retriedTimesMap != OpenSessionRetriedTimesMap_.end()) {
117 if (retriedTimesMap->second >= MAX_RETRY_COUNT) {
118 return false;
119 }
120 } else {
121 OpenSessionRetriedTimesMap_[cid] = 0;
122 }
123 OpenSessionRetriedTimesMap_[cid]++;
124 return true;
125 }
126
OnSessionOpened(const int sessionId,const int result)127 int SoftbusAgent::OnSessionOpened(const int sessionId, const int result)
128 {
129 auto session = make_shared<SoftbusSession>(sessionId);
130 auto cid = session->GetCid();
131
132 DeviceInfo info;
133 info.SetCid(cid);
134 if (result != 0) {
135 LOGE("OnSessionOpened failed, Is %{public}s Side, result:%{public}d",
136 (session->IsFromServer() == true) ? "Server" : "Client", result);
137 if (!session->IsFromServer()) { // client retry
138 if (IsContinueRetry(cid)) {
139 auto cmd = make_unique<DfsuCmd<NetworkAgentTemplate, const DeviceInfo>>(
140 &NetworkAgentTemplate::ConnectDeviceAsync, info);
141 cmd->UpdateOption({.tryTimes_ = 1});
142 Recv(move(cmd));
143 } else {
144 LOGE("Exceeded the maximum number of retries, not retry");
145 }
146 }
147 return result;
148 }
149
150 auto retriedTimesMap = OpenSessionRetriedTimesMap_.find(cid);
151 if (retriedTimesMap != OpenSessionRetriedTimesMap_.end()) {
152 OpenSessionRetriedTimesMap_.erase(cid);
153 }
154
155 int socket_fd = session->GetHandle();
156 LOGI(
157 "accept sesion, sessionid:%{public}d, Is %{public}s Side, fd %{public}d, from cid %{public}s, result "
158 "%{public}d",
159 sessionId, (session->IsFromServer() == true) ? "Server" : "Client", socket_fd, cid.c_str(), result);
160 session->DisableSessionListener();
161 AcceptSession(session);
162 return 0;
163 }
164
OnSessionClosed(int sessionId)165 void SoftbusAgent::OnSessionClosed(int sessionId)
166 {
167 auto session = make_shared<SoftbusSession>(sessionId);
168 auto cid = session->GetCid();
169 LOGI("Session to %{public}s closed by unknown reason, Is %{public}s Side", cid.c_str(),
170 (session->IsFromServer() == true) ? "Server" : "Client");
171 }
172 } // namespace DistributedFile
173 } // namespace Storage
174 } // namespace OHOS