Skip to content

Commit 660b21c

Browse files
committed
Improving implementation of _complete to be less complex.
1 parent e1b7e9f commit 660b21c

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

Release/src/http/common/http_msg.cpp

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -165,53 +165,41 @@ size_t http_msg_base::_get_content_length()
165165
return 0;
166166
}
167167

168-
/// <summary>
169-
/// Completes this message
170-
/// </summary>
171-
void http_msg_base::_complete(utility::size64_t body_size, const std::exception_ptr &exceptionPtr)
168+
// Helper function to inline continuation if possible.
169+
struct inline_continuation
172170
{
173-
const auto hasBody = outstream().is_valid();
174-
const auto hasException = exceptionPtr != std::exception_ptr();
175-
const auto &completionEvent = _get_data_available();
176-
auto closeTask = pplx::task_from_result();
177-
178-
if (hasBody)
171+
inline_continuation(pplx::task<void> &prev, const std::function<void(pplx::task<void>)> &next) : m_prev(prev), m_next(next) {}
172+
~inline_continuation()
179173
{
180-
if (hasException)
174+
if (m_prev.is_done())
181175
{
182-
closeTask = outstream().close(exceptionPtr);
176+
m_next(m_prev);
183177
}
184-
else if (m_default_outstream)
178+
else
185179
{
186-
closeTask = outstream().close();
180+
m_prev.then(m_next);
187181
}
188182
}
183+
pplx::task<void> & m_prev;
184+
std::function<void(pplx::task<void>)> m_next;
185+
private:
186+
inline_continuation(const inline_continuation &);
187+
inline_continuation &operator=(const inline_continuation &);
188+
};
189189

190-
if (hasException)
191-
{
192-
auto setException = [completionEvent, exceptionPtr](pplx::task<void> t)
193-
{
194-
// If closing stream throws an exception ignore since we already have an error.
195-
try { t.get(); } catch (...) {}
196-
completionEvent.set_exception(exceptionPtr);
197-
pplx::create_task(completionEvent).then([](pplx::task<utility::size64_t> t)
198-
{
199-
try { t.get(); } catch (...) {}
200-
});
201-
};
190+
void http_msg_base::_complete(utility::size64_t body_size, const std::exception_ptr &exceptionPtr)
191+
{
192+
const auto &completionEvent = _get_data_available();
193+
auto closeTask = pplx::task_from_result();
202194

203-
if (closeTask.is_done())
204-
{
205-
setException(closeTask);
206-
}
207-
else
195+
if (exceptionPtr == std::exception_ptr())
196+
{
197+
if (m_default_outstream)
208198
{
209-
closeTask.then(setException);
199+
closeTask = outstream().close();
210200
}
211-
}
212-
else
213-
{
214-
auto setBodySize = [completionEvent, body_size](pplx::task<void> t)
201+
202+
inline_continuation(closeTask, [completionEvent, body_size](pplx::task<void> t)
215203
{
216204
try
217205
{
@@ -224,19 +212,31 @@ void http_msg_base::_complete(utility::size64_t body_size, const std::exception_
224212
completionEvent.set_exception(std::current_exception());
225213
pplx::create_task(completionEvent).then([](pplx::task<utility::size64_t> t)
226214
{
227-
try { t.get(); } catch (...) {}
215+
try { t.get(); }
216+
catch (...) {}
228217
});
229218
}
230-
};
231-
232-
if (closeTask.is_done())
219+
});
220+
}
221+
else
222+
{
223+
if (outstream().is_valid())
233224
{
234-
setBodySize(closeTask);
225+
closeTask = outstream().close(exceptionPtr);
235226
}
236-
else
227+
228+
inline_continuation(closeTask, [completionEvent, exceptionPtr](pplx::task<void> t)
237229
{
238-
closeTask.then(setBodySize);
239-
}
230+
// If closing stream throws an exception ignore since we already have an error.
231+
try { t.get(); }
232+
catch (...) {}
233+
completionEvent.set_exception(exceptionPtr);
234+
pplx::create_task(completionEvent).then([](pplx::task<utility::size64_t> t)
235+
{
236+
try { t.get(); }
237+
catch (...) {}
238+
});
239+
});
240240
}
241241
}
242242

0 commit comments

Comments
 (0)