forked from Dids/seedery.io-mapgen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebServer.cs
More file actions
146 lines (128 loc) · 6.5 KB
/
WebServer.cs
File metadata and controls
146 lines (128 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading;
using System.Linq;
using System.Text;
using System.Web;
namespace SeederyIo
{
public class WebServer
{
private readonly HttpListener _listener = new HttpListener();
public WebServer()
{
if (!HttpListener.IsSupported) throw new NotSupportedException("Jeez, get a newer version of Windows!");
_listener.Prefixes.Add("http://localhost:5000/upload/");
_listener.Start();
}
public void Start()
{
ThreadPool.QueueUserWorkItem((o) =>
{
Console.WriteLine("Ready to receive maps..");
try
{
while (_listener.IsListening)
{
ThreadPool.QueueUserWorkItem((c) =>
{
var ctx = c as HttpListenerContext;
if (ctx == null) return;
try
{
Console.WriteLine("Receiving..");
// Parse the raw data first
string rawData;
using (var reader = new StreamReader(ctx.Request.InputStream, ctx.Request.ContentEncoding))
{
rawData = reader.ReadToEnd();
}
// Next translate the raw data to a more usable dictionary
Dictionary<string, string> postParams = new Dictionary<string, string>();
string[] rawParams = rawData.Split('&');
foreach (string param in rawParams)
{
string[] kvPair = param.Split('=');
string key = kvPair[0];
string value = Uri.UnescapeDataString(kvPair[1]);
postParams.Add(key, value);
}
// Get the individual fields
var protocol = postParams["protocol"];
var size = postParams["size"];
var seed = postParams["seed"];
var monuments = postParams["monuments"];
var filename = postParams["filename"];
var data = postParams["data"];
// Create the images directory if it doesn't exist yet
var imagesDirectory = Directory.GetCurrentDirectory() + "\\images";
if (!Directory.Exists(imagesDirectory)) Directory.CreateDirectory(imagesDirectory);
// Save the image
var imageFilePath = $"{imagesDirectory}\\{protocol}_{size}_{seed}.png";
using (FileStream imageStream = new FileStream(imageFilePath, FileMode.Create, FileAccess.Write))
{
// The image data is 64 encoded, so decode it first
byte[] bytes = Convert.FromBase64String(data);
// Next just write the bytes and close the stream
imageStream.Write(bytes, 0, bytes.Length);
imageStream.Close();
Console.WriteLine($"Map image saved to {imageFilePath}");
}
// Create the monuments directory if it doesn't exist yet
var monumentsDirectory = Directory.GetCurrentDirectory() + "\\monuments";
if (!Directory.Exists(monumentsDirectory)) Directory.CreateDirectory(monumentsDirectory);
// Save the monument data
var monumentsFilePath = $"{monumentsDirectory}\\{protocol}_{size}_{seed}.json";
using (FileStream monumentsStream = new FileStream(monumentsFilePath, FileMode.Create, FileAccess.Write))
{
// The monument data is a JSON string, so convert it to a byte array
byte[] bytes = GetBytes(monuments);
// Next just write the bytes and close the stream
monumentsStream.Write(bytes, 0, bytes.Length);
monumentsStream.Close();
Console.WriteLine($"Map monuments saved to {monumentsFilePath}");
}
// Send a response
ctx.Response.StatusCode = 200;
ctx.Response.ContentType = "text/html";
using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream, Encoding.UTF8)) writer.WriteLine("OK");
}
catch (Exception e)
{
Console.WriteLine("Exception occurred: " + e);
}
finally
{
// always close the stream
ctx.Response.OutputStream.Close();
}
}, _listener.GetContext());
}
}
catch (Exception e)
{
Console.WriteLine("Exception occurred: " + e);
}
});
}
public void Stop()
{
_listener.Stop();
_listener.Close();
}
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
}
}