IPCMessage.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using InABox.Clients;
  2. using InABox.Core;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net.NetworkInformation;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using MemoryPack;
  9. namespace InABox.IPC
  10. {
  11. public enum RequestError
  12. {
  13. NONE,
  14. DISCONNECTED,
  15. UNKNOWN,
  16. TIMEOUT
  17. }
  18. public enum Method
  19. {
  20. None,
  21. Query,
  22. Save,
  23. MultiSave,
  24. Delete,
  25. MultiDelete,
  26. QueryMultiple,
  27. Validate,
  28. Check2FA,
  29. Ping,
  30. Info,
  31. Push,
  32. Version,
  33. ReleaseNotes,
  34. Installer
  35. }
  36. [Serializable]
  37. [MemoryPackable]
  38. public partial class IPCMessage
  39. {
  40. public Guid RequestID;
  41. public Method Method;
  42. public string? Type;
  43. private string? Data;
  44. private byte[]? BinaryData;
  45. [NonSerialized]
  46. public RequestError ErrorCode;
  47. [MemoryPackConstructor]
  48. public IPCMessage()
  49. {
  50. }
  51. private IPCMessage(Guid requestID, Method method, string? type, string data, RequestError error = RequestError.NONE) : this()
  52. {
  53. RequestID = requestID;
  54. Method = method;
  55. Type = type;
  56. Data = data;
  57. BinaryData = null;
  58. ErrorCode = error;
  59. }
  60. private IPCMessage(Guid requestID, Method method, string? type, byte[] data, RequestError error = RequestError.NONE) : this()
  61. {
  62. RequestID = requestID;
  63. Method = method;
  64. Type = type;
  65. BinaryData = data;
  66. Data = null;
  67. ErrorCode = error;
  68. }
  69. public IPCMessage Respond<TResponse>(TResponse response) where TResponse : Response
  70. {
  71. return CreateRequest(RequestID, Method.None, Type, response);
  72. }
  73. private static IPCMessage CreateRequest(Guid requestID, Method method, string? type, object? data)
  74. {
  75. if (data is ISerializeBinary binary)
  76. {
  77. return new IPCMessage(requestID, method, type, binary.WriteBinary(BinarySerializationSettings.Latest));
  78. }
  79. {
  80. return new IPCMessage(requestID, method, type, Serialization.Serialize(data));
  81. }
  82. }
  83. public TRequest GetRequest<TRequest>()
  84. {
  85. if (BinaryData is not null)
  86. {
  87. return (TRequest)Serialization.ReadBinary(typeof(TRequest), BinaryData, BinarySerializationSettings.Latest);
  88. }
  89. else
  90. {
  91. return Serialization.Deserialize<TRequest>(Data);
  92. }
  93. }
  94. public object? GetRequest(Type TRequest)
  95. {
  96. if (BinaryData is not null)
  97. {
  98. return Serialization.ReadBinary(TRequest, BinaryData, BinarySerializationSettings.Latest);
  99. }
  100. else if(Data is not null)
  101. {
  102. return Serialization.Deserialize(TRequest, Data);
  103. }
  104. else
  105. {
  106. return null;
  107. }
  108. }
  109. public TResponse GetResponse<TResponse>() where TResponse : Response, new()
  110. {
  111. var start = DateTime.Now;
  112. var response = GetRequest<TResponse>();
  113. response ??= new TResponse();
  114. switch (ErrorCode)
  115. {
  116. case RequestError.NONE:
  117. break;
  118. case RequestError.DISCONNECTED:
  119. response.Status = StatusCode.Error;
  120. response.Messages.Add("Server disconnected");
  121. break;
  122. case RequestError.UNKNOWN:
  123. response.Status = StatusCode.Error;
  124. response.Messages.Add("Unknown Error");
  125. break;
  126. case RequestError.TIMEOUT:
  127. response.Status = StatusCode.Error;
  128. response.Messages.Add("Timeout");
  129. break;
  130. }
  131. return response;
  132. }
  133. public static IPCMessage Query<T>(QueryRequest<T> request) where T : Entity, new()
  134. {
  135. return CreateRequest(Guid.NewGuid(), Method.Query, typeof(T).Name, request);
  136. }
  137. public static IPCMessage Save<T>(SaveRequest<T> request) where T : Entity, new()
  138. {
  139. return CreateRequest(Guid.NewGuid(), Method.Save, typeof(T).Name, request);
  140. }
  141. public static IPCMessage MultiSave<T>(MultiSaveRequest<T> request) where T : Entity, new()
  142. {
  143. return CreateRequest(Guid.NewGuid(), Method.MultiSave, typeof(T).Name, request);
  144. }
  145. public static IPCMessage Delete<T>(DeleteRequest<T> request) where T : Entity, new()
  146. {
  147. return CreateRequest(Guid.NewGuid(), Method.Delete, typeof(T).Name, request);
  148. }
  149. public static IPCMessage MultiDelete<T>(MultiDeleteRequest<T> request) where T : Entity, new()
  150. {
  151. return CreateRequest(Guid.NewGuid(), Method.MultiDelete, typeof(T).Name, request);
  152. }
  153. public static IPCMessage QueryMultiple(MultiQueryRequest request)
  154. {
  155. return CreateRequest(Guid.NewGuid(), Method.QueryMultiple, null, request);
  156. }
  157. public static IPCMessage Validate(ValidateRequest request)
  158. {
  159. return CreateRequest(Guid.NewGuid(), Method.Validate, null, request);
  160. }
  161. public static IPCMessage Check2FA(Check2FARequest request)
  162. {
  163. return CreateRequest(Guid.NewGuid(), Method.Check2FA, null, request);
  164. }
  165. public static IPCMessage Error(RequestError error)
  166. {
  167. return new IPCMessage(Guid.NewGuid(), Method.None, null, "", error);
  168. }
  169. public static IPCMessage Ping(PingRequest request)
  170. {
  171. return CreateRequest(Guid.NewGuid(), Method.Ping, null, request);
  172. }
  173. public static IPCMessage Info(InfoRequest request)
  174. {
  175. return CreateRequest(Guid.NewGuid(), Method.Info, null, request);
  176. }
  177. public static IPCMessage Version(VersionRequest request)
  178. {
  179. return CreateRequest(Guid.NewGuid(), Method.Version, null, request);
  180. }
  181. public static IPCMessage ReleaseNotes(ReleaseNotesRequest request)
  182. {
  183. return CreateRequest(Guid.NewGuid(), Method.ReleaseNotes, null, request);
  184. }
  185. public static IPCMessage Installer(InstallerRequest request)
  186. {
  187. return CreateRequest(Guid.NewGuid(), Method.Installer, null, request);
  188. }
  189. public static IPCMessage Push<TPush>(TPush push) where TPush : BaseObject
  190. => Push(typeof(TPush), push);
  191. public static IPCMessage Push(Type TPush, BaseObject push)
  192. {
  193. return CreateRequest(Guid.NewGuid(), Method.Push, TPush.EntityName(), push);
  194. }
  195. }
  196. }