loupiottes/DMX-2.0/WebServer.cs

89 lines
2.3 KiB
C#

/*
Copyright (C) Arnaud Houdelette 2012-2014
Copyright (C) Emmanuel Langlois 2012-2014
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Web;
using System.Net;
using System.Threading;
using System.Collections.Generic;
namespace DMX2
{
public class WebServer : IDisposable
{
HttpListener listner;
Thread loopthread=null;
string page;
List<string> resources;
public WebServer ()
{
/*using (System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("page.html"))
using (System.IO.TextReader reader = new System.IO.StreamReader(stream))
page = reader.ReadToEnd();*/
resources = new List<string>(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames());
loopthread = new Thread(new ThreadStart(MainLoop));
loopthread.Start();
}
void MainLoop ()
{
listner = new HttpListener ();
listner.Prefixes.Add ("http://*:8089/");
listner.Start ();
HttpListenerContext context;
try {
while (null!=(context = listner.GetContext())) {
Console.WriteLine (context.Request.RawUrl);
string res = context.Request.Url.LocalPath.Remove(0,1).Replace('/','.');
if(resources.Contains(res))
using (System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(res))
{
byte[] buff = new byte[1024]; int l;
while( (l=stream.Read(buff,0,buff.Length))>0){
context.Response.OutputStream.Write(buff,0,l);
}
}
context.Response.OutputStream.Close ();
}
} catch {
}
}
#region IDisposable implementation
public void Dispose ()
{
listner.Close();
loopthread.Abort();
loopthread.Join();
}
#endregion
}
}