• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "network/softbus/softbus_session.h"
17 
18 #include "dfs_session.h"
19 #include "session.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 constexpr int32_t DEVICE_ID_SIZE_MAX = 65;
29 constexpr int32_t IS_SERVER = 0;
30 
SoftbusSession(int sessionId)31 SoftbusSession::SoftbusSession(int sessionId) : sessionId_(sessionId)
32 {
33     char perDevId[DEVICE_ID_SIZE_MAX] = "";
34     int ret = ::GetPeerDeviceId(sessionId_, perDevId, sizeof(perDevId));
35     if (ret != SOFTBUS_OK) {
36         LOGE("get my peer device id failed, errno:%{public}d, sessionId:%{public}d", ret, sessionId_);
37         cid_ = "";
38     } else {
39         cid_ = string(perDevId);
40     }
41 
42     int32_t socket_fd;
43     ret = ::GetSessionHandle(sessionId_, &socket_fd);
44     if (ret != SOFTBUS_OK) {
45         LOGE("get session socket fd failed, errno:%{public}d, sessionId:%{public}d", ret, sessionId_);
46         socketFd_ = INVALID_SOCKET_FD;
47     } else {
48         socketFd_ = socket_fd;
49     }
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     IsServerSide_ = (::GetSessionSide(sessionId_) == IS_SERVER) ? true : false;
61 }
62 
IsFromServer() const63 bool SoftbusSession::IsFromServer() const
64 {
65     return IsServerSide_;
66 }
67 
GetCid() const68 string SoftbusSession::GetCid() const
69 {
70     return cid_;
71 }
72 
GetHandle() const73 int32_t SoftbusSession::GetHandle() const
74 {
75     return socketFd_;
76 }
77 
GetSessionId() const78 int SoftbusSession::GetSessionId() const
79 {
80     return sessionId_;
81 }
82 
GetKey() const83 array<char, KEY_SIZE_MAX> SoftbusSession::GetKey() const
84 {
85     return key_;
86 }
87 
Release() const88 void SoftbusSession::Release() const
89 {
90     ::CloseSession(sessionId_);
91     LOGI("session closed, sessionId:%{public}d", sessionId_);
92 }
93 
DisableSessionListener() const94 void SoftbusSession::DisableSessionListener() const
95 {
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