2014-07-11 00:32:46 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
*
|
|
|
|
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2014-07-12 07:01:34 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/signal.h>
|
|
|
|
#endif
|
|
|
|
|
2014-07-11 00:32:46 +04:00
|
|
|
#include "shadow.h"
|
|
|
|
|
2014-07-12 03:30:40 +04:00
|
|
|
void* shadow_server_thread(rdpShadowServer* server)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
2014-07-11 01:20:41 +04:00
|
|
|
DWORD status;
|
|
|
|
DWORD nCount;
|
|
|
|
HANDLE events[32];
|
|
|
|
freerdp_listener* listener;
|
|
|
|
|
2014-07-12 03:30:40 +04:00
|
|
|
listener = server->listener;
|
2014-07-11 01:20:41 +04:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
nCount = 0;
|
|
|
|
|
|
|
|
if (listener->GetEventHandles(listener, events, &nCount) < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to get FreeRDP file descriptor\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
|
|
|
|
|
|
|
|
if (!listener->CheckFileDescriptor(listener))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to check FreeRDP file descriptor\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
listener->Close(listener);
|
|
|
|
|
2014-07-11 00:32:46 +04:00
|
|
|
ExitThread(0);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
int shadow_server_start(rdpShadowServer* server)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
2014-07-12 07:01:34 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
#endif
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
if (server->listener->Open(server->listener, NULL, server->port))
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
server->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)
|
|
|
|
shadow_server_thread, (void*) server, 0, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
int shadow_server_stop(rdpShadowServer* server)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
if (server->thread)
|
|
|
|
{
|
|
|
|
TerminateThread(server->thread, 0);
|
|
|
|
WaitForSingleObject(server->thread, INFINITE);
|
|
|
|
CloseHandle(server->thread);
|
|
|
|
|
|
|
|
server->listener->Close(server->listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
HANDLE shadow_server_get_thread(rdpShadowServer* server)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
|
|
|
return server->thread;
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
rdpShadowServer* shadow_server_new(int argc, char** argv)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
2014-07-11 01:20:41 +04:00
|
|
|
rdpShadowServer* server;
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-12 03:30:40 +04:00
|
|
|
server = (rdpShadowServer*) calloc(1, sizeof(rdpShadowServer));
|
2014-07-11 00:32:46 +04:00
|
|
|
|
2014-07-12 03:30:40 +04:00
|
|
|
if (!server)
|
|
|
|
return NULL;
|
2014-07-11 01:20:41 +04:00
|
|
|
|
2014-07-12 03:30:40 +04:00
|
|
|
server->port = 3389;
|
|
|
|
|
|
|
|
server->listener = freerdp_listener_new();
|
|
|
|
|
|
|
|
if (!server->listener)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
server->listener->info = (void*) server;
|
|
|
|
server->listener->PeerAccepted = shadow_client_accepted;
|
|
|
|
|
2014-07-12 08:01:29 +04:00
|
|
|
server->subsystem = x11_shadow_subsystem_new(server);
|
|
|
|
|
|
|
|
if (!server->subsystem)
|
|
|
|
return NULL;
|
|
|
|
|
2014-07-12 04:49:56 +04:00
|
|
|
server->screen = shadow_screen_new(server);
|
|
|
|
|
|
|
|
if (!server->screen)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
server->encoder = shadow_encoder_new(server);
|
|
|
|
|
|
|
|
if (!server->encoder)
|
|
|
|
return NULL;
|
|
|
|
|
2014-07-11 00:32:46 +04:00
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
2014-07-11 01:20:41 +04:00
|
|
|
void shadow_server_free(rdpShadowServer* server)
|
2014-07-11 00:32:46 +04:00
|
|
|
{
|
2014-07-12 03:30:40 +04:00
|
|
|
if (!server)
|
|
|
|
return;
|
|
|
|
|
|
|
|
freerdp_listener_free(server->listener);
|
|
|
|
|
2014-07-12 04:49:56 +04:00
|
|
|
shadow_encoder_free(server->encoder);
|
|
|
|
|
2014-07-12 07:01:34 +04:00
|
|
|
x11_shadow_subsystem_free(server->subsystem);
|
2014-07-12 03:30:40 +04:00
|
|
|
|
|
|
|
free(server);
|
2014-07-11 00:32:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
HANDLE thread;
|
|
|
|
DWORD dwExitCode;
|
2014-07-11 01:20:41 +04:00
|
|
|
rdpShadowServer* server;
|
|
|
|
|
|
|
|
server = shadow_server_new(argc, argv);
|
|
|
|
|
|
|
|
if (!server)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
shadow_server_start(server);
|
|
|
|
|
|
|
|
thread = shadow_server_get_thread(server);
|
|
|
|
|
|
|
|
WaitForSingleObject(thread, INFINITE);
|
|
|
|
|
|
|
|
GetExitCodeThread(thread, &dwExitCode);
|
|
|
|
|
|
|
|
shadow_server_free(server);
|
2014-07-11 00:32:46 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|