avoid waiting too long if the clipboard message is blocked

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2023-06-19 22:52:17 +08:00
parent 5a8c2d9cd0
commit 733e4236c4
7 changed files with 111 additions and 115 deletions

View File

@@ -1,3 +1,5 @@
// to-do: TOO MANY compilation warnings. Fix them.
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* Windows Clipboard Redirection
@@ -136,11 +138,13 @@ typedef struct _FORMAT_IDS FORMAT_IDS;
#define TAG "windows"
#ifdef WITH_DEBUG_CLIPRDR
#define DEBUG_CLIPRDR(fmt, ...) fprintf(stderr, "DEBUG %s[%d] %s() " fmt "\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__);fflush(stderr)
#define DEBUG_CLIPRDR(fmt, ...) \
fprintf(stderr, "DEBUG %s[%d] %s() " fmt "\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__); \
fflush(stderr)
#else
#define DEBUG_CLIPRDR(fmt, ...) \
do \
{ \
do \
{ \
} while (0)
#endif
@@ -1440,6 +1444,55 @@ static UINT cliprdr_send_format_list(wfClipboard *clipboard, UINT32 connID)
return rc;
}
UINT wait_response_event(wfClipboard *clipboard, HANDLE event, void *data)
{
UINT rc = ERROR_INTERNAL_ERROR;
clipboard->context->IsStopped = FALSE;
// with default 3min timeout
for (int i = 0; i < 20 * 60 * 3; i++)
{
DWORD waitRes = WaitForSingleObject(event, 50);
if (waitRes == WAIT_TIMEOUT && clipboard->context->IsStopped == FALSE)
{
continue;
}
if (clipboard->context->IsStopped == TRUE)
{
wf_do_empty_cliprdr(clipboard);
}
if (waitRes != WAIT_OBJECT_0)
{
return ERROR_INTERNAL_ERROR;
}
if (!ResetEvent(event))
{
// NOTE: critical error here, crash may be better
rc = ERROR_INTERNAL_ERROR;
}
if (data == NULL)
{
rc = ERROR_INTERNAL_ERROR;
}
return rc;
}
if (data != NULL)
{
if (!ResetEvent(event))
{
// NOTE: critical error here, crash may be better
}
return ERROR_SUCCESS;
}
return ERROR_INTERNAL_ERROR;
}
static UINT cliprdr_send_data_request(UINT32 connID, wfClipboard *clipboard, UINT32 formatId)
{
UINT rc;
@@ -1460,44 +1513,7 @@ static UINT cliprdr_send_data_request(UINT32 connID, wfClipboard *clipboard, UIN
return rc;
}
// with default 3min timeout
for (int i = 0; i < 20 * 60 * 3; i++)
{
DWORD waitRes = WaitForSingleObject(clipboard->response_data_event, 50);
if (waitRes == WAIT_TIMEOUT)
{
continue;
}
if (waitRes != WAIT_OBJECT_0)
{
return ERROR_INTERNAL_ERROR;
}
if (!ResetEvent(clipboard->response_data_event))
{
// NOTE: critical error here, crash may be better
rc = ERROR_INTERNAL_ERROR;
}
if (!clipboard->hmem)
{
rc = ERROR_INTERNAL_ERROR;
}
return rc;
}
if (clipboard->hmem)
{
if (!ResetEvent(clipboard->response_data_event))
{
// NOTE: critical error here, crash may be better
}
return ERROR_SUCCESS;
}
return ERROR_INTERNAL_ERROR;
wait_response_event(clipboard, clipboard->response_data_event, clipboard->hmem);
}
UINT cliprdr_send_request_filecontents(wfClipboard *clipboard, UINT32 connID, const void *streamid, ULONG index,
@@ -1525,43 +1541,7 @@ UINT cliprdr_send_request_filecontents(wfClipboard *clipboard, UINT32 connID, co
return rc;
}
// with default 3min timeout
for (int i = 0; i < 20 * 60 * 3; i++)
{
DWORD waitRes = WaitForSingleObject(clipboard->req_fevent, 50);
if (waitRes == WAIT_TIMEOUT)
{
continue;
}
if (waitRes != WAIT_OBJECT_0)
{
return ERROR_INTERNAL_ERROR;
}
if (!ResetEvent(clipboard->req_fevent))
{
// NOTE: critical error here, crash may be better
rc = ERROR_INTERNAL_ERROR;
}
if (!clipboard->req_fdata)
{
rc = ERROR_INTERNAL_ERROR;
}
return rc;
}
if (clipboard->req_fdata)
{
if (!ResetEvent(clipboard->req_fevent))
{
// NOTE: critical error here, crash may be better
}
return ERROR_SUCCESS;
}
return ERROR_INTERNAL_ERROR;
return wait_response_event(clipboard, clipboard->req_fevent, clipboard->req_fdata);
}
static UINT cliprdr_send_response_filecontents(
@@ -2069,8 +2049,8 @@ static BOOL wf_cliprdr_traverse_directory(wfClipboard *clipboard, WCHAR *Dir, si
while (FindNextFileW(hFind, &FindFileData))
{
// if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 &&
// wcscmp(FindFileData.cFileName, _T(".")) == 0 ||
// wcscmp(FindFileData.cFileName, _T("..")) == 0)
// wcscmp(FindFileData.cFileName, _T(".")) == 0 ||
// wcscmp(FindFileData.cFileName, _T("..")) == 0)
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 &&
wcscmp(FindFileData.cFileName, L".") == 0 ||
wcscmp(FindFileData.cFileName, L"..") == 0)
@@ -2123,7 +2103,8 @@ static UINT wf_cliprdr_send_client_capabilities(wfClipboard *clipboard)
return ERROR_INTERNAL_ERROR;
// Ignore ClientCapabilities for now
if (!clipboard->context->ClientCapabilities) {
if (!clipboard->context->ClientCapabilities)
{
return CHANNEL_RC_OK;
}
@@ -2147,7 +2128,7 @@ static UINT wf_cliprdr_monitor_ready(CliprdrClientContext *context,
const CLIPRDR_MONITOR_READY *monitorReady)
{
UINT rc;
wfClipboard *clipboard = (wfClipboard *)context->custom;
wfClipboard *clipboard = (wfClipboard *)context->Custom;
if (!context || !monitorReady)
return ERROR_INTERNAL_ERROR;
@@ -2171,7 +2152,7 @@ static UINT wf_cliprdr_server_capabilities(CliprdrClientContext *context,
{
UINT32 index;
CLIPRDR_CAPABILITY_SET *capabilitySet;
wfClipboard *clipboard = (wfClipboard *)context->custom;
wfClipboard *clipboard = (wfClipboard *)context->Custom;
if (!context || !capabilities)
return ERROR_INTERNAL_ERROR;
@@ -2205,7 +2186,7 @@ static UINT wf_cliprdr_server_format_list(CliprdrClientContext *context,
UINT32 i;
formatMapping *mapping;
CLIPRDR_FORMAT *format;
wfClipboard *clipboard = (wfClipboard *)context->custom;
wfClipboard *clipboard = (wfClipboard *)context->Custom;
if (!clear_format_map(clipboard))
return ERROR_INTERNAL_ERROR;
@@ -2241,7 +2222,7 @@ static UINT wf_cliprdr_server_format_list(CliprdrClientContext *context,
if (file_transferring(clipboard))
{
if (context->enableFiles)
if (context->EnableFiles)
{
UINT32 *p_conn_id = (UINT32 *)calloc(1, sizeof(UINT32));
*p_conn_id = formatList->connID;
@@ -2255,7 +2236,7 @@ static UINT wf_cliprdr_server_format_list(CliprdrClientContext *context,
}
else
{
if (context->enableOthers)
if (context->EnableOthers)
{
if (!try_open_clipboard(clipboard->hwnd))
return CHANNEL_RC_OK; /* Ignore, other app holding clipboard */
@@ -2264,7 +2245,7 @@ static UINT wf_cliprdr_server_format_list(CliprdrClientContext *context,
{
// Modified: do not apply delayed rendering
// for (i = 0; i < (UINT32)clipboard->map_size; i++)
// SetClipboardData(clipboard->format_mappings[i].local_format_id, NULL);
// SetClipboardData(clipboard->format_mappings[i].local_format_id, NULL);
FORMAT_IDS *format_ids = (FORMAT_IDS *)calloc(1, sizeof(FORMAT_IDS));
format_ids->connID = formatList->connID;
@@ -2399,7 +2380,7 @@ wf_cliprdr_server_format_data_request(CliprdrClientContext *context,
goto exit;
}
clipboard = (wfClipboard *)context->custom;
clipboard = (wfClipboard *)context->Custom;
if (!clipboard)
{
@@ -2579,7 +2560,7 @@ wf_cliprdr_server_format_data_response(CliprdrClientContext *context,
break;
}
clipboard = (wfClipboard *)context->custom;
clipboard = (wfClipboard *)context->Custom;
if (!clipboard)
{
rc = ERROR_INTERNAL_ERROR;
@@ -2660,7 +2641,7 @@ wf_cliprdr_server_file_contents_request(CliprdrClientContext *context,
goto exit;
}
clipboard = (wfClipboard *)context->custom;
clipboard = (wfClipboard *)context->Custom;
if (!clipboard)
{
@@ -2838,7 +2819,7 @@ exit:
}
// if (sRc != CHANNEL_RC_OK)
// return sRc;
// return sRc;
return rc;
}
@@ -2863,7 +2844,7 @@ wf_cliprdr_server_file_contents_response(CliprdrClientContext *context,
break;
}
clipboard = (wfClipboard *)context->custom;
clipboard = (wfClipboard *)context->Custom;
if (!clipboard)
{
rc = ERROR_INTERNAL_ERROR;
@@ -2951,7 +2932,7 @@ BOOL wf_cliprdr_init(wfClipboard *clipboard, CliprdrClientContext *cliprdr)
cliprdr->ServerFormatDataResponse = wf_cliprdr_server_format_data_response;
cliprdr->ServerFileContentsRequest = wf_cliprdr_server_file_contents_request;
cliprdr->ServerFileContentsResponse = wf_cliprdr_server_file_contents_response;
cliprdr->custom = (void *)clipboard;
cliprdr->Custom = (void *)clipboard;
return TRUE;
error:
wf_cliprdr_uninit(clipboard, cliprdr);
@@ -2963,7 +2944,7 @@ BOOL wf_cliprdr_uninit(wfClipboard *clipboard, CliprdrClientContext *cliprdr)
if (!clipboard || !cliprdr)
return FALSE;
cliprdr->custom = NULL;
cliprdr->Custom = NULL;
/* discard all contexts in clipboard */
if (try_open_clipboard(clipboard->hwnd))
@@ -3038,7 +3019,7 @@ BOOL empty_cliprdr(CliprdrClientContext *context, UINT32 connID)
return TRUE;
}
clipboard = (wfClipboard *)context->custom;
clipboard = (wfClipboard *)context->Custom;
if (!clipboard)
{
return FALSE;