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