@@ -165,53 +165,41 @@ size_t http_msg_base::_get_content_length()
165
165
return 0 ;
166
166
}
167
167
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
172
170
{
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 ()
179
173
{
180
- if (hasException )
174
+ if (m_prev. is_done () )
181
175
{
182
- closeTask = outstream (). close (exceptionPtr );
176
+ m_next (m_prev );
183
177
}
184
- else if (m_default_outstream)
178
+ else
185
179
{
186
- closeTask = outstream (). close ( );
180
+ m_prev. then (m_next );
187
181
}
188
182
}
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
+ };
189
189
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 ();
202
194
203
- if (closeTask.is_done ())
204
- {
205
- setException (closeTask);
206
- }
207
- else
195
+ if (exceptionPtr == std::exception_ptr ())
196
+ {
197
+ if (m_default_outstream)
208
198
{
209
- closeTask. then (setException );
199
+ closeTask = outstream (). close ( );
210
200
}
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)
215
203
{
216
204
try
217
205
{
@@ -224,19 +212,31 @@ void http_msg_base::_complete(utility::size64_t body_size, const std::exception_
224
212
completionEvent.set_exception (std::current_exception ());
225
213
pplx::create_task (completionEvent).then ([](pplx::task<utility::size64_t > t)
226
214
{
227
- try { t.get (); } catch (...) {}
215
+ try { t.get (); }
216
+ catch (...) {}
228
217
});
229
218
}
230
- };
231
-
232
- if (closeTask.is_done ())
219
+ });
220
+ }
221
+ else
222
+ {
223
+ if (outstream ().is_valid ())
233
224
{
234
- setBodySize ( closeTask);
225
+ closeTask = outstream (). close (exceptionPtr );
235
226
}
236
- else
227
+
228
+ inline_continuation (closeTask, [completionEvent, exceptionPtr](pplx::task<void > t)
237
229
{
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
+ });
240
240
}
241
241
}
242
242
0 commit comments