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 || socketFd < 0) { 34 LOGE("get session socket fd failed, errno:%{public}d, sessionId:%{public}d", ret, sessionId_); 35 socketFd_ = INVALID_SOCKET_FD; 36 return; 37 } 38 int32_t flags = fcntl(socketFd, F_GETFL, 0); 39 if (flags < 0) { 40 LOGE("SoftbusSession flags:%{public}d", flags); 41 return; 42 } 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 array<char, KEY_SIZE_MAX> key; 52 ret = ::GetSessionKey(sessionId_, key.data(), key.size()); 53 if (ret != SOFTBUS_OK) { 54 LOGE("get session key failed, errno:%{public}d, sessionId:%{public}d", ret, sessionId_); 55 key_ = {}; 56 } else { 57 key_ = key; 58 } 59 } 60 IsFromServer() const61 bool SoftbusSession::IsFromServer() const 62 { 63 return IsServerSide_; 64 } 65 SetFromServer(bool isServer)66 void SoftbusSession::SetFromServer(bool isServer) 67 { 68 IsServerSide_ = isServer; 69 } 70 GetCid() const71 string SoftbusSession::GetCid() const 72 { 73 return cid_; 74 } 75 GetHandle() const76 int32_t SoftbusSession::GetHandle() const 77 { 78 return socketFd_; 79 } 80 GetSessionId() const81 int SoftbusSession::GetSessionId() const 82 { 83 return sessionId_; 84 } 85 GetKey() const86 array<char, KEY_SIZE_MAX> SoftbusSession::GetKey() const 87 { 88 return key_; 89 } 90 Release() const91 void SoftbusSession::Release() const 92 { 93 Shutdown(sessionId_); 94 LOGI("session closed, sessionId:%{public}d", sessionId_); 95 } 96 DisableSessionListener() const97 void SoftbusSession::DisableSessionListener() const 98 { 99 LOGI("DisableSessionListener Enter."); 100 int32_t ret = ::DisableSessionListener(sessionId_); 101 if (ret != SOFTBUS_OK) { 102 LOGE("disableSessionlistener failed, errno:%{public}d, sessionId:%{public}d", ret, sessionId_); 103 return; 104 } 105 } 106 } // namespace DistributedFile 107 } // namespace Storage 108 } // namespace OHOS 109