blob: b4d372493ddebd69a0d37a0f85556376095dc8e7 [file] [log] [blame] [raw]

using RemoteView.PageHandlers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
namespace RemoteView
{
class Server
{
private Dictionary<String, AbstractPageHandler> decoder = new Dictionary<string, AbstractPageHandler>();
private HttpListener listener = new HttpListener();
/// <summary>
/// Constructor
/// </summary>
public Server()
{
// Homepage
decoder.Add("", new HomePageHandler());
decoder.Add("home", new HomePageHandler());
// javascript page handles client side clicks and screen image updates
decoder.Add("script", new JavascriptPageHandler());
// information about the system
decoder.Add("info", new InfoPageHandler());
// this pages process clicks into server side windows events
decoder.Add("leftclick", new LeftClickPageHandler());
decoder.Add("rightclick", new RightClickPageHandler());
decoder.Add("mousemove", new MouseMovePageHandler());
// image of choosen device as a png
decoder.Add("screen", new ScreenPageHandler());
// image of choosen device as a png
decoder.Add("favicon.ico", new IconPageHandler());
// 404 error page
decoder.Add("404", new NotFoundPageHandler());
}
/// <summary>
/// Start server
/// </summary>
/// <param name="port">port to listen to</param>
public void start(int port)
{
try
{
listener.Prefixes.Add("http://*:" + port + "/");
listener.Start();
}
catch
{
Console.WriteLine("Could not listen on port: {0}.", port);
return;
}
do
{
HttpListenerResponse response = null;
Stream output = null;
try
{
// Note: The GetContext method blocks while waiting for a request.
HttpListenerContext context = listener.GetContext();
String[] uri = context.Request.RawUrl.Split('/');
#if DEBUG
Console.WriteLine(context.Request.RawUrl);
#endif
AbstractPageHandler page;
bool found = decoder.TryGetValue(uri[1], out page);
if (!found)
{
// if page not found display 404 page
page = decoder["404"];
}
response = context.Response;
byte[] buffer = page.handleRequest(response, uri);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
}
catch (Exception e)
{
if (listener.IsListening) Console.WriteLine(e.Message);
}
finally
{
if (response != null)
{
response.Close();
try
{
if (output != null) output.Close();
}
catch { }
}
}
}
while (listener.IsListening);
listener.Stop();
}
/// <summary>
/// Stop server
/// </summary>
public void stop()
{
this.listener.Stop();
}
/// <summary>
/// is server running?
/// </summary>
/// <returns></returns>
public bool isRunning()
{
return listener.IsListening;
}
}
}