blob: 26c92eaafdf83dab9ceea97bd1e81142947119a4 (
plain) (
blame)
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
|
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/core/utils/stream/ResponseStream.h>
#include <aws/core/utils/memory/stl/AWSStringStream.h>
#include <aws/core/utils/logging/LogMacros.h>
#if defined(_GLIBCXX_FULLY_DYNAMIC_STRING) && _GLIBCXX_FULLY_DYNAMIC_STRING == 0 && defined(__ANDROID__)
#include <aws/core/utils/stream/SimpleStreamBuf.h>
using DefaultStreamBufType = Aws::Utils::Stream::SimpleStreamBuf;
#else
using DefaultStreamBufType = Aws::StringBuf;
#endif
using namespace Aws::Utils::Stream;
const int ResponseStream::ResponseStream::xindex = std::ios_base::xalloc();
ResponseStream::ResponseStream(void) :
m_underlyingStream(nullptr)
{
}
ResponseStream::ResponseStream(Aws::IOStream* underlyingStreamToManage) :
m_underlyingStream(underlyingStreamToManage)
{
RegisterStream();
}
ResponseStream::ResponseStream(const Aws::IOStreamFactory& factory) :
m_underlyingStream(factory())
{
RegisterStream();
}
ResponseStream::ResponseStream(ResponseStream&& toMove) : m_underlyingStream(toMove.m_underlyingStream)
{
toMove.DeregisterStream();
toMove.m_underlyingStream = nullptr;
RegisterStream();
}
ResponseStream& ResponseStream::operator=(ResponseStream&& toMove)
{
if(m_underlyingStream == toMove.m_underlyingStream)
{
return *this;
}
ReleaseStream();
toMove.DeregisterStream();
m_underlyingStream = toMove.m_underlyingStream;
toMove.m_underlyingStream = nullptr;
RegisterStream();
return *this;
}
Aws::IOStream& ResponseStream::GetUnderlyingStream() const
{
if (!m_underlyingStream)
{
assert(m_underlyingStream);
AWS_LOGSTREAM_FATAL("ResponseStream", "Unexpected nullptr m_underlyingStream");
static DefaultUnderlyingStream fallbackStream; // we are already in UB, let's just not crash existing apps
return fallbackStream;
}
return *m_underlyingStream;
}
ResponseStream::~ResponseStream()
{
ReleaseStream();
}
void ResponseStream::ReleaseStream()
{
if (m_underlyingStream)
{
DeregisterStream();
Aws::Delete(m_underlyingStream);
}
m_underlyingStream = nullptr;
}
void ResponseStream::RegisterStream()
{
if (m_underlyingStream)
{
ResponseStream* pThat = static_cast<ResponseStream*>(m_underlyingStream->pword(ResponseStream::xindex));
if (pThat != nullptr)
{
// callback is already registered
assert(pThat != this); // Underlying stream must not be owned by more than one ResponseStream
}
else
{
m_underlyingStream->register_callback(ResponseStream::StreamCallback, ResponseStream::xindex);
}
m_underlyingStream->pword(ResponseStream::xindex) = this;
}
}
void ResponseStream::DeregisterStream()
{
if (m_underlyingStream)
{
assert(static_cast<ResponseStream*>(m_underlyingStream->pword(ResponseStream::xindex)) == this); // Attempt to deregister another ResponseStream's stream
m_underlyingStream->pword(ResponseStream::xindex) = nullptr; // ios does not support deregister, so just erasing the context
}
}
void ResponseStream::StreamCallback(Aws::IOStream::event evt, std::ios_base& stream, int idx)
{
if (evt == std::ios_base::erase_event)
{
ResponseStream* pThis = static_cast<ResponseStream*>(stream.pword(idx));
if (pThis)
{
// m_underlyingStream is being destructed, let's avoid double destruction or having a dangling pointer
pThis->m_underlyingStream = nullptr;
}
}
}
static const char *DEFAULT_STREAM_TAG = "DefaultUnderlyingStream";
DefaultUnderlyingStream::DefaultUnderlyingStream() :
Base( Aws::New< DefaultStreamBufType >( DEFAULT_STREAM_TAG ) )
{}
DefaultUnderlyingStream::DefaultUnderlyingStream(Aws::UniquePtr<std::streambuf> buf) :
Base(buf.release())
{}
DefaultUnderlyingStream::~DefaultUnderlyingStream()
{
if( rdbuf() )
{
Aws::Delete( rdbuf() );
}
}
static const char* RESPONSE_STREAM_FACTORY_TAG = "ResponseStreamFactory";
Aws::IOStream* Aws::Utils::Stream::DefaultResponseStreamFactoryMethod()
{
return Aws::New<Aws::Utils::Stream::DefaultUnderlyingStream>(RESPONSE_STREAM_FACTORY_TAG);
}
|