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