1/*
2 * Copyright (c) 2023 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 SOCKET_CLIENT_H
17#define SOCKET_CLIENT_H
18
19#include <map>
20#include <mutex>
21
22#include "i_tunnel_client.h"
23#include "net_packet.h"
24#include "proto.h"
25#include "socket_connection.h"
26#include "stream_client.h"
27
28namespace OHOS {
29namespace Msdp {
30namespace DeviceStatus {
31class SocketClient final : public StreamClient {
32public:
33    SocketClient(std::shared_ptr<ITunnelClient> tunnel);
34    DISALLOW_COPY_AND_MOVE(SocketClient);
35    ~SocketClient() = default;
36
37    bool RegisterEvent(MessageId id, std::function<int32_t(const StreamClient&, NetPacket&)> callback);
38    void Start();
39    void Stop() override;
40
41private:
42    bool Connect();
43    int32_t Socket() override;
44    void OnPacket(NetPacket &pkt);
45    void OnDisconnected() override;
46    void Reconnect();
47    void OnMsgHandler(const StreamClient &client, NetPacket &pkt);
48
49    std::weak_ptr<ITunnelClient> tunnel_;
50    mutable std::mutex lock_;
51    std::map<MessageId, std::function<int32_t(const StreamClient&, NetPacket&)>> callbacks_;
52    std::shared_ptr<SocketConnection> socket_ { nullptr };
53    std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ { nullptr };
54};
55} // namespace DeviceStatus
56} // namespace Msdp
57} // namespace OHOS
58#endif // SOCKET_CLIENT_H