• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     int32_t Bind(const std::string &peerNetworkId, const std::string &pkgName);
42 
43     void Shutdown(int32_t socket);
44 
45     int32_t SendMessage(int32_t socket, const std::string &data);
46 
47     int32_t SendBytes(int32_t socket, const std::string &data);
48 
49     int32_t ObtainPeerDeviceId(int32_t socket, std::string &deviceId);
50 
51     void AddSessionListener(std::shared_ptr<SoftbusSessionListener> softbusSessionListener);
52 
53     /* *
54      * Callback adaptation for session channel opened.
55      *
56      * @param socket socket
57      */
58     void OnBind(int32_t socket, PeerSocketInfo info);
59 
60     /* *
61      * Callback adaptation for session channel closed.
62      *
63      * @param socket socket
64      */
65     void OnShutdown(int32_t socket, ShutdownReason reason);
66 
67     /* *
68      * Callback adaptation for data received by the session channel.
69      *
70      * @param socket socket
71      * @param data data received by the session channel
72      */
73     void OnMessage(int32_t socket, const void *data, int32_t dataLen);
74     void OnBytes(int32_t socket, const void *data, int32_t dataLen);
75 
76 private:
77     std::recursive_mutex socketLock_;
78 
79     std::vector<std::shared_ptr<SoftbusSessionListener>> sessionListeners_;
80     std::map<int32_t, std::string> mMap_;
81     static constexpr const int QOS_COUNT = 3;
82 };
83 } // namespace OHOS::AVSession
84 
85 #endif // SOFTBUS_SESSION_MANAGER_H
86