• 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 
34 protected:
35     struct ChannelHandShake {
36         char banner[12];  // must first index
37         union {
38             uint32_t channelId;
39             char connectKey[MAX_CONNECTKEY_SIZE];
40         };
41         char version[BUF_SIZE_TINY] = { 0 };
42     } __attribute__((packed));
43     uint32_t MallocChannel(HChannel *hOutChannel);
ReadChannel(HChannel hChannel,uint8_t * bufPtr,const int bytesIO)44     virtual int ReadChannel(HChannel hChannel, uint8_t *bufPtr, const int bytesIO)
45     {
46         return 0;
47     }
NotifyInstanceChannelFree(HChannel hChannel)48     virtual void NotifyInstanceChannelFree(HChannel hChannel) {};
49     void Send(const uint32_t channelId, uint8_t *bufPtr, const int size);
50     void SendChannel(HChannel hChannel, uint8_t *bufPtr, const int size);
51     void SendChannelWithCmd(HChannel hChannel, const uint16_t commandFlag, uint8_t *bufPtr, const int size);
52     void EchoToClient(HChannel hChannel, uint8_t *bufPtr, const int size);
ChannelSendSessionCtrlMsg(vector<uint8_t> & ctrlMsg,uint32_t sessionId)53     virtual bool ChannelSendSessionCtrlMsg(vector<uint8_t> &ctrlMsg, uint32_t sessionId)
54     {
55         return true;  // just server use
56     }
57 
58     string channelHostPort;
59     string channelHost;
60     uint16_t channelPort;
61     uv_loop_t *loopMain;
62     bool isServerOrClient;
63     uv_rwlock_t mainAsync;
64     uv_async_t asyncMainLoop;
65     list<void *> lstMainThreadOP;
66 
67 private:
68     static void MainAsyncCallback(uv_async_t *handle);
69     static void WriteCallback(uv_write_t *req, int status);
70     static void AsyncMainLoopTask(uv_idle_t *handle);
71     static void FreeChannelOpeate(uv_timer_t *handle);
72     static void FreeChannelFinally(uv_idle_t *handle);
73     void ClearChannels();
74     void FreeChannelContinue(HChannel hChannel);
75     bool SetChannelTCPString(const string &addrString);
76     uint32_t GetChannelPseudoUid();
77 
78     uv_rwlock_t lockMapChannel;  // protect mapChannel
79     map<uint32_t, HChannel> mapChannel;
80     uv_thread_t threadChanneMain;
81 };
82 }  // namespace Hdc
83 
84 #endif  // HDC_CHANNEL_H