1f857971dSopenharmony_ci/*
2f857971dSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f857971dSopenharmony_ci * you may not use this file except in compliance with the License.
5f857971dSopenharmony_ci * You may obtain a copy of the License at
6f857971dSopenharmony_ci *
7f857971dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f857971dSopenharmony_ci *
9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f857971dSopenharmony_ci * See the License for the specific language governing permissions and
13f857971dSopenharmony_ci * limitations under the License.
14f857971dSopenharmony_ci */
15f857971dSopenharmony_ci
16f857971dSopenharmony_ci#ifndef SOCKET_CLIENT_H
17f857971dSopenharmony_ci#define SOCKET_CLIENT_H
18f857971dSopenharmony_ci
19f857971dSopenharmony_ci#include <map>
20f857971dSopenharmony_ci#include <mutex>
21f857971dSopenharmony_ci
22f857971dSopenharmony_ci#include "i_tunnel_client.h"
23f857971dSopenharmony_ci#include "net_packet.h"
24f857971dSopenharmony_ci#include "proto.h"
25f857971dSopenharmony_ci#include "socket_connection.h"
26f857971dSopenharmony_ci#include "stream_client.h"
27f857971dSopenharmony_ci
28f857971dSopenharmony_cinamespace OHOS {
29f857971dSopenharmony_cinamespace Msdp {
30f857971dSopenharmony_cinamespace DeviceStatus {
31f857971dSopenharmony_citypedef std::function<void()> ConnectCallback;
32f857971dSopenharmony_ciclass SocketClient final : public StreamClient {
33f857971dSopenharmony_cipublic:
34f857971dSopenharmony_ci    SocketClient(std::shared_ptr<ITunnelClient> tunnel);
35f857971dSopenharmony_ci    DISALLOW_COPY_AND_MOVE(SocketClient);
36f857971dSopenharmony_ci    ~SocketClient() = default;
37f857971dSopenharmony_ci
38f857971dSopenharmony_ci    bool RegisterEvent(MessageId id, std::function<int32_t(const StreamClient&, NetPacket&)> callback);
39f857971dSopenharmony_ci    void Start();
40f857971dSopenharmony_ci    void Stop() override;
41f857971dSopenharmony_ci    void RegisterConnectedFunction(ConnectCallback funConnected);
42f857971dSopenharmony_ci    void RegisterDisconnectedFunction(ConnectCallback funDisconnected);
43f857971dSopenharmony_ci
44f857971dSopenharmony_ciprivate:
45f857971dSopenharmony_ci    bool Connect();
46f857971dSopenharmony_ci    int32_t Socket() override;
47f857971dSopenharmony_ci    void OnPacket(NetPacket &pkt);
48f857971dSopenharmony_ci    void OnDisconnected() override;
49f857971dSopenharmony_ci    void Reconnect();
50f857971dSopenharmony_ci    void OnMsgHandler(const StreamClient &client, NetPacket &pkt);
51f857971dSopenharmony_ci
52f857971dSopenharmony_ci    std::weak_ptr<ITunnelClient> tunnel_;
53f857971dSopenharmony_ci    mutable std::mutex lock_;
54f857971dSopenharmony_ci    std::map<MessageId, std::function<int32_t(const StreamClient&, NetPacket&)>> callbacks_;
55f857971dSopenharmony_ci    std::shared_ptr<SocketConnection> socket_ { nullptr };
56f857971dSopenharmony_ci    std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ { nullptr };
57f857971dSopenharmony_ci    ConnectCallback funConnected_ { nullptr };
58f857971dSopenharmony_ci    ConnectCallback funDisconnected_ { nullptr };
59f857971dSopenharmony_ci};
60f857971dSopenharmony_ci} // namespace DeviceStatus
61f857971dSopenharmony_ci} // namespace Msdp
62f857971dSopenharmony_ci} // namespace OHOS
63f857971dSopenharmony_ci#endif // SOCKET_CLIENT_H