22#include <freerdp/config.h>
27#include <winpr/winpr.h>
29#include <winpr/cast.h>
30#include <winpr/assert.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>
50#include <freerdp/build-config.h>
51#include <freerdp/version.h>
52#include <freerdp/utils/helpers.h>
57#include "sf_encomsp.h"
61#include <freerdp/log.h>
62#define TAG SERVER_TAG("sample")
64#define SAMPLE_SERVER_USE_CLIENT_RESOLUTION 1
65#define SAMPLE_SERVER_DEFAULT_WIDTH 1024
66#define SAMPLE_SERVER_DEFAULT_HEIGHT 768
70 BOOL test_dump_rfx_realtime;
71 const char* test_pcap_file;
72 const char* replay_dump;
77static void test_peer_context_free(freerdp_peer* client, rdpContext* ctx)
79 testPeerContext* context = (testPeerContext*)ctx;
85 winpr_image_free(context->image, TRUE);
86 if (context->debug_channel_thread)
88 WINPR_ASSERT(context->stopEvent);
89 (void)SetEvent(context->stopEvent);
90 (void)WaitForSingleObject(context->debug_channel_thread, INFINITE);
91 (void)CloseHandle(context->debug_channel_thread);
94 Stream_Free(context->s, TRUE);
95 free(context->bg_data);
96 rfx_context_free(context->rfx_context);
97 nsc_context_free(context->nsc_context);
99 if (context->debug_channel)
100 (void)WTSVirtualChannelClose(context->debug_channel);
102 sf_peer_audin_uninit(context);
104#if defined(CHANNEL_AINPUT_SERVER)
105 sf_peer_ainput_uninit(context);
108 rdpsnd_server_context_free(context->rdpsnd);
109 encomsp_server_context_free(context->encomsp);
111 WTSCloseServer(context->vcm);
116static BOOL test_peer_context_new(freerdp_peer* client, rdpContext* ctx)
118 testPeerContext* context = (testPeerContext*)ctx;
120 WINPR_ASSERT(client);
121 WINPR_ASSERT(context);
122 WINPR_ASSERT(ctx->settings);
124 context->image = winpr_image_new();
127 if (!(context->rfx_context = rfx_context_new_ex(
131 if (!rfx_context_reset(context->rfx_context, SAMPLE_SERVER_DEFAULT_WIDTH,
132 SAMPLE_SERVER_DEFAULT_HEIGHT))
137 if (!rfx_context_set_mode(context->rfx_context, WINPR_ASSERTING_INT_CAST(RLGR_MODE, rlgr)))
141 if (!(context->nsc_context = nsc_context_new()))
144 if (!(context->s = Stream_New(
nullptr, 65536)))
147 context->icon_x = UINT32_MAX;
148 context->icon_y = UINT32_MAX;
149 context->vcm = WTSOpenServerA((LPSTR)client->context);
151 if (!context->vcm || context->vcm == INVALID_HANDLE_VALUE)
156 test_peer_context_free(client, ctx);
161static BOOL test_peer_init(freerdp_peer* client)
163 WINPR_ASSERT(client);
165 client->ContextSize =
sizeof(testPeerContext);
166 client->ContextNew = test_peer_context_new;
167 client->ContextFree = test_peer_context_free;
168 return freerdp_peer_context_new(client);
172static wStream* test_peer_stream_init(testPeerContext* context)
174 WINPR_ASSERT(context);
175 WINPR_ASSERT(context->s);
177 Stream_Clear(context->s);
178 Stream_ResetPosition(context->s);
182static void test_peer_begin_frame(freerdp_peer* client)
184 rdpUpdate* update =
nullptr;
186 testPeerContext* context =
nullptr;
188 WINPR_ASSERT(client);
189 WINPR_ASSERT(client->context);
191 update = client->context->update;
192 WINPR_ASSERT(update);
194 context = (testPeerContext*)client->context;
195 WINPR_ASSERT(context);
197 fm.frameAction = SURFACECMD_FRAMEACTION_BEGIN;
198 fm.frameId = context->frame_id;
199 WINPR_ASSERT(update->SurfaceFrameMarker);
200 if (!update->SurfaceFrameMarker(update->context, &fm))
201 WLog_WARN(TAG,
"SurfaceFrameMarker failed");
204static void test_peer_end_frame(freerdp_peer* client)
206 rdpUpdate* update =
nullptr;
208 testPeerContext* context =
nullptr;
210 WINPR_ASSERT(client);
212 context = (testPeerContext*)client->context;
213 WINPR_ASSERT(context);
215 update = client->context->update;
216 WINPR_ASSERT(update);
218 fm.frameAction = SURFACECMD_FRAMEACTION_END;
219 fm.frameId = context->frame_id;
220 WINPR_ASSERT(update->SurfaceFrameMarker);
221 if (!update->SurfaceFrameMarker(update->context, &fm))
222 WLog_WARN(TAG,
"SurfaceFrameMarker failed");
227static BOOL stream_surface_bits_supported(
const rdpSettings* settings)
229 const UINT32 supported =
231 return ((supported & SURFCMDS_STREAM_SURFACE_BITS) != 0);
234static BOOL test_peer_draw_background(freerdp_peer* client,
const RFX_RECT* rect)
238 const UINT32 colorFormat = PIXEL_FORMAT_RGB24;
239 const size_t bpp = FreeRDPGetBytesPerPixel(colorFormat);
241 WINPR_ASSERT(client);
243 testPeerContext* context = (testPeerContext*)client->context;
244 WINPR_ASSERT(context);
246 rdpSettings* settings = client->context->settings;
247 WINPR_ASSERT(settings);
249 rdpUpdate* update = client->context->update;
250 WINPR_ASSERT(update);
259 wStream* s = test_peer_stream_init(context);
260 const size_t size = bpp * rect->width * rect->height;
264 BYTE* rgb_data = malloc(size);
267 WLog_ERR(TAG,
"Problem allocating memory");
271 memset(rgb_data, 0xA0, size);
273 if (RemoteFxCodec && stream_surface_bits_supported(settings))
275 WLog_DBG(TAG,
"Using RemoteFX codec");
276 rfx_context_set_pixel_format(context->rfx_context, colorFormat);
278 WINPR_ASSERT(bpp <= UINT16_MAX);
279 RFX_RECT rrect = { .x = 0, .y = 0, .width = rect->width, .height = rect->height };
281 if (!rfx_compose_message(context->rfx_context, s, &rrect, 1, rgb_data, rect->width,
282 rect->height, (UINT32)(bpp * rect->width)))
287 const UINT32 RemoteFxCodecId =
289 WINPR_ASSERT(RemoteFxCodecId <= UINT16_MAX);
290 cmd.bmp.codecID = (UINT16)RemoteFxCodecId;
291 cmd.cmdType = CMDTYPE_STREAM_SURFACE_BITS;
295 WLog_DBG(TAG,
"Using NSCodec");
296 if (!nsc_context_set_parameters(context->nsc_context, NSC_COLOR_FORMAT, colorFormat))
299 WINPR_ASSERT(bpp <= UINT16_MAX);
300 if (!nsc_compose_message(context->nsc_context, s, rgb_data, rect->width, rect->height,
301 (UINT32)(bpp * rect->width)))
304 WINPR_ASSERT(NSCodecId <= UINT16_MAX);
305 cmd.bmp.codecID = (UINT16)NSCodecId;
306 cmd.cmdType = CMDTYPE_SET_SURFACE_BITS;
309 cmd.destLeft = rect->x;
310 cmd.destTop = rect->y;
311 cmd.destRight = rect->x + rect->width;
312 cmd.destBottom = rect->y + rect->height;
315 cmd.bmp.width = rect->width;
316 cmd.bmp.height = rect->height;
318 cmd.bmp.bitmapDataLength = WINPR_ASSERTING_INT_CAST(UINT32, Stream_GetPosition(s));
319 cmd.bmp.bitmapData = Stream_Buffer(s);
321 ret = update->SurfaceBits(update->context, &cmd);
328static int open_icon(
wImage* img)
330 char* paths[] = { SAMPLE_RESOURCE_ROOT,
"." };
331 const char* names[] = {
"test_icon.webp",
"test_icon.png",
"test_icon.jpg",
"test_icon.bmp" };
333 for (
size_t x = 0; x < ARRAYSIZE(paths); x++)
335 const char* path = paths[x];
336 if (!winpr_PathFileExists(path))
339 for (
size_t y = 0; y < ARRAYSIZE(names); y++)
341 const char* name = names[y];
342 char* file = GetCombinedPath(path, name);
343 int rc = winpr_image_read(img, file);
349 WLog_ERR(TAG,
"Unable to open test icon");
354static BOOL test_peer_load_icon(freerdp_peer* client)
356 testPeerContext* context =
nullptr;
357 rdpSettings* settings =
nullptr;
359 WINPR_ASSERT(client);
361 context = (testPeerContext*)client->context;
362 WINPR_ASSERT(context);
364 settings = client->context->settings;
365 WINPR_ASSERT(settings);
370 WLog_ERR(TAG,
"Client doesn't support RemoteFX or NSCodec");
374 int rc = open_icon(context->image);
379 if (!(context->bg_data = calloc(context->image->height, 3ULL * context->image->width)))
382 memset(context->bg_data, 0xA0, 3ULL * context->image->height * context->image->width);
385 context->bg_data =
nullptr;
389static void test_send_cursor_update(freerdp_peer* client, UINT32 x, UINT32 y)
391 WINPR_ASSERT(client);
393 testPeerContext* context = (testPeerContext*)client->context;
394 WINPR_ASSERT(context);
396 rdpSettings* settings = client->context->settings;
400 if (context->image->width < 1 || !context->activated)
405 .width = WINPR_ASSERTING_INT_CAST(UINT16, context->image->width),
406 .height = WINPR_ASSERTING_INT_CAST(UINT16, context->image->height) };
410 if (context->icon_x + context->image->width > w)
412 if (context->icon_y + context->image->height > h)
414 if (x + context->image->width > w)
416 if (y + context->image->height > h)
420 if (RemoteFxCodec && stream_surface_bits_supported(settings))
422 const UINT32 RemoteFxCodecId =
424 WINPR_ASSERT(RemoteFxCodecId <= UINT16_MAX);
425 cmd.bmp.codecID = (UINT16)RemoteFxCodecId;
426 cmd.cmdType = CMDTYPE_STREAM_SURFACE_BITS;
431 WINPR_ASSERT(NSCodecId <= UINT16_MAX);
432 cmd.bmp.codecID = (UINT16)NSCodecId;
433 cmd.cmdType = CMDTYPE_SET_SURFACE_BITS;
436 wStream* s = test_peer_stream_init(context);
439 const UINT32 colorFormat =
440 context->image->bitsPerPixel > 24 ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_BGR24;
444 rfx_context_set_pixel_format(context->rfx_context, colorFormat);
445 if (!rfx_compose_message(context->rfx_context, s, &rect, 1, context->image->data,
446 rect.width, rect.height, context->image->scanline))
447 WLog_WARN(TAG,
"rfx_compose_message failed");
451 if (!nsc_context_set_parameters(context->nsc_context, NSC_COLOR_FORMAT, colorFormat))
452 WLog_WARN(TAG,
"nsc_context_set_parameters failed");
453 else if (!nsc_compose_message(context->nsc_context, s, context->image->data, rect.width,
454 rect.height, context->image->scanline))
455 WLog_WARN(TAG,
"rfx_compose_message failed");
461 cmd.destRight = x + rect.width;
462 cmd.destBottom = y + rect.height;
464 cmd.bmp.width = rect.width;
465 cmd.bmp.height = rect.height;
466 cmd.bmp.bitmapDataLength = (UINT32)Stream_GetPosition(s);
467 cmd.bmp.bitmapData = Stream_Buffer(s);
469 rdpUpdate* update = client->context->update;
470 WINPR_ASSERT(update);
471 WINPR_ASSERT(update->SurfaceBits);
472 if (!update->SurfaceBits(update->context, &cmd))
474 WLog_WARN(TAG,
"update->SurfaceBits failed");
480static void test_peer_draw_icon(freerdp_peer* client, UINT32 x, UINT32 y)
482 WINPR_ASSERT(client);
484 rdpSettings* settings = client->context->settings;
485 WINPR_ASSERT(settings);
490 test_peer_begin_frame(client);
492 testPeerContext* context = (testPeerContext*)client->context;
493 if ((context->icon_x != UINT32_MAX) && (context->icon_y != UINT32_MAX))
495 RFX_RECT rect = { .x = WINPR_ASSERTING_INT_CAST(uint16_t, context->icon_x),
496 .y = WINPR_ASSERTING_INT_CAST(uint16_t, context->icon_y),
497 .width = WINPR_ASSERTING_INT_CAST(uint16_t, context->image->width),
498 .height = WINPR_ASSERTING_INT_CAST(uint16_t, context->image->height) };
500 test_peer_draw_background(client, &rect);
502 test_send_cursor_update(client, x, y);
503 test_peer_end_frame(client);
507static BOOL test_sleep_tsdiff(UINT32* old_sec, UINT32* old_usec, UINT32 new_sec, UINT32 new_usec)
512 WINPR_ASSERT(old_sec);
513 WINPR_ASSERT(old_usec);
515 if ((*old_sec == 0) && (*old_usec == 0))
518 *old_usec = new_usec;
522 sec = new_sec - *old_sec;
523 usec = new_usec - *old_usec;
525 if ((sec < 0) || ((sec == 0) && (usec < 0)))
527 WLog_ERR(TAG,
"Invalid time stamp detected.");
532 *old_usec = new_usec;
541 Sleep((DWORD)sec * 1000);
549static BOOL tf_peer_dump_rfx(freerdp_peer* client)
553 UINT32 prev_seconds = 0;
554 UINT32 prev_useconds = 0;
555 rdpUpdate* update =
nullptr;
556 rdpPcap* pcap_rfx =
nullptr;
557 pcap_record record = WINPR_C_ARRAY_INIT;
559 WINPR_ASSERT(client);
560 WINPR_ASSERT(client->context);
562 struct server_info* info = client->ContextExtra;
565 s = Stream_New(
nullptr, 512);
570 update = client->context->update;
571 WINPR_ASSERT(update);
573 pcap_rfx = pcap_open(info->test_pcap_file, FALSE);
577 prev_seconds = prev_useconds = 0;
579 while (pcap_has_next_record(pcap_rfx))
581 if (!pcap_get_next_record_header(pcap_rfx, &record))
584 if (!Stream_EnsureCapacity(s, record.length))
587 record.data = Stream_Buffer(s);
588 if (!pcap_get_next_record_content(pcap_rfx, &record))
590 if (!Stream_SetPosition(s, Stream_Capacity(s)))
593 if (info->test_dump_rfx_realtime &&
594 test_sleep_tsdiff(&prev_seconds, &prev_useconds, record.header.ts_sec,
595 record.header.ts_usec) == FALSE)
598 WINPR_ASSERT(update->SurfaceCommand);
599 if (!update->SurfaceCommand(update->context, s))
602 WINPR_ASSERT(client->CheckFileDescriptor);
603 if (client->CheckFileDescriptor(client) != TRUE)
609 Stream_Free(s, TRUE);
610 pcap_close(pcap_rfx);
615static DWORD WINAPI tf_debug_channel_thread_func(LPVOID arg)
618 void* buffer =
nullptr;
619 DWORD BytesReturned = 0;
621 testPeerContext* context = (testPeerContext*)arg;
623 WINPR_ASSERT(context);
624 if (WTSVirtualChannelQuery(context->debug_channel, WTSVirtualFileHandle, &buffer,
625 &BytesReturned) == TRUE)
627 fd = *((
void**)buffer);
628 WTSFreeMemory(buffer);
630 if (!(context->event = CreateWaitObjectEvent(
nullptr, TRUE, FALSE, fd)))
634 wStream* s = Stream_New(
nullptr, 4096);
638 if (!WTSVirtualChannelWrite(context->debug_channel, (PCHAR)
"test1", 5, &written))
645 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
647 handles[nCount++] = context->event;
648 handles[nCount++] = freerdp_abort_event(&context->_p);
649 handles[nCount++] = context->stopEvent;
650 status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
659 Stream_ResetPosition(s);
661 if (WTSVirtualChannelRead(context->debug_channel, 0, Stream_BufferAs(s,
char),
662 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
664 if (BytesReturned == 0)
667 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
670 if (WTSVirtualChannelRead(context->debug_channel, 0, Stream_BufferAs(s,
char),
671 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
678 if (!Stream_SetPosition(s, BytesReturned))
680 WLog_DBG(TAG,
"got %" PRIu32
" bytes", BytesReturned);
683 Stream_Free(s, TRUE);
688static BOOL tf_peer_post_connect(freerdp_peer* client)
690 testPeerContext* context =
nullptr;
691 rdpSettings* settings =
nullptr;
693 WINPR_ASSERT(client);
695 context = (testPeerContext*)client->context;
696 WINPR_ASSERT(context);
698 settings = client->context->settings;
699 WINPR_ASSERT(settings);
707 WLog_DBG(TAG,
"Client %s is activated (osMajorType %" PRIu32
" osMinorType %" PRIu32
")",
708 client->local ?
"(local)" : client->hostname,
716 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 !=
nullptr)
758 WLog_DBG(TAG,
"Open channel rdpdbg.");
760 if (!(context->stopEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr)))
762 WLog_ERR(TAG,
"Failed to create stop event");
766 if (!(context->debug_channel_thread = CreateThread(
767 nullptr, 0, tf_debug_channel_thread_func, (
void*)context, 0,
nullptr)))
769 WLog_ERR(TAG,
"Failed to create debug channel thread");
770 (void)CloseHandle(context->stopEvent);
771 context->stopEvent =
nullptr;
777 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, RDPSND_CHANNEL_NAME))
779 if (!sf_peer_rdpsnd_init(context))
783 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, ENCOMSP_SVC_CHANNEL_NAME))
785 if (!sf_peer_encomsp_init(context))
790 if (!sf_peer_audin_init(context))
793#if defined(CHANNEL_AINPUT_SERVER)
794 sf_peer_ainput_init(context);
802static BOOL tf_peer_activate(freerdp_peer* client)
805 WINPR_ASSERT(client);
807 testPeerContext* context = (testPeerContext*)client->context;
808 WINPR_ASSERT(context);
810 rdpSettings* settings = client->context->settings;
811 WINPR_ASSERT(settings);
813 struct server_info* info = client->ContextExtra;
816 context->activated = TRUE;
823 if (info->test_pcap_file !=
nullptr)
828 if (!tf_peer_dump_rfx(client))
839 test_peer_begin_frame(client);
840 rc = test_peer_draw_background(client, &rect);
841 test_peer_end_frame(client);
848static BOOL tf_peer_synchronize_event(rdpInput* input, UINT32 flags)
852 WLog_DBG(TAG,
"Client sent a synchronize event (flags:0x%" PRIX32
")", flags);
857static BOOL tf_peer_keyboard_event(rdpInput* input, UINT16 flags, UINT8 code)
859 freerdp_peer* client =
nullptr;
860 rdpUpdate* update =
nullptr;
861 rdpContext* context =
nullptr;
862 testPeerContext* tcontext =
nullptr;
863 rdpSettings* settings =
nullptr;
867 context = input->context;
868 WINPR_ASSERT(context);
870 client = context->peer;
871 WINPR_ASSERT(client);
873 settings = context->settings;
874 WINPR_ASSERT(settings);
876 update = context->update;
877 WINPR_ASSERT(update);
879 tcontext = (testPeerContext*)context;
880 WINPR_ASSERT(tcontext);
882 WLog_DBG(TAG,
"Client sent a keyboard event (flags:0x%04" PRIX16
" code:0x%04" PRIX8
")", flags,
885 if (((flags & KBD_FLAGS_RELEASE) == 0) && (code == RDP_SCANCODE_KEY_G))
897 SAMPLE_SERVER_DEFAULT_WIDTH))
900 SAMPLE_SERVER_DEFAULT_HEIGHT))
904 if (!rfx_context_reset(tcontext->rfx_context,
909 WINPR_ASSERT(update->DesktopResize);
910 if (!update->DesktopResize(update->context))
912 tcontext->activated = FALSE;
914 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_C)
916 if (tcontext->debug_channel)
919 if (!WTSVirtualChannelWrite(tcontext->debug_channel, (PCHAR)
"test2", 5, &written))
923 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_X)
925 WINPR_ASSERT(client->Close);
926 if (!client->Close(client))
929 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_R)
931 tcontext->audin_open = !tcontext->audin_open;
933#if defined(CHANNEL_AINPUT_SERVER)
934 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_I)
936 tcontext->ainput_open = !tcontext->ainput_open;
939 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_S)
947static BOOL tf_peer_unicode_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
953 "Client sent a unicode keyboard event (flags:0x%04" PRIX16
" code:0x%04" PRIX16
")",
959static BOOL tf_peer_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
963 WINPR_ASSERT(input->context);
965 WLog_DBG(TAG,
"Client sent a mouse event (flags:0x%04" PRIX16
" pos:%" PRIu16
",%" PRIu16
")",
967 test_peer_draw_icon(input->context->peer, x + 10, y);
972static UINT32 add(UINT32 old, UINT32 max, INT16 diff)
980 return WINPR_ASSERTING_INT_CAST(uint32_t, val);
984static BOOL tf_peer_rel_mouse_event(rdpInput* input, UINT16 flags, INT16 xDelta, INT16 yDelta)
988 WINPR_ASSERT(input->context);
993 static UINT32 xpos = 0;
994 static UINT32 ypos = 0;
996 xpos = add(xpos, w, xDelta);
997 ypos = add(ypos, h, yDelta);
1000 "Client sent a relative mouse event (flags:0x%04" PRIX16
" pos:%" PRId16
",%" PRId16
1002 flags, xDelta, yDelta);
1003 test_peer_draw_icon(input->context->peer, xpos + 10, ypos);
1008static BOOL tf_peer_extended_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
1010 WINPR_UNUSED(flags);
1011 WINPR_ASSERT(input);
1012 WINPR_ASSERT(input->context);
1015 "Client sent an extended mouse event (flags:0x%04" PRIX16
" pos:%" PRIu16
",%" PRIu16
1018 test_peer_draw_icon(input->context->peer, x + 10, y);
1023static BOOL tf_peer_refresh_rect(rdpContext* context, BYTE count,
const RECTANGLE_16* areas)
1025 WINPR_UNUSED(context);
1026 WINPR_ASSERT(context);
1027 WINPR_ASSERT(areas || (count == 0));
1029 WLog_DBG(TAG,
"Client requested to refresh:");
1031 for (BYTE i = 0; i < count; i++)
1033 WLog_DBG(TAG,
" (%" PRIu16
", %" PRIu16
") (%" PRIu16
", %" PRIu16
")", areas[i].left,
1034 areas[i].top, areas[i].right, areas[i].bottom);
1041static BOOL tf_peer_suppress_output(rdpContext* context, BYTE allow,
const RECTANGLE_16* area)
1043 WINPR_UNUSED(context);
1049 "Client restore output (%" PRIu16
", %" PRIu16
") (%" PRIu16
", %" PRIu16
").",
1050 area->left, area->top, area->right, area->bottom);
1054 WLog_DBG(TAG,
"Client minimized and suppress output.");
1061static int hook_peer_write_pdu(rdpTransport* transport,
wStream* s)
1068 rdpContext* context = transport_get_context(transport);
1070 WINPR_ASSERT(context);
1073 freerdp_peer* client = context->peer;
1074 WINPR_ASSERT(client);
1076 testPeerContext* peerCtx = (testPeerContext*)client->context;
1077 WINPR_ASSERT(peerCtx);
1078 WINPR_ASSERT(peerCtx->io.WritePdu);
1087 CONNECTION_STATE state = freerdp_get_state(context);
1088 if (state < CONNECTION_STATE_NEGO)
1089 return peerCtx->io.WritePdu(transport, s);
1091 ls = Stream_New(
nullptr, 4096);
1095 while (stream_dump_get(context, &flags, ls, &offset, &ts) > 0)
1099 if (flags & STREAM_MSG_SRV_TX)
1101 if ((last_ts > 0) && (ts > last_ts))
1103 UINT64 diff = ts - last_ts;
1106 UINT32 d = diff > UINT32_MAX ? UINT32_MAX : (UINT32)diff;
1112 rc = peerCtx->io.WritePdu(transport, ls);
1116 Stream_ResetPosition(ls);
1120 Stream_Free(ls, TRUE);
1125static DWORD WINAPI test_peer_mainloop(LPVOID arg)
1128 DWORD error = CHANNEL_RC_OK;
1129 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
1132 testPeerContext* context =
nullptr;
1133 rdpSettings* settings =
nullptr;
1134 rdpInput* input =
nullptr;
1135 rdpUpdate* update =
nullptr;
1136 freerdp_peer* client = (freerdp_peer*)arg;
1138 WINPR_ASSERT(client);
1140 struct server_info* info = client->ContextExtra;
1143 if (!test_peer_init(client))
1145 freerdp_peer_free(client);
1150 WINPR_ASSERT(client->context);
1151 settings = client->context->settings;
1152 WINPR_ASSERT(settings);
1153 if (info->replay_dump)
1161 rdpPrivateKey* key = freerdp_key_new_from_file_enc(info->key,
nullptr);
1168 rdpCertificate* cert = freerdp_certificate_new_from_file(info->cert);
1184 ENCRYPTION_LEVEL_CLIENT_COMPATIBLE))
1202 client->PostConnect = tf_peer_post_connect;
1203 client->Activate = tf_peer_activate;
1205 WINPR_ASSERT(client->context);
1206 input = client->context->input;
1207 WINPR_ASSERT(input);
1209 input->SynchronizeEvent = tf_peer_synchronize_event;
1210 input->KeyboardEvent = tf_peer_keyboard_event;
1211 input->UnicodeKeyboardEvent = tf_peer_unicode_keyboard_event;
1212 input->MouseEvent = tf_peer_mouse_event;
1213 input->RelMouseEvent = tf_peer_rel_mouse_event;
1214 input->ExtendedMouseEvent = tf_peer_extended_mouse_event;
1216 update = client->context->update;
1217 WINPR_ASSERT(update);
1219 update->RefreshRect = tf_peer_refresh_rect;
1220 update->SuppressOutput = tf_peer_suppress_output;
1225 WINPR_ASSERT(client->Initialize);
1226 rc = client->Initialize(client);
1230 context = (testPeerContext*)client->context;
1231 WINPR_ASSERT(context);
1233 if (info->replay_dump)
1235 const rdpTransportIo* cb = freerdp_get_io_callbacks(client->context);
1236 rdpTransportIo replay;
1241 replay.WritePdu = hook_peer_write_pdu;
1242 if (!freerdp_set_io_callbacks(client->context, &replay))
1246 WLog_INFO(TAG,
"We've got a client %s", client->local ?
"(local)" : client->hostname);
1248 while (error == CHANNEL_RC_OK)
1252 WINPR_ASSERT(client->GetEventHandles);
1253 DWORD tmp = client->GetEventHandles(client, &handles[count], 32 - count);
1257 WLog_ERR(TAG,
"Failed to get FreeRDP transport event handles");
1264 HANDLE channelHandle = WTSVirtualChannelManagerGetEventHandle(context->vcm);
1265 handles[count++] = channelHandle;
1266 status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
1268 if (status == WAIT_FAILED)
1270 WLog_ERR(TAG,
"WaitForMultipleObjects failed (errno: %d)", errno);
1274 WINPR_ASSERT(client->CheckFileDescriptor);
1275 if (client->CheckFileDescriptor(client) != TRUE)
1278 if (WTSVirtualChannelManagerCheckFileDescriptor(context->vcm) != TRUE)
1282 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, DRDYNVC_SVC_CHANNEL_NAME))
1284 switch (WTSVirtualChannelManagerGetDrdynvcState(context->vcm))
1286 case DRDYNVC_STATE_NONE:
1289 case DRDYNVC_STATE_INITIALIZED:
1292 case DRDYNVC_STATE_READY:
1295 if (sf_peer_audin_running(context) != context->audin_open)
1297 if (!sf_peer_audin_running(context))
1298 sf_peer_audin_start(context);
1300 sf_peer_audin_stop(context);
1303#if defined(CHANNEL_AINPUT_SERVER)
1304 if (sf_peer_ainput_running(context) != context->ainput_open)
1306 if (!sf_peer_ainput_running(context))
1307 sf_peer_ainput_start(context);
1309 sf_peer_ainput_stop(context);
1315 case DRDYNVC_STATE_FAILED:
1322 WLog_INFO(TAG,
"Client %s disconnected.", client->local ?
"(local)" : client->hostname);
1324 WINPR_ASSERT(client->Disconnect);
1325 client->Disconnect(client);
1327 freerdp_peer_context_free(client);
1328 freerdp_peer_free(client);
1333static BOOL test_peer_accepted(freerdp_listener* instance, freerdp_peer* client)
1335 HANDLE hThread =
nullptr;
1337 WINPR_UNUSED(instance);
1339 WINPR_ASSERT(instance);
1340 WINPR_ASSERT(client);
1342 struct server_info* info = instance->info;
1343 client->ContextExtra = info;
1345 if (!(hThread = CreateThread(
nullptr, 0, test_peer_mainloop, (
void*)client, 0,
nullptr)))
1348 (void)CloseHandle(hThread);
1352static void test_server_mainloop(freerdp_listener* instance)
1354 HANDLE handles[32] = WINPR_C_ARRAY_INIT;
1358 WINPR_ASSERT(instance);
1361 WINPR_ASSERT(instance->GetEventHandles);
1362 count = instance->GetEventHandles(instance, handles, 32);
1366 WLog_ERR(TAG,
"Failed to get FreeRDP event handles");
1370 status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
1372 if (WAIT_FAILED == status)
1374 WLog_ERR(TAG,
"select failed");
1378 WINPR_ASSERT(instance->CheckFileDescriptor);
1379 if (instance->CheckFileDescriptor(instance) != TRUE)
1381 WLog_ERR(TAG,
"Failed to check FreeRDP file descriptor");
1386 WINPR_ASSERT(instance->Close);
1387 instance->Close(instance);
1392 const char spcap[7];
1393 const char sfast[7];
1394 const char sport[7];
1395 const char slocal_only[13];
1396 const char scert[7];
1398} options = { {
'-',
'-',
'p',
'c',
'a',
'p',
'=' },
1399 {
'-',
'-',
'f',
'a',
's',
't' },
1400 {
'-',
'-',
'p',
'o',
'r',
't',
'=' },
1401 {
'-',
'-',
'l',
'o',
'c',
'a',
'l',
'-',
'o',
'n',
'l',
'y' },
1402 {
'-',
'-',
'c',
'e',
'r',
't',
'=' },
1403 {
'-',
'-',
'k',
'e',
'y',
'=' } };
1405WINPR_PRAGMA_DIAG_PUSH
1406WINPR_PRAGMA_DIAG_IGNORED_FORMAT_NONLITERAL
1407WINPR_ATTR_FORMAT_ARG(2, 0)
1408static
void print_entry(FILE* fp, WINPR_FORMAT_ARG const
char* fmt, const
char* what,
size_t size)
1410 char buffer[32] = WINPR_C_ARRAY_INIT;
1411 strncpy(buffer, what, MIN(size,
sizeof(buffer) - 1));
1412 (void)fprintf(fp, fmt, buffer);
1414WINPR_PRAGMA_DIAG_POP
1417static int usage(
const char* app,
const char* invalid)
1421 (void)fprintf(fp,
"Invalid argument '%s'\n", invalid);
1422 (void)fprintf(fp,
"Usage: %s <arg>[ <arg> ...]\n", app);
1423 (void)fprintf(fp,
"Arguments:\n");
1424 print_entry(fp,
"\t%s<pcap file>\n", options.spcap,
sizeof(options.spcap));
1425 print_entry(fp,
"\t%s<cert file>\n", options.scert,
sizeof(options.scert));
1426 print_entry(fp,
"\t%s<key file>\n", options.skey,
sizeof(options.skey));
1427 print_entry(fp,
"\t%s\n", options.sfast,
sizeof(options.sfast));
1428 print_entry(fp,
"\t%s<port>\n", options.sport,
sizeof(options.sport));
1429 print_entry(fp,
"\t%s\n", options.slocal_only,
sizeof(options.slocal_only));
1433int main(
int argc,
char* argv[])
1436 BOOL started = FALSE;
1437 WSADATA wsaData = WINPR_C_ARRAY_INIT;
1438 freerdp_listener* instance =
nullptr;
1439 char* file =
nullptr;
1440 char name[MAX_PATH] = WINPR_C_ARRAY_INIT;
1442 BOOL localOnly = FALSE;
1443 struct server_info info = WINPR_C_ARRAY_INIT;
1444 const char* app = argv[0];
1446 info.test_dump_rfx_realtime = TRUE;
1459 if (!freerdp_setApplicationDetails(FREERDP_VENDOR_STRING,
"TFreeRDP", FREERDP_VERSION_MAJOR))
1464 for (
int i = 1; i < argc; i++)
1466 char* arg = argv[i];
1468 if (strncmp(arg, options.sfast,
sizeof(options.sfast)) == 0)
1469 info.test_dump_rfx_realtime = FALSE;
1470 else if (strncmp(arg, options.sport,
sizeof(options.sport)) == 0)
1472 const char* sport = &arg[
sizeof(options.sport)];
1473 port = strtol(sport,
nullptr, 10);
1475 if ((port < 1) || (port > UINT16_MAX) || (errno != 0))
1476 return usage(app, arg);
1478 else if (strncmp(arg, options.slocal_only,
sizeof(options.slocal_only)) == 0)
1480 else if (strncmp(arg, options.spcap,
sizeof(options.spcap)) == 0)
1482 info.test_pcap_file = &arg[
sizeof(options.spcap)];
1483 if (!winpr_PathFileExists(info.test_pcap_file))
1484 return usage(app, arg);
1486 else if (strncmp(arg, options.scert,
sizeof(options.scert)) == 0)
1488 info.cert = &arg[
sizeof(options.scert)];
1489 if (!winpr_PathFileExists(info.cert))
1490 return usage(app, arg);
1492 else if (strncmp(arg, options.skey,
sizeof(options.skey)) == 0)
1494 info.key = &arg[
sizeof(options.skey)];
1495 if (!winpr_PathFileExists(info.key))
1496 return usage(app, arg);
1499 return usage(app, arg);
1502 if (!WTSRegisterWtsApiFunctionTable(FreeRDP_InitWtsApi()))
1505 if (!winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT))
1508 instance = freerdp_listener_new();
1514 info.cert =
"server.crt";
1516 info.key =
"server.key";
1518 instance->info = (
void*)&info;
1519 instance->PeerAccepted = test_peer_accepted;
1521 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
1525 (void)sprintf_s(name,
sizeof(name),
"tfreerdp-server.%ld", port);
1526 file = GetKnownSubPath(KNOWN_PATH_TEMP, name);
1533 WINPR_ASSERT(instance->OpenLocal);
1534 started = instance->OpenLocal(instance, file);
1538 WINPR_ASSERT(instance->Open);
1539 started = instance->Open(instance,
nullptr, (UINT16)port);
1546 test_server_mainloop(instance);
1552 freerdp_listener_free(instance);
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL val)
Sets a BOOL settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
WINPR_ATTR_NODISCARD 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.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_string(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *val)
Sets a string settings value. The param is copied.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.