From fa93cdcb3a03a21726cc60772a5a535344a5b4fb Mon Sep 17 00:00:00 2001 From: Selva Nair Date: Thu, 31 Dec 2020 13:41:56 -0500 Subject: [PATCH] Update echo-msg window from the thread that owns the window Change the way echo-msg window is update (thread safety). When new echo-msg content is available for display, update the window from the thread owning it by sending a message to it. A blocking SendMessage (with a timeout) is used, as the window needs access to the config's echo-msg buffer which is cleared on return from this this call. Signed-off-by: Selva Nair --- echo.c | 37 ++++++++++++++++++++++++------------- main.c | 2 +- main.h | 1 + 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/echo.c b/echo.c index 94736c4..a071c25 100644 --- a/echo.c +++ b/echo.c @@ -259,20 +259,12 @@ echo_msg_display(connection_t *c, time_t timestamp, const char *title, int type) } if (type == ECHO_MSG_WINDOW) { - - HWND h = echo_msg_window; - if (h) + DWORD_PTR res; + UINT timeout = 5000; /* msec */ + if (echo_msg_window + && SendMessageTimeout(echo_msg_window, WM_OVPN_ECHOMSG, 0, (LPARAM) c, SMTO_BLOCK, timeout, &res) == 0) { - wchar_t from[256]; - _sntprintf_0(from, L"From: %s %s", c->config_name, _wctime(×tamp)); - - /* strip \n added by _wctime */ - if (wcslen(from) > 0) - from[wcslen(from)-1] = L'\0'; - - AddMessageBoxText(h, c->echo_msg.text, c->echo_msg.title, from); - SetForegroundWindow(h); - ShowWindow(h, SW_SHOW); + WriteStatusLog(c, L"GUI> Failed to display echo message: ", c->echo_msg.title, false); } } else /* notify */ @@ -537,6 +529,25 @@ MessageDialogFunc(HWND hwnd, UINT msg, UNUSED WPARAM wParam, LPARAM lParam) } break; + /* Must be sent with lParam = connection pointer + * Adds the current echo message and shows the window. + */ + case WM_OVPN_ECHOMSG: + { + connection_t *c = (connection_t *) lParam; + wchar_t from[256]; + _sntprintf_0(from, L"From: %s %s", c->config_name, _wctime(&c->echo_msg.fp.timestamp)); + + /* strip \n added by _wctime */ + if (wcslen(from) > 0) + from[wcslen(from)-1] = L'\0'; + + AddMessageBoxText(hwnd, c->echo_msg.text, c->echo_msg.title, from); + SetForegroundWindow(hwnd); + ShowWindow(hwnd, SW_SHOW); + } + break; + case WM_NOTIFY: nmh = (NMHDR*) lParam; /* We handle only EN_LINK messages */ diff --git a/main.c b/main.c index 37f9ad6..a8a987c 100644 --- a/main.c +++ b/main.c @@ -576,7 +576,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM } if (LOWORD(wParam) == IDM_SERVICE_RESTART) MyReStartService(); break; - + case WM_CLOSE: CloseApplication(hwnd); break; diff --git a/main.h b/main.h index 22a72ca..072099a 100644 --- a/main.h +++ b/main.h @@ -60,6 +60,7 @@ #define WM_OVPN_SILENT (WM_APP + 18) #define WM_OVPN_IMPORT (WM_APP + 20) #define WM_OVPN_RESCAN (WM_APP + 21) +#define WM_OVPN_ECHOMSG (WM_APP + 22) /* bool definitions */ #define bool int