RPCCommandHandler.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using GenHTTP.Api.Content.Authentication;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using Microsoft.Exchange.WebServices.Data;
  5. using NPOI.SS.Formula.Functions;
  6. namespace InABox.Rpc
  7. {
  8. public abstract class RpcCommandHandler<TSender, TParameters, TResult> : IRpcCommandHandler
  9. where TSender : class
  10. where TParameters : IRpcCommandParameters, new()
  11. where TResult : IRpcCommandResult
  12. {
  13. public TSender Sender { get; }
  14. public RpcCommandHandler(TSender sender)
  15. {
  16. Sender = sender ?? throw new ArgumentNullException(nameof(sender));
  17. }
  18. protected abstract TResult Execute(IRpcSession session, TParameters parameters);
  19. public byte[] Execute(IRpcSession session, byte[] payload)
  20. {
  21. var start = DateTime.Now;
  22. var parameters = Serialization.ReadBinary<TParameters>(payload, BinarySerializationSettings.Latest);
  23. //Logger.Send(LogType.Information, session.UserID, $"[{session.Platform} {session.Version}] {parameters.FullDescription()}");
  24. try
  25. {
  26. var result = Execute(session, parameters);
  27. var description = result.FullDescription();
  28. // Logger.Send(LogType.Information, session.UserID, string.Format("[{0} {1}] [{2:D8}] {3} Complete{4}",
  29. // session.Platform, session.Version,
  30. // (int)DateTime.Now.Subtract(start).TotalMilliseconds, parameters.ShortDescription(),
  31. // string.IsNullOrWhiteSpace(description) ? "" : $" {description}"));
  32. return Serialization.WriteBinary(result, BinarySerializationSettings.Latest);
  33. }
  34. catch (RpcException)
  35. {
  36. throw;
  37. }
  38. catch (Exception e)
  39. {
  40. Logger.Send(LogType.Information, session.UserID, string.Format("[{0} {1}] [{2:D8}] {3} failed: {4}\n\n{5}",
  41. session.Platform,
  42. session.Version,
  43. (int)DateTime.Now.Subtract(start).TotalMilliseconds,
  44. parameters.ShortDescription(),
  45. e.Message,
  46. e.StackTrace));
  47. throw new RpcException(e.Message, RpcError.SERVERERROR);
  48. }
  49. }
  50. }
  51. }