1 /*
2 * Copyright (c) 2021-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 "network/softbus/softbus_session.h"
17
18 #include "dfs_session.h"
19 #include "fcntl.h"
20 #include "utils_log.h"
21
22 namespace OHOS {
23 namespace Storage {
24 namespace DistributedFile {
25 using namespace std;
26
27 constexpr int32_t SOFTBUS_OK = 0;
28
SoftbusSession(int32_t sessionId, std::string peerDeviceId)29 SoftbusSession::SoftbusSession(int32_t sessionId, std::string peerDeviceId) : sessionId_(sessionId), cid_(peerDeviceId)
30 {
31 int32_t socketFd = INVALID_SOCKET_FD;
32 int32_t ret = ::GetSessionHandle(sessionId_, &socketFd);
33 if (ret != SOFTBUS_OK) {
34 LOGE("get session socket fd failed, errno:%{public}d, sessionId:%{public}d", ret, sessionId_);
35 socketFd_ = INVALID_SOCKET_FD;
36 }
37 int32_t flags = fcntl(socketFd, F_GETFL, 0);
38 if (flags < 0) {
39 LOGE("SoftbusSession flags:%{public}d", flags);
40 return;
41 }
42 if (ret == SOFTBUS_OK) {
43 flags = (int32_t)((uint32_t)flags & ~O_NONBLOCK);
44 ret = fcntl(socketFd, F_SETFL, flags);
45 if (ret < 0) {
46 LOGE("error flags:%{public}d", ret);
47 return;
48 }
49 socketFd_ = socketFd;
50 }
51
52 array<char, KEY_SIZE_MAX> key;
53 ret = ::GetSessionKey(sessionId_, key.data(), key.size());
54 if (ret != SOFTBUS_OK) {
55 LOGE("get session key failed, errno:%{public}d, sessionId:%{public}d", ret, sessionId_);
56 key_ = {};
57 } else {
58 key_ = key;
59 }
60 }
61
IsFromServer() const62 bool SoftbusSession::IsFromServer() const
63 {
64 return IsServerSide_;
65 }
66
GetCid() const67 string SoftbusSession::GetCid() const
68 {
69 return cid_;
70 }
71
GetHandle() const72 int32_t SoftbusSession::GetHandle() const
73 {
74 return socketFd_;
75 }
76
GetSessionId() const77 int SoftbusSession::GetSessionId() const
78 {
79 return sessionId_;
80 }
81
GetKey() const82 array<char, KEY_SIZE_MAX> SoftbusSession::GetKey() const
83 {
84 return key_;
85 }
86
Release() const87 void SoftbusSession::Release() const
88 {
89 Shutdown(sessionId_);
90 LOGI("session closed, sessionId:%{public}d", sessionId_);
91 }
92
DisableSessionListener() const93 void SoftbusSession::DisableSessionListener() const
94 {
95 LOGI("DisableSessionListener Enter.");
96 int32_t ret = ::DisableSessionListener(sessionId_);
97 if (ret != SOFTBUS_OK) {
98 LOGE("disableSessionlistener failed, errno:%{public}d, sessionId:%{public}d", ret, sessionId_);
99 return;
100 }
101 }
102 } // namespace DistributedFile
103 } // namespace Storage
104 } // namespace OHOS
105