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