FreeRDP/server/shadow/shadow.c

173 lines
3.3 KiB
C
Raw Normal View History

/**
* 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
#ifndef _WIN32
#include <sys/select.h>
#include <sys/signal.h>
#endif
#include "shadow.h"
void* shadow_server_thread(rdpShadowServer* server)
{
2014-07-11 01:20:41 +04:00
DWORD status;
DWORD nCount;
HANDLE events[32];
freerdp_listener* listener;
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);
ExitThread(0);
return NULL;
}
2014-07-11 01:20:41 +04:00
int shadow_server_start(rdpShadowServer* server)
{
#ifndef _WIN32
signal(SIGPIPE, SIG_IGN);
#endif
2014-07-11 01:20:41 +04:00
if (server->listener->Open(server->listener, NULL, server->port))
{
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)
{
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)
{
return server->thread;
}
2014-07-11 01:20:41 +04:00
rdpShadowServer* shadow_server_new(int argc, char** argv)
{
2014-07-11 01:20:41 +04:00
rdpShadowServer* server;
server = (rdpShadowServer*) calloc(1, sizeof(rdpShadowServer));
if (!server)
return NULL;
2014-07-11 01:20:41 +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;
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;
return server;
}
2014-07-11 01:20:41 +04:00
void shadow_server_free(rdpShadowServer* server)
{
if (!server)
return;
freerdp_listener_free(server->listener);
2014-07-12 04:49:56 +04:00
shadow_encoder_free(server->encoder);
x11_shadow_subsystem_free(server->subsystem);
free(server);
}
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);
return 0;
}