1 /* 2 * Copyright (C) 2021 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 HDC_CHANNEL_H 16 #define HDC_CHANNEL_H 17 #include "common.h" 18 19 namespace Hdc { 20 class HdcChannelBase { 21 public: 22 HdcChannelBase(const bool serverOrClient, const string &addrString, uv_loop_t *loopMainIn); 23 virtual ~HdcChannelBase(); 24 HChannel AdminChannel(const uint8_t op, const uint32_t channelId, HChannel hInput); 25 static void AllocCallback(uv_handle_t *handle, size_t sizeWanted, uv_buf_t *buf); 26 static void ReadStream(uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf); 27 void PushAsyncMessage(const uint32_t channelId, const uint8_t method, const void *data, const int dataSize); 28 void WorkerPendding(); 29 void FreeChannel(const uint32_t channelId); 30 void EchoToAllChannelsViaSessionId(uint32_t targetSessionId, const string &echo); 31 vector<uint8_t> GetChannelHandshake(string &connectKey) const; 32 33 protected: 34 struct ChannelHandShake { 35 char banner[12]; // must first index 36 union { 37 uint32_t channelId; 38 char connectKey[MAX_CONNECTKEY_SIZE]; 39 }; 40 } __attribute__((packed)); 41 uint32_t MallocChannel(HChannel *hOutChannel); ReadChannel(HChannel hChannel,uint8_t * bufPtr,const int bytesIO)42 virtual int ReadChannel(HChannel hChannel, uint8_t *bufPtr, const int bytesIO) 43 { 44 return 0; 45 } NotifyInstanceChannelFree(HChannel hChannel)46 virtual void NotifyInstanceChannelFree(HChannel hChannel) {}; 47 void Send(const uint32_t channelId, uint8_t *bufPtr, const int size); 48 void SendChannel(HChannel hChannel, uint8_t *bufPtr, const int size); 49 void EchoToClient(HChannel hChannel, uint8_t *bufPtr, const int size); ChannelSendSessionCtrlMsg(vector<uint8_t> & ctrlMsg,uint32_t sessionId)50 virtual bool ChannelSendSessionCtrlMsg(vector<uint8_t> &ctrlMsg, uint32_t sessionId) 51 { 52 return true; // just server use 53 } 54 55 string channelHostPort; 56 string channelHost; 57 uint16_t channelPort; 58 uv_loop_t *loopMain; 59 bool isServerOrClient; 60 uv_rwlock_t mainAsync; 61 uv_async_t asyncMainLoop; 62 list<void *> lstMainThreadOP; 63 64 private: 65 static void MainAsyncCallback(uv_async_t *handle); 66 static void WriteCallback(uv_write_t *req, int status); 67 static void AsyncMainLoopTask(uv_idle_t *handle); 68 static void FreeChannelOpeate(uv_timer_t *handle); 69 static void FreeChannelFinally(uv_idle_t *handle); 70 void ClearChannels(); 71 void FreeChannelContinue(HChannel hChannel); 72 bool SetChannelTCPString(const string &addrString); 73 uint32_t GetChannelPseudoUid(); 74 75 uv_rwlock_t lockMapChannel; // protect mapChannel 76 map<uint32_t, HChannel> mapChannel; 77 uv_thread_t threadChanneMain; 78 }; 79 } // namespace Hdc 80 81 #endif // HDC_CHANNEL_H