1 /*
2 * Copyright (c) 2023 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 "softbus_session.h"
17
18 #include "avsession_log.h"
19 #include "avsession_errors.h"
20 #include "softbus_session_manager.h"
21
22 namespace OHOS::AVSession {
OnConnectSession(int32_t sessionId)23 void SoftbusSession::OnConnectSession(int32_t sessionId)
24 {
25 std::string deviceId;
26 int ret = SoftbusSessionManager::GetInstance().ObtainPeerDeviceId(sessionId, deviceId);
27 CHECK_AND_RETURN_LOG(ret == AVSESSION_SUCCESS, "obtain peer device id failed");
28 std::lock_guard lockGuard(deviceMapLock_);
29 deviceToSessionMap_.insert({ deviceId, sessionId });
30 }
31
OnDisConnectSession(int32_t sessionId)32 void SoftbusSession::OnDisConnectSession(int32_t sessionId)
33 {
34 std::string deviceId;
35 int ret = SoftbusSessionManager::GetInstance().ObtainPeerDeviceId(sessionId, deviceId);
36 CHECK_AND_RETURN_LOG(ret == AVSESSION_SUCCESS, "obtain peer device id failed");
37 std::lock_guard lockGuard(deviceMapLock_);
38 deviceToSessionMap_.erase(deviceId);
39 }
40
SendByteToAll(const std::string & data)41 void SoftbusSession::SendByteToAll(const std::string &data)
42 {
43 SLOGI("SendByteToAll: %{public}s", data.c_str());
44 std::lock_guard lockGuard(deviceMapLock_);
45 for (auto it = deviceToSessionMap_.begin(); it != deviceToSessionMap_.end(); it++) {
46 SLOGI("SendByteToAll : %{public}s", data.c_str());
47 SoftbusSessionManager::GetInstance().SendBytes(it->second, data);
48 }
49 }
50
SendByte(const std::string & deviceId,const std::string & data)51 void SoftbusSession::SendByte(const std::string &deviceId, const std::string &data)
52 {
53 SLOGI("SendByte: %{public}s", data.c_str());
54 std::lock_guard lockGuard(deviceMapLock_);
55 auto iter = deviceToSessionMap_.find(deviceId);
56 if (iter != deviceToSessionMap_.end()) {
57 SoftbusSessionManager::GetInstance().SendBytes(iter->second, data);
58 }
59 }
60
SendByte(int32_t sessionId,const std::string & data)61 void SoftbusSession::SendByte(int32_t sessionId, const std::string &data)
62 {
63 SLOGI("SendByte: %{public}s", data.c_str());
64 SoftbusSessionManager::GetInstance().SendBytes(sessionId, data);
65 }
66
SendMessage(const std::string & deviceId,const std::string & data)67 void SoftbusSession::SendMessage(const std::string &deviceId, const std::string &data)
68 {
69 SLOGI("SendMessage: %{public}s", data.c_str());
70 std::lock_guard lockGuard(deviceMapLock_);
71 auto iter = deviceToSessionMap_.find(deviceId);
72 if (iter != deviceToSessionMap_.end()) {
73 SoftbusSessionManager::GetInstance().SendMessage(iter->second, data);
74 }
75 }
76
SendMessage(int32_t sessionId,const std::string & data)77 void SoftbusSession::SendMessage(int32_t sessionId, const std::string &data)
78 {
79 SLOGI("SendMessage: %{public}s", data.c_str());
80 SoftbusSessionManager::GetInstance().SendMessage(sessionId, data);
81 }
82 } // namespace OHOS::AVSession