1 /* 2 * Copyright (c) 2021-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 STREAM_CLIENT_H 17 #define STREAM_CLIENT_H 18 19 #include <functional> 20 #include <future> 21 #include <thread> 22 23 #include "net_packet.h" 24 #include "nocopyable.h" 25 #include "stream_socket.h" 26 27 namespace OHOS { 28 namespace Msdp { 29 namespace DeviceStatus { 30 class StreamClient; 31 using MsgClientFunCallback = std::function<void(const StreamClient&, NetPacket&)>; 32 class StreamClient : public StreamSocket { 33 public: 34 StreamClient(); 35 DISALLOW_COPY_AND_MOVE(StreamClient); 36 virtual ~StreamClient(); 37 38 virtual int32_t Socket() = 0; 39 virtual void Stop(); 40 int32_t StartConnect(); 41 bool SendMsg(const char *buf, size_t size) const; 42 bool SendMsg(const NetPacket &pkt) const; GetConnectedStatus()43 bool GetConnectedStatus() const 44 { 45 return hasConnected_; 46 } 47 48 protected: OnConnected()49 virtual void OnConnected() {} OnDisconnected()50 virtual void OnDisconnected() {} 51 bool StartClient(MsgClientFunCallback fun); 52 53 protected: 54 bool hasClient_ { false }; 55 bool isRunning_ { false }; 56 bool hasConnected_ { false }; 57 MsgClientFunCallback recvFun_ { nullptr }; 58 }; 59 } // namespace DeviceStatus 60 } // namespace Msdp 61 } // namespace OHOS 62 #endif // STREAM_CLIENT_H