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_ResetPosition(context->s);
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 if (!Stream_SetPosition(s, Stream_Capacity(s)))
590 if (info->test_dump_rfx_realtime &&
591 test_sleep_tsdiff(&prev_seconds, &prev_useconds, record.header.ts_sec,
592 record.header.ts_usec) == FALSE)
595 WINPR_ASSERT(update->SurfaceCommand);
596 if (!update->SurfaceCommand(update->context, s))
599 WINPR_ASSERT(client->CheckFileDescriptor);
600 if (client->CheckFileDescriptor(client) != TRUE)
606 Stream_Free(s, TRUE);
607 pcap_close(pcap_rfx);
612static DWORD WINAPI tf_debug_channel_thread_func(LPVOID arg)
615 void* buffer =
nullptr;
616 DWORD BytesReturned = 0;
618 testPeerContext* context = (testPeerContext*)arg;
620 WINPR_ASSERT(context);
621 if (WTSVirtualChannelQuery(context->debug_channel, WTSVirtualFileHandle, &buffer,
622 &BytesReturned) == TRUE)
624 fd = *((
void**)buffer);
625 WTSFreeMemory(buffer);
627 if (!(context->event = CreateWaitObjectEvent(
nullptr, TRUE, FALSE, fd)))
631 wStream* s = Stream_New(
nullptr, 4096);
635 if (!WTSVirtualChannelWrite(context->debug_channel, (PCHAR)
"test1", 5, &written))
642 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
644 handles[nCount++] = context->event;
645 handles[nCount++] = freerdp_abort_event(&context->_p);
646 handles[nCount++] = context->stopEvent;
647 status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
656 Stream_ResetPosition(s);
658 if (WTSVirtualChannelRead(context->debug_channel, 0, Stream_BufferAs(s,
char),
659 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
661 if (BytesReturned == 0)
664 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
667 if (WTSVirtualChannelRead(context->debug_channel, 0, Stream_BufferAs(s,
char),
668 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
675 if (!Stream_SetPosition(s, BytesReturned))
677 WLog_DBG(TAG,
"got %" PRIu32
" bytes", BytesReturned);
680 Stream_Free(s, TRUE);
685static BOOL tf_peer_post_connect(freerdp_peer* client)
687 testPeerContext* context =
nullptr;
688 rdpSettings* settings =
nullptr;
690 WINPR_ASSERT(client);
692 context = (testPeerContext*)client->context;
693 WINPR_ASSERT(context);
695 settings = client->context->settings;
696 WINPR_ASSERT(settings);
704 WLog_DBG(TAG,
"Client %s is activated (osMajorType %" PRIu32
" osMinorType %" PRIu32
")",
705 client->local ?
"(local)" : client->hostname,
713 WLog_DBG(TAG,
" and wants to login automatically as %s\\%s", Domain ? Domain :
"",
718 WLog_DBG(TAG,
"Client requested desktop: %" PRIu32
"x%" PRIu32
"x%" PRIu32
"",
722#if (SAMPLE_SERVER_USE_CLIENT_RESOLUTION == 1)
724 if (!rfx_context_reset(context->rfx_context,
729 WLog_DBG(TAG,
"Using resolution requested by client.");
731 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) =
732 context->rfx_context->width;
733 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight) =
734 context->rfx_context->height;
735 WLog_DBG(TAG,
"Resizing client to %" PRIu32
"x%" PRIu32
"",
736 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth),
737 client->freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
738 client->update->DesktopResize(client->update->context);
743 if (!test_peer_load_icon(client))
745 WLog_DBG(TAG,
"Unable to load icon");
749 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm,
"rdpdbg"))
751 context->debug_channel = WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION,
"rdpdbg");
753 if (context->debug_channel !=
nullptr)
755 WLog_DBG(TAG,
"Open channel rdpdbg.");
757 if (!(context->stopEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr)))
759 WLog_ERR(TAG,
"Failed to create stop event");
763 if (!(context->debug_channel_thread = CreateThread(
764 nullptr, 0, tf_debug_channel_thread_func, (
void*)context, 0,
nullptr)))
766 WLog_ERR(TAG,
"Failed to create debug channel thread");
767 (void)CloseHandle(context->stopEvent);
768 context->stopEvent =
nullptr;
774 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, RDPSND_CHANNEL_NAME))
776 if (!sf_peer_rdpsnd_init(context))
780 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, ENCOMSP_SVC_CHANNEL_NAME))
782 if (!sf_peer_encomsp_init(context))
787 if (!sf_peer_audin_init(context))
790#if defined(CHANNEL_AINPUT_SERVER)
791 sf_peer_ainput_init(context);
799static BOOL tf_peer_activate(freerdp_peer* client)
802 WINPR_ASSERT(client);
804 testPeerContext* context = (testPeerContext*)client->context;
805 WINPR_ASSERT(context);
807 rdpSettings* settings = client->context->settings;
808 WINPR_ASSERT(settings);
810 struct server_info* info = client->ContextExtra;
813 context->activated = TRUE;
820 if (info->test_pcap_file !=
nullptr)
825 if (!tf_peer_dump_rfx(client))
836 test_peer_begin_frame(client);
837 rc = test_peer_draw_background(client, &rect);
838 test_peer_end_frame(client);
845static BOOL tf_peer_synchronize_event(rdpInput* input, UINT32 flags)
849 WLog_DBG(TAG,
"Client sent a synchronize event (flags:0x%" PRIX32
")", flags);
854static BOOL tf_peer_keyboard_event(rdpInput* input, UINT16 flags, UINT8 code)
856 freerdp_peer* client =
nullptr;
857 rdpUpdate* update =
nullptr;
858 rdpContext* context =
nullptr;
859 testPeerContext* tcontext =
nullptr;
860 rdpSettings* settings =
nullptr;
864 context = input->context;
865 WINPR_ASSERT(context);
867 client = context->peer;
868 WINPR_ASSERT(client);
870 settings = context->settings;
871 WINPR_ASSERT(settings);
873 update = context->update;
874 WINPR_ASSERT(update);
876 tcontext = (testPeerContext*)context;
877 WINPR_ASSERT(tcontext);
879 WLog_DBG(TAG,
"Client sent a keyboard event (flags:0x%04" PRIX16
" code:0x%04" PRIX8
")", flags,
882 if (((flags & KBD_FLAGS_RELEASE) == 0) && (code == RDP_SCANCODE_KEY_G))
894 SAMPLE_SERVER_DEFAULT_WIDTH))
897 SAMPLE_SERVER_DEFAULT_HEIGHT))
901 if (!rfx_context_reset(tcontext->rfx_context,
906 WINPR_ASSERT(update->DesktopResize);
907 if (!update->DesktopResize(update->context))
909 tcontext->activated = FALSE;
911 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_C)
913 if (tcontext->debug_channel)
916 if (!WTSVirtualChannelWrite(tcontext->debug_channel, (PCHAR)
"test2", 5, &written))
920 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_X)
922 WINPR_ASSERT(client->Close);
923 if (!client->Close(client))
926 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_R)
928 tcontext->audin_open = !tcontext->audin_open;
930#if defined(CHANNEL_AINPUT_SERVER)
931 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_I)
933 tcontext->ainput_open = !tcontext->ainput_open;
936 else if (((flags & KBD_FLAGS_RELEASE) == 0) && code == RDP_SCANCODE_KEY_S)
944static BOOL tf_peer_unicode_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
950 "Client sent a unicode keyboard event (flags:0x%04" PRIX16
" code:0x%04" PRIX16
")",
956static BOOL tf_peer_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
960 WINPR_ASSERT(input->context);
962 WLog_DBG(TAG,
"Client sent a mouse event (flags:0x%04" PRIX16
" pos:%" PRIu16
",%" PRIu16
")",
964 test_peer_draw_icon(input->context->peer, x + 10, y);
969static UINT32 add(UINT32 old, UINT32 max, INT16 diff)
977 return WINPR_ASSERTING_INT_CAST(uint32_t, val);
981static BOOL tf_peer_rel_mouse_event(rdpInput* input, UINT16 flags, INT16 xDelta, INT16 yDelta)
985 WINPR_ASSERT(input->context);
990 static UINT32 xpos = 0;
991 static UINT32 ypos = 0;
993 xpos = add(xpos, w, xDelta);
994 ypos = add(ypos, h, yDelta);
997 "Client sent a relative mouse event (flags:0x%04" PRIX16
" pos:%" PRId16
",%" PRId16
999 flags, xDelta, yDelta);
1000 test_peer_draw_icon(input->context->peer, xpos + 10, ypos);
1005static BOOL tf_peer_extended_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
1007 WINPR_UNUSED(flags);
1008 WINPR_ASSERT(input);
1009 WINPR_ASSERT(input->context);
1012 "Client sent an extended mouse event (flags:0x%04" PRIX16
" pos:%" PRIu16
",%" PRIu16
1015 test_peer_draw_icon(input->context->peer, x + 10, y);
1020static BOOL tf_peer_refresh_rect(rdpContext* context, BYTE count,
const RECTANGLE_16* areas)
1022 WINPR_UNUSED(context);
1023 WINPR_ASSERT(context);
1024 WINPR_ASSERT(areas || (count == 0));
1026 WLog_DBG(TAG,
"Client requested to refresh:");
1028 for (BYTE i = 0; i < count; i++)
1030 WLog_DBG(TAG,
" (%" PRIu16
", %" PRIu16
") (%" PRIu16
", %" PRIu16
")", areas[i].left,
1031 areas[i].top, areas[i].right, areas[i].bottom);
1038static BOOL tf_peer_suppress_output(rdpContext* context, BYTE allow,
const RECTANGLE_16* area)
1040 WINPR_UNUSED(context);
1046 "Client restore output (%" PRIu16
", %" PRIu16
") (%" PRIu16
", %" PRIu16
").",
1047 area->left, area->top, area->right, area->bottom);
1051 WLog_DBG(TAG,
"Client minimized and suppress output.");
1058static int hook_peer_write_pdu(rdpTransport* transport,
wStream* s)
1065 rdpContext* context = transport_get_context(transport);
1067 WINPR_ASSERT(context);
1070 freerdp_peer* client = context->peer;
1071 WINPR_ASSERT(client);
1073 testPeerContext* peerCtx = (testPeerContext*)client->context;
1074 WINPR_ASSERT(peerCtx);
1075 WINPR_ASSERT(peerCtx->io.WritePdu);
1084 CONNECTION_STATE state = freerdp_get_state(context);
1085 if (state < CONNECTION_STATE_NEGO)
1086 return peerCtx->io.WritePdu(transport, s);
1088 ls = Stream_New(
nullptr, 4096);
1092 while (stream_dump_get(context, &flags, ls, &offset, &ts) > 0)
1096 if (flags & STREAM_MSG_SRV_TX)
1098 if ((last_ts > 0) && (ts > last_ts))
1100 UINT64 diff = ts - last_ts;
1103 UINT32 d = diff > UINT32_MAX ? UINT32_MAX : (UINT32)diff;
1109 rc = peerCtx->io.WritePdu(transport, ls);
1113 Stream_ResetPosition(ls);
1117 Stream_Free(ls, TRUE);
1122static DWORD WINAPI test_peer_mainloop(LPVOID arg)
1125 DWORD error = CHANNEL_RC_OK;
1126 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
1129 testPeerContext* context =
nullptr;
1130 rdpSettings* settings =
nullptr;
1131 rdpInput* input =
nullptr;
1132 rdpUpdate* update =
nullptr;
1133 freerdp_peer* client = (freerdp_peer*)arg;
1135 WINPR_ASSERT(client);
1137 struct server_info* info = client->ContextExtra;
1140 if (!test_peer_init(client))
1142 freerdp_peer_free(client);
1147 WINPR_ASSERT(client->context);
1148 settings = client->context->settings;
1149 WINPR_ASSERT(settings);
1150 if (info->replay_dump)
1158 rdpPrivateKey* key = freerdp_key_new_from_file_enc(info->key,
nullptr);
1165 rdpCertificate* cert = freerdp_certificate_new_from_file(info->cert);
1179 ENCRYPTION_LEVEL_CLIENT_COMPATIBLE))
1197 client->PostConnect = tf_peer_post_connect;
1198 client->Activate = tf_peer_activate;
1200 WINPR_ASSERT(client->context);
1201 input = client->context->input;
1202 WINPR_ASSERT(input);
1204 input->SynchronizeEvent = tf_peer_synchronize_event;
1205 input->KeyboardEvent = tf_peer_keyboard_event;
1206 input->UnicodeKeyboardEvent = tf_peer_unicode_keyboard_event;
1207 input->MouseEvent = tf_peer_mouse_event;
1208 input->RelMouseEvent = tf_peer_rel_mouse_event;
1209 input->ExtendedMouseEvent = tf_peer_extended_mouse_event;
1211 update = client->context->update;
1212 WINPR_ASSERT(update);
1214 update->RefreshRect = tf_peer_refresh_rect;
1215 update->SuppressOutput = tf_peer_suppress_output;
1220 WINPR_ASSERT(client->Initialize);
1221 rc = client->Initialize(client);
1225 context = (testPeerContext*)client->context;
1226 WINPR_ASSERT(context);
1228 if (info->replay_dump)
1230 const rdpTransportIo* cb = freerdp_get_io_callbacks(client->context);
1231 rdpTransportIo replay;
1236 replay.WritePdu = hook_peer_write_pdu;
1237 if (!freerdp_set_io_callbacks(client->context, &replay))
1241 WLog_INFO(TAG,
"We've got a client %s", client->local ?
"(local)" : client->hostname);
1243 while (error == CHANNEL_RC_OK)
1247 WINPR_ASSERT(client->GetEventHandles);
1248 DWORD tmp = client->GetEventHandles(client, &handles[count], 32 - count);
1252 WLog_ERR(TAG,
"Failed to get FreeRDP transport event handles");
1259 HANDLE channelHandle = WTSVirtualChannelManagerGetEventHandle(context->vcm);
1260 handles[count++] = channelHandle;
1261 status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
1263 if (status == WAIT_FAILED)
1265 WLog_ERR(TAG,
"WaitForMultipleObjects failed (errno: %d)", errno);
1269 WINPR_ASSERT(client->CheckFileDescriptor);
1270 if (client->CheckFileDescriptor(client) != TRUE)
1273 if (WTSVirtualChannelManagerCheckFileDescriptor(context->vcm) != TRUE)
1277 if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, DRDYNVC_SVC_CHANNEL_NAME))
1279 switch (WTSVirtualChannelManagerGetDrdynvcState(context->vcm))
1281 case DRDYNVC_STATE_NONE:
1284 case DRDYNVC_STATE_INITIALIZED:
1287 case DRDYNVC_STATE_READY:
1290 if (sf_peer_audin_running(context) != context->audin_open)
1292 if (!sf_peer_audin_running(context))
1293 sf_peer_audin_start(context);
1295 sf_peer_audin_stop(context);
1298#if defined(CHANNEL_AINPUT_SERVER)
1299 if (sf_peer_ainput_running(context) != context->ainput_open)
1301 if (!sf_peer_ainput_running(context))
1302 sf_peer_ainput_start(context);
1304 sf_peer_ainput_stop(context);
1310 case DRDYNVC_STATE_FAILED:
1317 WLog_INFO(TAG,
"Client %s disconnected.", client->local ?
"(local)" : client->hostname);
1319 WINPR_ASSERT(client->Disconnect);
1320 client->Disconnect(client);
1322 freerdp_peer_context_free(client);
1323 freerdp_peer_free(client);
1328static BOOL test_peer_accepted(freerdp_listener* instance, freerdp_peer* client)
1330 HANDLE hThread =
nullptr;
1332 WINPR_UNUSED(instance);
1334 WINPR_ASSERT(instance);
1335 WINPR_ASSERT(client);
1337 struct server_info* info = instance->info;
1338 client->ContextExtra = info;
1340 if (!(hThread = CreateThread(
nullptr, 0, test_peer_mainloop, (
void*)client, 0,
nullptr)))
1343 (void)CloseHandle(hThread);
1347static void test_server_mainloop(freerdp_listener* instance)
1349 HANDLE handles[32] = WINPR_C_ARRAY_INIT;
1353 WINPR_ASSERT(instance);
1356 WINPR_ASSERT(instance->GetEventHandles);
1357 count = instance->GetEventHandles(instance, handles, 32);
1361 WLog_ERR(TAG,
"Failed to get FreeRDP event handles");
1365 status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
1367 if (WAIT_FAILED == status)
1369 WLog_ERR(TAG,
"select failed");
1373 WINPR_ASSERT(instance->CheckFileDescriptor);
1374 if (instance->CheckFileDescriptor(instance) != TRUE)
1376 WLog_ERR(TAG,
"Failed to check FreeRDP file descriptor");
1381 WINPR_ASSERT(instance->Close);
1382 instance->Close(instance);
1387 const char spcap[7];
1388 const char sfast[7];
1389 const char sport[7];
1390 const char slocal_only[13];
1391 const char scert[7];
1393} options = { {
'-',
'-',
'p',
'c',
'a',
'p',
'=' },
1394 {
'-',
'-',
'f',
'a',
's',
't' },
1395 {
'-',
'-',
'p',
'o',
'r',
't',
'=' },
1396 {
'-',
'-',
'l',
'o',
'c',
'a',
'l',
'-',
'o',
'n',
'l',
'y' },
1397 {
'-',
'-',
'c',
'e',
'r',
't',
'=' },
1398 {
'-',
'-',
'k',
'e',
'y',
'=' } };
1400WINPR_PRAGMA_DIAG_PUSH
1401WINPR_PRAGMA_DIAG_IGNORED_FORMAT_NONLITERAL
1402WINPR_ATTR_FORMAT_ARG(2, 0)
1403static
void print_entry(FILE* fp, WINPR_FORMAT_ARG const
char* fmt, const
char* what,
size_t size)
1405 char buffer[32] = WINPR_C_ARRAY_INIT;
1406 strncpy(buffer, what, MIN(size,
sizeof(buffer) - 1));
1407 (void)fprintf(fp, fmt, buffer);
1409WINPR_PRAGMA_DIAG_POP
1412static int usage(
const char* app,
const char* invalid)
1416 (void)fprintf(fp,
"Invalid argument '%s'\n", invalid);
1417 (void)fprintf(fp,
"Usage: %s <arg>[ <arg> ...]\n", app);
1418 (void)fprintf(fp,
"Arguments:\n");
1419 print_entry(fp,
"\t%s<pcap file>\n", options.spcap,
sizeof(options.spcap));
1420 print_entry(fp,
"\t%s<cert file>\n", options.scert,
sizeof(options.scert));
1421 print_entry(fp,
"\t%s<key file>\n", options.skey,
sizeof(options.skey));
1422 print_entry(fp,
"\t%s\n", options.sfast,
sizeof(options.sfast));
1423 print_entry(fp,
"\t%s<port>\n", options.sport,
sizeof(options.sport));
1424 print_entry(fp,
"\t%s\n", options.slocal_only,
sizeof(options.slocal_only));
1428int main(
int argc,
char* argv[])
1431 BOOL started = FALSE;
1432 WSADATA wsaData = WINPR_C_ARRAY_INIT;
1433 freerdp_listener* instance =
nullptr;
1434 char* file =
nullptr;
1435 char name[MAX_PATH] = WINPR_C_ARRAY_INIT;
1437 BOOL localOnly = FALSE;
1438 struct server_info info = WINPR_C_ARRAY_INIT;
1439 const char* app = argv[0];
1441 info.test_dump_rfx_realtime = TRUE;
1445 for (
int i = 1; i < argc; i++)
1447 char* arg = argv[i];
1449 if (strncmp(arg, options.sfast,
sizeof(options.sfast)) == 0)
1450 info.test_dump_rfx_realtime = FALSE;
1451 else if (strncmp(arg, options.sport,
sizeof(options.sport)) == 0)
1453 const char* sport = &arg[
sizeof(options.sport)];
1454 port = strtol(sport,
nullptr, 10);
1456 if ((port < 1) || (port > UINT16_MAX) || (errno != 0))
1457 return usage(app, arg);
1459 else if (strncmp(arg, options.slocal_only,
sizeof(options.slocal_only)) == 0)
1461 else if (strncmp(arg, options.spcap,
sizeof(options.spcap)) == 0)
1463 info.test_pcap_file = &arg[
sizeof(options.spcap)];
1464 if (!winpr_PathFileExists(info.test_pcap_file))
1465 return usage(app, arg);
1467 else if (strncmp(arg, options.scert,
sizeof(options.scert)) == 0)
1469 info.cert = &arg[
sizeof(options.scert)];
1470 if (!winpr_PathFileExists(info.cert))
1471 return usage(app, arg);
1473 else if (strncmp(arg, options.skey,
sizeof(options.skey)) == 0)
1475 info.key = &arg[
sizeof(options.skey)];
1476 if (!winpr_PathFileExists(info.key))
1477 return usage(app, arg);
1480 return usage(app, arg);
1483 if (!WTSRegisterWtsApiFunctionTable(FreeRDP_InitWtsApi()))
1486 if (!winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT))
1489 instance = freerdp_listener_new();
1495 info.cert =
"server.crt";
1497 info.key =
"server.key";
1499 instance->info = (
void*)&info;
1500 instance->PeerAccepted = test_peer_accepted;
1502 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
1506 (void)sprintf_s(name,
sizeof(name),
"tfreerdp-server.%ld", port);
1507 file = GetKnownSubPath(KNOWN_PATH_TEMP, name);
1514 WINPR_ASSERT(instance->OpenLocal);
1515 started = instance->OpenLocal(instance, file);
1519 WINPR_ASSERT(instance->Open);
1520 started = instance->Open(instance,
nullptr, (UINT16)port);
1527 test_server_mainloop(instance);
1533 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.