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 #ifndef SOFTBUS_SESSION_MANAGER_H 17 #define SOFTBUS_SESSION_MANAGER_H 18 19 #include <string> 20 #include <mutex> 21 22 namespace OHOS::AVSession { 23 class SoftbusSessionListener { 24 public: 25 virtual void OnSessionOpened(int32_t sessionId) = 0; 26 virtual void OnSessionClosed(int32_t sessionId) = 0; 27 virtual void OnMessageReceived(int32_t sessionId, const std::string &data) = 0; 28 virtual void OnBytesReceived(int32_t sessionId, const std::string &data) = 0; 29 virtual ~SoftbusSessionListener() = default; 30 }; 31 32 class SoftbusSessionManager { 33 public: 34 static SoftbusSessionManager& GetInstance(); 35 36 int32_t CreateSessionServer(const std::string &pkg); 37 38 int32_t RemoveSessionServer(const std::string &pkg); 39 40 int32_t OpenSession(const std::string &deviceId, const std::string &groupId); 41 42 int32_t CloseSession(int32_t sessionId); 43 44 int32_t SendMessage(int32_t sessionId, const std::string &data); 45 46 int32_t SendBytes(int32_t sessionId, const std::string &data); 47 48 int32_t ObtainPeerDeviceId(int32_t sessionId, std::string &deviceId); 49 50 int32_t GetPeerSessionName(int32_t sessionId, std::string &sessionName); 51 52 bool IsServerSide(int32_t sessionId); 53 54 void AddSessionListener(std::shared_ptr<SoftbusSessionListener> softbusSessionListener); 55 56 /* * 57 * Callback adaptation for session channel opened. 58 * 59 * @param sessionId sessionId 60 */ 61 int32_t OnSessionOpened(int32_t sessionId, int32_t result); 62 63 /* * 64 * Callback adaptation for session channel closed. 65 * 66 * @param sessionId sessionId 67 */ 68 void OnSessionClosed(int32_t sessionId); 69 70 /* * 71 * Callback adaptation for data received by the session channel. 72 * 73 * @param sessionId sessionId 74 * @param data data received by the session channel 75 */ 76 void OnMessageReceived(int32_t sessionId, const std::string &data); 77 void OnBytesReceived(int32_t sessionId, const std::string &data); 78 79 private: 80 bool isSessionServerRunning_ = false; 81 std::recursive_mutex sessionLock_; 82 std::vector<std::shared_ptr<SoftbusSessionListener>> sessionListeners_; 83 84 static constexpr const int DEVICE_INFO_MAX_LENGTH = 256; 85 }; 86 } // namespace OHOS::AVSession 87 88 #endif // SOFTBUS_SESSION_MANAGER_H 89