• 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 
59     string channelHostPort;
60     string channelHost;
61     uint16_t channelPort;
62     uv_loop_t *loopMain;
63     bool isServerOrClient; // true is server, false is client
64     uv_rwlock_t mainAsync;
65     uv_async_t asyncMainLoop;
66     list<void *> lstMainThreadOP;
67 
68 private:
69     static void MainAsyncCallback(uv_async_t *handle);
70     static void FileCmdWriteCallback(uv_write_t *req, int status);
71     static void WriteCallback(uv_write_t *req, int status);
72     static void AsyncMainLoopTask(uv_idle_t *handle);
73     static void FreeChannelOpeate(uv_timer_t *handle);
74     static void FreeChannelFinally(uv_idle_t *handle);
75     void ClearChannels();
76     void FreeChannelContinue(HChannel hChannel);
77     bool SetChannelTCPString(const string &addrString);
78     uint32_t GetChannelPseudoUid();
79 
80     uv_rwlock_t lockMapChannel;  // protect mapChannel
81     map<uint32_t, HChannel> mapChannel;
82     uv_thread_t threadChanneMain;
83 
84 private:
85     void DispMntnInfo(HChannel hChannel);
86 };
87 }  // namespace Hdc
88 
89 #endif  // HDC_CHANNEL_H
90