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