Tanoda
HttpServer.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4using System.Net;
5using System.Threading.Tasks;
6using System;
7using System.Threading;
8
9public class HttpServer : MonoBehaviour
10{
11 private readonly HttpListener _listener = new HttpListener();
12 public Dictionary<string, byte[]> loadedFiles = new Dictionary<string, byte[]>();
13 public HttpServer()
14 {
15 bool flag = !HttpListener.IsSupported;
16 if (flag)
17 {
18 throw new NotSupportedException("Needs Windows XP SP2, Server 2003 or later.");
19 }
20
21 this._listener.Prefixes.Add("http://*:8069/");
22
23 this._listener.Start();
24 }
25 public void Stop()
26 {
27 this._listener.Stop();
28 this._listener.Close();
29 }
30
31 public void Run()
32 {
33 ThreadPool.QueueUserWorkItem(delegate (object o)
34 {
35 Console.WriteLine("web server started...");
36 try
37 {
38 while (this._listener.IsListening)
39 {
40 ThreadPool.QueueUserWorkItem(delegate (object c)
41 {
42 HttpListenerContext httpListenerContext = c as HttpListenerContext;
43 try
44 {
45 Console.WriteLine("AbsolutePath request: " + httpListenerContext.Request.Url.AbsolutePath);
46 var file = httpListenerContext.Request.Url.AbsolutePath.Remove(0, 1);
47
48 if (loadedFiles.ContainsKey(file))
49 {
50 byte[] bytes = loadedFiles[httpListenerContext.Request.Url.AbsolutePath.Remove(0, 1)];
51 httpListenerContext.Response.ContentLength64 = (long)bytes.Length;
52 httpListenerContext.Response.OutputStream.Write(bytes, 0, bytes.Length);
53 }
54 }
55 catch
56 {
57 }
58 finally
59 {
60 httpListenerContext.Response.OutputStream.Close();
61 }
62 }, this._listener.GetContext());
63 }
64 }
65 catch
66 {
67 }
68 });
69 }
70}
void Stop()
Definition: HttpServer.cs:25
Dictionary< string, byte[]> loadedFiles
Definition: HttpServer.cs:12
void Run()
Definition: HttpServer.cs:31