1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/common/common.h>
#include <aws/core/utils/event/EventHeader.h>
#include <aws/core/utils/event/EventMessage.h>
#include <aws/core/utils/event/EventStreamDecoder.h>
#include <aws/core/utils/logging/LogMacros.h>
#include <aws/core/utils/UnreferencedParam.h>
#include <aws/core/utils/memory/AWSMemory.h>
namespace Aws
{
namespace Utils
{
namespace Event
{
static const char EVENT_STREAM_DECODER_CLASS_TAG[] = "Aws::Utils::Event::EventStreamDecoder";
EventStreamDecoder::EventStreamDecoder(EventStreamHandler* handler) : m_eventStreamHandler(handler)
{
aws_event_stream_streaming_decoder_init(&m_decoder,
get_aws_allocator(),
onPayloadSegment,
onPreludeReceived,
onHeaderReceived,
onError,
(void*)handler);
}
EventStreamDecoder::~EventStreamDecoder()
{
aws_event_stream_streaming_decoder_clean_up(&m_decoder);
}
void EventStreamDecoder::Pump(const ByteBuffer& data)
{
Pump(data, data.GetLength());
}
void EventStreamDecoder::Pump(const ByteBuffer& data, size_t length)
{
aws_byte_buf dataBuf = aws_byte_buf_from_array(static_cast<uint8_t*>(data.GetUnderlyingData()), length);
aws_event_stream_streaming_decoder_pump(&m_decoder, &dataBuf);
}
void EventStreamDecoder::Reset()
{
m_eventStreamHandler->Reset();
}
void EventStreamDecoder::ResetEventStreamHandler(EventStreamHandler* handler)
{
aws_event_stream_streaming_decoder_init(&m_decoder, get_aws_allocator(),
onPayloadSegment,
onPreludeReceived,
onHeaderReceived,
onError,
reinterpret_cast<void *>(handler));
}
void EventStreamDecoder::onPayloadSegment(
aws_event_stream_streaming_decoder* decoder,
aws_byte_buf* payload,
int8_t isFinalSegment,
void* context)
{
AWS_UNREFERENCED_PARAM(decoder);
auto handler = static_cast<EventStreamHandler*>(context);
assert(handler);
if (!handler)
{
AWS_LOGSTREAM_ERROR(EVENT_STREAM_DECODER_CLASS_TAG, "Payload received, but handler is null.");
return;
}
handler->WriteMessageEventPayload(static_cast<unsigned char*>(payload->buffer), payload->len);
// Complete payload received
if (isFinalSegment == 1)
{
assert(handler->IsMessageCompleted());
handler->OnEvent();
handler->Reset();
}
}
void EventStreamDecoder::onPreludeReceived(
aws_event_stream_streaming_decoder* decoder,
aws_event_stream_message_prelude* prelude,
void* context)
{
AWS_UNREFERENCED_PARAM(decoder);
auto handler = static_cast<EventStreamHandler*>(context);
handler->Reset();
//Encounter internal error in prelude received.
//This error will be handled by OnError callback function later.
if (prelude->total_len < prelude->headers_len + 16)
{
return;
}
handler->SetMessageMetadata(prelude->total_len, prelude->headers_len,
prelude->total_len - prelude->headers_len - 4/*total byte-length*/ - 4/*headers byte-length*/ - 4/*prelude crc*/ - 4/*message crc*/);
AWS_LOGSTREAM_TRACE(EVENT_STREAM_DECODER_CLASS_TAG, "Message received, the expected length of the message is: " << prelude->total_len <<
" bytes, and the expected length of the header is: " << prelude->headers_len << " bytes");
//Handle empty message
//if (handler->m_message.GetHeadersLength() == 0 && handler->m_message.GetPayloadLength() == 0)
if (handler->IsMessageCompleted())
{
handler->OnEvent();
handler->Reset();
}
}
void EventStreamDecoder::onHeaderReceived(
aws_event_stream_streaming_decoder* decoder,
aws_event_stream_message_prelude* prelude,
aws_event_stream_header_value_pair* header,
void* context)
{
AWS_UNREFERENCED_PARAM(decoder);
AWS_UNREFERENCED_PARAM(prelude);
auto handler = static_cast<EventStreamHandler*>(context);
assert(handler);
if (!handler)
{
AWS_LOGSTREAM_ERROR(EVENT_STREAM_DECODER_CLASS_TAG, "Header received, but handler is null.");
return;
}
// The length of a header = 1 byte (to represent the length of header name) + length of header name + 1 byte (to represent header type)
// + 2 bytes (to represent length of header value) + length of header value
handler->InsertMessageEventHeader(Aws::String(header->header_name, header->header_name_len),
1 + header->header_name_len + 1 + 2 + header->header_value_len, EventHeaderValue(header));
// Handle messages only have headers, but without payload.
//if (handler->m_message.GetHeadersLength() == handler->m_headersBytesReceived() && handler->m_message.GetPayloadLength() == 0)
if (handler->IsMessageCompleted())
{
handler->OnEvent();
handler->Reset();
}
}
void EventStreamDecoder::onError(
aws_event_stream_streaming_decoder* decoder,
aws_event_stream_message_prelude* prelude,
int error_code,
const char* message,
void* context)
{
AWS_UNREFERENCED_PARAM(decoder);
AWS_UNREFERENCED_PARAM(prelude);
auto handler = static_cast<EventStreamHandler*>(context);
handler->SetFailure();
handler->SetInternalError(error_code);
handler->WriteMessageEventPayload(reinterpret_cast<const unsigned char*>(message), strlen(message));
handler->OnEvent();
}
} // namespace Event
} // namespace Utils
} // namespace Aws
|