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 "if_mmi_client.h" 20 21 #include "client_msg_handler.h" 22 23 namespace OHOS { 24 namespace MMI { 25 class MMIClient final : public UDSClient, public IfMMIClient, public std::enable_shared_from_this<IfMMIClient> { 26 public: 27 MMIClient() = default; 28 DISALLOW_COPY_AND_MOVE(MMIClient); 29 ~MMIClient() override; 30 31 int32_t Socket() override; 32 void SetEventHandler(EventHandlerPtr eventHandler) override; 33 void MarkIsEventHandlerChanged(EventHandlerPtr eventHandler) override; 34 bool Start() override; 35 void RegisterConnectedFunction(ConnectCallback fun) override; 36 void RegisterDisconnectedFunction(ConnectCallback fun) override; 37 void Stop() override; 38 bool SendMessage(const NetPacket& pkt) const override; 39 bool GetCurrentConnectedStatus() const override; 40 void OnRecvMsg(const char *buf, size_t size) override; 41 int32_t Reconnect() override; 42 void OnDisconnect() override; 43 MMIClientPtr GetSharedPtr() override; IsEventHandlerChanged()44 bool IsEventHandlerChanged() override 45 { 46 return isEventHandlerChanged_; 47 } 48 EventHandlerPtr GetEventHandler() const override; 49 50 private: 51 bool StartEventRunner(); 52 void OnReconnect(); 53 bool AddFdListener(int32_t fd, bool selfCreate = false); 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 void SetScheduler(); 60 61 private: 62 ClientMsgHandler msgHandler_; 63 ConnectCallback funConnected_; 64 ConnectCallback funDisconnected_; 65 CircleStreamBuffer circBuf_; 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