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 <selva.nair@gmail.com>
pull/392/head
Selva Nair 2020-12-31 13:41:56 -05:00
parent 273322efb0
commit e4fba0c003
3 changed files with 26 additions and 14 deletions

37
echo.c
View File

@ -259,20 +259,12 @@ echo_msg_display(connection_t *c, time_t timestamp, const char *title, int type)
} }
if (type == ECHO_MSG_WINDOW) if (type == ECHO_MSG_WINDOW)
{ {
DWORD_PTR res;
HWND h = echo_msg_window; UINT timeout = 5000; /* msec */
if (h) if (echo_msg_window
&& SendMessageTimeout(echo_msg_window, WM_OVPN_ECHOMSG, 0, (LPARAM) c, SMTO_BLOCK, timeout, &res) == 0)
{ {
wchar_t from[256]; WriteStatusLog(c, L"GUI> Failed to display echo message: ", c->echo_msg.title, false);
_sntprintf_0(from, L"From: %s %s", c->config_name, _wctime(&timestamp));
/* 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);
} }
} }
else /* notify */ else /* notify */
@ -537,6 +529,25 @@ MessageDialogFunc(HWND hwnd, UINT msg, UNUSED WPARAM wParam, LPARAM lParam)
} }
break; 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: case WM_NOTIFY:
nmh = (NMHDR*) lParam; nmh = (NMHDR*) lParam;
/* We handle only EN_LINK messages */ /* We handle only EN_LINK messages */

1
main.h
View File

@ -60,6 +60,7 @@
#define WM_OVPN_SILENT (WM_APP + 18) #define WM_OVPN_SILENT (WM_APP + 18)
#define WM_OVPN_IMPORT (WM_APP + 20) #define WM_OVPN_IMPORT (WM_APP + 20)
#define WM_OVPN_RESCAN (WM_APP + 21) #define WM_OVPN_RESCAN (WM_APP + 21)
#define WM_OVPN_ECHOMSG (WM_APP + 22)
/* bool definitions */ /* bool definitions */
#define bool int #define bool int