• 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 OHOS_DM_TRANSPORT_H
17 #define OHOS_DM_TRANSPORT_H
18 
19 #include <set>
20 
21 #include "event_handler.h"
22 #include "socket.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
26 class DMCommTool;
27 class DMTransport {
28 public:
29     explicit DMTransport(std::shared_ptr<DMCommTool> dmCommToolPtr);
30     int32_t Init();
31     int32_t UnInit();
32     virtual ~DMTransport() = default;
33     // open softbus channel with remote device by networkid.
34     int32_t StartSocket(const std::string &rmtNetworkId, int32_t &socketId);
35     // stop softbus channel with remote device by networkid.
36     int32_t StopSocket(const std::string &rmtNetworkId);
37     int32_t Send(const std::string &rmtNetworkId, const std::string &payload, int32_t socketId);
38     int32_t OnSocketOpened(int32_t socketId, const PeerSocketInfo &info);
39     void OnSocketClosed(int32_t socketId, ShutdownReason reason);
40     void OnBytesReceived(int32_t socketId, const void *data, uint32_t dataLen);
41 
42 private:
43     int32_t CreateServerSocket();
44     int32_t CreateClientSocket(const std::string &remoteDevId);
45     bool IsDeviceSessionOpened(const std::string &remoteDevId, int32_t &socketId);
46     std::string GetRemoteNetworkIdBySocketId(int32_t socketId);
47     void ClearDeviceSocketOpened(const std::string &remoteDevId, int32_t socketId);
48     void HandleReceiveMessage(const int32_t socketId, const std::string &payload);
49 
50 private:
51     std::mutex rmtSocketIdMtx_;
52     // record the socket id for the connection with remote devices, <remote networkId, socketId>
53     std::map<std::string, std::set<int32_t>> remoteDevSocketIds_;
54     std::set<int32_t> sourceSocketIds_;
55     std::atomic<int32_t> localServerSocket_;
56     std::string localSocketName_;
57     std::atomic<bool> isSocketSvrCreateFlag_;
58     std::weak_ptr<DMCommTool> dmCommToolWPtr_;
59 };
60 } // DistributedHardware
61 } // OHOS
62 #endif