diff --git a/server/Windows/CMakeLists.txt b/server/Windows/CMakeLists.txt index 91c612d5e..53d81aacd 100644 --- a/server/Windows/CMakeLists.txt +++ b/server/Windows/CMakeLists.txt @@ -1,33 +1,35 @@ -# FreeRDP: A Remote Desktop Protocol Client -# FreeRDP Windows Server cmake build script -# -# Copyright 2011 O.S. Systems Software Ltda. -# Copyright 2011 Otavio Salvador -# Copyright 2011 Marc-Andre Moreau -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -add_executable(wfreerdp-server - wf_input.c - wf_input.h - wf_mirage.c - wf_mirage.h - wf_peer.c - wf_peer.h - wfreerdp.c - wfreerdp.h) - -target_link_libraries(wfreerdp-server freerdp-core) -target_link_libraries(wfreerdp-server freerdp-utils) -target_link_libraries(wfreerdp-server freerdp-codec) -target_link_libraries(wfreerdp-server freerdp-channels) +# FreeRDP: A Remote Desktop Protocol Client +# FreeRDP Windows Server cmake build script +# +# Copyright 2011 O.S. Systems Software Ltda. +# Copyright 2011 Otavio Salvador +# Copyright 2011 Marc-Andre Moreau +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +add_executable(wfreerdp-server + wf_input.c + wf_input.h + wf_mirage.c + wf_mirage.h + wf_peer.c + wf_peer.h + wf_info.c + wf_info.h + wfreerdp.c + wfreerdp.h) + +target_link_libraries(wfreerdp-server freerdp-core) +target_link_libraries(wfreerdp-server freerdp-utils) +target_link_libraries(wfreerdp-server freerdp-codec) +target_link_libraries(wfreerdp-server freerdp-channels) diff --git a/server/Windows/wf_info.c b/server/Windows/wf_info.c new file mode 100644 index 000000000..efc5a1329 --- /dev/null +++ b/server/Windows/wf_info.c @@ -0,0 +1,134 @@ +/** + * FreeRDP: A Remote Desktop Protocol Client + * FreeRDP Windows Server + * + * Copyright 2012 Corey Clayton + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include "wf_info.h" +#include "wf_mirage.h" + +wfInfo * wf_info_init(wfInfo * info) +{ + if(!info) + { + info = (wfInfo*)malloc(sizeof(wfInfo)); //free this on shutdown + memset(info, 0, sizeof(wfInfo)); + + info->mutex = CreateMutex( + NULL, // default security attributes + FALSE, // initially not owned + NULL); // unnamed mutex + + if (info->mutex == NULL) + { + _tprintf(_T("CreateMutex error: %d\n"), GetLastError()); + } + + info->encodeMutex = CreateMutex( + NULL, // default security attributes + FALSE, // initially not owned + NULL); // unnamed mutex + + if (info->encodeMutex == NULL) + { + _tprintf(_T("CreateMutex error: %d\n"), GetLastError()); + } + + info->can_send_mutex = CreateMutex( + NULL, // default security attributes + FALSE, // initially not owned + NULL); // unnamed mutex + + if (info->can_send_mutex == NULL) + { + _tprintf(_T("CreateMutex error: %d\n"), GetLastError()); + } + } + + return info; +} + +void wf_info_mirror_init(wfInfo * info, wfPeerContext* context) +{ + DWORD dRes; + + + dRes = WaitForSingleObject( + info->mutex, // handle to mutex + INFINITE); // no time-out interval + + switch(dRes) + { + case WAIT_OBJECT_0: + if(info->subscribers == 0) + { + //only the first peer needs to call this. + context->wfInfo = info; + wf_check_disp_devices(context->wfInfo); + wf_disp_device_set_attatch(context->wfInfo, 1); + wf_update_mirror_drv(context->wfInfo, 0); + wf_map_mirror_mem(context->wfInfo); + + context->rfx_context = rfx_context_new(); + context->rfx_context->mode = RLGR3; + context->rfx_context->width = context->wfInfo->width; + context->rfx_context->height = context->wfInfo->height; + + rfx_context_set_pixel_format(context->rfx_context, RDP_PIXEL_FORMAT_B8G8R8A8); + context->s = stream_new(65536); + } + ++info->subscribers; + + if (! ReleaseMutex(info->mutex)) + { + _tprintf(_T("Error releasing mutex\n")); + } + + break; + + default: + _tprintf(_T("Error waiting for mutex: %d\n"), dRes); + } + +} + + +//todo: i think i can replace all the context->info here with info +//in fact i may not even care about subscribers +void wf_info_subscriber_release(wfInfo* info, wfPeerContext* context) +{ + WaitForSingleObject(info->mutex, INFINITE); + if (context && (info->subscribers == 1)) + { + --info->subscribers; + //only the last peer needs to call this + wf_mirror_cleanup(context->wfInfo); + wf_disp_device_set_attatch(context->wfInfo, 0); + wf_update_mirror_drv(context->wfInfo, 1); + + stream_free(context->s); + rfx_context_free(context->rfx_context); + } + else + { + --info->subscribers; + } + ReleaseMutex(info->mutex); +} \ No newline at end of file diff --git a/server/Windows/wf_info.h b/server/Windows/wf_info.h new file mode 100644 index 000000000..b149adf5a --- /dev/null +++ b/server/Windows/wf_info.h @@ -0,0 +1,61 @@ +/** + * FreeRDP: A Remote Desktop Protocol Client + * FreeRDP Windows Server + * + * Copyright 2012 Corey Clayton + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef WF_INFO_H +#define WF_INFO_H + +//#include "wfreerdp.h" + +struct wf_peer_context; +typedef struct wf_peer_context wfPeerContext; + +struct wf_info +{ + HDC driverDC; + BOOL activated; + void* changeBuffer; + LPTSTR deviceKey; + TCHAR deviceName[32]; + int subscribers; + int threadCnt; + int height; + int width; + int bitsPerPix; + + HANDLE mutex, encodeMutex, can_send_mutex; + + unsigned long lastUpdate; + unsigned long nextUpdate; + + long invalid_x1; + long invalid_y1; + + long invalid_x2; + long invalid_y2; + + +}; +typedef struct wf_info wfInfo; + +wfInfo* wf_info_init(wfInfo* info); +void wf_info_mirror_init(wfInfo* info, wfPeerContext* context); +void wf_info_subscriber_release(wfInfo* info, wfPeerContext* context); + + +#endif \ No newline at end of file diff --git a/server/Windows/wf_peer.c b/server/Windows/wf_peer.c index b72a5740e..b1f0cab6f 100644 --- a/server/Windows/wf_peer.c +++ b/server/Windows/wf_peer.c @@ -31,108 +31,34 @@ void wf_peer_context_new(freerdp_peer* client, wfPeerContext* context) { - DWORD dRes; - - if(!wfInfoSingleton) - { - wfInfoSingleton = (wfInfo*)malloc(sizeof(wfInfo)); //free this on shutdown - memset(wfInfoSingleton, 0, sizeof(wfInfo)); - - wfInfoSingleton->mutex = CreateMutex( - NULL, // default security attributes - FALSE, // initially not owned - NULL); // unnamed mutex - - if (wfInfoSingleton->mutex == NULL) - { - _tprintf(_T("CreateMutex error: %d\n"), GetLastError()); - } - - wfInfoSingleton->encodeMutex = CreateMutex( - NULL, // default security attributes - FALSE, // initially not owned - NULL); // unnamed mutex - - if (wfInfoSingleton->encodeMutex == NULL) - { - _tprintf(_T("CreateMutex error: %d\n"), GetLastError()); - } - } - - dRes = WaitForSingleObject( - wfInfoSingleton->mutex, // handle to mutex - INFINITE); // no time-out interval - - switch(dRes) - { - case WAIT_OBJECT_0: - if(wfInfoSingleton->subscribers == 0) - { - //only the first peer needs to call this. - context->wfInfo = wfInfoSingleton; - wf_check_disp_devices(context->wfInfo); - wf_disp_device_set_attatch(context->wfInfo, 1); - wf_update_mirror_drv(context->wfInfo, 0); - wf_map_mirror_mem(context->wfInfo); - - context->rfx_context = rfx_context_new(); - context->rfx_context->mode = RLGR3; - context->rfx_context->width = context->wfInfo->width; - context->rfx_context->height = context->wfInfo->height; - - rfx_context_set_pixel_format(context->rfx_context, RDP_PIXEL_FORMAT_B8G8R8A8); - context->s = stream_new(65536); - } - ++wfInfoSingleton->subscribers; - - if (! ReleaseMutex(wfInfoSingleton->mutex)) - { - _tprintf(_T("Error releasing mutex\n")); - } - - break; - - default: - _tprintf(_T("Error waiting for mutex: %d\n"), dRes); - } - + DWORD dRes; + wfInfoSingleton = wf_info_init(wfInfoSingleton); + wf_info_mirror_init(wfInfoSingleton, context); } void wf_peer_context_free(freerdp_peer* client, wfPeerContext* context) -{ - - if (context && (wfInfoSingleton->subscribers == 1)) - { - --wfInfoSingleton->subscribers; - //only the last peer needs to call this - wf_mirror_cleanup(context->wfInfo); - wf_disp_device_set_attatch(context->wfInfo, 0); - wf_update_mirror_drv(context->wfInfo, 1); - - stream_free(context->s); - rfx_context_free(context->rfx_context); - } - else - { - --wfInfoSingleton->subscribers; - } +{ + wf_info_subscriber_release(wfInfoSingleton, context); } static DWORD WINAPI wf_peer_mirror_monitor(LPVOID lpParam) { DWORD dRes; - DWORD start, end, diff; + DWORD start, end, diff; + DWORD rate; GETCHANGESBUF* buf; freerdp_peer* client; unsigned long i; + rate = 1000; client = (freerdp_peer*)lpParam; buf = (GETCHANGESBUF*)wfInfoSingleton->changeBuffer; - + //todo: make sure we dont encode after no clients while(1) { - start = GetTickCount(); + start = GetTickCount(); + /* dRes = WaitForSingleObject( wfInfoSingleton->mutex, // handle to mutex INFINITE); // no time-out interval @@ -168,12 +94,14 @@ static DWORD WINAPI wf_peer_mirror_monitor(LPVOID lpParam) default: _tprintf(_T("Error waiting for mutex: %d\n"), dRes); } + */ end = GetTickCount(); - diff = end - start; - if(diff < 42) + diff = end - start; + if(diff < rate) { - Sleep(42 - diff); + printf("sleeping for %d ms...\n", rate - diff); + Sleep(rate - diff); } @@ -271,9 +199,11 @@ void wf_rfx_encode(freerdp_peer* client) /* printf("encode %d\n", wfi->nextUpdate - wfi->lastUpdate); - printf("\tinvlaid region = (%d, %d), (%d, %d)\n", wfi->invalid_x1, wfi->invalid_y1, wfi->invalid_x2, wfi->invalid_y2); - wfi->lastUpdate = wfi->nextUpdate; + printf("\tinvlaid region = (%d, %d), (%d, %d)\n", wfi->invalid_x1, wfi->invalid_y1, wfi->invalid_x2, wfi->invalid_y2); + */ + + wfi->lastUpdate = wfi->nextUpdate; if ( (wfi->invalid_x1 >= wfi->invalid_x2) || (wfi->invalid_y1 >= wfi->invalid_y2) ) return; @@ -320,6 +250,10 @@ void wf_peer_init(freerdp_peer* client) _tprintf(_T("Trying to create a monitor thread...\n")); + if (CreateThread(NULL, 0, wf_peer_mirror_monitor, client, 0, NULL) != 0) + _tprintf(_T("Created!\n")); + + /* if(wfInfoSingleton) { dRes = WaitForSingleObject( @@ -346,9 +280,9 @@ void wf_peer_init(freerdp_peer* client) default: _tprintf(_T("Error waiting for mutex: %d\n"), dRes); } - - + } + */ } @@ -395,3 +329,10 @@ void wf_peer_synchronize_event(rdpInput* input, uint32 flags) { } + +void wf_peer_send_changes() +{ + //get can_send_mutex + + printf("sending\n"); +} diff --git a/server/Windows/wf_peer.h b/server/Windows/wf_peer.h index 57138d6be..d8fae6152 100644 --- a/server/Windows/wf_peer.h +++ b/server/Windows/wf_peer.h @@ -21,6 +21,7 @@ #define WF_PEER_H #include "wfreerdp.h" +#include "wf_info.h" void wf_peer_context_new(freerdp_peer* client, wfPeerContext* context); void wf_peer_context_free(freerdp_peer* client, wfPeerContext* context); @@ -35,6 +36,8 @@ boolean wf_peer_activate(freerdp_peer* client); void wf_peer_synchronize_event(rdpInput* input, uint32 flags); +void wf_peer_send_changes(); + wfInfo * wfInfoSingleton; #endif /* WF_PEER_H */ diff --git a/server/Windows/wfreerdp.c b/server/Windows/wfreerdp.c index bb9573619..fefa8ef5a 100644 --- a/server/Windows/wfreerdp.c +++ b/server/Windows/wfreerdp.c @@ -42,6 +42,8 @@ int g_thread_count = 0; //extern wfInfo * wfInfoSingleton; +BOOL derp = false; + static DWORD WINAPI wf_peer_main_loop(LPVOID lpParam) { int dRes; @@ -88,6 +90,7 @@ static DWORD WINAPI wf_peer_main_loop(LPVOID lpParam) break; } + /* if(wfInfoSingleton) { //wf_peer_rfx_update(client, 0, 0, wfInfoSingleton->width, wfInfoSingleton->height); @@ -128,6 +131,7 @@ static DWORD WINAPI wf_peer_main_loop(LPVOID lpParam) _tprintf(_T("Error waiting for mutex: %d\n"), dRes); } } + */ } printf("Client %s disconnected.\n", client->local ? "(local)" : client->hostname); diff --git a/server/Windows/wfreerdp.h b/server/Windows/wfreerdp.h index 6e0f0487f..11f402e60 100644 --- a/server/Windows/wfreerdp.h +++ b/server/Windows/wfreerdp.h @@ -22,34 +22,7 @@ #include #include - -struct wf_info -{ - HDC driverDC; - boolean activated; - void* changeBuffer; - LPTSTR deviceKey; - TCHAR deviceName[32]; - int subscribers; - int threadCnt; - int height; - int width; - int bitsPerPix; - - HANDLE mutex, encodeMutex; - - unsigned long lastUpdate; - unsigned long nextUpdate; - - long invalid_x1; - long invalid_y1; - - long invalid_x2; - long invalid_y2; - - -}; -typedef struct wf_info wfInfo; +#include "wf_info.h" struct wf_peer_context {