• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     void SendWithCmd(const uint32_t channelId, const uint16_t commandFlag, uint8_t *bufPtr, const int size);
33 public:
34     std::atomic<int> queuedPackages;
35 protected:
36     struct ChannelHandShake {
37         char banner[12];  // must first index
38         union {
39             uint32_t channelId;
40             char connectKey[MAX_CONNECTKEY_SIZE];
41         };
42         char version[BUF_SIZE_TINY] = { 0 };
43     } __attribute__((packed));
44     uint32_t MallocChannel(HChannel *hOutChannel);
ReadChannel(HChannel hChannel,uint8_t * bufPtr,const int bytesIO)45     virtual int ReadChannel(HChannel hChannel, uint8_t *bufPtr, const int bytesIO)
46     {
47         return 0;
48     }
NotifyInstanceChannelFree(HChannel hChannel)49     virtual void NotifyInstanceChannelFree(HChannel hChannel) {};
50     void Send(const uint32_t channelId, uint8_t *bufPtr, const int size);
51     void SendChannel(HChannel hChannel, uint8_t *bufPtr, const int size, const uint16_t commandFlag = 0);
52     void SendChannelWithCmd(HChannel hChannel, const uint16_t commandFlag, uint8_t *bufPtr, const int size);
53     void EchoToClient(HChannel hChannel, uint8_t *bufPtr, const int size);
ChannelSendSessionCtrlMsg(vector<uint8_t> & ctrlMsg,uint32_t sessionId)54     virtual bool ChannelSendSessionCtrlMsg(vector<uint8_t> &ctrlMsg, uint32_t sessionId)
55     {
56         return true;  // just server use
57     }
58 #ifdef HDC_HOST
59     void PrintChannel(const uint32_t channelId);
FillChannelResult(HChannel channel,bool isSuccess,const char * faultInfo)60     inline void FillChannelResult(HChannel channel, bool isSuccess, const char *faultInfo)
61     {
62         channel->isSuccess = isSuccess;
63         channel->faultInfo += faultInfo;
64     }
65 #endif
66 
67     string channelHostPort;
68     string channelHost;
69     uint16_t channelPort;
70     uv_loop_t *loopMain;
71     LoopStatus loopMainStatus;
72     bool isServerOrClient; // true is server, false is client
73     uv_rwlock_t mainAsync;
74     uv_async_t asyncMainLoop;
75     list<void *> lstMainThreadOP;
76 
77 private:
78     static void MainAsyncCallback(uv_async_t *handle);
79     static void FileCmdWriteCallback(uv_write_t *req, int status);
80     static void WriteCallback(uv_write_t *req, int status);
81     static void AsyncMainLoopTask(uv_idle_t *handle);
82     static void FreeChannelOpeate(uv_timer_t *handle);
83     static void FreeChannelFinally(uv_idle_t *handle);
84     void ClearChannels();
85     void FreeChannelContinue(HChannel hChannel);
86     bool SetChannelTCPString(const string &addrString);
87     uint32_t GetChannelPseudoUid();
88 
89     uv_rwlock_t lockMapChannel;  // protect mapChannel
90     map<uint32_t, HChannel> mapChannel;
91     uv_thread_t threadChanneMain;
92 
93 private:
94     void DispMntnInfo(HChannel hChannel);
95 };
96 }  // namespace Hdc
97 
98 #endif  // HDC_CHANNEL_H
99