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>
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;
74static 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);
113static BOOL test_peer_context_new(freerdp_peer* client, rdpContext* ctx)
115 testPeerContext* context = (testPeerContext*)ctx;
117 WINPR_ASSERT(client);
118 WINPR_ASSERT(context);
119 WINPR_ASSERT(ctx->settings);
121 context->image = winpr_image_new();
124 if (!(context->rfx_context = rfx_context_new_ex(
128 if (!rfx_context_reset(context->rfx_context, SAMPLE_SERVER_DEFAULT_WIDTH,
129 SAMPLE_SERVER_DEFAULT_HEIGHT))
134 if (!rfx_context_set_mode(context->rfx_context, WINPR_ASSERTING_INT_CAST(RLGR_MODE, rlgr)))
138 if (!(context->nsc_context = nsc_context_new()))
141 if (!(context->s = Stream_New(
nullptr, 65536)))
144 context->icon_x = UINT32_MAX;
145 context->icon_y = UINT32_MAX;
146 context->vcm = WTSOpenServerA((LPSTR)client->context);
148 if (!context->vcm || context->vcm == INVALID_HANDLE_VALUE)
153 test_peer_context_free(client, ctx);
158static BOOL test_peer_init(freerdp_peer* client)
160 WINPR_ASSERT(client);
162 client->ContextSize =
sizeof(testPeerContext);
163 client->ContextNew = test_peer_context_new;
164 client->ContextFree = test_peer_context_free;
165 return freerdp_peer_context_new(client);
169static wStream* test_peer_stream_init(testPeerContext* context)
171 WINPR_ASSERT(context);
172 WINPR_ASSERT(context->s);
174 Stream_Clear(context->s);
175 Stream_SetPosition(context->s, 0);
179static void test_peer_begin_frame(freerdp_peer* client)
181 rdpUpdate* update =
nullptr;
183 testPeerContext* context =
nullptr;
185 WINPR_ASSERT(client);
186 WINPR_ASSERT(client->context);
188 update = client->context->update;
189 WINPR_ASSERT(update);
191 context = (testPeerContext*)client->context;
192 WINPR_ASSERT(context);
194 fm.frameAction = SURFACECMD_FRAMEACTION_BEGIN;
195 fm.frameId = context->frame_id;
196 WINPR_ASSERT(update->SurfaceFrameMarker);
197 if (!update->SurfaceFrameMarker(update->context, &fm))
198 WLog_WARN(TAG,
"SurfaceFrameMarker failed");
201static void test_peer_end_frame(freerdp_peer* client)
203 rdpUpdate* update =
nullptr;
205 testPeerContext* context =
nullptr;
207 WINPR_ASSERT(client);
209 context = (testPeerContext*)client->context;
210 WINPR_ASSERT(context);
212 update = client->context->update;
213 WINPR_ASSERT(update);
215 fm.frameAction = SURFACECMD_FRAMEACTION_END;
216 fm.frameId = context->frame_id;
217 WINPR_ASSERT(update->SurfaceFrameMarker);
218 if (!update->SurfaceFrameMarker(update->context, &fm))
219 WLog_WARN(TAG,
"SurfaceFrameMarker failed");
224static BOOL stream_surface_bits_supported(
const rdpSettings* settings)
226 const UINT32 supported =
228 return ((supported & SURFCMDS_STREAM_SURFACE_BITS) != 0);
231static BOOL test_peer_draw_background(freerdp_peer* client,
const RFX_RECT* rect)
235 const UINT32 colorFormat = PIXEL_FORMAT_RGB24;
236 const size_t bpp = FreeRDPGetBytesPerPixel(colorFormat);
238 WINPR_ASSERT(client);
240 testPeerContext* context = (testPeerContext*)client->context;
241 WINPR_ASSERT(context);
243 rdpSettings* settings = client->context->settings;
244 WINPR_ASSERT(settings);
246 rdpUpdate* update = client->context->update;
247 WINPR_ASSERT(update);
256 wStream* s = test_peer_stream_init(context);
257 const size_t size = bpp * rect->width * rect->height;
261 BYTE* rgb_data = malloc(size);
264 WLog_ERR(TAG,
"Problem allocating memory");
268 memset(rgb_data, 0xA0, size);
270 if (RemoteFxCodec && stream_surface_bits_supported(settings))
272 WLog_DBG(TAG,
"Using RemoteFX codec");
273 rfx_context_set_pixel_format(context->rfx_context, colorFormat);
275 WINPR_ASSERT(bpp <= UINT16_MAX);
276 RFX_RECT rrect = { .x = 0, .y = 0, .width = rect->width, .height = rect->height };
278 if (!rfx_compose_message(context->rfx_context, s, &rrect, 1, rgb_data, rect->width,
279 rect->height, (UINT32)(bpp * rect->width)))
284 const UINT32 RemoteFxCodecId =
286 WINPR_ASSERT(RemoteFxCodecId <= UINT16_MAX);
287 cmd.bmp.codecID = (UINT16)RemoteFxCodecId;
288 cmd.cmdType = CMDTYPE_STREAM_SURFACE_BITS;
292 WLog_DBG(TAG,
"Using NSCodec");
293 if (!nsc_context_set_parameters(context->nsc_context, NSC_COLOR_FORMAT, colorFormat))
296 WINPR_ASSERT(bpp <= UINT16_MAX);
297 if (!nsc_compose_message(context->nsc_context, s, rgb_data, rect->width, rect->height,
298 (UINT32)(bpp * rect->width)))
301 WINPR_ASSERT(NSCodecId <= UINT16_MAX);
302 cmd.bmp.codecID = (UINT16)NSCodecId;
303 cmd.cmdType = CMDTYPE_SET_SURFACE_BITS;
306 cmd.destLeft = rect->x;
307 cmd.destTop = rect->y;
308 cmd.destRight = rect->x + rect->width;
309 cmd.destBottom = rect->y + rect->height;
312 cmd.bmp.width = rect->width;
313 cmd.bmp.height = rect->height;
314 WINPR_ASSERT(Stream_GetPosition(s) <= UINT32_MAX);
315 cmd.bmp.bitmapDataLength = (UINT32)Stream_GetPosition(s);
316 cmd.bmp.bitmapData = Stream_Buffer(s);
318 ret = update->SurfaceBits(update->context, &cmd);
325static int open_icon(
wImage* img)
327 char* paths[] = { SAMPLE_RESOURCE_ROOT,
"." };
328 const char* names[] = {
"test_icon.webp",
"test_icon.png",
"test_icon.jpg",
"test_icon.bmp" };
330 for (
size_t x = 0; x < ARRAYSIZE(paths); x++)
332 const char* path = paths[x];
333 if (!winpr_PathFileExists(path))
336 for (
size_t y = 0; y < ARRAYSIZE(names); y++)
338 const char* name = names[y];
339 char* file = GetCombinedPath(path, name);
340 int rc = winpr_image_read(img, file);
346 WLog_ERR(TAG,
"Unable to open test icon");
351static BOOL test_peer_load_icon(freerdp_peer* client)
353 testPeerContext* context =
nullptr;
354 rdpSettings* settings =
nullptr;
356 WINPR_ASSERT(client);
358 context = (testPeerContext*)client->context;
359 WINPR_ASSERT(context);
361 settings = client->context->settings;
362 WINPR_ASSERT(settings);
367 WLog_ERR(TAG,
"Client doesn't support RemoteFX or NSCodec");
371 int rc = open_icon(context->image);
376 if (!(context->bg_data = calloc(context->image->height, 3ULL * context->image->width)))
379 memset(context->bg_data, 0xA0, 3ULL * context->image->height * context->image->width);
382 context->bg_data =
nullptr;
386static void test_send_cursor_update(freerdp_peer* client, UINT32 x, UINT32 y)
388 WINPR_ASSERT(client);
390 testPeerContext* context = (testPeerContext*)client->context;
391 WINPR_ASSERT(context);
393 rdpSettings* settings = client->context->settings;
397 if (context->image->width < 1 || !context->activated)
402 .width = WINPR_ASSERTING_INT_CAST(UINT16, context->image->width),
403 .height = WINPR_ASSERTING_INT_CAST(UINT16, context->image->height) };
407 if (context->icon_x + context->image->width > w)
409 if (context->icon_y + context->image->height > h)
411 if (x + context->image->width > w)
413 if (y + context->image->height > h)
417 if (RemoteFxCodec && stream_surface_bits_supported(settings))
419 const UINT32 RemoteFxCodecId =
421 WINPR_ASSERT(RemoteFxCodecId <= UINT16_MAX);
422 cmd.bmp.codecID = (UINT16)RemoteFxCodecId;
423 cmd.cmdType = CMDTYPE_STREAM_SURFACE_BITS;
428 WINPR_ASSERT(NSCodecId <= UINT16_MAX);
429 cmd.bmp.codecID = (UINT16)NSCodecId;
430 cmd.cmdType = CMDTYPE_SET_SURFACE_BITS;
433 wStream* s = test_peer_stream_init(context);
436 const UINT32 colorFormat =
437 context->image->bitsPerPixel > 24 ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_BGR24;
441 rfx_context_set_pixel_format(context->rfx_context, colorFormat);
442 if (!rfx_compose_message(context->rfx_context, s, &rect, 1, context->image->data,
443 rect.width, rect.height, context->image->scanline))
444 WLog_WARN(TAG,
"rfx_compose_message failed");
448 if (!nsc_context_set_parameters(context->nsc_context, NSC_COLOR_FORMAT, colorFormat))
449 WLog_WARN(TAG,
"nsc_context_set_parameters failed");
450 else if (!nsc_compose_message(context->nsc_context, s, context->image->data, rect.width,
451 rect.height, context->image->scanline))
452 WLog_WARN(TAG,
"rfx_compose_message failed");
458 cmd.destRight = x + rect.width;
459 cmd.destBottom = y + rect.height;
461 cmd.bmp.width = rect.width;
462 cmd.bmp.height = rect.height;
463 cmd.bmp.bitmapDataLength = (UINT32)Stream_GetPosition(s);
464 cmd.bmp.bitmapData = Stream_Buffer(s);
466 rdpUpdate* update = client->context->update;
467 WINPR_ASSERT(update);
468 WINPR_ASSERT(update->SurfaceBits);
469 if (!update->SurfaceBits(update->context, &cmd))
471 WLog_WARN(TAG,
"update->SurfaceBits failed");
477static void test_peer_draw_icon(freerdp_peer* client, UINT32 x, UINT32 y)
479 WINPR_ASSERT(client);
481 rdpSettings* settings = client->context->settings;
482 WINPR_ASSERT(settings);
487 test_peer_begin_frame(client);
489 testPeerContext* context = (testPeerContext*)client->context;
490 if ((context->icon_x != UINT32_MAX) && (context->icon_y != UINT32_MAX))
492 RFX_RECT rect = { .x = WINPR_ASSERTING_INT_CAST(uint16_t, context->icon_x),
493 .y = WINPR_ASSERTING_INT_CAST(uint16_t, context->icon_y),
494 .width = WINPR_ASSERTING_INT_CAST(uint16_t, context->image->width),
495 .height = WINPR_ASSERTING_INT_CAST(uint16_t, context->image->height) };
497 test_peer_draw_background(client, &rect);
499 test_send_cursor_update(client, x, y);
500 test_peer_end_frame(client);
504static BOOL test_sleep_tsdiff(UINT32* old_sec, UINT32* old_usec, UINT32 new_sec, UINT32 new_usec)
509 WINPR_ASSERT(old_sec);
510 WINPR_ASSERT(old_usec);
512 if ((*old_sec == 0) && (*old_usec == 0))
515 *old_usec = new_usec;
519 sec = new_sec - *old_sec;
520 usec = new_usec - *old_usec;
522 if ((sec < 0) || ((sec == 0) && (usec < 0)))
524 WLog_ERR(TAG,
"Invalid time stamp detected.");
529 *old_usec = new_usec;
538 Sleep((DWORD)sec * 1000);
546static BOOL tf_peer_dump_rfx(freerdp_peer* client)
550 UINT32 prev_seconds = 0;
551 UINT32 prev_useconds = 0;
552 rdpUpdate* update =
nullptr;
553 rdpPcap* pcap_rfx =
nullptr;
554 pcap_record record = WINPR_C_ARRAY_INIT;
556 WINPR_ASSERT(client);
557 WINPR_ASSERT(client->context);
559 struct server_info* info = client->ContextExtra;
562 s = Stream_New(
nullptr, 512);
567 update = client->context->update;
568 WINPR_ASSERT(update);
570 pcap_rfx = pcap_open(info->test_pcap_file, FALSE);
574 prev_seconds = prev_useconds = 0;
576 while (pcap_has_next_record(pcap_rfx))
578 if (!pcap_get_next_record_header(pcap_rfx, &record))
581 if (!Stream_EnsureCapacity(s, record.length))
584 record.data = Stream_Buffer(s);
585 if (!pcap_get_next_record_content(pcap_rfx, &record))
587 Stream_SetPosition(s, Stream_Capacity(s));
589 if (info->test_dump_rfx_realtime &&
590 test_sleep_tsdiff(&prev_seconds, &prev_useconds, record.header.ts_sec,
591 record.header.ts_usec) == FALSE)
594 WINPR_ASSERT(update->SurfaceCommand);
595 if (!update->SurfaceCommand(update->context, s))
598 WINPR_ASSERT(client->CheckFileDescriptor);
599 if (client->CheckFileDescriptor(client) != TRUE)
605 Stream_Free(s, TRUE);
606 pcap_close(pcap_rfx);
611static DWORD WINAPI tf_debug_channel_thread_func(LPVOID arg)
614 void* buffer =
nullptr;
615 DWORD BytesReturned = 0;
617 testPeerContext* context = (testPeerContext*)arg;
619 WINPR_ASSERT(context);
620 if (WTSVirtualChannelQuery(context->debug_channel, WTSVirtualFileHandle, &buffer,
621 &BytesReturned) == TRUE)
623 fd = *((
void**)buffer);
624 WTSFreeMemory(buffer);
626 if (!(context->event = CreateWaitObjectEvent(
nullptr, TRUE, FALSE, fd)))
630 wStream* s = Stream_New(
nullptr, 4096);
634 if (!WTSVirtualChannelWrite(context->debug_channel, (PCHAR)
"test1", 5, &written))
641 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
643 handles[nCount++] = context->event;
644 handles[nCount++] = freerdp_abort_event(&context->_p);
645 handles[nCount++] = context->stopEvent;
646 status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
655 Stream_SetPosition(s, 0);
657 if (WTSVirtualChannelRead(context->debug_channel, 0, Stream_BufferAs(s,
char),
658 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
660 if (BytesReturned == 0)
663 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
666 if (WTSVirtualChannelRead(context->debug_channel, 0, Stream_BufferAs(s,
char),
667 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
674 Stream_SetPosition(s, BytesReturned);
675 WLog_DBG(TAG,
"got %" PRIu32
" bytes", BytesReturned);
678 Stream_Free(s, TRUE);
683static BOOL tf_peer_post_connect(freerdp_peer* client)
685 testPeerContext* context =
nullptr;
686 rdpSettings* settings =
nullptr;
688 WINPR_ASSERT(client);
690 context = (testPeerContext*)client->context;
691 WINPR_ASSERT(context);
693 settings = client->context->settings;
694 WINPR_ASSERT(settings);
702 WLog_DBG(TAG,
"Client %s is activated (osMajorType %" PRIu32
" osMinorType %" PRIu32
")",
703 client->local ?
"(local)" : client->hostname,
711 WLog_DBG(TAG,
" and wants to login automatically as %s\\%s", Domain ? Domain :
"",
716 WLog_DBG(TAG,
"Client requested desktop: %" PRIu32
"x%" PRIu32
"x%" PRIu32
"",
720#if (SAMPLE_SERVER_USE_CLIENT_RESOLUTION == 1)
722 if (!rfx_context_reset(context->rfx_context,
727 WLog_DBG(TAG,
"Using resolution requested by client.");
729 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) =
730 context->rfx_context->width;
731 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight) =
732 context->rfx_context->height;
733 WLog_DBG(TAG,
"Resizing client to %" PRIu32
"x%" PRIu32
"",
734 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
735 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
736 client->update->DesktopResize(client->update->context);
741 if (!test_peer_load_icon(client))
743 WLog_DBG(TAG,
"Unable to load icon");
747 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm,
"rdpdbg"))
749 context->debug_channel = WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION,
"rdpdbg");
751 if (context->debug_channel !=
nullptr)
753 WLog_DBG(TAG,
"Open channel rdpdbg.");
755 if (!(context->stopEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr)))
757 WLog_ERR(TAG,
"Failed to create stop event");
761 if (!(context->debug_channel_thread = CreateThread(
762 nullptr, 0, tf_debug_channel_thread_func, (
void*)context, 0,
nullptr)))
764 WLog_ERR(TAG,
"Failed to create debug channel thread");
765 (void)CloseHandle(context->stopEvent);
766 context->stopEvent =
nullptr;
772 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, RDPSND_CHANNEL_NAME))
774 if (!sf_peer_rdpsnd_init(context))
778 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, ENCOMSP_SVC_CHANNEL_NAME))
780 if (!sf_peer_encomsp_init(context))
785 if (!sf_peer_audin_init(context))
788#if defined(CHANNEL_AINPUT_SERVER)
789 sf_peer_ainput_init(context);
797static BOOL tf_peer_activate(freerdp_peer* client)
800 WINPR_ASSERT(client);
802 testPeerContext* context = (testPeerContext*)client->context;
803 WINPR_ASSERT(context);
805 rdpSettings* settings = client->context->settings;
806 WINPR_ASSERT(settings);
808 struct server_info* info = client->ContextExtra;
811 context->activated = TRUE;
818 if (info->test_pcap_file !=
nullptr)
823 if (!tf_peer_dump_rfx(client))
834 test_peer_begin_frame(client);
835 rc = test_peer_draw_background(client, &rect);
836 test_peer_end_frame(client);
843static BOOL tf_peer_synchronize_event(rdpInput* input, UINT32 flags)
847 WLog_DBG(TAG,
"Client sent a synchronize event (flags:0x%" PRIX32
")", flags);
852static BOOL tf_peer_keyboard_event(rdpInput* input, UINT16 flags, UINT8 code)
854 freerdp_peer* client =
nullptr;
855 rdpUpdate* update =
nullptr;
856 rdpContext* context =
nullptr;
857 testPeerContext* tcontext =
nullptr;
858 rdpSettings* settings =
nullptr;
862 context = input->context;
863 WINPR_ASSERT(context);
865 client = context->peer;
866 WINPR_ASSERT(client);
868 settings = context->settings;
869 WINPR_ASSERT(settings);
871 update = context->update;
872 WINPR_ASSERT(update);
874 tcontext = (testPeerContext*)context;
875 WINPR_ASSERT(tcontext);
877 WLog_DBG(TAG,
"Client sent a keyboard event (flags:0x%04" PRIX16
" code:0x%04" PRIX8
")", flags,
880 if (((flags & KBD_FLAGS_RELEASE) == 0) && (code == RDP_SCANCODE_KEY_G))
892 SAMPLE_SERVER_DEFAULT_WIDTH))
895 SAMPLE_SERVER_DEFAULT_HEIGHT))
899 if (!rfx_context_reset(tcontext->rfx_context,
904 WINPR_ASSERT(update->DesktopResize);
905 if (!update->DesktopResize(update->context))
907 tcontext->activated = FALSE;
909 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_C)
911 if (tcontext->debug_channel)
914 if (!WTSVirtualChannelWrite(tcontext->debug_channel, (PCHAR)
"test2", 5, &written))
918 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_X)
920 WINPR_ASSERT(client->Close);
921 if (!client->Close(client))
924 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_R)
926 tcontext->audin_open = !tcontext->audin_open;
928#if defined(CHANNEL_AINPUT_SERVER)
929 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_I)
931 tcontext->ainput_open = !tcontext->ainput_open;
934 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_S)
942static BOOL tf_peer_unicode_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
948 "Client sent a unicode keyboard event (flags:0x%04" PRIX16
" code:0x%04" PRIX16
")",
954static BOOL tf_peer_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
958 WINPR_ASSERT(input->context);
960 WLog_DBG(TAG,
"Client sent a mouse event (flags:0x%04" PRIX16
" pos:%" PRIu16
",%" PRIu16
")",
962 test_peer_draw_icon(input->context->peer, x + 10, y);
967static UINT32 add(UINT32 old, UINT32 max, INT16 diff)
975 return WINPR_ASSERTING_INT_CAST(uint32_t, val);
979static BOOL tf_peer_rel_mouse_event(rdpInput* input, UINT16 flags, INT16 xDelta, INT16 yDelta)
983 WINPR_ASSERT(input->context);
988 static UINT32 xpos = 0;
989 static UINT32 ypos = 0;
991 xpos = add(xpos, w, xDelta);
992 ypos = add(ypos, h, yDelta);
995 "Client sent a relative mouse event (flags:0x%04" PRIX16
" pos:%" PRId16
",%" PRId16
997 flags, xDelta, yDelta);
998 test_peer_draw_icon(input->context->peer, xpos + 10, ypos);
1003static BOOL tf_peer_extended_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
1005 WINPR_UNUSED(flags);
1006 WINPR_ASSERT(input);
1007 WINPR_ASSERT(input->context);
1010 "Client sent an extended mouse event (flags:0x%04" PRIX16
" pos:%" PRIu16
",%" PRIu16
1013 test_peer_draw_icon(input->context->peer, x + 10, y);
1018static BOOL tf_peer_refresh_rect(rdpContext* context, BYTE count,
const RECTANGLE_16* areas)
1020 WINPR_UNUSED(context);
1021 WINPR_ASSERT(context);
1022 WINPR_ASSERT(areas || (count == 0));
1024 WLog_DBG(TAG,
"Client requested to refresh:");
1026 for (BYTE i = 0; i < count; i++)
1028 WLog_DBG(TAG,
" (%" PRIu16
", %" PRIu16
") (%" PRIu16
", %" PRIu16
")", areas[i].left,
1029 areas[i].top, areas[i].right, areas[i].bottom);
1036static BOOL tf_peer_suppress_output(rdpContext* context, BYTE allow,
const RECTANGLE_16* area)
1038 WINPR_UNUSED(context);
1044 "Client restore output (%" PRIu16
", %" PRIu16
") (%" PRIu16
", %" PRIu16
").",
1045 area->left, area->top, area->right, area->bottom);
1049 WLog_DBG(TAG,
"Client minimized and suppress output.");
1056static int hook_peer_write_pdu(rdpTransport* transport,
wStream* s)
1063 rdpContext* context = transport_get_context(transport);
1065 WINPR_ASSERT(context);
1068 freerdp_peer* client = context->peer;
1069 WINPR_ASSERT(client);
1071 testPeerContext* peerCtx = (testPeerContext*)client->context;
1072 WINPR_ASSERT(peerCtx);
1073 WINPR_ASSERT(peerCtx->io.WritePdu);
1082 CONNECTION_STATE state = freerdp_get_state(context);
1083 if (state < CONNECTION_STATE_NEGO)
1084 return peerCtx->io.WritePdu(transport, s);
1086 ls = Stream_New(
nullptr, 4096);
1090 while (stream_dump_get(context, &flags, ls, &offset, &ts) > 0)
1094 if (flags & STREAM_MSG_SRV_TX)
1096 if ((last_ts > 0) && (ts > last_ts))
1098 UINT64 diff = ts - last_ts;
1101 UINT32 d = diff > UINT32_MAX ? UINT32_MAX : (UINT32)diff;
1107 rc = peerCtx->io.WritePdu(transport, ls);
1111 Stream_SetPosition(ls, 0);
1115 Stream_Free(ls, TRUE);
1120static DWORD WINAPI test_peer_mainloop(LPVOID arg)
1123 DWORD error = CHANNEL_RC_OK;
1124 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
1127 testPeerContext* context =
nullptr;
1128 rdpSettings* settings =
nullptr;
1129 rdpInput* input =
nullptr;
1130 rdpUpdate* update =
nullptr;
1131 freerdp_peer* client = (freerdp_peer*)arg;
1133 WINPR_ASSERT(client);
1135 struct server_info* info = client->ContextExtra;
1138 if (!test_peer_init(client))
1140 freerdp_peer_free(client);
1145 WINPR_ASSERT(client->context);
1146 settings = client->context->settings;
1147 WINPR_ASSERT(settings);
1148 if (info->replay_dump)
1156 rdpPrivateKey* key = freerdp_key_new_from_file_enc(info->key,
nullptr);
1163 rdpCertificate* cert = freerdp_certificate_new_from_file(info->cert);
1177 ENCRYPTION_LEVEL_CLIENT_COMPATIBLE))
1195 client->PostConnect = tf_peer_post_connect;
1196 client->Activate = tf_peer_activate;
1198 WINPR_ASSERT(client->context);
1199 input = client->context->input;
1200 WINPR_ASSERT(input);
1202 input->SynchronizeEvent = tf_peer_synchronize_event;
1203 input->KeyboardEvent = tf_peer_keyboard_event;
1204 input->UnicodeKeyboardEvent = tf_peer_unicode_keyboard_event;
1205 input->MouseEvent = tf_peer_mouse_event;
1206 input->RelMouseEvent = tf_peer_rel_mouse_event;
1207 input->ExtendedMouseEvent = tf_peer_extended_mouse_event;
1209 update = client->context->update;
1210 WINPR_ASSERT(update);
1212 update->RefreshRect = tf_peer_refresh_rect;
1213 update->SuppressOutput = tf_peer_suppress_output;
1218 WINPR_ASSERT(client->Initialize);
1219 rc = client->Initialize(client);
1223 context = (testPeerContext*)client->context;
1224 WINPR_ASSERT(context);
1226 if (info->replay_dump)
1228 const rdpTransportIo* cb = freerdp_get_io_callbacks(client->context);
1229 rdpTransportIo replay;
1234 replay.WritePdu = hook_peer_write_pdu;
1235 if (!freerdp_set_io_callbacks(client->context, &replay))
1239 WLog_INFO(TAG,
"We've got a client %s", client->local ?
"(local)" : client->hostname);
1241 while (error == CHANNEL_RC_OK)
1245 WINPR_ASSERT(client->GetEventHandles);
1246 DWORD tmp = client->GetEventHandles(client, &handles[count], 32 - count);
1250 WLog_ERR(TAG,
"Failed to get FreeRDP transport event handles");
1257 HANDLE channelHandle = WTSVirtualChannelManagerGetEventHandle(context->vcm);
1258 handles[count++] = channelHandle;
1259 status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
1261 if (status == WAIT_FAILED)
1263 WLog_ERR(TAG,
"WaitForMultipleObjects failed (errno: %d)", errno);
1267 WINPR_ASSERT(client->CheckFileDescriptor);
1268 if (client->CheckFileDescriptor(client) != TRUE)
1271 if (WTSVirtualChannelManagerCheckFileDescriptor(context->vcm) != TRUE)
1275 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, DRDYNVC_SVC_CHANNEL_NAME))
1277 switch (WTSVirtualChannelManagerGetDrdynvcState(context->vcm))
1279 case DRDYNVC_STATE_NONE:
1282 case DRDYNVC_STATE_INITIALIZED:
1285 case DRDYNVC_STATE_READY:
1288 if (sf_peer_audin_running(context) != context->audin_open)
1290 if (!sf_peer_audin_running(context))
1291 sf_peer_audin_start(context);
1293 sf_peer_audin_stop(context);
1296#if defined(CHANNEL_AINPUT_SERVER)
1297 if (sf_peer_ainput_running(context) != context->ainput_open)
1299 if (!sf_peer_ainput_running(context))
1300 sf_peer_ainput_start(context);
1302 sf_peer_ainput_stop(context);
1308 case DRDYNVC_STATE_FAILED:
1315 WLog_INFO(TAG,
"Client %s disconnected.", client->local ?
"(local)" : client->hostname);
1317 WINPR_ASSERT(client->Disconnect);
1318 client->Disconnect(client);
1320 freerdp_peer_context_free(client);
1321 freerdp_peer_free(client);
1326static BOOL test_peer_accepted(freerdp_listener* instance, freerdp_peer* client)
1328 HANDLE hThread =
nullptr;
1330 WINPR_UNUSED(instance);
1332 WINPR_ASSERT(instance);
1333 WINPR_ASSERT(client);
1335 struct server_info* info = instance->info;
1336 client->ContextExtra = info;
1338 if (!(hThread = CreateThread(
nullptr, 0, test_peer_mainloop, (
void*)client, 0,
nullptr)))
1341 (void)CloseHandle(hThread);
1345static void test_server_mainloop(freerdp_listener* instance)
1347 HANDLE handles[32] = WINPR_C_ARRAY_INIT;
1351 WINPR_ASSERT(instance);
1354 WINPR_ASSERT(instance->GetEventHandles);
1355 count = instance->GetEventHandles(instance, handles, 32);
1359 WLog_ERR(TAG,
"Failed to get FreeRDP event handles");
1363 status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
1365 if (WAIT_FAILED == status)
1367 WLog_ERR(TAG,
"select failed");
1371 WINPR_ASSERT(instance->CheckFileDescriptor);
1372 if (instance->CheckFileDescriptor(instance) != TRUE)
1374 WLog_ERR(TAG,
"Failed to check FreeRDP file descriptor");
1379 WINPR_ASSERT(instance->Close);
1380 instance->Close(instance);
1385 const char spcap[7];
1386 const char sfast[7];
1387 const char sport[7];
1388 const char slocal_only[13];
1389 const char scert[7];
1391} options = { {
'-',
'-',
'p',
'c',
'a',
'p',
'=' },
1392 {
'-',
'-',
'f',
'a',
's',
't' },
1393 {
'-',
'-',
'p',
'o',
'r',
't',
'=' },
1394 {
'-',
'-',
'l',
'o',
'c',
'a',
'l',
'-',
'o',
'n',
'l',
'y' },
1395 {
'-',
'-',
'c',
'e',
'r',
't',
'=' },
1396 {
'-',
'-',
'k',
'e',
'y',
'=' } };
1398WINPR_PRAGMA_DIAG_PUSH
1399WINPR_PRAGMA_DIAG_IGNORED_FORMAT_NONLITERAL
1400WINPR_ATTR_FORMAT_ARG(2, 0)
1401static
void print_entry(FILE* fp, WINPR_FORMAT_ARG const
char* fmt, const
char* what,
size_t size)
1403 char buffer[32] = WINPR_C_ARRAY_INIT;
1404 strncpy(buffer, what, MIN(size,
sizeof(buffer) - 1));
1405 (void)fprintf(fp, fmt, buffer);
1407WINPR_PRAGMA_DIAG_POP
1410static int usage(
const char* app,
const char* invalid)
1414 (void)fprintf(fp,
"Invalid argument '%s'\n", invalid);
1415 (void)fprintf(fp,
"Usage: %s <arg>[ <arg> ...]\n", app);
1416 (void)fprintf(fp,
"Arguments:\n");
1417 print_entry(fp,
"\t%s<pcap file>\n", options.spcap,
sizeof(options.spcap));
1418 print_entry(fp,
"\t%s<cert file>\n", options.scert,
sizeof(options.scert));
1419 print_entry(fp,
"\t%s<key file>\n", options.skey,
sizeof(options.skey));
1420 print_entry(fp,
"\t%s\n", options.sfast,
sizeof(options.sfast));
1421 print_entry(fp,
"\t%s<port>\n", options.sport,
sizeof(options.sport));
1422 print_entry(fp,
"\t%s\n", options.slocal_only,
sizeof(options.slocal_only));
1426int main(
int argc,
char* argv[])
1429 BOOL started = FALSE;
1430 WSADATA wsaData = WINPR_C_ARRAY_INIT;
1431 freerdp_listener* instance =
nullptr;
1432 char* file =
nullptr;
1433 char name[MAX_PATH] = WINPR_C_ARRAY_INIT;
1435 BOOL localOnly = FALSE;
1436 struct server_info info = WINPR_C_ARRAY_INIT;
1437 const char* app = argv[0];
1439 info.test_dump_rfx_realtime = TRUE;
1443 for (
int i = 1; i < argc; i++)
1445 char* arg = argv[i];
1447 if (strncmp(arg, options.sfast,
sizeof(options.sfast)) == 0)
1448 info.test_dump_rfx_realtime = FALSE;
1449 else if (strncmp(arg, options.sport,
sizeof(options.sport)) == 0)
1451 const char* sport = &arg[
sizeof(options.sport)];
1452 port = strtol(sport,
nullptr, 10);
1454 if ((port < 1) || (port > UINT16_MAX) || (errno != 0))
1455 return usage(app, arg);
1457 else if (strncmp(arg, options.slocal_only,
sizeof(options.slocal_only)) == 0)
1459 else if (strncmp(arg, options.spcap,
sizeof(options.spcap)) == 0)
1461 info.test_pcap_file = &arg[
sizeof(options.spcap)];
1462 if (!winpr_PathFileExists(info.test_pcap_file))
1463 return usage(app, arg);
1465 else if (strncmp(arg, options.scert,
sizeof(options.scert)) == 0)
1467 info.cert = &arg[
sizeof(options.scert)];
1468 if (!winpr_PathFileExists(info.cert))
1469 return usage(app, arg);
1471 else if (strncmp(arg, options.skey,
sizeof(options.skey)) == 0)
1473 info.key = &arg[
sizeof(options.skey)];
1474 if (!winpr_PathFileExists(info.key))
1475 return usage(app, arg);
1478 return usage(app, arg);
1481 WTSRegisterWtsApiFunctionTable(FreeRDP_InitWtsApi());
1482 winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT);
1483 instance = freerdp_listener_new();
1489 info.cert =
"server.crt";
1491 info.key =
"server.key";
1493 instance->info = (
void*)&info;
1494 instance->PeerAccepted = test_peer_accepted;
1496 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
1500 (void)sprintf_s(name,
sizeof(name),
"tfreerdp-server.%ld", port);
1501 file = GetKnownSubPath(KNOWN_PATH_TEMP, name);
1508 WINPR_ASSERT(instance->OpenLocal);
1509 started = instance->OpenLocal(instance, file);
1513 WINPR_ASSERT(instance->Open);
1514 started = instance->Open(instance,
nullptr, (UINT16)port);
1521 test_server_mainloop(instance);
1527 freerdp_listener_free(instance);
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
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 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.
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_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
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.