Add exception handling in Router (when handling c2s messages)

This commit is contained in:
ChronosX88 2020-10-10 23:47:10 +04:00
parent f6d92ecba0
commit 400c5410e9
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A

View File

@ -40,6 +40,7 @@ namespace Zirconium.Core
session.ConnectionHandler.SendMessage(serializedMsg); session.ConnectionHandler.SendMessage(serializedMsg);
return; return;
} }
var handlerTasks = new List<Task>();
foreach (var h in handlers) foreach (var h in handlers)
{ {
if (h.IsAuthorizationRequired()) if (h.IsAuthorizationRequired())
@ -59,15 +60,15 @@ namespace Zirconium.Core
catch (Exception e) catch (Exception e)
{ {
Log.Warning(e.Message); Log.Warning(e.Message);
var errorMsg = OtherUtils.GenerateProtocolError(
var serializedMsg = JsonConvert.SerializeObject(
OtherUtils.GenerateProtocolError(
message, message,
"unauthorized", "unauthorized",
"Unauthorized access", "Unauthorized access",
new Dictionary<string, object>() new Dictionary<string, object>()
)
); );
errorMsg.From = _app.Config.ServerID;
var serializedMsg = JsonConvert.SerializeObject(errorMsg);
session.ConnectionHandler.SendMessage(serializedMsg); session.ConnectionHandler.SendMessage(serializedMsg);
return; return;
} }
@ -77,11 +78,17 @@ namespace Zirconium.Core
} }
} }
Task.Run(() => var task = Task.Run(() =>
{ {
// probably need to wrap whole foreach body, not only HandleMessage call - need to investigate // probably need to wrap whole foreach body, not only HandleMessage call - need to investigate
h.HandleMessage(session, message); h.HandleMessage(session, message);
}); });
handlerTasks.Add(task);
}
try {
Task.WaitAll(handlerTasks.ToArray());
} catch (Exception e) {
Log.Error(e.ToString());
} }
} }