-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathHttpWriteDataStreamBuf.cpp
More file actions
188 lines (158 loc) · 6.05 KB
/
Copy pathHttpWriteDataStreamBuf.cpp
File metadata and controls
188 lines (158 loc) · 6.05 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/core/http/HttpClient.h>
#include <aws/core/utils/stream/HttpWriteDataStreamBuf.h>
#include <chrono>
#include <utility>
namespace {
const char* WRITE_DATA_BUF_LOG_NAME = "HttpWriteDataStreamBuf";
}
Aws::Utils::Stream::HttpWriteDataStreamBuf::HttpWriteDataStreamBuf(const std::shared_ptr<Aws::Http::HttpClient>& client,
size_t bufferLength,
size_t requestTimeoutMs)
: m_client{client}, m_buffer{bufferLength} {
if (requestTimeoutMs > 0) {
m_hasDeadline = true;
m_deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(requestTimeoutMs);
}
ResetPutArea();
}
void Aws::Utils::Stream::HttpWriteDataStreamBuf::ResetPutArea() {
auto* base = reinterpret_cast<char*>(m_buffer.GetUnderlyingData());
setp(base, base + m_buffer.GetLength());
}
Aws::Utils::Stream::HttpWriteDataStreamBuf::~HttpWriteDataStreamBuf() {
Close();
WaitForStreamComplete();
}
Aws::Crt::Optional<Aws::Client::AWSError<Aws::Client::CoreErrors>> Aws::Utils::Stream::HttpWriteDataStreamBuf::Initialize(
const std::shared_ptr<Aws::Http::HttpRequest>& request) {
std::mutex acquireMutex;
std::condition_variable acquireCondition;
bool connectionAcquired = false;
Aws::Crt::Optional<Aws::Client::AWSError<Aws::Client::CoreErrors>> error;
// Callback captures stack locals by reference. This is safe because
// we block on acquireCondition.wait() below, keeping the stack frame
// alive until the callback has fired.
m_client->AcquireConnection(request,
[&acquireMutex, &acquireCondition, &connectionAcquired, &error, this](
std::shared_ptr<Aws::Http::Connection> connection, int errorCode) -> void {
{
std::unique_lock<std::mutex> const lock{acquireMutex};
if (errorCode != AWS_ERROR_SUCCESS) {
error =
Aws::Client::AWSError<Aws::Client::CoreErrors>(Aws::Client::CoreErrors::NETWORK_CONNECTION, false);
}
m_connection = std::move(connection);
connectionAcquired = true;
}
acquireCondition.notify_all();
});
std::unique_lock<std::mutex> lock{acquireMutex};
acquireCondition.wait(lock, [&connectionAcquired]() -> bool { return connectionAcquired; });
if (error.has_value()) {
return error;
}
m_stream = m_connection->NewClientStream(request, [this](int errorCode) -> void {
{
std::unique_lock<std::mutex> const lock{m_shutdownMutex};
if (errorCode != AWS_ERROR_SUCCESS) {
AWS_LOGSTREAM_ERROR(WRITE_DATA_BUF_LOG_NAME, "Stream completed with error: " << errorCode);
m_writeError = true;
}
m_streamComplete = true;
}
m_shutdownCondition.notify_all();
});
if (!m_stream) {
return Aws::Client::AWSError<Aws::Client::CoreErrors>(Aws::Client::CoreErrors::NETWORK_CONNECTION, false);
}
m_stream->Activate();
m_state = STATE::INITIALIZED;
return error;
}
void Aws::Utils::Stream::HttpWriteDataStreamBuf::Close() {
{
std::unique_lock<std::mutex> const lock{m_shutdownMutex};
if (m_state != STATE::INITIALIZED) {
return;
}
m_state = STATE::SHUT_DOWN;
}
SendBuffer(/*endStream=*/true);
}
std::shared_ptr<Aws::Http::HttpResponse> Aws::Utils::Stream::HttpWriteDataStreamBuf::GetResponse() const {
assert(m_stream);
return m_stream->GetResponse();
}
void Aws::Utils::Stream::HttpWriteDataStreamBuf::WaitForStreamComplete() {
std::unique_lock<std::mutex> lock{m_shutdownMutex};
if (m_state == STATE::UNINITIALIZED) {
return;
}
m_shutdownCondition.wait(lock, [this]() -> bool { return m_streamComplete; });
}
std::streambuf::int_type Aws::Utils::Stream::HttpWriteDataStreamBuf::overflow(std::streambuf::int_type c) {
if (traits_type::eq_int_type(c, traits_type::eof())) {
return traits_type::not_eof(c);
}
if (!SendBuffer(false)) {
return traits_type::eof();
}
*pptr() = traits_type::to_char_type(c);
pbump(1);
return c;
}
std::streamsize Aws::Utils::Stream::HttpWriteDataStreamBuf::xsputn(const char* s, std::streamsize n) {
std::streamsize written = 0;
while (written < n) {
std::streamsize const space = epptr() - pptr();
std::streamsize const chunk = std::min(space, n - written);
std::memcpy(pptr(), s + written, static_cast<size_t>(chunk));
pbump(static_cast<int>(chunk));
written += chunk;
if (pptr() == epptr()) {
if (!SendBuffer(false)) {
return written;
}
}
}
return written;
}
int Aws::Utils::Stream::HttpWriteDataStreamBuf::sync() {
return SendBuffer(false) ? 0 : -1;
}
bool Aws::Utils::Stream::HttpWriteDataStreamBuf::SendBuffer(bool endStream) {
if (!endStream && (m_writeError || m_state != STATE::INITIALIZED)) {
return false;
}
auto data = Aws::MakeShared<Aws::StringStream>(WRITE_DATA_BUF_LOG_NAME);
data->write(pbase(), pptr() - pbase());
m_writeInProgress = true;
m_stream->WriteData(
data,
[this](int errorCode) -> void {
std::unique_lock<std::mutex> const lock{m_writeMutex};
m_writeInProgress = false;
m_writeError = (errorCode != AWS_ERROR_SUCCESS);
m_writeComplete.notify_one();
},
endStream);
std::unique_lock<std::mutex> lock{m_writeMutex};
if (m_hasDeadline) {
bool completed = m_writeComplete.wait_until(lock, m_deadline,
[this]() -> bool { return !m_writeInProgress; });
if (!completed) {
m_writeError = true;
m_writeInProgress = false;
ResetPutArea();
return false;
}
} else {
m_writeComplete.wait(lock, [this]() -> bool { return !m_writeInProgress; });
}
ResetPutArea();
return !m_writeError;
}