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 #include <map>
22 
23 #include "socket.h"
24 
25 namespace OHOS::AVSession {
26 class SoftbusSessionListener {
27 public:
28     virtual void OnBind(int32_t socket, PeerSocketInfo info) = 0;
29     virtual void OnShutdown(int32_t socket, ShutdownReason reason) = 0;
30     virtual void OnBytes(int32_t socket, const void *data, int32_t dataLen) = 0;
31     virtual void OnMessage(int32_t socket, const void *data, int32_t dataLen) = 0;
32     virtual ~SoftbusSessionListener() = default;
33 };
34 
35 class SoftbusSessionManager {
36 public:
37     static SoftbusSessionManager& GetInstance();
38 
39     int32_t Socket(const std::string &pkgName);
40 
41     void Shutdown(int32_t socket);
42 
43     int32_t SendMessage(int32_t socket, const std::string &data);
44 
45     int32_t SendBytes(int32_t socket, const std::string &data);
46 
47     int32_t ObtainPeerDeviceId(int32_t socket, std::string &deviceId);
48 
49     void AddSessionListener(std::shared_ptr<SoftbusSessionListener> softbusSessionListener);
50 
51     /* *
52      * Callback adaptation for session channel opened.
53      *
54      * @param socket socket
55      */
56     void OnBind(int32_t socket, PeerSocketInfo info);
57 
58     /* *
59      * Callback adaptation for session channel closed.
60      *
61      * @param socket socket
62      */
63     void OnShutdown(int32_t socket, ShutdownReason reason);
64 
65     /* *
66      * Callback adaptation for data received by the session channel.
67      *
68      * @param socket socket
69      * @param data data received by the session channel
70      */
71     void OnMessage(int32_t socket, const void *data, int32_t dataLen);
72     void OnBytes(int32_t socket, const void *data, int32_t dataLen);
73 
74 private:
75     std::recursive_mutex socketLock_;
76 
77     std::vector<std::shared_ptr<SoftbusSessionListener>> sessionListeners_;
78     std::map<int32_t, std::string> mMap_;
79     static constexpr const int QOS_COUNT = 3;
80 };
81 } // namespace OHOS::AVSession
82 
83 #endif // SOFTBUS_SESSION_MANAGER_H
84