AWS SDK for C++  1.9.125
AWS SDK for C++
EventStreamHandler.h
Go to the documentation of this file.
1 
6 #pragma once
7 
16 #include <cassert>
17 
18 namespace Aws
19 {
20  namespace Utils
21  {
22  namespace Event
23  {
29  {
30  public:
32  m_failure(false), m_internalError(EventStreamErrors::EVENT_STREAM_NO_ERROR), m_headersBytesReceived(0), m_payloadBytesReceived(0)
33  {}
34 
35  virtual ~EventStreamHandler() = default;
36 
40  inline operator bool() const { return !m_failure; }
41 
45  inline void SetFailure() { m_failure = true; }
46 
50  inline virtual void Reset()
51  {
52  m_failure = false;
53  m_internalError = EventStreamErrors::EVENT_STREAM_NO_ERROR;
54  m_headersBytesReceived = 0;
55  m_payloadBytesReceived = 0;
56 
57  m_message.Reset();
58  }
59 
63  inline void SetInternalError(int errorCode = 0)
64  {
65  m_internalError = static_cast<EventStreamErrors>(errorCode);
66  }
67 
72  {
73  return m_internalError;
74  }
75 
82  inline virtual bool IsMessageCompleted()
83  {
84  return m_message.GetHeadersLength() == m_headersBytesReceived && m_message.GetPayloadLength() == m_payloadBytesReceived;
85  }
86 
90  inline virtual void SetMessageMetadata(size_t totalLength, size_t headersLength, size_t payloadLength)
91  {
92  m_message.SetTotalLength(totalLength);
93  m_message.SetHeadersLength(headersLength);
94  m_message.SetPayloadLength(payloadLength);
95  assert(totalLength == 12/*prelude length*/ + headersLength + payloadLength + 4/*message crc length*/);
96  if (totalLength != headersLength + payloadLength + 16)
97  {
98  AWS_LOG_WARN("EventStreamHandler", "Message total length mismatch.");
99  }
100  }
101 
105  inline virtual void WriteMessageEventPayload(const unsigned char* data, size_t dataLength)
106  {
107  m_message.WriteEventPayload(data, dataLength);
108  m_payloadBytesReceived += dataLength;
109  }
110 
114  inline virtual Aws::Vector<unsigned char>&& GetEventPayloadWithOwnership() { return m_message.GetEventPayloadWithOwnership(); }
115 
119  inline virtual Aws::String GetEventPayloadAsString() { return m_message.GetEventPayloadAsString(); }
120 
124  inline virtual void InsertMessageEventHeader(const String& eventHeaderName, size_t eventHeaderLength, const Aws::Utils::Event::EventHeaderValue& eventHeaderValue)
125  {
126  m_message.InsertEventHeader(eventHeaderName, eventHeaderValue);
127  m_headersBytesReceived += eventHeaderLength;
128  }
129 
130  inline virtual const Aws::Utils::Event::EventHeaderValueCollection& GetEventHeaders() { return m_message.GetEventHeaders(); }
131 
136  virtual void OnEvent() = 0;
137 
138  private:
139  bool m_failure;
140  EventStreamErrors m_internalError;
141  size_t m_headersBytesReceived;
142  size_t m_payloadBytesReceived;
143  Aws::Utils::Event::Message m_message;
144  };
145  }
146  }
147 }
EventHeader.h
Aws::Utils::Event::EventStreamHandler::Reset
virtual void Reset()
Definition: EventStreamHandler.h:50
Aws::Utils::Event::EventStreamErrors
EventStreamErrors
Definition: EventStreamErrors.h:21
Aws::Utils::Event::EventHeaderValueCollection
Aws::Map< Aws::String, EventHeaderValue > EventHeaderValueCollection
Definition: EventHeader.h:317
Aws::Utils::Event::EventStreamHandler::~EventStreamHandler
virtual ~EventStreamHandler()=default
Aws::Utils::Event::EventStreamHandler::WriteMessageEventPayload
virtual void WriteMessageEventPayload(const unsigned char *data, size_t dataLength)
Definition: EventStreamHandler.h:105
AWS_CORE_API
#define AWS_CORE_API
Definition: Core_EXPORTS.h:25
Aws::Utils::Event::EventStreamHandler::OnEvent
virtual void OnEvent()=0
Aws::IoTWireless::Model::Event
Event
Definition: Event.h:17
EventStreamErrors.h
Aws::Utils::Event::EventStreamHandler::GetInternalError
EventStreamErrors GetInternalError()
Definition: EventStreamHandler.h:71
Aws::Utils::Event::EventStreamHandler::SetMessageMetadata
virtual void SetMessageMetadata(size_t totalLength, size_t headersLength, size_t payloadLength)
Definition: EventStreamHandler.h:90
Aws::Utils::Event::EventStreamHandler::InsertMessageEventHeader
virtual void InsertMessageEventHeader(const String &eventHeaderName, size_t eventHeaderLength, const Aws::Utils::Event::EventHeaderValue &eventHeaderValue)
Definition: EventStreamHandler.h:124
Aws::Utils::Event::EventStreamHandler::GetEventHeaders
virtual const Aws::Utils::Event::EventHeaderValueCollection & GetEventHeaders()
Definition: EventStreamHandler.h:130
Aws::Vector
std::vector< T, Aws::Allocator< T > > Vector
Definition: AWSVector.h:17
AWSStreamFwd.h
Aws::Utils::Event::EventStreamHandler::GetEventPayloadWithOwnership
virtual Aws::Vector< unsigned char > && GetEventPayloadWithOwnership()
Definition: EventStreamHandler.h:114
Aws::Utils::Event::EventHeaderValue
Definition: EventHeader.h:29
EventMessage.h
Aws::Utils::Event::EventStreamHandler::SetFailure
void SetFailure()
Definition: EventStreamHandler.h:45
AWS_LOG_WARN
#define AWS_LOG_WARN(tag,...)
Definition: LogMacros.h:79
Aws::Utils::Event::EventStreamHandler::IsMessageCompleted
virtual bool IsMessageCompleted()
Definition: EventStreamHandler.h:82
Aws::Utils::Event::EventStreamHandler::SetInternalError
void SetInternalError(int errorCode=0)
Definition: EventStreamHandler.h:63
Aws::Utils::Event::EventStreamHandler
Definition: EventStreamHandler.h:29
AWSStringStream.h
AWSString.h
Aws::Utils::Event::Message
Definition: EventMessage.h:30
Core_EXPORTS.h
Aws::Utils::Event::EventStreamHandler::GetEventPayloadAsString
virtual Aws::String GetEventPayloadAsString()
Definition: EventStreamHandler.h:119
Aws::Utils::Event::EventStreamHandler::EventStreamHandler
EventStreamHandler()
Definition: EventStreamHandler.h:31
Aws
Definition: AccessManagementClient.h:15
HttpTypes.h
Aws::String
std::basic_string< char, std::char_traits< char >, Aws::Allocator< char > > String
Definition: AWSString.h:97