Skip to content

Commit 1bcf079

Browse files
authored
Handling exceptions from WinRT APIs calls seperately (#189)
1 parent a5fe486 commit 1bcf079

1 file changed

Lines changed: 77 additions & 61 deletions

File tree

Source/WebSocket/WinRT/winrt_websocket.cpp

Lines changed: 77 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -187,45 +187,53 @@ try
187187
websocketTask->m_messageWebSocket->Closed += ref new TypedEventHandler<IWebSocket^, WebSocketClosedEventArgs^>(websocketTask->m_context, &ReceiveContext::OnClosed);
188188

189189
HC_TRACE_INFORMATION(WEBSOCKET, "Websocket [ID %llu]: connecting to %s", websocket->id, websocket->uri.c_str());
190-
websocketTask->m_connectAsyncOp = websocketTask->m_messageWebSocket->ConnectAsync(cxUri);
191190

192-
websocketTask->m_connectAsyncOp->Completed = ref new AsyncActionCompletedHandler(
193-
[websocket, websocketTask, asyncBlock](
194-
Windows::Foundation::IAsyncAction^ asyncOp,
195-
Windows::Foundation::AsyncStatus status)
191+
try
196192
{
197-
UNREFERENCED_PARAMETER(status);
198-
try
193+
websocketTask->m_connectAsyncOp = websocketTask->m_messageWebSocket->ConnectAsync(cxUri);
194+
195+
websocketTask->m_connectAsyncOp->Completed = ref new AsyncActionCompletedHandler(
196+
[websocket, websocketTask, asyncBlock](
197+
Windows::Foundation::IAsyncAction^ asyncOp,
198+
Windows::Foundation::AsyncStatus status)
199199
{
200-
websocketTask->m_messageDataWriter = ref new DataWriter(websocketTask->m_messageWebSocket->OutputStream);
201-
if (status == Windows::Foundation::AsyncStatus::Error)
200+
UNREFERENCED_PARAMETER(status);
201+
try
202+
{
203+
websocketTask->m_messageDataWriter = ref new DataWriter(websocketTask->m_messageWebSocket->OutputStream);
204+
if (status == Windows::Foundation::AsyncStatus::Error)
205+
{
206+
websocketTask->m_connectAsyncOpResult = E_FAIL;
207+
}
208+
else
209+
{
210+
websocketTask->m_connectAsyncOpResult = S_OK;
211+
}
212+
}
213+
catch (Platform::Exception^ e)
214+
{
215+
websocketTask->m_connectAsyncOpResult = e->HResult;
216+
}
217+
catch (...)
202218
{
203219
websocketTask->m_connectAsyncOpResult = E_FAIL;
204220
}
221+
if (FAILED(websocketTask->m_connectAsyncOpResult))
222+
{
223+
HC_TRACE_ERROR(WEBSOCKET, "Websocket [ID %llu]: connect failed 0x%0.8x", websocket->id, websocketTask->m_connectAsyncOpResult);
224+
}
205225
else
206226
{
207-
websocketTask->m_connectAsyncOpResult = S_OK;
227+
HC_TRACE_INFORMATION(WEBSOCKET, "Websocket [ID %llu] connect complete", websocket->id);
208228
}
209-
}
210-
catch (Platform::Exception^ e)
211-
{
212-
websocketTask->m_connectAsyncOpResult = e->HResult;
213-
}
214-
catch (...)
215-
{
216-
websocketTask->m_connectAsyncOpResult = E_FAIL;
217-
}
218-
if (FAILED(websocketTask->m_connectAsyncOpResult))
219-
{
220-
HC_TRACE_ERROR(WEBSOCKET, "Websocket [ID %llu]: connect failed 0x%0.8x", websocket->id, websocketTask->m_connectAsyncOpResult);
221-
}
222-
else
223-
{
224-
HC_TRACE_INFORMATION(WEBSOCKET, "Websocket [ID %llu] connect complete", websocket->id);
225-
}
226229

227-
CompleteAsync(asyncBlock, websocketTask->m_connectAsyncOpResult, sizeof(WebSocketCompletionResult));
228-
});
230+
CompleteAsync(asyncBlock, websocketTask->m_connectAsyncOpResult, sizeof(WebSocketCompletionResult));
231+
});
232+
}
233+
catch (Platform::Exception^ e)
234+
{
235+
HC_TRACE_ERROR(WEBSOCKET, "Websocket [ID %llu]: ConnectAsync failed = 0x%0.8x", websocketTask->m_websocketHandle->id, e->HResult);
236+
}
229237

230238
return E_PENDING;
231239
}
@@ -350,44 +358,52 @@ try
350358
}
351359

352360
auto websocketTask = sendMsgContext->websocketTask;
353-
auto websocket = websocketTask->m_websocketHandle;
354-
HC_TRACE_INFORMATION(WEBSOCKET, "Websocket [ID %llu]: Send message executing", websocket->id);
355361

