• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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  * Description: softbus connection.
15  * Author: sunhong
16  * Create: 2022-01-22
17  */
18 
19 #ifndef SOFTBUSCONNECTION_H
20 #define SOFTBUSCONNECTION_H
21 
22 #include <string>
23 #include <memory>
24 #include <mutex>
25 #include <unordered_map>
26 #include <vector>
27 
28 #include "../../include/connection.h"
29 #include "softbus_wrapper.h"
30 
31 namespace OHOS {
32 namespace CastEngine {
33 namespace CastEngineService {
34 class SoftBusConnection : public Connection, public Channel, public std::enable_shared_from_this<SoftBusConnection> {
35 public:
36     using Connection::channelRequest_;
37 
38     SoftBusConnection();
39     ~SoftBusConnection() override;
40 
41     static std::pair<bool, std::shared_ptr<SoftBusConnection>> GetConnection(int sessionId);
42     int StartConnection(const ChannelRequest &request, std::shared_ptr<IChannelListener> channelListener) override;
43     int StartListen(const ChannelRequest &request, std::shared_ptr<IChannelListener> channelListener) override;
44     void CloseConnection() override;
45     bool Send(const uint8_t *buf, int bufLen) override;
46     SoftBusWrapper &GetSoftBus();
47     bool GetActivelyOpenFlag() const;
48     void SetActivelyOpenFlag(bool isActivelyOpen);
49     bool GetPassiveCloseFlag() const;
50     void SetPassiveCloseFlag(bool isPassiveClose);
51     static std::unordered_map<std::string, std::shared_ptr<SoftBusConnection>> connectionMap_;
52     static std::mutex connectionMapMtx_;
53     static IFileSendListener fileSendListener_;
54     static IFileReceiveListener fileReceiveListener_;
55 private:
56     static int OnConnectionSessionOpened(int sessionId, int result);
57     static void OnConnectionSessionClosed(int sessionId);
58     static void OnConnectionMessageReceived(int sessionId, const void *data, unsigned int dataLen);
59     static void OnConnectionBytesReceived(int sessionId, const void *data, unsigned int dataLen);
60     static void OnConnectionStreamReceived(int sessionId, const StreamData *data, const StreamData *ext,
61         const StreamFrameInfo *param);
62     static void OnConnectionSessionEvent(int sessionId, int eventId, int tvCount, const QosTv *tvList);
63 
64     // Softbus file send callback function
65     static int OnSendFileProcess(int sessionId, uint64_t bytesUpload, uint64_t bytesTotal);
66     static int OnSendFileFinished(int sessionId, const char *firstFile);
67     static void OnFileTransError(int sessionId);
68 
69     // Softbus file recv callback function
70     static int OnReceiveFileStarted(int sessionId, const char *files, int fileCnt);
71     static int OnReceiveFileProcess(int sessionId, const char *firstFile, uint64_t bytesUpload, uint64_t bytesTotal);
72     static void OnReceiveFileFinished(int sessionId, const char *files, int fileCnt);
73 
74     int SetupSession(std::shared_ptr<IChannelListener> channelListener, std::shared_ptr<SoftBusConnection> hold);
75     void StashConnectionInfo(const ChannelRequest &request);
76     int GetSessionType(ModuleType moduleType) const;
77     std::string CreateSessionName(ModuleType moduleType, int sessionId);
78 
79     static const std::string PACKAGE_NAME;
80     static const std::string AUTH_SESSION_NAME_FACTOR;
81     static const std::string RTSP_SESSION_NAME_FACTOR;
82     static const std::string RTCP_SESSION_NAME_FACTOR;
83     static const std::string VIDEO_SESSION_NAME_FACTOR;
84     static const std::string AUDIO_SESSION_NAME_FACTOR;
85     static const std::string CONTROL_SESSION_NAME_FACTOR;
86     static const std::string STREAM_SESSION_NAME_FACTOR;
87     static const std::string FILES_SESSION_NAME_FACTOR;
88     static const std::string BYTES_SESSION_NAME_FACTOR;
89     static const std::string SESSION_NAME_PREFIX;
90     static const int RET_ERR = -1;
91     static const int RET_OK = 0;
92 
93     bool isActivelyOpen_;
94     bool isPassiveClose_;
95     ISessionListener sessionListener_ = { OnConnectionSessionOpened, OnConnectionSessionClosed,
96         OnConnectionBytesReceived, OnConnectionMessageReceived, OnConnectionStreamReceived, OnConnectionSessionEvent };
97     SoftBusWrapper softbus_;
98 };
99 } // namespace CastEngineService
100 } // namespace CastEngine
101 } // namespace OHOS
102 
103 #endif
104