aws-crt-cpp
C++ wrapper around the aws-c-* libraries. Provides Cross-Platform Transport Protocols and SSL/TLS implementations for C++.
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>
10 #include <aws/io/channel.h>
11 
12 #include <chrono>
13 #include <cstddef>
14 
15 struct aws_array_list;
16 struct aws_io_message;
17 
18 namespace Aws
19 {
20  namespace Crt
21  {
22  namespace Io
23  {
24  enum class ChannelDirection
25  {
26  Read,
27  Write,
28  };
29 
30  enum class MessageType
31  {
33  };
34 
42  {
43  public:
44  virtual ~ChannelHandler() = default;
45 
46  ChannelHandler(const ChannelHandler &) = delete;
47  ChannelHandler &operator=(const ChannelHandler &) = delete;
48 
49  protected:
63  virtual int ProcessReadMessage(struct aws_io_message *message) = 0;
64 
74  virtual int ProcessWriteMessage(struct aws_io_message *message) = 0;
75 
84  virtual int IncrementReadWindow(size_t size) = 0;
85 
98  virtual void ProcessShutdown(
99  ChannelDirection dir,
100  int errorCode,
101  bool freeScarceResourcesImmediately) = 0;
102 
106  virtual size_t InitialWindowSize() = 0;
107 
112  virtual size_t MessageOverhead() = 0;
113 
117  virtual void ResetStatistics() {};
118 
123  virtual void GatherStatistics(struct aws_array_list *) {}
124 
125  public:
127  struct aws_channel_handler *SeatForCInterop(const std::shared_ptr<ChannelHandler> &selfRef);
128 
132  bool ChannelsThreadIsCallersThread() const;
133 
139  void ShutDownChannel(int errorCode);
140 
145  void ScheduleTask(std::function<void(TaskStatus)> &&task);
146 
152  void ScheduleTask(std::function<void(TaskStatus)> &&task, std::chrono::nanoseconds run_in);
153 
154  protected:
155  ChannelHandler(Allocator *allocator = ApiAllocator());
156 
160  struct aws_io_message *AcquireMessageFromPool(MessageType messageType, size_t sizeHint);
161 
167  struct aws_io_message *AcquireMaxSizeMessageForWrite();
168 
174  bool SendMessage(struct aws_io_message *message, ChannelDirection direction);
175 
180  bool IncrementUpstreamReadWindow(size_t windowUpdateSize);
181 
186  void OnShutdownComplete(ChannelDirection direction, int errorCode, bool freeScarceResourcesImmediately);
187 
193  size_t DownstreamReadWindow() const;
194 
199  size_t UpstreamMessageOverhead() const;
200 
201  struct aws_channel_slot *GetSlot() const;
202 
203  struct aws_channel_handler m_handler;
205 
206  private:
207  std::shared_ptr<ChannelHandler> m_selfReference;
208  static struct aws_channel_handler_vtable s_vtable;
209 
210  static void s_Destroy(struct aws_channel_handler *handler);
211  static int s_ProcessReadMessage(
212  struct aws_channel_handler *,
213  struct aws_channel_slot *,
214  struct aws_io_message *);
215  static int s_ProcessWriteMessage(
216  struct aws_channel_handler *,
217  struct aws_channel_slot *,
218  struct aws_io_message *);
219  static int s_IncrementReadWindow(struct aws_channel_handler *, struct aws_channel_slot *, size_t size);
220  static int s_ProcessShutdown(
221  struct aws_channel_handler *,
222  struct aws_channel_slot *,
223  enum aws_channel_direction,
224  int errorCode,
225  bool freeScarceResourcesImmediately);
226  static size_t s_InitialWindowSize(struct aws_channel_handler *);
227  static size_t s_MessageOverhead(struct aws_channel_handler *);
228  static void s_ResetStatistics(struct aws_channel_handler *);
229  static void s_GatherStatistics(struct aws_channel_handler *, struct aws_array_list *statsList);
230  };
231  } // namespace Io
232  } // namespace Crt
233 } // namespace Aws
Definition: ChannelHandler.h:41
#define AWS_CRT_CPP_API
Definition: Exports.h:36
ChannelDirection
Definition: ChannelHandler.h:24
aws_allocator Allocator
Definition: Allocator.h:14
Allocator * m_allocator
Definition: ChannelHandler.h:204
MessageType
Definition: ChannelHandler.h:30
TaskStatus
Definition: EventLoopGroup.h:24
AWS_CRT_CPP_API Allocator * ApiAllocator() noexcept
Definition: Allocator.cpp:24
virtual void ResetStatistics()
Definition: ChannelHandler.h:117
virtual void GatherStatistics(struct aws_array_list *)
Definition: ChannelHandler.h:123
Definition: Allocator.h:10