AWS IoT Device SDK C++ v2  1.33.0
AWS IoT Device SDK C++ v2
ChannelHandler.h
Go to the documentation of this file.
1 #pragma once
2 
7 #include <aws/crt/Exports.h>
8 #include <aws/crt/Types.h>
9 #include <aws/io/channel.h>
10 
11 #include <chrono>
12 #include <cstddef>
13 
14 struct aws_array_list;
15 struct aws_io_message;
16 
17 namespace Aws
18 {
19  namespace Crt
20  {
21  namespace Io
22  {
23  enum class ChannelDirection
24  {
25  Read,
26  Write,
27  };
28 
29  enum class MessageType
30  {
32  };
33 
34  enum class TaskStatus
35  {
36  RunReady,
37  Canceled,
38  };
39 
47  {
48  public:
49  virtual ~ChannelHandler() = default;
50 
51  ChannelHandler(const ChannelHandler &) = delete;
53 
54  protected:
68  virtual int ProcessReadMessage(struct aws_io_message *message) = 0;
69 
79  virtual int ProcessWriteMessage(struct aws_io_message *message) = 0;
80 
89  virtual int IncrementReadWindow(size_t size) = 0;
90 
103  virtual void ProcessShutdown(
104  ChannelDirection dir,
105  int errorCode,
106  bool freeScarceResourcesImmediately) = 0;
107 
111  virtual size_t InitialWindowSize() = 0;
112 
117  virtual size_t MessageOverhead() = 0;
118 
122  virtual void ResetStatistics() {};
123 
128  virtual void GatherStatistics(struct aws_array_list *) {}
129 
130  public:
132  struct aws_channel_handler *SeatForCInterop(const std::shared_ptr<ChannelHandler> &selfRef);
133 
137  bool ChannelsThreadIsCallersThread() const;
138 
144  void ShutDownChannel(int errorCode);
145 
150  void ScheduleTask(std::function<void(TaskStatus)> &&task);
151 
157  void ScheduleTask(std::function<void(TaskStatus)> &&task, std::chrono::nanoseconds run_in);
158 
159  protected:
160  ChannelHandler(Allocator *allocator = ApiAllocator());
161 
165  struct aws_io_message *AcquireMessageFromPool(MessageType messageType, size_t sizeHint);
166 
172  struct aws_io_message *AcquireMaxSizeMessageForWrite();
173 
179  bool SendMessage(struct aws_io_message *message, ChannelDirection direction);
180 
185  bool IncrementUpstreamReadWindow(size_t windowUpdateSize);
186 
191  void OnShutdownComplete(ChannelDirection direction, int errorCode, bool freeScarceResourcesImmediately);
192 
198  size_t DownstreamReadWindow() const;
199 
204  size_t UpstreamMessageOverhead() const;
205 
206  struct aws_channel_slot *GetSlot() const;
207 
208  struct aws_channel_handler m_handler;
210 
211  private:
212  std::shared_ptr<ChannelHandler> m_selfReference;
213  static struct aws_channel_handler_vtable s_vtable;
214 
215  static void s_Destroy(struct aws_channel_handler *handler);
216  static int s_ProcessReadMessage(
217  struct aws_channel_handler *,
218  struct aws_channel_slot *,
219  struct aws_io_message *);
220  static int s_ProcessWriteMessage(
221  struct aws_channel_handler *,
222  struct aws_channel_slot *,
223  struct aws_io_message *);
224  static int s_IncrementReadWindow(struct aws_channel_handler *, struct aws_channel_slot *, size_t size);
225  static int s_ProcessShutdown(
226  struct aws_channel_handler *,
227  struct aws_channel_slot *,
228  enum aws_channel_direction,
229  int errorCode,
230  bool freeScarceResourcesImmediately);
231  static size_t s_InitialWindowSize(struct aws_channel_handler *);
232  static size_t s_MessageOverhead(struct aws_channel_handler *);
233  static void s_ResetStatistics(struct aws_channel_handler *);
234  static void s_GatherStatistics(struct aws_channel_handler *, struct aws_array_list *statsList);
235  };
236  } // namespace Io
237  } // namespace Crt
238 } // namespace Aws
Aws::Crt::Io::ChannelHandler::ResetStatistics
virtual void ResetStatistics()
Definition: ChannelHandler.h:122
Aws::Crt::Io::ChannelHandler::GatherStatistics
virtual void GatherStatistics(struct aws_array_list *)
Definition: ChannelHandler.h:128
Aws::Crt::Io::ChannelHandler::operator=
ChannelHandler & operator=(const ChannelHandler &)=delete
Aws::Crt::Io::ChannelHandler::ChannelHandler
ChannelHandler(const ChannelHandler &)=delete
Aws::Crt::ApiAllocator
AWS_CRT_CPP_API Allocator * ApiAllocator() noexcept
Definition: Allocator.cpp:24
Aws::Crt::Io::ChannelDirection::Write
@ Write
Aws::Crt::Io::ChannelHandler::~ChannelHandler
virtual ~ChannelHandler()=default
Aws::Crt::Io::ChannelHandler::MessageOverhead
virtual size_t MessageOverhead()=0
Aws::Crt::Io::ChannelHandler::InitialWindowSize
virtual size_t InitialWindowSize()=0
Aws::Crt::Io::ChannelDirection
ChannelDirection
Definition: ChannelHandler.h:24
Aws::Crt::Io::TaskStatus::RunReady
@ RunReady
Aws::Crt::Io::ChannelHandler
Definition: ChannelHandler.h:47
Aws::Crt::Io::ChannelHandler::ProcessWriteMessage
virtual int ProcessWriteMessage(struct aws_io_message *message)=0
Aws::Crt::Io::MessageType
MessageType
Definition: ChannelHandler.h:30
Aws::Crt::Io::MessageType::ApplicationData
@ ApplicationData
Aws::Crt::Io::TaskStatus
TaskStatus
Definition: ChannelHandler.h:35
Aws::Crt::Io::ChannelHandler::ProcessShutdown
virtual void ProcessShutdown(ChannelDirection dir, int errorCode, bool freeScarceResourcesImmediately)=0
Types.h
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Aws
Definition: Allocator.h:11
Aws::Crt::Allocator
aws_allocator Allocator
Definition: Allocator.h:14
Exports.h
Aws::Crt::Io::ChannelDirection::Read
@ Read
Aws::Crt::Io::ChannelHandler::IncrementReadWindow
virtual int IncrementReadWindow(size_t size)=0
Aws::Crt::Io::ChannelHandler::ProcessReadMessage
virtual int ProcessReadMessage(struct aws_io_message *message)=0
Aws::Crt::Io::ChannelHandler::m_allocator
Allocator * m_allocator
Definition: ChannelHandler.h:209