• 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  */
15 
16 #ifndef DSOFTBUS_ADAPTER_IMPL_H
17 #define DSOFTBUS_ADAPTER_IMPL_H
18 
19 #include <map>
20 #include <set>
21 
22 #include "nocopyable.h"
23 #include "socket.h"
24 
25 #include "circle_stream_buffer.h"
26 #include "i_dsoftbus_adapter.h"
27 #include "net_packet.h"
28 
29 namespace OHOS {
30 namespace Msdp {
31 namespace DeviceStatus {
32 class DSoftbusAdapterImpl final : public IDSoftbusAdapter {
33     class Observer final {
34     public:
Observer(std::shared_ptr<IDSoftbusObserver> observer)35         explicit Observer(std::shared_ptr<IDSoftbusObserver> observer) : observer_(observer) { }
36 
37         Observer() = default;
38         ~Observer() = default;
39         DISALLOW_COPY_AND_MOVE(Observer);
40 
Lock()41         std::shared_ptr<IDSoftbusObserver> Lock() const noexcept
42         {
43             return observer_.lock();
44         }
45 
46         bool operator<(const Observer &other) const noexcept
47         {
48             return (observer_.lock() < other.observer_.lock());
49         }
50 
51     private:
52         std::weak_ptr<IDSoftbusObserver> observer_;
53     };
54 
55     struct Session {
SessionSession56         Session(int32_t socket) : socket_(socket) { }
SessionSession57         Session(const Session &other) : socket_(other.socket_) { }
58         DISALLOW_MOVE(Session);
59 
60         Session &operator=(const Session &other) = delete;
61 
62         int32_t socket_;
63         CircleStreamBuffer buffer_;
64     };
65 
66 public:
67     DSoftbusAdapterImpl() = default;
68     ~DSoftbusAdapterImpl();
69     DISALLOW_COPY_AND_MOVE(DSoftbusAdapterImpl);
70 
71     int32_t Enable() override;
72     void Disable() override;
73 
74     void AddObserver(std::shared_ptr<IDSoftbusObserver> observer) override;
75     void RemoveObserver(std::shared_ptr<IDSoftbusObserver> observer) override;
76 
77     int32_t OpenSession(const std::string &networkId) override;
78     void CloseSession(const std::string &networkId) override;
79     void CloseAllSessions() override;
80 
81     int32_t SendPacket(const std::string &networkId, NetPacket &packet) override;
82     int32_t SendParcel(const std::string &networkId, Parcel &parcel) override;
83     int32_t BroadcastPacket(NetPacket &packet) override;
84 
85     void OnBind(int32_t socket, PeerSocketInfo info);
86     void OnShutdown(int32_t socket, ShutdownReason reason);
87     void OnBytes(int32_t socket, const void *data, uint32_t dataLen);
88 
89     static std::shared_ptr<DSoftbusAdapterImpl> GetInstance();
90     static void DestroyInstance();
91 
92 private:
93     int32_t InitSocket(SocketInfo info, int32_t socketType, int32_t &socket);
94     int32_t SetupServer();
95     void ShutdownServer();
96     int32_t OpenSessionLocked(const std::string &networkId);
97     void CloseAllSessionsLocked();
98     void OnConnectedLocked(const std::string &networkId);
99     void ConfigTcpAlive(int32_t socket);
100     int32_t FindConnection(const std::string &networkId);
101     void HandleSessionData(const std::string &networkId, CircleStreamBuffer &circleBuffer);
102     void HandlePacket(const std::string &networkId, NetPacket &packet);
103     void HandleRawData(const std::string &networkId, const void *data, uint32_t dataLen);
104     bool CheckDeviceOnline(const std::string &networkId);
105 
106     std::recursive_mutex lock_;
107     int32_t socketFd_ { -1 };
108     std::string localSessionName_;
109     std::set<Observer> observers_;
110     std::map<std::string, Session> sessions_;
111 
112     static std::mutex mutex_;
113     static std::shared_ptr<DSoftbusAdapterImpl> instance_;
114 };
115 } // namespace DeviceStatus
116 } // namespace Msdp
117 } // namespace OHOS
118 #endif // DSOFTBUS_ADAPTER_IMPL_H
119