-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWickerNetwork.cs
361 lines (326 loc) · 15.6 KB
/
WickerNetwork.cs
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
using Newtonsoft.Json;
using System.Net;
using System.Reflection;
using System.Web;
using UnityEngine;
namespace WickerREST
{
public class WickerNetwork
{
private HttpListener? listener;
private Thread? listenerThread;
private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
private const string COMMANDS_PATH = "/commands";
private const string HEARTBEAT_PATH = "/heartbeat";
private const string FAVICON_PATH = "/favicon.ico";
private const string INDEX_URL = @"https://raw.githubusercontent.com/derekShaheen/WickerREST/main/web/index.html";
private const string FAVICON_URL = @"https://raw.githubusercontent.com/derekShaheen/WickerREST/main/web/resources/favicon.ico";
private const string DWYL_URL = @"https://hits.dwyl.com/derekShaheen/WickerREST.svg";
private List<string> _logEntries = new List<string>();
private object _logLock = new object(); // For thread-safety
// Instance of this class
private static WickerNetwork? instance;
// Singleton pattern
public static WickerNetwork Instance
{
get
{
if (instance == null)
{
instance = new WickerNetwork();
}
return instance;
}
private set
{
instance = value;
}
}
internal void StartServer(int port, CancellationTokenSource cancellationToken)
{
cancellationTokenSource = cancellationToken;
listener = new HttpListener();
listener.Prefixes.Add($"http://localhost:{port}/");
listener.Start();
listenerThread = new Thread(() => HandleRequests(cancellationTokenSource.Token));
WickerServer.Instance.LogMessage("Starting listener thread...", 1);
listenerThread?.Start(); // Start the listener thread
}
private async void HandleRequests(CancellationToken cancellationToken)
{
if (listener == null)
return;
try
{
while (listener.IsListening && !cancellationToken.IsCancellationRequested)
{
var context = listener.GetContext();
var request = context.Request;
var response = context.Response;
if (request == null || response == null || request.Url == null || Commands.Instance.CommandHandlers == null)
return;
try
{
if (request.HttpMethod == "GET" && Commands.Instance.CommandHandlers.TryGetValue(request.Url.AbsolutePath, out var handlerTuple))
{
MethodInfo methodToInvoke = handlerTuple.Method;
var parameterInfos = methodToInvoke.GetParameters();
// Assuming all parameters are strings for simplicity; adjust as needed.
object[]? parameters = new object[parameterInfos.Length];
// Parse query parameters
var query = HttpUtility.ParseQueryString(request.Url.Query);
//parameters[0] = response; // Pass the response object
if (parameterInfos.Length > 0)
{
for (int i = 0; i < parameterInfos.Length; i++)
{
var paramInfo = parameterInfos[i];
var paramValue = query[paramInfo.Name];
// Convert paramValue to the correct type as needed
if(paramValue != null)
parameters[i] = Convert.ChangeType(paramValue, paramInfo.ParameterType);
}
}
// Queue the action to be executed on the main thread
WickerServer.Instance.ExecuteOnMainThread(() =>
{
methodToInvoke.Invoke(null, parameters);
}, response);
}
else if (request.Url.AbsolutePath == "/")
{
await ServeFrontEnd(response, WickerServer.Instance.FrontEnd != null ? WickerServer.Instance.FrontEnd.Value : "index.html");
}
else if (request.Url.AbsolutePath == FAVICON_PATH)
{
await ServeFavicon(response, Path.Combine(WickerServer.resourcesPath, "favicon.ico"));
}
else if (request.Url.AbsolutePath == COMMANDS_PATH)
{
ServeCommandHandlers(response);
}
else if (request.Url.AbsolutePath == HEARTBEAT_PATH)
{
ServeHeartbeat(response);
}
else
{
SendResponse(response, "Invalid request.", statusCode: 404);
}
//response.OutputStream.Close();
}
catch (Exception ex)
{
if (ex is OperationCanceledException) break;
WickerServer.Instance.LogMessage($"Server error: {ex.Message}", 1);
SendResponse(response, $"Server error. {ex.Message}", statusCode: 500);
}
}
}
catch (Exception ex)
{
if (ex is HttpListenerException || ex is ObjectDisposedException)
{
//User is trying to close the server improperly. Suppress here, handle on application quit.
}
}
}
private void ServeHeartbeat(HttpListenerResponse response)
{
var logResults = _logEntries;
var gameVariables = Commands.Instance.GameVariableMethods?.Select(kvp => new
{
VariableName = kvp.Key,
Value = kvp.Value?.Invoke()?.ToString()
}).ToDictionary(kvp => kvp.VariableName, kvp => kvp.Value) ?? new Dictionary<string, string>();
// Combine game variables and log results in one object
var heartbeatResponse = new
{
GameVariables = gameVariables,
LogResults = logResults
};
var json = JsonConvert.SerializeObject(heartbeatResponse);
SendResponse(response, json, statusCode: 200, contentType: "application/json");
logResults.Clear();
}
private void ServeCommandHandlers(HttpListenerResponse response)
{
if (Commands.Instance.CommandHandlers != null && Commands.Instance.CommandHandlers.Count > 0)
{
var commandsInfo = Commands.Instance.CommandHandlers.Select(handler => new {
Path = handler.Key,
Parameters = handler.Value.Method.GetParameters()
.Select(p => {
Dictionary<string, string[]> autoCompleteOptions = new Dictionary<string, string[]>();
if (!string.IsNullOrEmpty(handler.Value.AutoCompleteMethodName))
{
MethodInfo? autoCompleteMethod;
try
{
autoCompleteMethod = handler.Value.Method.DeclaringType.GetMethod(handler.Value.AutoCompleteMethodName, BindingFlags.Static | BindingFlags.Public);
if (autoCompleteMethod != null && autoCompleteMethod.ReturnType == typeof(Dictionary<string, string[]>))
{
autoCompleteOptions = (Dictionary<string, string[]>)autoCompleteMethod.Invoke(null, null);
}
}
catch (Exception ex)
{
WickerServer.Instance.LogMessage($"Error invoking AutoComplete method: {ex.Message}");
}
}
return new
{
p.Name,
Type = p.ParameterType.Name,
DefaultValue = p.HasDefaultValue ? p.DefaultValue?.ToString() : null,
AutoCompleteOptions = autoCompleteOptions.ContainsKey(p.Name) ? autoCompleteOptions[p.Name] : Array.Empty<string>()
};
})
.ToArray(),
Category = handler.Value.Category,
Description = handler.Value.Description
}).ToArray();
var productName = Application.productName;
var responseContent = new
{
productName = productName,
commands = commandsInfo,
};
var commandsJson = JsonConvert.SerializeObject(responseContent);
SendResponse(response, commandsJson, statusCode: 200, contentType: "application/json");
}
else
{
SendResponse(response, @"", statusCode: 200);
}
}
/// <summary>
/// Serve an HTML page from the user data directory.
/// </summary>
/// <param name="response"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public void ServeHTMLPage(HttpListenerResponse response, string fileName)
{
var filePath = Path.Combine(WickerServer.userDataPath, fileName);
//await Utilities.EnsureFileExists(filePath, INDEX_URL);
if (File.Exists(filePath))
{
var pageContent = File.ReadAllBytes(filePath);
SendResponse(response, System.Text.Encoding.UTF8.GetString(pageContent), statusCode: 200, contentType: "text/html");
}
else
{
SendResponse(response, "Page not found.", statusCode: 404);
}
}
private async System.Threading.Tasks.Task ServeFrontEnd(HttpListenerResponse response, string fileName)
{
var filePath = Path.Combine(WickerServer.resourcesPath, fileName);
await Utilities.EnsureFileExists(filePath, INDEX_URL);
if (File.Exists(filePath))
{
var pageContent = File.ReadAllBytes(filePath);
SendResponse(response, System.Text.Encoding.UTF8.GetString(pageContent), statusCode: 200, contentType: "text/html");
}
else
{
SendResponse(response, "Page not found.", statusCode: 404);
}
}
private async System.Threading.Tasks.Task ServeFavicon(HttpListenerResponse response, string filePath)
{
await Utilities.EnsureFileExists(filePath, FAVICON_URL, true, true);
var fileInfo = new FileInfo(filePath);
if (fileInfo.Exists)
{
try
{
response.ContentType = "image/x-icon";
response.ContentLength64 = fileInfo.Length;
using (var fileStream = fileInfo.OpenRead())
{
await fileStream.CopyToAsync(response.OutputStream);
}
response.StatusCode = 200; // OK
}
catch (Exception)
{
//
}
}
else
{
response.StatusCode = 404; // Not Found
// Make sure not to close the response here if it's managed elsewhere
}
}
internal void StopServer()
{
if (listener != null)
{
WickerServer.Instance.LogMessage("Shutting down REST client...");
listener.Stop(); // Stop the listener
listener.Close(); // Clean up listener resources
cancellationTokenSource.Cancel(); // Signal cancellation
}
if (listenerThread != null && listenerThread.IsAlive)
{
listenerThread.Join(); // Wait for the thread to complete execution
}
}
public void SendResponse(HttpListenerResponse response, string message, bool closeResponse = true, int statusCode = 200, string contentType = "text/plain")
{
try
{
// Attempt to set response properties and write the response
response.StatusCode = statusCode;
response.ContentType = contentType;
var buffer = System.Text.Encoding.UTF8.GetBytes(message);
response.ContentLength64 = buffer.Length;
response.OutputStream.Write(buffer, 0, buffer.Length);
}
catch (InvalidOperationException ex)
{
// This exception is thrown if we try to modify the response after it's been sent
// Log the exception or handle it as needed
WickerServer.Instance.LogMessage($"Attempted to write to an already closed response: {ex.Message}");
}
finally
{
// Always ensure the output stream is closed in a finally block to avoid resource leaks
try
{
if (closeResponse)
{
response.OutputStream.Close();
}
}
catch (Exception ex)
{
// Handle or log exceptions thrown from trying to close the output stream
WickerServer.Instance.LogMessage($"Error closing response output stream: {ex.Message}");
}
}
}
/// <summary>
/// Log a message to the response log. This will be sent on /heartbeat requests.
/// </summary>
/// <param name="message"></param>
public void LogResponse(string message)
{
lock (_logLock)
{
string formattedMessage = message.Replace(Environment.NewLine, "<br>");
formattedMessage = formattedMessage.Replace("\\n", "<br>");
_logEntries.Add(formattedMessage);
}
}
//public void LogResponse(HttpListenerResponse response, string message)
//{
// // Replace newline characters with HTML line breaks to preserve formatting in the web page
// string formattedMessage = message.Replace(Environment.NewLine, "<br>");
// SendResponse(response, formattedMessage, statusCode: 200, contentType: "text/html");
//}
}
}