From 91f97a65b2c6d1fad3a211a6739a49d9908f69a2 Mon Sep 17 00:00:00 2001 From: Michael VanOverbeek Date: Wed, 15 Feb 2017 21:30:58 +0000 Subject: whoa mud refactoring --- ShiftOS.Server/Program.cs | 99 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 80 insertions(+), 19 deletions(-) (limited to 'ShiftOS.Server/Program.cs') diff --git a/ShiftOS.Server/Program.cs b/ShiftOS.Server/Program.cs index 69571d0..376f95f 100644 --- a/ShiftOS.Server/Program.cs +++ b/ShiftOS.Server/Program.cs @@ -36,6 +36,7 @@ using System.Net.Sockets; using System.Security.Cryptography; using System.IO.Compression; using System.Reflection; +using System.Threading; namespace ShiftOS.Server { @@ -230,36 +231,96 @@ namespace ShiftOS.Server { foreach (var attrib in method.GetCustomAttributes(false)) { - if (attrib is MudRequestAttribute) + new Thread(() => { - if ((attrib as MudRequestAttribute).RequestName == msg.Name) + if (attrib is MudRequestAttribute) { - try + var mAttrib = attrib as MudRequestAttribute; + if (mAttrib.RequestName == msg.Name) { - object contents = msg.Contents; try { - contents = JsonConvert.DeserializeObject>(msg.Contents); + object contents = null; + bool throwOnNull = false; + + + if(mAttrib.ExpectedType == typeof(int)) + { + int result = 0; + if (int.TryParse(msg.Contents, out result) == true) + { + contents = result; + } + else + { + throw new MudException($"Protocol error: {msg.Name} expects a 32-bit signed integer for the message contents."); + } + } + else if(mAttrib.ExpectedType == typeof(long)) + { + long result = 0; + if (long.TryParse(msg.Contents, out result) == true) + { + contents = result; + } + else + { + throw new MudException($"Protocol error: {msg.Name} expects a 64-bit signed integer for the message contents."); + } + } + else if(mAttrib.ExpectedType == typeof(bool)) + { + throwOnNull = true; + if(msg.Contents.ToLower() == "true") + { + contents = true; + } + else if (msg.Contents.ToLower() == "false") + { + contents = false; + } + else + { + contents = null; + throw new MudException("Protocol error: " + msg.Name + " expects a content type of 'boolean'. Please send either 'true' or 'false'."); + } + } + else if(mAttrib.ExpectedType == null) + { + throwOnNull = false; + } + else + { + //object type + object result = null; + try + { + result = Convert.ChangeType(JsonConvert.DeserializeObject(msg.Contents), mAttrib.ExpectedType); + } + catch + { + result = null; + } + if (result == null) + throw new MudException($"Protocol error: {msg.Name} expects an object of type {mAttrib.ExpectedType.FullName}. Please send a JSON string representing an object of this type."); + contents = result; + } + + method?.Invoke(null, new[] { msg.GUID, contents }); + } + catch (Exception mEx) + { + Console.WriteLine(mEx); + ClientDispatcher.DispatchTo("Error", msg.GUID, mEx); } catch { - + Console.WriteLine($@"[{DateTime.Now}] {method.Name}: Missing guid and content parameters, request handler NOT RAN."); } - - method?.Invoke(null, new[] { msg.GUID, contents }); - } - catch (Exception mEx) - { - Console.WriteLine(mEx); - ClientDispatcher.DispatchTo("Error", msg.GUID, mEx); - } - catch - { - Console.WriteLine($@"[{DateTime.Now}] {method.Name}: Missing guid and content parameters, request handler NOT RAN."); + return; } - return; } - } + }).Start(); } } } -- cgit v1.2.3