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