AWS SDK for C++

AWS SDK for C++ Version 1.11.440

Loading...
Searching...
No Matches
EventStreamHandler.h
1
6#pragma once
7
8#include <aws/core/Core_EXPORTS.h>
9#include <aws/core/http/HttpTypes.h>
10#include <aws/core/utils/event/EventHeader.h>
11#include <aws/core/utils/event/EventMessage.h>
12#include <aws/core/utils/event/EventStreamErrors.h>
13#include <aws/core/utils/memory/stl/AWSString.h>
14#include <aws/core/utils/memory/stl/AWSStreamFwd.h>
15#include <aws/core/utils/memory/stl/AWSStringStream.h>
16#include <cassert>
17
18namespace Aws
19{
20 namespace Utils
21 {
22 namespace Event
23 {
25 {
28 };
29
34 class AWS_CORE_API EventStreamHandler
35 {
36 public:
38 m_failure(false), m_internalError(EventStreamErrors::EVENT_STREAM_NO_ERROR), m_headersBytesReceived(0), m_payloadBytesReceived(0), m_message()
39 {}
40
41 virtual ~EventStreamHandler() = default;
42
46 inline operator bool() const { return !m_failure; }
47
51 inline void SetFailure() { m_failure = true; }
52
56 inline virtual void Reset()
57 {
58 m_failure = false;
59 m_internalError = EventStreamErrors::EVENT_STREAM_NO_ERROR;
60 m_headersBytesReceived = 0;
61 m_payloadBytesReceived = 0;
62
63 m_message.Reset();
64 }
65
69 inline void SetInternalError(int errorCode = 0)
70 {
71 m_internalError = static_cast<EventStreamErrors>(errorCode);
72 }
73
78 {
79 return m_internalError;
80 }
81
88 inline virtual bool IsMessageCompleted()
89 {
90 return m_message.GetHeadersLength() == m_headersBytesReceived && m_message.GetPayloadLength() == m_payloadBytesReceived;
91 }
92
96 inline virtual void SetMessageMetadata(size_t totalLength, size_t headersLength, size_t payloadLength)
97 {
98 m_message.SetTotalLength(totalLength);
99 m_message.SetHeadersLength(headersLength);
100 m_message.SetPayloadLength(payloadLength);
101 assert(totalLength == 12/*prelude length*/ + headersLength + payloadLength + 4/*message crc length*/);
102 if (totalLength != headersLength + payloadLength + 16)
103 {
104 AWS_LOG_WARN("EventStreamHandler", "Message total length mismatch.");
105 }
106 }
107
111 inline virtual void WriteMessageEventPayload(const unsigned char* data, size_t dataLength)
112 {
113 m_message.WriteEventPayload(data, dataLength);
114 m_payloadBytesReceived += dataLength;
115 }
116
120 inline virtual Aws::Vector<unsigned char>&& GetEventPayloadWithOwnership() { return m_message.GetEventPayloadWithOwnership(); }
121
125 inline virtual Aws::String GetEventPayloadAsString() { return m_message.GetEventPayloadAsString(); }
126
130 inline virtual void InsertMessageEventHeader(const String& eventHeaderName, size_t eventHeaderLength, const Aws::Utils::Event::EventHeaderValue& eventHeaderValue)
131 {
132 m_message.InsertEventHeader(eventHeaderName, eventHeaderValue);
133 m_headersBytesReceived += eventHeaderLength;
134 }
135
136 inline virtual const Aws::Utils::Event::EventHeaderValueCollection& GetEventHeaders() { return m_message.GetEventHeaders(); }
137
139 {
141 using SrcT = Aws::Utils::Event::EventHeaderValueCollection::value_type;
142 using DstT = Http::HeaderValueCollection::value_type;
143 std::transform(m_message.GetEventHeaders().cbegin(), m_message.GetEventHeaders().cend(),
144 std::inserter(output, output.end()),
145 [](const SrcT& src)
146 {
147 return DstT(src.first, src.second.ToString());
148 });
149 return output;
150 }
151
156 virtual void OnEvent() = 0;
157
158 private:
159 bool m_failure;
160 EventStreamErrors m_internalError;
161 size_t m_headersBytesReceived;
162 size_t m_payloadBytesReceived;
164 };
165 }
166 }
167}
virtual void SetMessageMetadata(size_t totalLength, size_t headersLength, size_t payloadLength)
virtual const Http::HeaderValueCollection GetEventHeadersAsHttpHeaders() const
virtual Aws::Vector< unsigned char > && GetEventPayloadWithOwnership()
virtual void InsertMessageEventHeader(const String &eventHeaderName, size_t eventHeaderLength, const Aws::Utils::Event::EventHeaderValue &eventHeaderValue)
virtual void WriteMessageEventPayload(const unsigned char *data, size_t dataLength)
virtual const Aws::Utils::Event::EventHeaderValueCollection & GetEventHeaders()
Aws::Map< Aws::String, Aws::String > HeaderValueCollection
Definition HttpTypes.h:62
Aws::Map< Aws::String, EventHeaderValue > EventHeaderValueCollection
std::basic_string< char, std::char_traits< char >, Aws::Allocator< char > > String
Definition AWSString.h:97
std::vector< T, Aws::Allocator< T > > Vector
Definition AWSVector.h:17