22 #include <freerdp/config.h>
27 #include <winpr/winpr.h>
28 #include <winpr/crt.h>
29 #include <winpr/cast.h>
30 #include <winpr/assert.h>
31 #include <winpr/ssl.h>
32 #include <winpr/synch.h>
33 #include <winpr/file.h>
34 #include <winpr/string.h>
35 #include <winpr/path.h>
36 #include <winpr/image.h>
37 #include <winpr/winsock.h>
39 #include <freerdp/streamdump.h>
40 #include <freerdp/transport_io.h>
42 #include <freerdp/channels/wtsvc.h>
43 #include <freerdp/channels/channels.h>
44 #include <freerdp/channels/drdynvc.h>
46 #include <freerdp/freerdp.h>
47 #include <freerdp/constants.h>
48 #include <freerdp/server/rdpsnd.h>
49 #include <freerdp/settings.h>
51 #include "sf_ainput.h"
53 #include "sf_rdpsnd.h"
54 #include "sf_encomsp.h"
58 #include <freerdp/log.h>
59 #define TAG SERVER_TAG("sample")
61 #define SAMPLE_SERVER_USE_CLIENT_RESOLUTION 1
62 #define SAMPLE_SERVER_DEFAULT_WIDTH 1024
63 #define SAMPLE_SERVER_DEFAULT_HEIGHT 768
67 BOOL test_dump_rfx_realtime;
68 const char* test_pcap_file;
69 const char* replay_dump;
74 static void test_peer_context_free(freerdp_peer* client, rdpContext* ctx)
76 testPeerContext* context = (testPeerContext*)ctx;
82 winpr_image_free(context->image, TRUE);
83 if (context->debug_channel_thread)
85 WINPR_ASSERT(context->stopEvent);
86 (void)SetEvent(context->stopEvent);
87 (void)WaitForSingleObject(context->debug_channel_thread, INFINITE);
88 (void)CloseHandle(context->debug_channel_thread);
91 Stream_Free(context->s, TRUE);
92 free(context->bg_data);
93 rfx_context_free(context->rfx_context);
94 nsc_context_free(context->nsc_context);
96 if (context->debug_channel)
97 (void)WTSVirtualChannelClose(context->debug_channel);
99 sf_peer_audin_uninit(context);
101 #if defined(CHANNEL_AINPUT_SERVER)
102 sf_peer_ainput_uninit(context);
105 rdpsnd_server_context_free(context->rdpsnd);
106 encomsp_server_context_free(context->encomsp);
108 WTSCloseServer(context->vcm);
112 static BOOL test_peer_context_new(freerdp_peer* client, rdpContext* ctx)
114 testPeerContext* context = (testPeerContext*)ctx;
116 WINPR_ASSERT(client);
117 WINPR_ASSERT(context);
118 WINPR_ASSERT(ctx->settings);
120 context->image = winpr_image_new();
123 if (!(context->rfx_context = rfx_context_new_ex(
127 if (!rfx_context_reset(context->rfx_context, SAMPLE_SERVER_DEFAULT_WIDTH,
128 SAMPLE_SERVER_DEFAULT_HEIGHT))
132 rfx_context_set_mode(context->rfx_context, rlgr);
134 if (!(context->nsc_context = nsc_context_new()))
137 if (!(context->s = Stream_New(NULL, 65536)))
140 context->icon_x = UINT32_MAX;
141 context->icon_y = UINT32_MAX;
142 context->vcm = WTSOpenServerA((LPSTR)client->context);
144 if (!context->vcm || context->vcm == INVALID_HANDLE_VALUE)
149 test_peer_context_free(client, ctx);
153 static BOOL test_peer_init(freerdp_peer* client)
155 WINPR_ASSERT(client);
157 client->ContextSize =
sizeof(testPeerContext);
158 client->ContextNew = test_peer_context_new;
159 client->ContextFree = test_peer_context_free;
160 return freerdp_peer_context_new(client);
163 static wStream* test_peer_stream_init(testPeerContext* context)
165 WINPR_ASSERT(context);
166 WINPR_ASSERT(context->s);
168 Stream_Clear(context->s);
169 Stream_SetPosition(context->s, 0);
173 static void test_peer_begin_frame(freerdp_peer* client)
175 rdpUpdate* update = NULL;
177 testPeerContext* context = NULL;
179 WINPR_ASSERT(client);
180 WINPR_ASSERT(client->context);
182 update = client->context->update;
183 WINPR_ASSERT(update);
185 context = (testPeerContext*)client->context;
186 WINPR_ASSERT(context);
188 fm.frameAction = SURFACECMD_FRAMEACTION_BEGIN;
189 fm.frameId = context->frame_id;
190 WINPR_ASSERT(update->SurfaceFrameMarker);
191 update->SurfaceFrameMarker(update->context, &fm);
194 static void test_peer_end_frame(freerdp_peer* client)
196 rdpUpdate* update = NULL;
198 testPeerContext* context = NULL;
200 WINPR_ASSERT(client);
202 context = (testPeerContext*)client->context;
203 WINPR_ASSERT(context);
205 update = client->context->update;
206 WINPR_ASSERT(update);
208 fm.frameAction = SURFACECMD_FRAMEACTION_END;
209 fm.frameId = context->frame_id;
210 WINPR_ASSERT(update->SurfaceFrameMarker);
211 update->SurfaceFrameMarker(update->context, &fm);
215 static BOOL stream_surface_bits_supported(
const rdpSettings* settings)
217 const UINT32 supported =
219 return ((supported & SURFCMDS_STREAM_SURFACE_BITS) != 0);
222 static BOOL test_peer_draw_background(freerdp_peer* client)
227 BYTE* rgb_data = NULL;
228 const rdpSettings* settings = NULL;
229 rdpUpdate* update = NULL;
231 testPeerContext* context = NULL;
233 const UINT32 colorFormat = PIXEL_FORMAT_RGB24;
234 const size_t bpp = FreeRDPGetBytesPerPixel(colorFormat);
236 WINPR_ASSERT(client);
237 context = (testPeerContext*)client->context;
238 WINPR_ASSERT(context);
240 settings = client->context->settings;
241 WINPR_ASSERT(settings);
243 update = client->context->update;
244 WINPR_ASSERT(update);
253 s = test_peer_stream_init(context);
258 size = bpp * rect.width * rect.height;
260 if (!(rgb_data = malloc(size)))
262 WLog_ERR(TAG,
"Problem allocating memory");
266 memset(rgb_data, 0xA0, size);
268 if (RemoteFxCodec && stream_surface_bits_supported(settings))
270 WLog_DBG(TAG,
"Using RemoteFX codec");
271 rfx_context_set_pixel_format(context->rfx_context, colorFormat);
273 WINPR_ASSERT(bpp <= UINT16_MAX);
274 if (!rfx_compose_message(context->rfx_context, s, &rect, 1, rgb_data, rect.width,
275 rect.height, (UINT32)(bpp * rect.width)))
280 const UINT32 RemoteFxCodecId =
282 WINPR_ASSERT(RemoteFxCodecId <= UINT16_MAX);
283 cmd.bmp.codecID = (UINT16)RemoteFxCodecId;
284 cmd.cmdType = CMDTYPE_STREAM_SURFACE_BITS;
288 WLog_DBG(TAG,
"Using NSCodec");
289 nsc_context_set_parameters(context->nsc_context, NSC_COLOR_FORMAT, colorFormat);
291 WINPR_ASSERT(bpp <= UINT16_MAX);
292 nsc_compose_message(context->nsc_context, s, rgb_data, rect.width, rect.height,
293 (UINT32)(bpp * rect.width));
295 WINPR_ASSERT(NSCodecId <= UINT16_MAX);
296 cmd.bmp.codecID = (UINT16)NSCodecId;
297 cmd.cmdType = CMDTYPE_SET_SURFACE_BITS;
302 cmd.destRight = rect.width;
303 cmd.destBottom = rect.height;
306 cmd.bmp.width = rect.width;
307 cmd.bmp.height = rect.height;
308 WINPR_ASSERT(Stream_GetPosition(s) <= UINT32_MAX);
309 cmd.bmp.bitmapDataLength = (UINT32)Stream_GetPosition(s);
310 cmd.bmp.bitmapData = Stream_Buffer(s);
311 test_peer_begin_frame(client);
312 update->SurfaceBits(update->context, &cmd);
313 test_peer_end_frame(client);
320 static int open_icon(
wImage* img)
322 char* paths[] = { SAMPLE_RESOURCE_ROOT,
"." };
323 const char* names[] = {
"test_icon.webp",
"test_icon.png",
"test_icon.jpg",
"test_icon.bmp" };
325 for (
size_t x = 0; x < ARRAYSIZE(paths); x++)
327 const char* path = paths[x];
328 if (!winpr_PathFileExists(path))
331 for (
size_t y = 0; y < ARRAYSIZE(names); y++)
333 const char* name = names[y];
334 char* file = GetCombinedPath(path, name);
335 int rc = winpr_image_read(img, file);
341 WLog_ERR(TAG,
"Unable to open test icon");
345 static BOOL test_peer_load_icon(freerdp_peer* client)
347 testPeerContext* context = NULL;
348 rdpSettings* settings = NULL;
350 WINPR_ASSERT(client);
352 context = (testPeerContext*)client->context;
353 WINPR_ASSERT(context);
355 settings = client->context->settings;
356 WINPR_ASSERT(settings);
361 WLog_ERR(TAG,
"Client doesn't support RemoteFX or NSCodec");
365 int rc = open_icon(context->image);
370 if (!(context->bg_data = calloc(context->image->height, 3ULL * context->image->width)))
373 memset(context->bg_data, 0xA0, 3ULL * context->image->height * context->image->width);
376 context->bg_data = NULL;
380 static void test_peer_draw_icon(freerdp_peer* client, UINT32 x, UINT32 y)
384 rdpUpdate* update = NULL;
385 rdpSettings* settings = NULL;
387 testPeerContext* context = NULL;
389 WINPR_ASSERT(client);
391 context = (testPeerContext*)client->context;
392 WINPR_ASSERT(context);
394 update = client->context->update;
395 WINPR_ASSERT(update);
397 settings = client->context->settings;
398 WINPR_ASSERT(settings);
403 if (context->image->width < 1 || !context->activated)
409 rect.width = WINPR_ASSERTING_INT_CAST(UINT16, context->image->width);
410 rect.height = WINPR_ASSERTING_INT_CAST(UINT16, context->image->height);
414 if (context->icon_x + context->image->width > w)
416 if (context->icon_y + context->image->height > h)
418 if (x + context->image->width > w)
420 if (y + context->image->height > h)
423 test_peer_begin_frame(client);
426 if (RemoteFxCodec && stream_surface_bits_supported(settings))
428 const UINT32 RemoteFxCodecId =
430 WINPR_ASSERT(RemoteFxCodecId <= UINT16_MAX);
431 cmd.bmp.codecID = (UINT16)RemoteFxCodecId;
432 cmd.cmdType = CMDTYPE_STREAM_SURFACE_BITS;
437 WINPR_ASSERT(NSCodecId <= UINT16_MAX);
438 cmd.bmp.codecID = (UINT16)NSCodecId;
439 cmd.cmdType = CMDTYPE_SET_SURFACE_BITS;
442 if (context->icon_x != UINT32_MAX)
444 const UINT32 colorFormat = PIXEL_FORMAT_RGB24;
445 const UINT32 bpp = FreeRDPGetBytesPerPixel(colorFormat);
446 s = test_peer_stream_init(context);
450 rfx_context_set_pixel_format(context->rfx_context, colorFormat);
451 rfx_compose_message(context->rfx_context, s, &rect, 1, context->bg_data, rect.width,
452 rect.height, rect.width * bpp);
456 nsc_context_set_parameters(context->nsc_context, NSC_COLOR_FORMAT, colorFormat);
457 nsc_compose_message(context->nsc_context, s, context->bg_data, rect.width, rect.height,
461 cmd.destLeft = context->icon_x;
462 cmd.destTop = context->icon_y;
463 cmd.destRight = context->icon_x + rect.width;
464 cmd.destBottom = context->icon_y + rect.height;
467 cmd.bmp.width = rect.width;
468 cmd.bmp.height = rect.height;
469 cmd.bmp.bitmapDataLength = (UINT32)Stream_GetPosition(s);
470 cmd.bmp.bitmapData = Stream_Buffer(s);
471 WINPR_ASSERT(update->SurfaceBits);
472 update->SurfaceBits(update->context, &cmd);
475 s = test_peer_stream_init(context);
478 const UINT32 colorFormat =
479 context->image->bitsPerPixel > 24 ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_BGR24;
483 rfx_context_set_pixel_format(context->rfx_context, colorFormat);
484 rfx_compose_message(context->rfx_context, s, &rect, 1, context->image->data, rect.width,
485 rect.height, context->image->scanline);
489 nsc_context_set_parameters(context->nsc_context, NSC_COLOR_FORMAT, colorFormat);
490 nsc_compose_message(context->nsc_context, s, context->image->data, rect.width,
491 rect.height, context->image->scanline);
497 cmd.destRight = x + rect.width;
498 cmd.destBottom = y + rect.height;
500 cmd.bmp.width = rect.width;
501 cmd.bmp.height = rect.height;
502 cmd.bmp.bitmapDataLength = (UINT32)Stream_GetPosition(s);
503 cmd.bmp.bitmapData = Stream_Buffer(s);
504 WINPR_ASSERT(update->SurfaceBits);
505 update->SurfaceBits(update->context, &cmd);
508 test_peer_end_frame(client);
511 static BOOL test_sleep_tsdiff(UINT32* old_sec, UINT32* old_usec, UINT32 new_sec, UINT32 new_usec)
516 WINPR_ASSERT(old_sec);
517 WINPR_ASSERT(old_usec);
519 if ((*old_sec == 0) && (*old_usec == 0))
522 *old_usec = new_usec;
526 sec = new_sec - *old_sec;
527 usec = new_usec - *old_usec;
529 if ((sec < 0) || ((sec == 0) && (usec < 0)))
531 WLog_ERR(TAG,
"Invalid time stamp detected.");
536 *old_usec = new_usec;
545 Sleep((DWORD)sec * 1000);
553 static BOOL tf_peer_dump_rfx(freerdp_peer* client)
557 UINT32 prev_seconds = 0;
558 UINT32 prev_useconds = 0;
559 rdpUpdate* update = NULL;
560 rdpPcap* pcap_rfx = NULL;
561 pcap_record record = { 0 };
562 struct server_info* info = NULL;
564 WINPR_ASSERT(client);
565 WINPR_ASSERT(client->context);
567 info = client->ContextExtra;
570 s = Stream_New(NULL, 512);
575 update = client->context->update;
576 WINPR_ASSERT(update);
578 pcap_rfx = pcap_open(info->test_pcap_file, FALSE);
582 prev_seconds = prev_useconds = 0;
584 while (pcap_has_next_record(pcap_rfx))
586 if (!pcap_get_next_record_header(pcap_rfx, &record))
589 if (!Stream_EnsureCapacity(s, record.length))
592 record.data = Stream_Buffer(s);
593 pcap_get_next_record_content(pcap_rfx, &record);
594 Stream_SetPosition(s, Stream_Capacity(s));
596 if (info->test_dump_rfx_realtime &&
597 test_sleep_tsdiff(&prev_seconds, &prev_useconds, record.header.ts_sec,
598 record.header.ts_usec) == FALSE)
601 WINPR_ASSERT(update->SurfaceCommand);
602 update->SurfaceCommand(update->context, s);
604 WINPR_ASSERT(client->CheckFileDescriptor);
605 if (client->CheckFileDescriptor(client) != TRUE)
611 Stream_Free(s, TRUE);
612 pcap_close(pcap_rfx);
616 static DWORD WINAPI tf_debug_channel_thread_func(LPVOID arg)
620 DWORD BytesReturned = 0;
622 testPeerContext* context = (testPeerContext*)arg;
624 WINPR_ASSERT(context);
625 if (WTSVirtualChannelQuery(context->debug_channel, WTSVirtualFileHandle, &buffer,
626 &BytesReturned) == TRUE)
628 fd = *((
void**)buffer);
629 WTSFreeMemory(buffer);
631 if (!(context->event = CreateWaitObjectEvent(NULL, TRUE, FALSE, fd)))
635 wStream* s = Stream_New(NULL, 4096);
639 if (!WTSVirtualChannelWrite(context->debug_channel, (PCHAR)
"test1", 5, &written))
646 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = { 0 };
648 handles[nCount++] = context->event;
649 handles[nCount++] = freerdp_abort_event(&context->_p);
650 handles[nCount++] = context->stopEvent;
651 status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
660 Stream_SetPosition(s, 0);
662 if (WTSVirtualChannelRead(context->debug_channel, 0, Stream_BufferAs(s,
char),
663 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
665 if (BytesReturned == 0)
668 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
671 if (WTSVirtualChannelRead(context->debug_channel, 0, Stream_BufferAs(s,
char),
672 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
679 Stream_SetPosition(s, BytesReturned);
680 WLog_DBG(TAG,
"got %" PRIu32
" bytes", BytesReturned);
683 Stream_Free(s, TRUE);
687 static BOOL tf_peer_post_connect(freerdp_peer* client)
689 testPeerContext* context = NULL;
690 rdpSettings* settings = NULL;
692 WINPR_ASSERT(client);
694 context = (testPeerContext*)client->context;
695 WINPR_ASSERT(context);
697 settings = client->context->settings;
698 WINPR_ASSERT(settings);
706 WLog_DBG(TAG,
"Client %s is activated (osMajorType %" PRIu32
" osMinorType %" PRIu32
")",
707 client->local ?
"(local)" : client->hostname,
715 WLog_DBG(TAG,
" and wants to login automatically as %s\\%s", Domain ? Domain :
"",
721 WLog_DBG(TAG,
"Client requested desktop: %" PRIu32
"x%" PRIu32
"x%" PRIu32
"",
725 #if (SAMPLE_SERVER_USE_CLIENT_RESOLUTION == 1)
727 if (!rfx_context_reset(context->rfx_context,
732 WLog_DBG(TAG,
"Using resolution requested by client.");
734 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) =
735 context->rfx_context->width;
736 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight) =
737 context->rfx_context->height;
738 WLog_DBG(TAG,
"Resizing client to %" PRIu32
"x%" PRIu32
"",
739 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
740 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
741 client->update->DesktopResize(client->update->context);
746 if (!test_peer_load_icon(client))
748 WLog_DBG(TAG,
"Unable to load icon");
752 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm,
"rdpdbg"))
754 context->debug_channel = WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION,
"rdpdbg");
756 if (context->debug_channel != NULL)
758 WLog_DBG(TAG,
"Open channel rdpdbg.");
760 if (!(context->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
762 WLog_ERR(TAG,
"Failed to create stop event");
766 if (!(context->debug_channel_thread =
767 CreateThread(NULL, 0, tf_debug_channel_thread_func, (
void*)context, 0, NULL)))
769 WLog_ERR(TAG,
"Failed to create debug channel thread");
770 (void)CloseHandle(context->stopEvent);
771 context->stopEvent = NULL;
777 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, RDPSND_CHANNEL_NAME))
779 sf_peer_rdpsnd_init(context);
782 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, ENCOMSP_SVC_CHANNEL_NAME))
784 sf_peer_encomsp_init(context);
788 sf_peer_audin_init(context);
790 #if defined(CHANNEL_AINPUT_SERVER)
791 sf_peer_ainput_init(context);
798 static BOOL tf_peer_activate(freerdp_peer* client)
800 testPeerContext* context = NULL;
801 struct server_info* info = NULL;
802 rdpSettings* settings = NULL;
804 WINPR_ASSERT(client);
806 context = (testPeerContext*)client->context;
807 WINPR_ASSERT(context);
809 settings = client->context->settings;
810 WINPR_ASSERT(settings);
812 info = client->ContextExtra;
815 context->activated = TRUE;
822 if (info->test_pcap_file != NULL)
827 if (!tf_peer_dump_rfx(client))
831 test_peer_draw_background(client);
836 static BOOL tf_peer_synchronize_event(rdpInput* input, UINT32 flags)
840 WLog_DBG(TAG,
"Client sent a synchronize event (flags:0x%" PRIX32
")", flags);
844 static BOOL tf_peer_keyboard_event(rdpInput* input, UINT16 flags, UINT8 code)
846 freerdp_peer* client = NULL;
847 rdpUpdate* update = NULL;
848 rdpContext* context = NULL;
849 testPeerContext* tcontext = NULL;
850 rdpSettings* settings = NULL;
854 context = input->context;
855 WINPR_ASSERT(context);
857 client = context->peer;
858 WINPR_ASSERT(client);
860 settings = context->settings;
861 WINPR_ASSERT(settings);
863 update = context->update;
864 WINPR_ASSERT(update);
866 tcontext = (testPeerContext*)context;
867 WINPR_ASSERT(tcontext);
869 WLog_DBG(TAG,
"Client sent a keyboard event (flags:0x%04" PRIX16
" code:0x%04" PRIX8
")", flags,
872 if (((flags & KBD_FLAGS_RELEASE) == 0) && (code == RDP_SCANCODE_KEY_G))
884 SAMPLE_SERVER_DEFAULT_WIDTH))
887 SAMPLE_SERVER_DEFAULT_HEIGHT))
891 if (!rfx_context_reset(tcontext->rfx_context,
896 WINPR_ASSERT(update->DesktopResize);
897 update->DesktopResize(update->context);
898 tcontext->activated = FALSE;
900 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_C)
902 if (tcontext->debug_channel)
905 if (!WTSVirtualChannelWrite(tcontext->debug_channel, (PCHAR)
"test2", 5, &written))
909 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_X)
911 WINPR_ASSERT(client->Close);
912 client->Close(client);
914 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_R)
916 tcontext->audin_open = !tcontext->audin_open;
918 #if defined(CHANNEL_AINPUT_SERVER)
919 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_I)
921 tcontext->ainput_open = !tcontext->ainput_open;
924 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_S)
931 static BOOL tf_peer_unicode_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
937 "Client sent a unicode keyboard event (flags:0x%04" PRIX16
" code:0x%04" PRIX16
")",
942 static BOOL tf_peer_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
946 WINPR_ASSERT(input->context);
950 test_peer_draw_icon(input->context->peer, x + 10, y);
954 static BOOL tf_peer_extended_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
958 WINPR_ASSERT(input->context);
962 test_peer_draw_icon(input->context->peer, x + 10, y);
966 static BOOL tf_peer_refresh_rect(rdpContext* context, BYTE count,
const RECTANGLE_16* areas)
968 WINPR_UNUSED(context);
969 WINPR_ASSERT(context);
970 WINPR_ASSERT(areas || (count == 0));
972 WLog_DBG(TAG,
"Client requested to refresh:");
974 for (BYTE i = 0; i < count; i++)
976 WLog_DBG(TAG,
" (%" PRIu16
", %" PRIu16
") (%" PRIu16
", %" PRIu16
")", areas[i].left,
977 areas[i].top, areas[i].right, areas[i].bottom);
983 static BOOL tf_peer_suppress_output(rdpContext* context, BYTE allow,
const RECTANGLE_16* area)
985 WINPR_UNUSED(context);
991 "Client restore output (%" PRIu16
", %" PRIu16
") (%" PRIu16
", %" PRIu16
").",
992 area->left, area->top, area->right, area->bottom);
996 WLog_DBG(TAG,
"Client minimized and suppress output.");
1002 static int hook_peer_write_pdu(rdpTransport* transport,
wStream* s)
1007 const struct server_info* info = NULL;
1008 freerdp_peer* client = NULL;
1009 testPeerContext* peerCtx = NULL;
1012 rdpContext* context = transport_get_context(transport);
1014 WINPR_ASSERT(context);
1017 client = context->peer;
1018 WINPR_ASSERT(client);
1020 peerCtx = (testPeerContext*)client->context;
1021 WINPR_ASSERT(peerCtx);
1022 WINPR_ASSERT(peerCtx->io.WritePdu);
1024 info = client->ContextExtra;
1035 CONNECTION_STATE state = freerdp_get_state(context);
1036 if (state < CONNECTION_STATE_NEGO)
1037 return peerCtx->io.WritePdu(transport, s);
1039 ls = Stream_New(NULL, 4096);
1043 while (stream_dump_get(context, &flags, ls, &offset, &ts) > 0)
1047 if (flags & STREAM_MSG_SRV_TX)
1049 if ((last_ts > 0) && (ts > last_ts))
1051 UINT64 diff = ts - last_ts;
1054 UINT32 d = diff > UINT32_MAX ? UINT32_MAX : (UINT32)diff;
1060 rc = peerCtx->io.WritePdu(transport, ls);
1064 Stream_SetPosition(ls, 0);
1068 Stream_Free(ls, TRUE);
1072 static DWORD WINAPI test_peer_mainloop(LPVOID arg)
1075 DWORD error = CHANNEL_RC_OK;
1076 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = { 0 };
1079 testPeerContext* context = NULL;
1080 struct server_info* info = NULL;
1081 rdpSettings* settings = NULL;
1082 rdpInput* input = NULL;
1083 rdpUpdate* update = NULL;
1084 freerdp_peer* client = (freerdp_peer*)arg;
1086 WINPR_ASSERT(client);
1088 info = client->ContextExtra;
1091 if (!test_peer_init(client))
1093 freerdp_peer_free(client);
1098 WINPR_ASSERT(client->context);
1099 settings = client->context->settings;
1100 WINPR_ASSERT(settings);
1101 if (info->replay_dump)
1108 rdpPrivateKey* key = freerdp_key_new_from_file(info->key);
1113 rdpCertificate* cert = freerdp_certificate_new_from_file(info->cert);
1126 ENCRYPTION_LEVEL_CLIENT_COMPATIBLE))
1142 client->PostConnect = tf_peer_post_connect;
1143 client->Activate = tf_peer_activate;
1145 WINPR_ASSERT(client->context);
1146 input = client->context->input;
1147 WINPR_ASSERT(input);
1149 input->SynchronizeEvent = tf_peer_synchronize_event;
1150 input->KeyboardEvent = tf_peer_keyboard_event;
1151 input->UnicodeKeyboardEvent = tf_peer_unicode_keyboard_event;
1152 input->MouseEvent = tf_peer_mouse_event;
1153 input->ExtendedMouseEvent = tf_peer_extended_mouse_event;
1155 update = client->context->update;
1156 WINPR_ASSERT(update);
1158 update->RefreshRect = tf_peer_refresh_rect;
1159 update->SuppressOutput = tf_peer_suppress_output;
1164 WINPR_ASSERT(client->Initialize);
1165 rc = client->Initialize(client);
1169 context = (testPeerContext*)client->context;
1170 WINPR_ASSERT(context);
1172 if (info->replay_dump)
1174 const rdpTransportIo* cb = freerdp_get_io_callbacks(client->context);
1175 rdpTransportIo replay;
1180 replay.WritePdu = hook_peer_write_pdu;
1181 freerdp_set_io_callbacks(client->context, &replay);
1184 WLog_INFO(TAG,
"We've got a client %s", client->local ?
"(local)" : client->hostname);
1186 while (error == CHANNEL_RC_OK)
1190 WINPR_ASSERT(client->GetEventHandles);
1191 DWORD tmp = client->GetEventHandles(client, &handles[count], 32 - count);
1195 WLog_ERR(TAG,
"Failed to get FreeRDP transport event handles");
1202 HANDLE channelHandle = WTSVirtualChannelManagerGetEventHandle(context->vcm);
1203 handles[count++] = channelHandle;
1204 status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
1206 if (status == WAIT_FAILED)
1208 WLog_ERR(TAG,
"WaitForMultipleObjects failed (errno: %d)", errno);
1212 WINPR_ASSERT(client->CheckFileDescriptor);
1213 if (client->CheckFileDescriptor(client) != TRUE)
1216 if (WaitForSingleObject(channelHandle, 0) != WAIT_OBJECT_0)
1219 if (WTSVirtualChannelManagerCheckFileDescriptor(context->vcm) != TRUE)
1223 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, DRDYNVC_SVC_CHANNEL_NAME))
1225 switch (WTSVirtualChannelManagerGetDrdynvcState(context->vcm))
1227 case DRDYNVC_STATE_NONE:
1230 case DRDYNVC_STATE_INITIALIZED:
1233 case DRDYNVC_STATE_READY:
1236 if (sf_peer_audin_running(context) != context->audin_open)
1238 if (!sf_peer_audin_running(context))
1239 sf_peer_audin_start(context);
1241 sf_peer_audin_stop(context);
1244 #if defined(CHANNEL_AINPUT_SERVER)
1245 if (sf_peer_ainput_running(context) != context->ainput_open)
1247 if (!sf_peer_ainput_running(context))
1248 sf_peer_ainput_start(context);
1250 sf_peer_ainput_stop(context);
1256 case DRDYNVC_STATE_FAILED:
1263 WLog_INFO(TAG,
"Client %s disconnected.", client->local ?
"(local)" : client->hostname);
1265 WINPR_ASSERT(client->Disconnect);
1266 client->Disconnect(client);
1268 freerdp_peer_context_free(client);
1269 freerdp_peer_free(client);
1273 static BOOL test_peer_accepted(freerdp_listener* instance, freerdp_peer* client)
1275 HANDLE hThread = NULL;
1276 struct server_info* info = NULL;
1278 WINPR_UNUSED(instance);
1280 WINPR_ASSERT(instance);
1281 WINPR_ASSERT(client);
1283 info = instance->info;
1284 client->ContextExtra = info;
1286 if (!(hThread = CreateThread(NULL, 0, test_peer_mainloop, (
void*)client, 0, NULL)))
1289 (void)CloseHandle(hThread);
1293 static void test_server_mainloop(freerdp_listener* instance)
1295 HANDLE handles[32] = { 0 };
1299 WINPR_ASSERT(instance);
1302 WINPR_ASSERT(instance->GetEventHandles);
1303 count = instance->GetEventHandles(instance, handles, 32);
1307 WLog_ERR(TAG,
"Failed to get FreeRDP event handles");
1311 status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
1313 if (WAIT_FAILED == status)
1315 WLog_ERR(TAG,
"select failed");
1319 WINPR_ASSERT(instance->CheckFileDescriptor);
1320 if (instance->CheckFileDescriptor(instance) != TRUE)
1322 WLog_ERR(TAG,
"Failed to check FreeRDP file descriptor");
1327 WINPR_ASSERT(instance->Close);
1328 instance->Close(instance);
1333 const char spcap[7];
1334 const char sfast[7];
1335 const char sport[7];
1336 const char slocal_only[13];
1337 const char scert[7];
1339 } options = {
"--pcap=",
"--fast",
"--port=",
"--local-only",
"--cert=",
"--key=" };
1341 WINPR_PRAGMA_DIAG_PUSH
1342 WINPR_PRAGMA_DIAG_IGNORED_FORMAT_NONLITERAL
1343 WINPR_ATTR_FORMAT_ARG(2, 0)
1344 static
void print_entry(FILE* fp, WINPR_FORMAT_ARG const
char* fmt, const
char* what,
size_t size)
1346 char buffer[32] = { 0 };
1347 strncpy(buffer, what, MIN(size,
sizeof(buffer) - 1));
1348 (void)fprintf(fp, fmt, buffer);
1350 WINPR_PRAGMA_DIAG_POP
1352 static int usage(
const char* app,
const char* invalid)
1356 (void)fprintf(fp,
"Invalid argument '%s'\n", invalid);
1357 (void)fprintf(fp,
"Usage: %s <arg>[ <arg> ...]\n", app);
1358 (void)fprintf(fp,
"Arguments:\n");
1359 print_entry(fp,
"\t%s<pcap file>\n", options.spcap,
sizeof(options.spcap));
1360 print_entry(fp,
"\t%s<cert file>\n", options.scert,
sizeof(options.scert));
1361 print_entry(fp,
"\t%s<key file>\n", options.skey,
sizeof(options.skey));
1362 print_entry(fp,
"\t%s\n", options.sfast,
sizeof(options.sfast));
1363 print_entry(fp,
"\t%s<port>\n", options.sport,
sizeof(options.sport));
1364 print_entry(fp,
"\t%s\n", options.slocal_only,
sizeof(options.slocal_only));
1368 int main(
int argc,
char* argv[])
1371 BOOL started = FALSE;
1373 freerdp_listener* instance = NULL;
1375 char name[MAX_PATH] = { 0 };
1377 BOOL localOnly = FALSE;
1378 struct server_info info = { 0 };
1379 const char* app = argv[0];
1381 info.test_dump_rfx_realtime = TRUE;
1385 for (
int i = 1; i < argc; i++)
1387 char* arg = argv[i];
1389 if (strncmp(arg, options.sfast,
sizeof(options.sfast)) == 0)
1390 info.test_dump_rfx_realtime = FALSE;
1391 else if (strncmp(arg, options.sport,
sizeof(options.sport)) == 0)
1393 const char* sport = &arg[
sizeof(options.sport)];
1394 port = strtol(sport, NULL, 10);
1396 if ((port < 1) || (port > UINT16_MAX) || (errno != 0))
1397 return usage(app, arg);
1399 else if (strncmp(arg, options.slocal_only,
sizeof(options.slocal_only)) == 0)
1401 else if (strncmp(arg, options.spcap,
sizeof(options.spcap)) == 0)
1403 info.test_pcap_file = &arg[
sizeof(options.spcap)];
1404 if (!winpr_PathFileExists(info.test_pcap_file))
1405 return usage(app, arg);
1407 else if (strncmp(arg, options.scert,
sizeof(options.scert)) == 0)
1409 info.cert = &arg[
sizeof(options.scert)];
1410 if (!winpr_PathFileExists(info.cert))
1411 return usage(app, arg);
1413 else if (strncmp(arg, options.skey,
sizeof(options.skey)) == 0)
1415 info.key = &arg[
sizeof(options.skey)];
1416 if (!winpr_PathFileExists(info.key))
1417 return usage(app, arg);
1420 return usage(app, arg);
1423 WTSRegisterWtsApiFunctionTable(FreeRDP_InitWtsApi());
1424 winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT);
1425 instance = freerdp_listener_new();
1431 info.cert =
"server.crt";
1433 info.key =
"server.key";
1435 instance->info = (
void*)&info;
1436 instance->PeerAccepted = test_peer_accepted;
1438 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
1442 (void)sprintf_s(name,
sizeof(name),
"tfreerdp-server.%ld", port);
1443 file = GetKnownSubPath(KNOWN_PATH_TEMP, name);
1450 WINPR_ASSERT(instance->OpenLocal);
1451 started = instance->OpenLocal(instance, file);
1455 WINPR_ASSERT(instance->Open);
1456 started = instance->Open(instance, NULL, (UINT16)port);
1463 test_server_mainloop(instance);
1469 freerdp_listener_free(instance);
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_set_string(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *param)
Sets a string settings value. The param is copied.
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
FREERDP_API BOOL freerdp_settings_set_pointer_len(rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id, const void *data, size_t len)
Set a pointer to value data.
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL param)
Sets a BOOL settings value.