1 /*
2  * Copyright (c) 2021-2022 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 MMI_CLIENT_H
17 #define MMI_CLIENT_H
18 
19 #include "nocopyable.h"
20 
21 #include "if_mmi_client.h"
22 
23 #include "circle_stream_buffer.h"
24 #include "client_msg_handler.h"
25 
26 namespace OHOS {
27 namespace MMI {
28 class MMIClient final : public UDSClient, public IfMMIClient, public std::enable_shared_from_this<IfMMIClient> {
29 public:
30     MMIClient() = default;
31     DISALLOW_COPY_AND_MOVE(MMIClient);
32     ~MMIClient() override;
33 
34     int32_t Socket() override;
35     void SetEventHandler(EventHandlerPtr eventHandler) override;
36     void MarkIsEventHandlerChanged(EventHandlerPtr eventHandler) override;
37     bool Start() override;
38     void RegisterConnectedFunction(ConnectCallback fun) override;
39     void RegisterDisconnectedFunction(ConnectCallback fun) override;
40     void Stop() override;
41     bool SendMessage(const NetPacket& pkt) const override;
42     bool GetCurrentConnectedStatus() const override;
43     void OnRecvMsg(const char *buf, size_t size) override;
44     int32_t Reconnect() override;
45     void OnDisconnect() override;
46     MMIClientPtr GetSharedPtr() override;
IsEventHandlerChanged()47     bool IsEventHandlerChanged() override
48     {
49         return isEventHandlerChanged_;
50     }
51     EventHandlerPtr GetEventHandler() const override;
52 
53 private:
54     bool StartEventRunner();
55     void OnReconnect();
56     bool AddFdListener(int32_t fd);
57     bool DelFdListener(int32_t fd);
58     void OnPacket(NetPacket& pkt);
59     const std::string& GetErrorStr(ErrCode code) const;
60     void OnConnected() override;
61     void OnDisconnected() override;
62     void SetScheduler();
63 
64 private:
65     ClientMsgHandler msgHandler_;
66     ConnectCallback funConnected_;
67     ConnectCallback funDisconnected_;
68     CircleStreamBuffer circBuf_;
69     std::mutex mtx_;
70     EventHandlerPtr eventHandler_ { nullptr };
71     bool isEventHandlerChanged_ { false };
72     bool isListening_ { false };
73 };
74 } // namespace MMI
75 } // namespace OHOS
76 #endif // MMI_CLIENT_H
77