Blame server/Sample/sfreerdp.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * FreeRDP Test Server
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
Packit 1fb8d4
 * Copyright 2011 Vic Lee
Packit 1fb8d4
 * Copyright 2014 Norbert Federa <norbert.federa@thincast.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <errno.h>
Packit 1fb8d4
#include <signal.h>
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/ssl.h>
Packit 1fb8d4
#include <winpr/synch.h>
Packit 1fb8d4
#include <winpr/string.h>
Packit 1fb8d4
#include <winpr/path.h>
Packit 1fb8d4
#include <winpr/winsock.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/channels/wtsvc.h>
Packit 1fb8d4
#include <freerdp/channels/channels.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/constants.h>
Packit 1fb8d4
#include <freerdp/server/rdpsnd.h>
Packit 1fb8d4
Packit 1fb8d4
#include "sf_audin.h"
Packit 1fb8d4
#include "sf_rdpsnd.h"
Packit 1fb8d4
#include "sf_encomsp.h"
Packit 1fb8d4
Packit 1fb8d4
#include "sfreerdp.h"
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/log.h>
Packit 1fb8d4
#define TAG SERVER_TAG("sample")
Packit 1fb8d4
Packit 1fb8d4
#define SAMPLE_SERVER_USE_CLIENT_RESOLUTION 1
Packit 1fb8d4
#define SAMPLE_SERVER_DEFAULT_WIDTH 1024
Packit 1fb8d4
#define SAMPLE_SERVER_DEFAULT_HEIGHT 768
Packit 1fb8d4
Packit 1fb8d4
static char* test_pcap_file = NULL;
Packit 1fb8d4
static BOOL test_dump_rfx_realtime = TRUE;
Packit 1fb8d4
Packit Service 5a9772
static BOOL test_peer_context_new(freerdp_peer* client, rdpContext* ctx)
Packit 1fb8d4
{
Packit Service 5a9772
	testPeerContext* context = (testPeerContext*)ctx;
Packit 1fb8d4
	if (!(context->rfx_context = rfx_context_new(TRUE)))
Packit 1fb8d4
		goto fail_rfx_context;
Packit 1fb8d4
Packit 1fb8d4
	if (!rfx_context_reset(context->rfx_context, SAMPLE_SERVER_DEFAULT_WIDTH,
Packit 1fb8d4
	                       SAMPLE_SERVER_DEFAULT_HEIGHT))
Packit 1fb8d4
		goto fail_rfx_context;
Packit 1fb8d4
Packit 1fb8d4
	context->rfx_context->mode = RLGR3;
Packit 1fb8d4
	rfx_context_set_pixel_format(context->rfx_context, PIXEL_FORMAT_RGB24);
Packit 1fb8d4
Packit 1fb8d4
	if (!(context->nsc_context = nsc_context_new()))
Packit 1fb8d4
		goto fail_nsc_context;
Packit 1fb8d4
Packit Service 5a9772
	if (!nsc_context_set_parameters(context->nsc_context, NSC_COLOR_FORMAT, PIXEL_FORMAT_RGB24))
Packit Service 5a9772
		goto fail_stream_new;
Packit 1fb8d4
Packit 1fb8d4
	if (!(context->s = Stream_New(NULL, 65536)))
Packit 1fb8d4
		goto fail_stream_new;
Packit 1fb8d4
Packit 1fb8d4
	context->icon_x = -1;
Packit 1fb8d4
	context->icon_y = -1;
Packit Service 5a9772
	context->vcm = WTSOpenServerA((LPSTR)client->context);
Packit 1fb8d4
Packit 1fb8d4
	if (!context->vcm || context->vcm == INVALID_HANDLE_VALUE)
Packit 1fb8d4
		goto fail_open_server;
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
fail_open_server:
Packit 1fb8d4
	context->vcm = NULL;
Packit 1fb8d4
	Stream_Free(context->s, TRUE);
Packit 1fb8d4
	context->s = NULL;
Packit 1fb8d4
fail_stream_new:
Packit 1fb8d4
	nsc_context_free(context->nsc_context);
Packit 1fb8d4
	context->nsc_context = NULL;
Packit 1fb8d4
fail_nsc_context:
Packit 1fb8d4
	rfx_context_free(context->rfx_context);
Packit 1fb8d4
	context->rfx_context = NULL;
Packit 1fb8d4
fail_rfx_context:
Packit 1fb8d4
	return FALSE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static void test_peer_context_free(freerdp_peer* client, rdpContext* ctx)
Packit 1fb8d4
{
Packit Service 5a9772
	testPeerContext* context = (testPeerContext*)ctx;
Packit Service 5a9772
	WINPR_UNUSED(client);
Packit Service 5a9772
Packit 1fb8d4
	if (context)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (context->debug_channel_thread)
Packit 1fb8d4
		{
Packit 1fb8d4
			SetEvent(context->stopEvent);
Packit 1fb8d4
			WaitForSingleObject(context->debug_channel_thread, INFINITE);
Packit 1fb8d4
			CloseHandle(context->debug_channel_thread);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		Stream_Free(context->s, TRUE);
Packit 1fb8d4
		free(context->icon_data);
Packit 1fb8d4
		free(context->bg_data);
Packit 1fb8d4
		rfx_context_free(context->rfx_context);
Packit 1fb8d4
		nsc_context_free(context->nsc_context);
Packit 1fb8d4
Packit 1fb8d4
		if (context->debug_channel)
Packit 1fb8d4
			WTSVirtualChannelClose(context->debug_channel);
Packit 1fb8d4
Packit Service 5a9772
		sf_peer_audin_uninit(context);
Packit 1fb8d4
Packit 1fb8d4
		if (context->rdpsnd)
Packit 1fb8d4
			rdpsnd_server_context_free(context->rdpsnd);
Packit 1fb8d4
Packit 1fb8d4
		if (context->encomsp)
Packit 1fb8d4
			encomsp_server_context_free(context->encomsp);
Packit 1fb8d4
Packit Service 5a9772
		WTSCloseServer((HANDLE)context->vcm);
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL test_peer_init(freerdp_peer* client)
Packit 1fb8d4
{
Packit 1fb8d4
	client->ContextSize = sizeof(testPeerContext);
Packit Service 5a9772
	client->ContextNew = test_peer_context_new;
Packit Service 5a9772
	client->ContextFree = test_peer_context_free;
Packit 1fb8d4
	return freerdp_peer_context_new(client);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static wStream* test_peer_stream_init(testPeerContext* context)
Packit 1fb8d4
{
Packit 1fb8d4
	Stream_Clear(context->s);
Packit 1fb8d4
	Stream_SetPosition(context->s, 0);
Packit 1fb8d4
	return context->s;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void test_peer_begin_frame(freerdp_peer* client)
Packit 1fb8d4
{
Packit 1fb8d4
	rdpUpdate* update = client->update;
Packit Service 5a9772
	SURFACE_FRAME_MARKER fm = { 0 };
Packit Service 5a9772
	testPeerContext* context = (testPeerContext*)client->context;
Packit 1fb8d4
	fm.frameAction = SURFACECMD_FRAMEACTION_BEGIN;
Packit 1fb8d4
	fm.frameId = context->frame_id;
Packit 1fb8d4
	update->SurfaceFrameMarker(update->context, &fm;;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void test_peer_end_frame(freerdp_peer* client)
Packit 1fb8d4
{
Packit 1fb8d4
	rdpUpdate* update = client->update;
Packit Service 5a9772
	SURFACE_FRAME_MARKER fm = { 0 };
Packit Service 5a9772
	testPeerContext* context = (testPeerContext*)client->context;
Packit 1fb8d4
	fm.frameAction = SURFACECMD_FRAMEACTION_END;
Packit 1fb8d4
	fm.frameId = context->frame_id;
Packit 1fb8d4
	update->SurfaceFrameMarker(update->context, &fm;;
Packit 1fb8d4
	context->frame_id++;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL test_peer_draw_background(freerdp_peer* client)
Packit 1fb8d4
{
Packit 1fb8d4
	int size;
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	RFX_RECT rect;
Packit 1fb8d4
	BYTE* rgb_data;
Packit 1fb8d4
	rdpUpdate* update = client->update;
Packit 1fb8d4
	SURFACE_BITS_COMMAND cmd = { 0 };
Packit Service 5a9772
	testPeerContext* context = (testPeerContext*)client->context;
Packit 1fb8d4
	BOOL ret = FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (!client->settings->RemoteFxCodec && !client->settings->NSCodec)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	s = test_peer_stream_init(context);
Packit 1fb8d4
	rect.x = 0;
Packit 1fb8d4
	rect.y = 0;
Packit 1fb8d4
	rect.width = client->settings->DesktopWidth;
Packit 1fb8d4
	rect.height = client->settings->DesktopHeight;
Packit 1fb8d4
	size = rect.width * rect.height * 3;
Packit 1fb8d4
Packit 1fb8d4
	if (!(rgb_data = malloc(size)))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Problem allocating memory");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	memset(rgb_data, 0xA0, size);
Packit 1fb8d4
Packit 1fb8d4
	if (client->settings->RemoteFxCodec)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_DBG(TAG, "Using RemoteFX codec");
Packit Service 5a9772
		if (!rfx_compose_message(context->rfx_context, s, &rect, 1, rgb_data, rect.width,
Packit Service 5a9772
		                         rect.height, rect.width * 3))
Packit 1fb8d4
		{
Packit 1fb8d4
			goto out;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		cmd.bmp.codecID = client->settings->RemoteFxCodecId;
Packit Service 5a9772
		cmd.cmdType = CMDTYPE_STREAM_SURFACE_BITS;
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_DBG(TAG, "Using NSCodec");
Packit Service 5a9772
		nsc_compose_message(context->nsc_context, s, rgb_data, rect.width, rect.height,
Packit Service 5a9772
		                    rect.width * 3);
Packit 1fb8d4
		cmd.bmp.codecID = client->settings->NSCodecId;
Packit Service 5a9772
		cmd.cmdType = CMDTYPE_SET_SURFACE_BITS;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	cmd.destLeft = 0;
Packit 1fb8d4
	cmd.destTop = 0;
Packit 1fb8d4
	cmd.destRight = rect.width;
Packit 1fb8d4
	cmd.destBottom = rect.height;
Packit 1fb8d4
	cmd.bmp.bpp = 32;
Packit 1fb8d4
	cmd.bmp.flags = 0;
Packit 1fb8d4
	cmd.bmp.width = rect.width;
Packit 1fb8d4
	cmd.bmp.height = rect.height;
Packit 1fb8d4
	cmd.bmp.bitmapDataLength = Stream_GetPosition(s);
Packit 1fb8d4
	cmd.bmp.bitmapData = Stream_Buffer(s);
Packit 1fb8d4
	test_peer_begin_frame(client);
Packit 1fb8d4
	update->SurfaceBits(update->context, &cmd);
Packit 1fb8d4
	test_peer_end_frame(client);
Packit 1fb8d4
	ret = TRUE;
Packit 1fb8d4
out:
Packit 1fb8d4
	free(rgb_data);
Packit 1fb8d4
	return ret;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL test_peer_load_icon(freerdp_peer* client)
Packit 1fb8d4
{
Packit Service 5a9772
	testPeerContext* context = (testPeerContext*)client->context;
Packit 1fb8d4
	FILE* fp;
Packit 1fb8d4
	int i;
Packit 1fb8d4
	char line[50];
Packit 1fb8d4
	BYTE* rgb_data = NULL;
Packit 1fb8d4
	int c;
Packit 1fb8d4
Packit 1fb8d4
	if (!client->settings->RemoteFxCodec && !client->settings->NSCodec)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Client doesn't support RemoteFX or NSCodec");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if ((fp = fopen("test_icon.ppm", "r")) == NULL)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Unable to open test icon");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* P3 */
Packit 1fb8d4
	fgets(line, sizeof(line), fp);
Packit 1fb8d4
	/* Creater comment */
Packit 1fb8d4
	fgets(line, sizeof(line), fp);
Packit 1fb8d4
	/* width height */
Packit 1fb8d4
	fgets(line, sizeof(line), fp);
Packit 1fb8d4
Packit 1fb8d4
	if (sscanf(line, "%d %d", &context->icon_width, &context->icon_height) < 2)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Problem while extracting width/height from the icon file");
Packit 1fb8d4
		goto out_fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Max */
Packit 1fb8d4
	fgets(line, sizeof(line), fp);
Packit 1fb8d4
Packit 1fb8d4
	if (!(rgb_data = calloc(context->icon_height, context->icon_width * 3)))
Packit 1fb8d4
		goto out_fail;
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < context->icon_width * context->icon_height * 3; i++)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (!fgets(line, sizeof(line), fp) || (sscanf(line, "%d", &c) != 1))
Packit 1fb8d4
			goto out_fail;
Packit 1fb8d4
Packit 1fb8d4
		rgb_data[i] = (BYTE)c;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* background with same size, which will be used to erase the icon from old position */
Packit 1fb8d4
	if (!(context->bg_data = calloc(context->icon_height, context->icon_width * 3)))
Packit 1fb8d4
		goto out_fail;
Packit 1fb8d4
Packit 1fb8d4
	memset(context->bg_data, 0xA0, context->icon_width * context->icon_height * 3);
Packit 1fb8d4
	context->icon_data = rgb_data;
Packit 1fb8d4
	fclose(fp);
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
out_fail:
Packit 1fb8d4
	free(rgb_data);
Packit Service 5a9772
	context->bg_data = NULL;
Packit 1fb8d4
	fclose(fp);
Packit 1fb8d4
	return FALSE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void test_peer_draw_icon(freerdp_peer* client, int x, int y)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	RFX_RECT rect;
Packit 1fb8d4
	rdpUpdate* update = client->update;
Packit 1fb8d4
	SURFACE_BITS_COMMAND cmd = { 0 };
Packit Service 5a9772
	testPeerContext* context = (testPeerContext*)client->context;
Packit 1fb8d4
Packit 1fb8d4
	if (client->update->dump_rfx)
Packit 1fb8d4
		return;
Packit 1fb8d4
Packit 1fb8d4
	if (!context)
Packit 1fb8d4
		return;
Packit 1fb8d4
Packit 1fb8d4
	if (context->icon_width < 1 || !context->activated)
Packit 1fb8d4
		return;
Packit 1fb8d4
Packit 1fb8d4
	test_peer_begin_frame(client);
Packit 1fb8d4
	rect.x = 0;
Packit 1fb8d4
	rect.y = 0;
Packit 1fb8d4
	rect.width = context->icon_width;
Packit 1fb8d4
	rect.height = context->icon_height;
Packit 1fb8d4
Packit Service 5a9772
	if (client->settings->RemoteFxCodec)
Packit Service 5a9772
	{
Packit Service 5a9772
		cmd.bmp.codecID = client->settings->RemoteFxCodecId;
Packit Service 5a9772
		cmd.cmdType = CMDTYPE_STREAM_SURFACE_BITS;
Packit Service 5a9772
	}
Packit Service 5a9772
	else
Packit Service 5a9772
	{
Packit Service 5a9772
		cmd.bmp.codecID = client->settings->NSCodecId;
Packit Service 5a9772
		cmd.cmdType = CMDTYPE_SET_SURFACE_BITS;
Packit Service 5a9772
	}
Packit Service 5a9772
Packit 1fb8d4
	if (context->icon_x >= 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		s = test_peer_stream_init(context);
Packit 1fb8d4
Packit 1fb8d4
		if (client->settings->RemoteFxCodec)
Packit Service 5a9772
			rfx_compose_message(context->rfx_context, s, &rect, 1, context->bg_data, rect.width,
Packit Service 5a9772
			                    rect.height, rect.width * 3);
Packit 1fb8d4
		else
Packit Service 5a9772
			nsc_compose_message(context->nsc_context, s, context->bg_data, rect.width, rect.height,
Packit Service 5a9772
			                    rect.width * 3);
Packit 1fb8d4
Packit 1fb8d4
		cmd.destLeft = context->icon_x;
Packit 1fb8d4
		cmd.destTop = context->icon_y;
Packit 1fb8d4
		cmd.destRight = context->icon_x + context->icon_width;
Packit 1fb8d4
		cmd.destBottom = context->icon_y + context->icon_height;
Packit 1fb8d4
		cmd.bmp.bpp = 32;
Packit 1fb8d4
		cmd.bmp.flags = 0;
Packit 1fb8d4
		cmd.bmp.width = context->icon_width;
Packit 1fb8d4
		cmd.bmp.height = context->icon_height;
Packit 1fb8d4
		cmd.bmp.bitmapDataLength = Stream_GetPosition(s);
Packit 1fb8d4
		cmd.bmp.bitmapData = Stream_Buffer(s);
Packit 1fb8d4
		update->SurfaceBits(update->context, &cmd);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	s = test_peer_stream_init(context);
Packit 1fb8d4
Packit 1fb8d4
	if (client->settings->RemoteFxCodec)
Packit Service 5a9772
		rfx_compose_message(context->rfx_context, s, &rect, 1, context->icon_data, rect.width,
Packit Service 5a9772
		                    rect.height, rect.width * 3);
Packit 1fb8d4
	else
Packit Service 5a9772
		nsc_compose_message(context->nsc_context, s, context->icon_data, rect.width, rect.height,
Packit Service 5a9772
		                    rect.width * 3);
Packit 1fb8d4
Packit 1fb8d4
	cmd.destLeft = x;
Packit 1fb8d4
	cmd.destTop = y;
Packit 1fb8d4
	cmd.destRight = x + context->icon_width;
Packit 1fb8d4
	cmd.destBottom = y + context->icon_height;
Packit 1fb8d4
	cmd.bmp.bpp = 32;
Packit 1fb8d4
	cmd.bmp.width = context->icon_width;
Packit 1fb8d4
	cmd.bmp.height = context->icon_height;
Packit 1fb8d4
	cmd.bmp.bitmapDataLength = Stream_GetPosition(s);
Packit 1fb8d4
	cmd.bmp.bitmapData = Stream_Buffer(s);
Packit 1fb8d4
	update->SurfaceBits(update->context, &cmd);
Packit 1fb8d4
	context->icon_x = x;
Packit 1fb8d4
	context->icon_y = y;
Packit 1fb8d4
	test_peer_end_frame(client);
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL test_sleep_tsdiff(UINT32* old_sec, UINT32* old_usec, UINT32 new_sec, UINT32 new_usec)
Packit 1fb8d4
{
Packit 1fb8d4
	INT32 sec, usec;
Packit 1fb8d4
Packit 1fb8d4
	if ((*old_sec == 0) && (*old_usec == 0))
Packit 1fb8d4
	{
Packit 1fb8d4
		*old_sec = new_sec;
Packit 1fb8d4
		*old_usec = new_usec;
Packit 1fb8d4
		return TRUE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	sec = new_sec - *old_sec;
Packit 1fb8d4
	usec = new_usec - *old_usec;
Packit 1fb8d4
Packit 1fb8d4
	if ((sec < 0) || ((sec == 0) && (usec < 0)))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Invalid time stamp detected.");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	*old_sec = new_sec;
Packit 1fb8d4
	*old_usec = new_usec;
Packit 1fb8d4
Packit 1fb8d4
	while (usec < 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		usec += 1000000;
Packit 1fb8d4
		sec--;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (sec > 0)
Packit 1fb8d4
		Sleep(sec * 1000);
Packit 1fb8d4
Packit 1fb8d4
	if (usec > 0)
Packit 1fb8d4
		USleep(usec);
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL tf_peer_dump_rfx(freerdp_peer* client)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	UINT32 prev_seconds;
Packit 1fb8d4
	UINT32 prev_useconds;
Packit 1fb8d4
	rdpUpdate* update;
Packit 1fb8d4
	rdpPcap* pcap_rfx;
Packit 1fb8d4
	pcap_record record;
Packit 1fb8d4
	s = Stream_New(NULL, 512);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	update = client->update;
Packit 1fb8d4
Packit 1fb8d4
	if (!(pcap_rfx = pcap_open(test_pcap_file, FALSE)))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	prev_seconds = prev_useconds = 0;
Packit 1fb8d4
Packit 1fb8d4
	while (pcap_has_next_record(pcap_rfx))
Packit 1fb8d4
	{
Packit 1fb8d4
		if (!pcap_get_next_record_header(pcap_rfx, &record))
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		if (!Stream_EnsureCapacity(s, record.length))
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		record.data = Stream_Buffer(s);
Packit 1fb8d4
		pcap_get_next_record_content(pcap_rfx, &record);
Packit 1fb8d4
		Stream_SetPointer(s, Stream_Buffer(s) + Stream_Capacity(s));
Packit 1fb8d4
Packit Service 5a9772
		if (test_dump_rfx_realtime &&
Packit Service 5a9772
		    test_sleep_tsdiff(&prev_seconds, &prev_useconds, record.header.ts_sec,
Packit Service 5a9772
		                      record.header.ts_usec) == FALSE)
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		update->SurfaceCommand(update->context, s);
Packit 1fb8d4
Packit 1fb8d4
		if (client->CheckFileDescriptor(client) != TRUE)
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
	pcap_close(pcap_rfx);
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static DWORD WINAPI tf_debug_channel_thread_func(LPVOID arg)
Packit 1fb8d4
{
Packit 1fb8d4
	void* fd;
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	void* buffer;
Packit 1fb8d4
	DWORD BytesReturned = 0;
Packit 1fb8d4
	ULONG written;
Packit Service 5a9772
	testPeerContext* context = (testPeerContext*)arg;
Packit 1fb8d4
Packit Service 5a9772
	if (WTSVirtualChannelQuery(context->debug_channel, WTSVirtualFileHandle, &buffer,
Packit Service 5a9772
	                           &BytesReturned) == TRUE)
Packit 1fb8d4
	{
Packit Service 5a9772
		fd = *((void**)buffer);
Packit 1fb8d4
		WTSFreeMemory(buffer);
Packit 1fb8d4
Packit 1fb8d4
		if (!(context->event = CreateWaitObjectEvent(NULL, TRUE, FALSE, fd)))
Packit 1fb8d4
			return 0;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	s = Stream_New(NULL, 4096);
Packit 1fb8d4
	WTSVirtualChannelWrite(context->debug_channel, (PCHAR) "test1", 5, &written);
Packit 1fb8d4
Packit 1fb8d4
	while (1)
Packit 1fb8d4
	{
Packit 1fb8d4
		WaitForSingleObject(context->event, INFINITE);
Packit 1fb8d4
Packit 1fb8d4
		if (WaitForSingleObject(context->stopEvent, 0) == WAIT_OBJECT_0)
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		Stream_SetPosition(s, 0);
Packit 1fb8d4
Packit Service 5a9772
		if (WTSVirtualChannelRead(context->debug_channel, 0, (PCHAR)Stream_Buffer(s),
Packit 1fb8d4
		                          Stream_Capacity(s), &BytesReturned) == FALSE)
Packit 1fb8d4
		{
Packit 1fb8d4
			if (BytesReturned == 0)
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			Stream_EnsureRemainingCapacity(s, BytesReturned);
Packit 1fb8d4
Packit Service 5a9772
			if (WTSVirtualChannelRead(context->debug_channel, 0, (PCHAR)Stream_Buffer(s),
Packit 1fb8d4
			                          Stream_Capacity(s), &BytesReturned) == FALSE)
Packit 1fb8d4
			{
Packit 1fb8d4
				/* should not happen */
Packit 1fb8d4
				break;
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		Stream_SetPosition(s, BytesReturned);
Packit Service 5a9772
		WLog_DBG(TAG, "got %" PRIu32 " bytes", BytesReturned);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
	return 0;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL tf_peer_post_connect(freerdp_peer* client)
Packit 1fb8d4
{
Packit Service 5a9772
	testPeerContext* context = (testPeerContext*)client->context;
Packit 1fb8d4
	/**
Packit 1fb8d4
	 * This callback is called when the entire connection sequence is done, i.e. we've received the
Packit 1fb8d4
	 * Font List PDU from the client and sent out the Font Map PDU.
Packit 1fb8d4
	 * The server may start sending graphics output and receiving keyboard/mouse input after this
Packit 1fb8d4
	 * callback returns.
Packit 1fb8d4
	 */
Packit Service 5a9772
	WLog_DBG(TAG, "Client %s is activated (osMajorType %" PRIu32 " osMinorType %" PRIu32 ")",
Packit Service 5a9772
	         client->local ? "(local)" : client->hostname, client->settings->OsMajorType,
Packit Service 5a9772
	         client->settings->OsMinorType);
Packit 1fb8d4
Packit 1fb8d4
	if (client->settings->AutoLogonEnabled)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_DBG(TAG, " and wants to login automatically as %s\\%s",
Packit 1fb8d4
		         client->settings->Domain ? client->settings->Domain : "",
Packit 1fb8d4
		         client->settings->Username);
Packit 1fb8d4
		/* A real server may perform OS login here if NLA is not executed previously. */
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	WLog_DBG(TAG, "");
Packit Service 5a9772
	WLog_DBG(TAG, "Client requested desktop: %" PRIu32 "x%" PRIu32 "x%" PRIu32 "",
Packit 1fb8d4
	         client->settings->DesktopWidth, client->settings->DesktopHeight,
Packit 1fb8d4
	         client->settings->ColorDepth);
Packit 1fb8d4
#if (SAMPLE_SERVER_USE_CLIENT_RESOLUTION == 1)
Packit 1fb8d4
Packit 1fb8d4
	if (!rfx_context_reset(context->rfx_context, client->settings->DesktopWidth,
Packit 1fb8d4
	                       client->settings->DesktopHeight))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	WLog_DBG(TAG, "Using resolution requested by client.");
Packit 1fb8d4
#else
Packit 1fb8d4
	client->settings->DesktopWidth = context->rfx_context->width;
Packit 1fb8d4
	client->settings->DesktopHeight = context->rfx_context->height;
Packit Service 5a9772
	WLog_DBG(TAG, "Resizing client to %" PRIu32 "x%" PRIu32 "", client->settings->DesktopWidth,
Packit 1fb8d4
	         client->settings->DesktopHeight);
Packit 1fb8d4
	client->update->DesktopResize(client->update->context);
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit Service 5a9772
	/* A real server should tag the peer as activated here and start sending updates in main loop.
Packit Service 5a9772
	 */
Packit 1fb8d4
	if (!test_peer_load_icon(client))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_DBG(TAG, "Unable to load icon");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, "rdpdbg"))
Packit 1fb8d4
	{
Packit Service 5a9772
		context->debug_channel = WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, "rdpdbg");
Packit 1fb8d4
Packit 1fb8d4
		if (context->debug_channel != NULL)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_DBG(TAG, "Open channel rdpdbg.");
Packit 1fb8d4
Packit 1fb8d4
			if (!(context->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
Packit 1fb8d4
			{
Packit 1fb8d4
				WLog_ERR(TAG, "Failed to create stop event");
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit Service 5a9772
			if (!(context->debug_channel_thread =
Packit Service 5a9772
			          CreateThread(NULL, 0, tf_debug_channel_thread_func, (void*)context, 0, NULL)))
Packit 1fb8d4
			{
Packit 1fb8d4
				WLog_ERR(TAG, "Failed to create debug channel thread");
Packit 1fb8d4
				CloseHandle(context->stopEvent);
Packit 1fb8d4
				context->stopEvent = NULL;
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, "rdpsnd"))
Packit 1fb8d4
	{
Packit 1fb8d4
		sf_peer_rdpsnd_init(context); /* Audio Output */
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, ENCOMSP_SVC_CHANNEL_NAME))
Packit 1fb8d4
	{
Packit 1fb8d4
		sf_peer_encomsp_init(context); /* Lync Multiparty */
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Dynamic Virtual Channels */
Packit 1fb8d4
	sf_peer_audin_init(context); /* Audio Input */
Packit 1fb8d4
	/* Return FALSE here would stop the execution of the peer main loop. */
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL tf_peer_activate(freerdp_peer* client)
Packit 1fb8d4
{
Packit Service 5a9772
	testPeerContext* context = (testPeerContext*)client->context;
Packit 1fb8d4
	context->activated = TRUE;
Packit Service 5a9772
	// client->settings->CompressionLevel = PACKET_COMPR_TYPE_8K;
Packit Service 5a9772
	// client->settings->CompressionLevel = PACKET_COMPR_TYPE_64K;
Packit Service 5a9772
	// client->settings->CompressionLevel = PACKET_COMPR_TYPE_RDP6;
Packit 1fb8d4
	client->settings->CompressionLevel = PACKET_COMPR_TYPE_RDP61;
Packit 1fb8d4
Packit 1fb8d4
	if (test_pcap_file != NULL)
Packit 1fb8d4
	{
Packit 1fb8d4
		client->update->dump_rfx = TRUE;
Packit 1fb8d4
Packit 1fb8d4
		if (!tf_peer_dump_rfx(client))
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
		test_peer_draw_background(client);
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL tf_peer_synchronize_event(rdpInput* input, UINT32 flags)
Packit 1fb8d4
{
Packit Service 5a9772
	WINPR_UNUSED(input);
Packit Service 5a9772
	WLog_DBG(TAG, "Client sent a synchronize event (flags:0x%" PRIX32 ")", flags);
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL tf_peer_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
Packit 1fb8d4
{
Packit 1fb8d4
	freerdp_peer* client = input->context->peer;
Packit 1fb8d4
	rdpUpdate* update = client->update;
Packit Service 5a9772
	testPeerContext* context = (testPeerContext*)input->context;
Packit Service 5a9772
	WLog_DBG(TAG, "Client sent a keyboard event (flags:0x%04" PRIX16 " code:0x%04" PRIX16 ")",
Packit Service 5a9772
	         flags, code);
Packit 1fb8d4
Packit 1fb8d4
	if ((flags & 0x4000) && code == 0x22) /* 'g' key */
Packit 1fb8d4
	{
Packit 1fb8d4
		if (client->settings->DesktopWidth != 800)
Packit 1fb8d4
		{
Packit 1fb8d4
			client->settings->DesktopWidth = 800;
Packit 1fb8d4
			client->settings->DesktopHeight = 600;
Packit 1fb8d4
		}
Packit 1fb8d4
		else
Packit 1fb8d4
		{
Packit 1fb8d4
			client->settings->DesktopWidth = SAMPLE_SERVER_DEFAULT_WIDTH;
Packit 1fb8d4
			client->settings->DesktopHeight = SAMPLE_SERVER_DEFAULT_HEIGHT;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (!rfx_context_reset(context->rfx_context, client->settings->DesktopWidth,
Packit 1fb8d4
		                       client->settings->DesktopHeight))
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
Packit 1fb8d4
		update->DesktopResize(update->context);
Packit 1fb8d4
		context->activated = FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
	else if ((flags & 0x4000) && code == 0x2E) /* 'c' key */
Packit 1fb8d4
	{
Packit 1fb8d4
		if (context->debug_channel)
Packit 1fb8d4
		{
Packit 1fb8d4
			ULONG written;
Packit 1fb8d4
			WTSVirtualChannelWrite(context->debug_channel, (PCHAR) "test2", 5, &written);
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
	else if ((flags & 0x4000) && code == 0x2D) /* 'x' key */
Packit 1fb8d4
	{
Packit 1fb8d4
		client->Close(client);
Packit 1fb8d4
	}
Packit 1fb8d4
	else if ((flags & 0x4000) && code == 0x13) /* 'r' key */
Packit 1fb8d4
	{
Packit Service 5a9772
		context->audin_open = !context->audin_open;
Packit 1fb8d4
	}
Packit 1fb8d4
	else if ((flags & 0x4000) && code == 0x1F) /* 's' key */
Packit 1fb8d4
	{
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL tf_peer_unicode_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
Packit 1fb8d4
{
Packit Service 5a9772
	WINPR_UNUSED(input);
Packit Service 5a9772
	WLog_DBG(TAG,
Packit Service 5a9772
	         "Client sent a unicode keyboard event (flags:0x%04" PRIX16 " code:0x%04" PRIX16 ")",
Packit 1fb8d4
	         flags, code);
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL tf_peer_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
Packit 1fb8d4
{
Packit Service 5a9772
	WINPR_UNUSED(flags);
Packit Service 5a9772
	// WLog_DBG(TAG, "Client sent a mouse event (flags:0x%04"PRIX16" pos:%"PRIu16",%"PRIu16")",
Packit Service 5a9772
	// flags, x, y);
Packit 1fb8d4
	test_peer_draw_icon(input->context->peer, x + 10, y);
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
BOOL tf_peer_extended_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
Packit 1fb8d4
{
Packit Service 5a9772
	WINPR_UNUSED(flags);
Packit Service 5a9772
	// WLog_DBG(TAG, "Client sent an extended mouse event (flags:0x%04"PRIX16"
Packit Service 5a9772
	// pos:%"PRIu16",%"PRIu16")", flags, x, y);
Packit Service 5a9772
	test_peer_draw_icon(input->context->peer, x + 10, y);
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL tf_peer_refresh_rect(rdpContext* context, BYTE count, const RECTANGLE_16* areas)
Packit 1fb8d4
{
Packit 1fb8d4
	BYTE i;
Packit Service 5a9772
	WINPR_UNUSED(context);
Packit 1fb8d4
	WLog_DBG(TAG, "Client requested to refresh:");
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < count; i++)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_DBG(TAG, "  (%" PRIu16 ", %" PRIu16 ") (%" PRIu16 ", %" PRIu16 ")", areas[i].left,
Packit Service 5a9772
		         areas[i].top, areas[i].right, areas[i].bottom);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL tf_peer_suppress_output(rdpContext* context, BYTE allow, const RECTANGLE_16* area)
Packit 1fb8d4
{
Packit Service 5a9772
	WINPR_UNUSED(context);
Packit Service 5a9772
Packit 1fb8d4
	if (allow > 0)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_DBG(TAG,
Packit Service 5a9772
		         "Client restore output (%" PRIu16 ", %" PRIu16 ") (%" PRIu16 ", %" PRIu16 ").",
Packit Service 5a9772
		         area->left, area->top, area->right, area->bottom);
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_DBG(TAG, "Client minimized and suppress output.");
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static DWORD WINAPI test_peer_mainloop(LPVOID arg)
Packit 1fb8d4
{
Packit Service 5a9772
	DWORD error = CHANNEL_RC_OK;
Packit 1fb8d4
	HANDLE handles[32];
Packit 1fb8d4
	DWORD count;
Packit 1fb8d4
	DWORD status;
Packit 1fb8d4
	testPeerContext* context;
Packit Service 5a9772
	freerdp_peer* client = (freerdp_peer*)arg;
Packit 1fb8d4
Packit 1fb8d4
	if (!test_peer_init(client))
Packit 1fb8d4
	{
Packit 1fb8d4
		freerdp_peer_free(client);
Packit 1fb8d4
		return 0;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Initialize the real server settings here */
Packit 1fb8d4
	client->settings->CertificateFile = _strdup("server.crt");
Packit 1fb8d4
	client->settings->PrivateKeyFile = _strdup("server.key");
Packit 1fb8d4
	client->settings->RdpKeyFile = _strdup("server.key");
Packit 1fb8d4
Packit Service 5a9772
	if (!client->settings->CertificateFile || !client->settings->PrivateKeyFile ||
Packit Service 5a9772
	    !client->settings->RdpKeyFile)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Memory allocation failed (strdup)");
Packit 1fb8d4
		freerdp_peer_free(client);
Packit 1fb8d4
		return 0;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	client->settings->RdpSecurity = TRUE;
Packit 1fb8d4
	client->settings->TlsSecurity = TRUE;
Packit 1fb8d4
	client->settings->NlaSecurity = FALSE;
Packit 1fb8d4
	client->settings->EncryptionLevel = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE;
Packit 1fb8d4
	/* client->settings->EncryptionLevel = ENCRYPTION_LEVEL_HIGH; */
Packit 1fb8d4
	/* client->settings->EncryptionLevel = ENCRYPTION_LEVEL_LOW; */
Packit 1fb8d4
	/* client->settings->EncryptionLevel = ENCRYPTION_LEVEL_FIPS; */
Packit 1fb8d4
	client->settings->RemoteFxCodec = TRUE;
Packit Service 5a9772
	client->settings->NSCodec = TRUE;
Packit 1fb8d4
	client->settings->ColorDepth = 32;
Packit 1fb8d4
	client->settings->SuppressOutput = TRUE;
Packit 1fb8d4
	client->settings->RefreshRect = TRUE;
Packit 1fb8d4
	client->PostConnect = tf_peer_post_connect;
Packit 1fb8d4
	client->Activate = tf_peer_activate;
Packit 1fb8d4
	client->input->SynchronizeEvent = tf_peer_synchronize_event;
Packit 1fb8d4
	client->input->KeyboardEvent = tf_peer_keyboard_event;
Packit 1fb8d4
	client->input->UnicodeKeyboardEvent = tf_peer_unicode_keyboard_event;
Packit 1fb8d4
	client->input->MouseEvent = tf_peer_mouse_event;
Packit 1fb8d4
	client->input->ExtendedMouseEvent = tf_peer_extended_mouse_event;
Packit 1fb8d4
	client->update->RefreshRect = tf_peer_refresh_rect;
Packit 1fb8d4
	client->update->SuppressOutput = tf_peer_suppress_output;
Packit 1fb8d4
	client->settings->MultifragMaxRequestSize = 0xFFFFFF; /* FIXME */
Packit 1fb8d4
	client->Initialize(client);
Packit Service 5a9772
	context = (testPeerContext*)client->context;
Packit Service 5a9772
	WLog_INFO(TAG, "We've got a client %s", client->local ? "(local)" : client->hostname);
Packit 1fb8d4
Packit Service 5a9772
	while (error == CHANNEL_RC_OK)
Packit 1fb8d4
	{
Packit 1fb8d4
		count = 0;
Packit 1fb8d4
		{
Packit 1fb8d4
			DWORD tmp = client->GetEventHandles(client, &handles[count], 32 - count);
Packit 1fb8d4
Packit 1fb8d4
			if (tmp == 0)
Packit 1fb8d4
			{
Packit 1fb8d4
				WLog_ERR(TAG, "Failed to get FreeRDP transport event handles");
Packit 1fb8d4
				break;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			count += tmp;
Packit 1fb8d4
		}
Packit 1fb8d4
		handles[count++] = WTSVirtualChannelManagerGetEventHandle(context->vcm);
Packit 1fb8d4
		status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
Packit 1fb8d4
Packit 1fb8d4
		if (status == WAIT_FAILED)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "WaitForMultipleObjects failed (errno: %d)", errno);
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (client->CheckFileDescriptor(client) != TRUE)
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		if (WTSVirtualChannelManagerCheckFileDescriptor(context->vcm) != TRUE)
Packit 1fb8d4
			break;
Packit Service 5a9772
Packit Service 5a9772
		/* Handle dynamic virtual channel intializations */
Packit Service 5a9772
		if (WTSVirtualChannelManagerIsChannelJoined(context->vcm, "drdynvc"))
Packit Service 5a9772
		{
Packit Service 5a9772
			switch (WTSVirtualChannelManagerGetDrdynvcState(context->vcm))
Packit Service 5a9772
			{
Packit Service 5a9772
				case DRDYNVC_STATE_NONE:
Packit Service 5a9772
					break;
Packit Service 5a9772
Packit Service 5a9772
				case DRDYNVC_STATE_INITIALIZED:
Packit Service 5a9772
					break;
Packit Service 5a9772
Packit Service 5a9772
				case DRDYNVC_STATE_READY:
Packit Service 5a9772
Packit Service 5a9772
					/* Here is the correct state to start dynamic virtual channels */
Packit Service 5a9772
					if (sf_peer_audin_running(context) != context->audin_open)
Packit Service 5a9772
					{
Packit Service 5a9772
						if (!sf_peer_audin_running(context))
Packit Service 5a9772
							sf_peer_audin_start(context);
Packit Service 5a9772
						else
Packit Service 5a9772
							sf_peer_audin_stop(context);
Packit Service 5a9772
					}
Packit Service 5a9772
Packit Service 5a9772
					break;
Packit Service 5a9772
Packit Service 5a9772
				case DRDYNVC_STATE_FAILED:
Packit Service 5a9772
				default:
Packit Service 5a9772
					break;
Packit Service 5a9772
			}
Packit Service 5a9772
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	WLog_INFO(TAG, "Client %s disconnected.", client->local ? "(local)" : client->hostname);
Packit 1fb8d4
	client->Disconnect(client);
Packit 1fb8d4
	freerdp_peer_context_free(client);
Packit 1fb8d4
	freerdp_peer_free(client);
Packit Service 5a9772
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL test_peer_accepted(freerdp_listener* instance, freerdp_peer* client)
Packit 1fb8d4
{
Packit 1fb8d4
	HANDLE hThread;
Packit Service 5a9772
	WINPR_UNUSED(instance);
Packit 1fb8d4
Packit Service 5a9772
	if (!(hThread = CreateThread(NULL, 0, test_peer_mainloop, (void*)client, 0, NULL)))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	CloseHandle(hThread);
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void test_server_mainloop(freerdp_listener* instance)
Packit 1fb8d4
{
Packit 1fb8d4
	HANDLE handles[32];
Packit 1fb8d4
	DWORD count;
Packit 1fb8d4
	DWORD status;
Packit 1fb8d4
Packit 1fb8d4
	while (1)
Packit 1fb8d4
	{
Packit 1fb8d4
		count = instance->GetEventHandles(instance, handles, 32);
Packit 1fb8d4
Packit 1fb8d4
		if (0 == count)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "Failed to get FreeRDP event handles");
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		status = WaitForMultipleObjects(count, handles, FALSE, INFINITE);
Packit 1fb8d4
Packit 1fb8d4
		if (WAIT_FAILED == status)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "select failed");
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (instance->CheckFileDescriptor(instance) != TRUE)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "Failed to check FreeRDP file descriptor");
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	instance->Close(instance);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
int main(int argc, char* argv[])
Packit 1fb8d4
{
Packit Service 5a9772
	const char spcap[] = "--";
Packit Service 5a9772
	const char sfast[] = "--fast";
Packit Service 5a9772
	const char sport[] = "--port=";
Packit Service 5a9772
	const char slocal_only[] = "--local_only";
Packit 1fb8d4
	WSADATA wsaData;
Packit 1fb8d4
	freerdp_listener* instance;
Packit 1fb8d4
	char* file;
Packit 1fb8d4
	char name[MAX_PATH];
Packit 1fb8d4
	long port = 3389, i;
Packit 1fb8d4
	BOOL localOnly = FALSE;
Packit 1fb8d4
	errno = 0;
Packit 1fb8d4
Packit 1fb8d4
	for (i = 1; i < argc; i++)
Packit 1fb8d4
	{
Packit 1fb8d4
		char* arg = argv[i];
Packit 1fb8d4
Packit Service 5a9772
		if (strncmp(arg, sfast, sizeof(sfast)) == 0)
Packit 1fb8d4
			test_dump_rfx_realtime = FALSE;
Packit Service 5a9772
		else if (strncmp(arg, sport, sizeof(sport) - 1) == 0)
Packit 1fb8d4
		{
Packit 1fb8d4
			StrSep(&arg, "=");
Packit 1fb8d4
Packit 1fb8d4
			if (!arg)
Packit 1fb8d4
				return -1;
Packit 1fb8d4
Packit 1fb8d4
			port = strtol(arg, NULL, 10);
Packit 1fb8d4
Packit 1fb8d4
			if ((port < 1) || (port > 0xFFFF) || (errno != 0))
Packit 1fb8d4
				return -1;
Packit 1fb8d4
		}
Packit Service 5a9772
		else if (strncmp(arg, slocal_only, sizeof(slocal_only)) == 0)
Packit 1fb8d4
			localOnly = TRUE;
Packit Service 5a9772
		else if (strncmp(arg, spcap, sizeof(spcap)) == 0)
Packit 1fb8d4
			test_pcap_file = arg;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	WTSRegisterWtsApiFunctionTable(FreeRDP_InitWtsApi());
Packit 1fb8d4
	winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT);
Packit 1fb8d4
	instance = freerdp_listener_new();
Packit 1fb8d4
Packit 1fb8d4
	if (!instance)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	instance->PeerAccepted = test_peer_accepted;
Packit 1fb8d4
Packit 1fb8d4
	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		freerdp_listener_free(instance);
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Open the server socket and start listening. */
Packit 1fb8d4
	sprintf_s(name, sizeof(name), "tfreerdp-server.%ld", port);
Packit 1fb8d4
	file = GetKnownSubPath(KNOWN_PATH_TEMP, name);
Packit 1fb8d4
Packit 1fb8d4
	if (!file)
Packit 1fb8d4
	{
Packit 1fb8d4
		freerdp_listener_free(instance);
Packit 1fb8d4
		WSACleanup();
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if ((localOnly || instance->Open(instance, NULL, port)) && instance->OpenLocal(instance, file))
Packit 1fb8d4
	{
Packit Service 5a9772
		/* Entering the server main loop. In a real server the listener can be run in its own
Packit Service 5a9772
		 * thread. */
Packit 1fb8d4
		test_server_mainloop(instance);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	free(file);
Packit 1fb8d4
	freerdp_listener_free(instance);
Packit 1fb8d4
	WSACleanup();
Packit 1fb8d4
	return 0;
Packit 1fb8d4
}