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 86 void OnBind(int32_t socket, PeerSocketInfo info); 87 void OnShutdown(int32_t socket, ShutdownReason reason); 88 void OnBytes(int32_t socket, const void *data, uint32_t dataLen); 89 90 static std::shared_ptr<DSoftbusAdapterImpl> GetInstance(); 91 static void DestroyInstance(); 92 93 private: 94 int32_t InitSocket(SocketInfo info, int32_t socketType, int32_t &socket); 95 int32_t SetupServer(); 96 void ShutdownServer(); 97 int32_t OpenSessionLocked(const std::string &networkId); 98 void CloseAllSessionsLocked(); 99 void OnConnectedLocked(const std::string &networkId); 100 void ConfigTcpAlive(int32_t socket); 101 int32_t FindConnection(const std::string &networkId); 102 void HandleSessionData(const std::string &networkId, CircleStreamBuffer &circleBuffer); 103 void HandlePacket(const std::string &networkId, NetPacket &packet); 104 void HandleRawData(const std::string &networkId, const void *data, uint32_t dataLen); 105 bool CheckDeviceOnline(const std::string &networkId); 106 107 std::recursive_mutex lock_; 108 int32_t socketFd_ { -1 }; 109 std::string localSessionName_; 110 std::set<Observer> observers_; 111 std::map<std::string, Session> sessions_; 112 113 static std::mutex mutex_; 114 static std::shared_ptr<DSoftbusAdapterImpl> instance_; 115 }; 116 } // namespace DeviceStatus 117 } // namespace Msdp 118 } // namespace OHOS 119 #endif // DSOFTBUS_ADAPTER_IMPL_H 120