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 <atomic> 20 #include <map> 21 #include <set> 22 23 #include "event_handler.h" 24 #include "nocopyable.h" 25 #include "socket.h" 26 27 #include "circle_stream_buffer.h" 28 #include "i_dsoftbus_adapter.h" 29 #include "net_packet.h" 30 #include <shared_mutex> 31 32 namespace OHOS { 33 namespace Msdp { 34 namespace DeviceStatus { 35 class DSoftbusAdapterImpl final : public IDSoftbusAdapter { 36 class Observer final { 37 public: Observer(std::shared_ptr<IDSoftbusObserver> observer)38 explicit Observer(std::shared_ptr<IDSoftbusObserver> observer) 39 : observer_(observer) {} 40 41 Observer() = default; 42 ~Observer() = default; 43 DISALLOW_COPY_AND_MOVE(Observer); 44 Lock()45 std::shared_ptr<IDSoftbusObserver> Lock() const noexcept 46 { 47 return observer_.lock(); 48 } 49 50 bool operator<(const Observer &other) const noexcept 51 { 52 return (observer_.lock() < other.observer_.lock()); 53 } 54 55 private: 56 std::weak_ptr<IDSoftbusObserver> observer_; 57 }; 58 59 struct Session { SessionSession60 Session(int32_t socket) : socket_(socket) {} SessionSession61 Session(const Session &other) : socket_(other.socket_) {} 62 DISALLOW_MOVE(Session); 63 64 Session& operator=(const Session &other) = delete; 65 66 int32_t socket_; 67 CircleStreamBuffer buffer_; 68 }; 69 70 public: 71 DSoftbusAdapterImpl() = default; 72 ~DSoftbusAdapterImpl(); 73 DISALLOW_COPY_AND_MOVE(DSoftbusAdapterImpl); 74 75 int32_t Enable() override; 76 void Disable() override; 77 78 void AddObserver(std::shared_ptr<IDSoftbusObserver> observer) override; 79 void RemoveObserver(std::shared_ptr<IDSoftbusObserver> observer) override; 80 81 int32_t OpenSession(const std::string &networkId) override; 82 void CloseSession(const std::string &networkId) override; 83 void CloseAllSessions() override; 84 85 int32_t SendPacket(const std::string &networkId, NetPacket &packet) override; 86 int32_t SendParcel(const std::string &networkId, Parcel &parcel) override; 87 int32_t BroadcastPacket(NetPacket &packet) override; 88 void StartHeartBeat(const std::string &networkId) override; 89 void StopHeartBeat(const std::string &networkId) override; 90 91 bool HasSessionExisted(const std::string &networkId) override; 92 93 void OnBind(int32_t socket, PeerSocketInfo info); 94 void OnShutdown(int32_t socket, ShutdownReason reason); 95 void OnBytes(int32_t socket, const void *data, uint32_t dataLen); 96 97 static std::shared_ptr<DSoftbusAdapterImpl> GetInstance(); 98 static void DestroyInstance(); 99 100 private: 101 int32_t InitSocket(SocketInfo info, int32_t socketType, int32_t &socket); 102 int32_t SetupServer(); 103 void ShutdownServer(); 104 int32_t OpenSessionLocked(const std::string &networkId); 105 void CloseAllSessionsLocked(); 106 void OnConnectedLocked(const std::string &networkId); 107 void ConfigTcpAlive(int32_t socket); 108 int32_t FindConnection(const std::string &networkId); 109 void HandleSessionData(const std::string &networkId, CircleStreamBuffer &circleBuffer); 110 void HandlePacket(const std::string &networkId, NetPacket &packet); 111 void HandleRawData(const std::string &networkId, const void *data, uint32_t dataLen); 112 bool CheckDeviceOnline(const std::string &networkId); 113 void InitHeartBeat(); 114 int32_t KeepHeartBeating(const std::string &networkId); 115 void UpdateHeartBeatState(const std::string &networkId, bool state); 116 bool GetHeartBeatState(const std::string &networkId); 117 118 /* 119 These four interfaces followed only read members, use shared_lock to avoid dead lock. 120 SendPacket 121 SendParcel 122 BroadcastPacket 123 OnBytes 124 */ 125 std::shared_mutex lock_; 126 int32_t socketFd_ { -1 }; 127 std::string localSessionName_; 128 std::set<Observer> observers_; 129 std::map<std::string, Session> sessions_; 130 std::shared_ptr<AppExecFwk::EventHandler> eventHandler_; 131 NetPacket heartBeatPacket_ { MessageId::DSOFTBUS_HEART_BEAT_PACKET }; 132 std::unordered_map<std::string, bool> heartBeatStates_; 133 std::shared_mutex heartBeatLock_; 134 135 static std::mutex mutex_; 136 static std::shared_ptr<DSoftbusAdapterImpl> instance_; 137 }; 138 } // namespace DeviceStatus 139 } // namespace Msdp 140 } // namespace OHOS 141 #endif // DSOFTBUS_ADAPTER_IMPL_H 142