356-
auto msg = sendMsgContext->nextMessage;
357-
HC_TRACE_INFORMATION(WEBSOCKET, "Websocket [ID %llu]: Message [ID %llu] [%s]", websocket->id, msg->m_id, msg->m_message.c_str());
362+
try
363+
{
364+
auto websocket = websocketTask->m_websocketHandle;
365+
HC_TRACE_INFORMATION(WEBSOCKET, "Websocket [ID %llu]: Send message executing", websocket->id);
358366

359-
websocketTask->m_messageWebSocket->Control->MessageType = SocketMessageType::Utf8;
360-
unsigned char* uchar = reinterpret_cast<unsigned char*>(const_cast<char*>(msg->m_message.c_str()));
361-
websocketTask->m_messageDataWriter->WriteBytes(Platform::ArrayReference<unsigned char>(uchar, static_cast<unsigned int>(msg->m_message.length())));
367+
auto msg = sendMsgContext->nextMessage;
368+
HC_TRACE_INFORMATION(WEBSOCKET, "Websocket [ID %llu]: Message [ID %llu] [%s]", websocket->id, msg->m_id, msg->m_message.c_str());
362369

363-
msg->m_storeAsyncOp = websocketTask->m_messageDataWriter->StoreAsync();
370+
websocketTask->m_messageWebSocket->Control->MessageType = SocketMessageType::Utf8;
371+
unsigned char* uchar = reinterpret_cast<unsigned char*>(const_cast<char*>(msg->m_message.c_str()));
372+
websocketTask->m_messageDataWriter->WriteBytes(Platform::ArrayReference<unsigned char>(uchar, static_cast<unsigned int>(msg->m_message.length())));
364373

365-
msg->m_storeAsyncOp->Completed = ref new AsyncOperationCompletedHandler<unsigned int>(
366-
[websocketTask, msg, asyncBlock](Windows::Foundation::IAsyncOperation<unsigned int>^ asyncOp, Windows::Foundation::AsyncStatus status)
367-
{
368-
try
369-
{
370-
msg->m_storeAsyncOpStatus = status;
371-
unsigned int result = asyncOp->GetResults();
372-
HC_TRACE_INFORMATION(WEBSOCKET, "Websocket [ID %llu]: Message [ID %llu] send complete = %d", websocketTask->m_websocketHandle->id, msg->m_id, result);
373-
msg->m_storeAsyncResult = result;
374-
}
375-
catch (Platform::Exception^ ex)
376-
{
377-
msg->m_storeAsyncResult = ex->HResult;
378-
}
379-
catch (...)
380-
{
381-
msg->m_storeAsyncResult = E_FAIL;
382-
}
374+
msg->m_storeAsyncOp = websocketTask->m_messageDataWriter->StoreAsync();
383375

384-
if (FAILED(msg->m_storeAsyncResult))
376+
msg->m_storeAsyncOp->Completed = ref new AsyncOperationCompletedHandler<unsigned int>(
377+
[websocketTask, msg, asyncBlock](Windows::Foundation::IAsyncOperation<unsigned int>^ asyncOp, Windows::Foundation::AsyncStatus status)
385378
{
386-
HC_TRACE_ERROR(WEBSOCKET, "Websocket [ID %llu]: Message [ID %llu] send failed = 0x%0.8x", websocketTask->m_websocketHandle->id, msg->m_id, msg->m_storeAsyncResult);
387-
}
388-
CompleteAsync(asyncBlock, msg->m_storeAsyncResult, sizeof(WebSocketCompletionResult));
389-
MessageWebSocketSendMessage(websocketTask);
390-
});
379+
try
380+
{
381+
msg->m_storeAsyncOpStatus = status;
382+
unsigned int result = asyncOp->GetResults();
383+
HC_TRACE_INFORMATION(WEBSOCKET, "Websocket [ID %llu]: Message [ID %llu] send complete = %d", websocketTask->m_websocketHandle->id, msg->m_id, result);
384+
msg->m_storeAsyncResult = result;
385+
}
386+
catch (Platform::Exception^ ex)
387+
{
388+
msg->m_storeAsyncResult = ex->HResult;
389+
}
390+
catch (...)
391+
{
392+
msg->m_storeAsyncResult = E_FAIL;
393+
}
394+
395+
if (FAILED(msg->m_storeAsyncResult))
396+
{
397+
HC_TRACE_ERROR(WEBSOCKET, "Websocket [ID %llu]: Message [ID %llu] send failed = 0x%0.8x", websocketTask->m_websocketHandle->id, msg->m_id, msg->m_storeAsyncResult);
398+
}
399+
CompleteAsync(asyncBlock, msg->m_storeAsyncResult, sizeof(WebSocketCompletionResult));
400+
MessageWebSocketSendMessage(websocketTask);
401+
});
402+
}
403+
catch (Platform::Exception^ e)
404+
{
405+
HC_TRACE_ERROR(WEBSOCKET, "Websocket [ID %llu]: Send failed = 0x%0.8x", websocketTask->m_websocketHandle->id, e->HResult);
406+
}
391407

392408
return E_PENDING;
393409
}

0 commit comments

Comments
 (0)