[client,SDL] move thread cleanup to own function

The cleanup code for the RDP thread of the SDL clients grew quite
complex. Move to a function to reduce complexity and make the main
thread more readable again.
This commit is contained in:
akallabeth 2024-09-17 09:06:22 +02:00 committed by Armin Novak
parent b7601ec755
commit 8ef4b2aefa
No known key found for this signature in database
GPG Key ID: 2CF4A2D2D3D72105
2 changed files with 97 additions and 78 deletions

View File

@ -1167,6 +1167,54 @@ static void sdl_post_final_disconnect(freerdp* instance)
return;
}
static void sdl_client_cleanup(SdlContext* sdl, int exit_code, char* error_msg)
{
WINPR_ASSERT(sdl);
rdpContext* context = sdl->context();
WINPR_ASSERT(context);
rdpSettings* settings = context->settings;
WINPR_ASSERT(settings);
sdl->rdp_thread_running = false;
bool showError = false;
if (freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
WLog_Print(sdl->log, WLOG_INFO, "Authentication only, exit status %s [%" PRId32 "]",
sdl_map_to_code_tag(exit_code), exit_code);
else
{
switch (exit_code)
{
case SDL_EXIT_SUCCESS:
case SDL_EXIT_DISCONNECT:
case SDL_EXIT_LOGOFF:
case SDL_EXIT_DISCONNECT_BY_USER:
case SDL_EXIT_CONNECT_CANCELLED:
break;
default:
{
std::lock_guard<CriticalSection> lock(sdl->critical);
if (sdl->connection_dialog && error_msg)
{
sdl->connection_dialog->showError(error_msg);
showError = true;
}
}
break;
}
}
free(error_msg);
if (!showError)
sdl_hide_connection_dialog(sdl);
sdl->exit_code = exit_code;
sdl_push_user_event(SDL_USEREVENT_QUIT);
#if SDL_VERSION_ATLEAST(2, 0, 16)
SDL_TLSCleanup();
#endif
}
/* RDP main loop.
* Connects RDP, loops while running and handles event and dispatch, cleans up
* after the connection ends. */
@ -1189,49 +1237,12 @@ static DWORD WINAPI sdl_client_thread_proc(SdlContext* sdl)
BOOL rc = freerdp_connect(instance);
rdpContext* context = sdl->context();
WINPR_ASSERT(context);
rdpSettings* settings = context->settings;
WINPR_ASSERT(settings);
ScopeGuard guard(
[&]()
{
sdl->rdp_thread_running = false;
bool showError = false;
if (freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
WLog_Print(sdl->log, WLOG_INFO, "Authentication only, exit status %s [%" PRId32 "]",
sdl_map_to_code_tag(exit_code), exit_code);
else
{
switch (exit_code)
{
case SDL_EXIT_SUCCESS:
case SDL_EXIT_DISCONNECT:
case SDL_EXIT_LOGOFF:
case SDL_EXIT_DISCONNECT_BY_USER:
case SDL_EXIT_CONNECT_CANCELLED:
break;
default:
{
std::lock_guard<CriticalSection> lock(sdl->critical);
if (sdl->connection_dialog && error_msg)
{
sdl->connection_dialog->showError(error_msg);
showError = true;
}
}
break;
}
}
free(error_msg);
if (!showError)
sdl_hide_connection_dialog(sdl);
sdl->exit_code = exit_code;
sdl_push_user_event(SDL_USEREVENT_QUIT);
#if SDL_VERSION_ATLEAST(2, 0, 16)
SDL_TLSCleanup();
#endif
});
ScopeGuard guard([&]() { sdl_client_cleanup(sdl, exit_code, error_msg); });
if (!rc)
{

View File

@ -1152,6 +1152,51 @@ static void sdl_post_final_disconnect(freerdp* instance)
return;
}
static void sdl_client_cleanup(SdlContext* sdl, int exit_code, char* error_msg)
{
WINPR_ASSERT(sdl);
rdpContext* context = sdl->context();
WINPR_ASSERT(context);
rdpSettings* settings = context->settings;
WINPR_ASSERT(settings);
sdl->rdp_thread_running = false;
bool showError = false;
if (freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
WLog_Print(sdl->log, WLOG_INFO, "Authentication only, exit status %s [%" PRId32 "]",
sdl_map_to_code_tag(exit_code), exit_code);
else
{
switch (exit_code)
{
case SDL_EXIT_SUCCESS:
case SDL_EXIT_DISCONNECT:
case SDL_EXIT_LOGOFF:
case SDL_EXIT_DISCONNECT_BY_USER:
case SDL_EXIT_CONNECT_CANCELLED:
break;
default:
{
std::lock_guard<CriticalSection> lock(sdl->critical);
if (sdl->connection_dialog && error_msg)
{
sdl->connection_dialog->showError(error_msg);
showError = true;
}
}
break;
}
}
free(error_msg);
if (!showError)
sdl_hide_connection_dialog(sdl);
sdl->exit_code = exit_code;
sdl_push_user_event(SDL_EVENT_USER_QUIT);
SDL_CleanupTLS();
}
/* RDP main loop.
* Connects RDP, loops while running and handles event and dispatch, cleans up
* after the connection ends. */
@ -1177,44 +1222,7 @@ static DWORD WINAPI sdl_client_thread_proc(SdlContext* sdl)
rdpSettings* settings = context->settings;
WINPR_ASSERT(settings);
ScopeGuard guard(
[&]()
{
sdl->rdp_thread_running = false;
bool showError = false;
if (freerdp_settings_get_bool(settings, FreeRDP_AuthenticationOnly))
WLog_Print(sdl->log, WLOG_INFO, "Authentication only, exit status %s [%" PRId32 "]",
sdl_map_to_code_tag(exit_code), exit_code);
else
{
switch (exit_code)
{
case SDL_EXIT_SUCCESS:
case SDL_EXIT_DISCONNECT:
case SDL_EXIT_LOGOFF:
case SDL_EXIT_DISCONNECT_BY_USER:
case SDL_EXIT_CONNECT_CANCELLED:
break;
default:
{
std::lock_guard<CriticalSection> lock(sdl->critical);
if (sdl->connection_dialog && error_msg)
{
sdl->connection_dialog->showError(error_msg);
showError = true;
}
}
break;
}
}
free(error_msg);
if (!showError)
sdl_hide_connection_dialog(sdl);
sdl->exit_code = exit_code;
sdl_push_user_event(SDL_EVENT_USER_QUIT);
SDL_CleanupTLS();
});
ScopeGuard guard([&]() { sdl_client_cleanup(sdl, exit_code, error_msg); });
if (!rc)
{