From 88ad2661bd2e0322c405f033065b7931cb8d441f Mon Sep 17 00:00:00 2001 From: C-o-r-E Date: Thu, 30 Aug 2012 15:50:46 -0400 Subject: [PATCH] wfreerdp-server: cleaned up synchronization code --- server/Windows/wf_info.c | 28 ++++------------------------ server/Windows/wf_info.h | 2 +- server/Windows/wf_peer.c | 25 ++++++++++++++++++++----- 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/server/Windows/wf_info.c b/server/Windows/wf_info.c index ac45b6f54..9e783962d 100644 --- a/server/Windows/wf_info.c +++ b/server/Windows/wf_info.c @@ -31,11 +31,11 @@ extern wfInfo * wfInfoSingleton; -int wf_info_lock() +int wf_info_lock(DWORD ms) { DWORD dRes; - dRes = WaitForSingleObject(wfInfoSingleton->mutex, INFINITE); + dRes = WaitForSingleObject(wfInfoSingleton->mutex, ms); switch(dRes) { @@ -207,13 +207,6 @@ void wf_info_subscriber_release(wfInfo* wfi, wfPeerContext* context) BOOL wf_info_has_subscribers(wfInfo* wfi) { - int subs; - - WaitForSingleObject(wfi->mutex, INFINITE); - - subs = wfi->subscribers; - ReleaseMutex(wfi->mutex); - if (wfi->subscribers > 0) return TRUE; @@ -223,37 +216,26 @@ BOOL wf_info_has_subscribers(wfInfo* wfi) BOOL wf_info_have_updates(wfInfo* wfi) { - BOOL status = TRUE; - - WaitForSingleObject(wfi->mutex, INFINITE); - if (wfi->nextUpdate == wfi->lastUpdate) - status = FALSE; - - ReleaseMutex(wfi->mutex); + return FALSE; - return status; + return TRUE; } void wf_info_updated(wfInfo* wfi) { - WaitForSingleObject(wfi->mutex, INFINITE); wfi->lastUpdate = wfi->nextUpdate; - ReleaseMutex(wfi->mutex); } void wf_info_update_changes(wfInfo* wfi) { GETCHANGESBUF* buf; - WaitForSingleObject(wfi->mutex, INFINITE); - buf = (GETCHANGESBUF*) wfi->changeBuffer; wfi->nextUpdate = buf->buffer->counter; - ReleaseMutex(wfi->mutex); } void wf_info_find_invalid_region(wfInfo* wfi) @@ -261,7 +243,6 @@ void wf_info_find_invalid_region(wfInfo* wfi) int i; GETCHANGESBUF* buf; - WaitForSingleObject(wfi->mutex, INFINITE); buf = (GETCHANGESBUF*) wfi->changeBuffer; if (wfi->enc_data == FALSE) @@ -292,7 +273,6 @@ void wf_info_find_invalid_region(wfInfo* wfi) if (wfi->invalid_y2 >= wfi->height) wfi->invalid_y2 = wfi->height - 1; - ReleaseMutex(wfi->mutex); } void wf_info_clear_invalid_region(wfInfo* wfi) diff --git a/server/Windows/wf_info.h b/server/Windows/wf_info.h index abc21c9d2..2116ce29d 100644 --- a/server/Windows/wf_info.h +++ b/server/Windows/wf_info.h @@ -53,7 +53,7 @@ struct wf_info }; typedef struct wf_info wfInfo; -int wf_info_lock(); +int wf_info_lock(DWORD ms); int wf_info_unlock(); wfInfo* wf_info_init(wfInfo* wfi); diff --git a/server/Windows/wf_peer.c b/server/Windows/wf_peer.c index 39b6fde1a..0595e727a 100644 --- a/server/Windows/wf_peer.c +++ b/server/Windows/wf_peer.c @@ -35,6 +35,7 @@ void wf_peer_context_new(freerdp_peer* client, wfPeerContext* context) { + wfInfoSingleton = wf_info_init(wfInfoSingleton); wf_info_mirror_init(wfInfoSingleton, context); } @@ -55,21 +56,27 @@ static DWORD WINAPI wf_peer_mirror_monitor(LPVOID lpParam) rate = 1000 / fps; client = (freerdp_peer*) lpParam; - /* TODO: do not encode when no clients are connected */ - /* TODO: refactor this */ - while (wf_info_has_subscribers(wfInfoSingleton)) + while (1) { beg = GetTickCount(); + wf_info_lock(INFINITE); + if (wf_info_has_subscribers(wfInfoSingleton)) { - wf_info_update_changes(wfInfoSingleton); + wf_info_update_changes(wfInfoSingleton); if (wf_info_have_updates(wfInfoSingleton)) { wf_rfx_encode(client); } } + else + { + wf_info_unlock(); + } + + wf_info_unlock(); end = GetTickCount(); diff = end - beg; @@ -107,6 +114,9 @@ void wf_rfx_encode(freerdp_peer* client) BYTE* srcp; BYTE* dstp; #endif + + if(client->activated == FALSE) + return; wfp = (wfPeerContext*) client->context; dRes = WaitForSingleObject(wfInfoSingleton->encodeMutex, INFINITE); @@ -306,10 +316,14 @@ void wf_peer_send_changes(rdpUpdate* update) case WAIT_OBJECT_0: /* are there changes to send? */ - if (!wf_info_have_updates(wfInfoSingleton) || + + if ( ((wf_info_lock(0) != 0)) || + !wf_info_have_updates(wfInfoSingleton) || !wf_info_have_invalid_region(wfInfoSingleton) || (wfInfoSingleton->enc_data == FALSE)) { + //we dont send + wf_info_unlock(); ReleaseMutex(wfInfoSingleton->encodeMutex); break; } @@ -319,6 +333,7 @@ void wf_peer_send_changes(rdpUpdate* update) update->SurfaceBits(update->context, &update->surface_bits_command); wfInfoSingleton->enc_data = FALSE; + wf_info_unlock(); ReleaseMutex(wfInfoSingleton->encodeMutex); break;