JCEngine.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Collections.Generic;
  3. using BestHTTP.WebSocket;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. namespace JCEngineCore {
  7. public class JCEngine {
  8. private static string url;
  9. public static void boot(string url, JCEntity entity) {
  10. JCEngine.url = url;
  11. new JCEngineCore.WebSocketServer(url, entity);
  12. }
  13. public static void reboot(JCEntity entity) {
  14. new JCEngineCore.WebSocketServer(url, entity);
  15. }
  16. }
  17. public class JCEntity {
  18. public int id;
  19. public JCEngineCore.Channel channel;
  20. public bool isValid;
  21. public bool loaded;
  22. public Dictionary<string, object> components = new Dictionary<string, object>();
  23. public virtual void onLoad() {}
  24. public virtual void onReload() {}
  25. public virtual void onDestroy() {}
  26. public virtual void onMiss() {}
  27. public bool call(string func, params object[] args) {
  28. return call(func, args, null);
  29. }
  30. public bool call(string func, object[] args = null, Delegate callback = null) {
  31. if (this.isValid) {
  32. string uuid = "";
  33. int type = Convert.ToInt32(JCEngineCore.DataType.FUNCTION);
  34. if (func.IndexOf(".") > -1) {
  35. type = Convert.ToInt32(JCEngineCore.DataType.METHOD);
  36. uuid = JCEngineCore.CallbackHandler.addCallback(callback);
  37. }
  38. if (args == null) {
  39. args = new object[]{};
  40. }
  41. JCEngineCore.Data data = new JCEngineCore.Data();
  42. data.uuid = uuid;
  43. data.type = type;
  44. data.func = func;
  45. data.args = args;
  46. this.channel.writeAndFlush(JsonConvert.SerializeObject(data));
  47. return true;
  48. }
  49. return false;
  50. }
  51. }
  52. public class Channel {
  53. private WebSocket webSocket;
  54. public Channel (WebSocket webSocket) {
  55. this.webSocket = webSocket;
  56. }
  57. public void writeAndFlush(string text) {
  58. this.webSocket.Send(text);
  59. }
  60. public void close() {
  61. this.webSocket.Close();
  62. }
  63. }
  64. class WebSocketServer {
  65. private WebSocket webSocket;
  66. private JCEntity tempEntity;
  67. public WebSocketServer(string url, JCEntity entity) {
  68. this.webSocket = new WebSocket(new Uri(url));
  69. this.webSocket.StartPingThread = true;
  70. this.tempEntity = entity;
  71. this.webSocket.OnOpen += delegate(WebSocket webSocket) {
  72. this.call("loadTempEntity");
  73. };
  74. this.webSocket.OnClosed += delegate(WebSocket webSocket, UInt16 code, string message) {
  75. this.destroyTempEntity();
  76. };
  77. this.webSocket.OnError += delegate(WebSocket webSocket, Exception ex) {
  78. this.destroyTempEntity();
  79. };
  80. this.webSocket.OnMessage += delegate(WebSocket webSocket, string message) {
  81. this.invoke(JsonConvert.DeserializeObject<Data>(message));
  82. };
  83. this.webSocket.Open();
  84. }
  85. private void call(string func, object[] args = null) {
  86. if (args == null) {
  87. args = new object[]{};
  88. }
  89. Data data = new Data();
  90. data.uuid = "";
  91. data.type = Convert.ToInt32(DataType.EVENT);
  92. data.func = func;
  93. data.args = args;
  94. this.webSocket.Send(JsonConvert.SerializeObject(data));
  95. }
  96. private void invoke(Data data) {
  97. DataType dataType = (DataType) data.type;
  98. if (dataType == DataType.EVENT) {
  99. System.Reflection.MethodInfo method = this.GetType().GetMethod(data.func);
  100. Utility.FormatArgsType(data.args, method.GetParameters());
  101. method.Invoke(this, data.args);
  102. return;
  103. }
  104. if (dataType == DataType.FUNCTION) {
  105. if (this.tempEntity.isValid) {
  106. string func = data.func;
  107. object context = this.tempEntity;;
  108. int pointIndex = func.LastIndexOf(".");
  109. if (pointIndex > -1) {
  110. context = null;
  111. string key = func.Substring(0, pointIndex);
  112. object matchContext;
  113. this.tempEntity.components.TryGetValue(key, out matchContext);
  114. if (matchContext != null) {
  115. func = func.Substring(pointIndex + 1);
  116. context = matchContext;
  117. }
  118. }
  119. if (context != null) {
  120. System.Reflection.MethodInfo method = context.GetType().GetMethod(func);
  121. Utility.FormatArgsType(data.args, method.GetParameters());
  122. method.Invoke(context, data.args);
  123. }
  124. }
  125. return;
  126. }
  127. if (dataType == DataType.METHOD) {
  128. CallbackHandler.handleCallback(data);
  129. }
  130. }
  131. public void loadTempEntity(int id) {
  132. this.tempEntity.id = id;
  133. this.tempEntity.channel = new Channel(this.webSocket);
  134. this.tempEntity.isValid = true;
  135. try {
  136. if (this.tempEntity.loaded) {
  137. this.tempEntity.onReload();
  138. } else {
  139. this.tempEntity.onLoad();
  140. }
  141. } catch (Exception) {}
  142. this.tempEntity.loaded = true;
  143. }
  144. public void destroyTempEntity() {
  145. if (this.tempEntity.isValid) {
  146. this.tempEntity.isValid = false;
  147. this.tempEntity.onDestroy();
  148. } else {
  149. this.tempEntity.onMiss();
  150. }
  151. }
  152. }
  153. class CallbackHandler {
  154. private static int nextID = 0;
  155. private static Dictionary<string, CallbackInfo> mapper = new Dictionary<string, CallbackInfo>();
  156. private static string uuid() {
  157. nextID++;
  158. return nextID.ToString();
  159. }
  160. public static string addCallback(Delegate callback) {
  161. string uuid = CallbackHandler.uuid();
  162. if (callback != null) {
  163. CallbackInfo callbackInfo = new CallbackInfo();
  164. callbackInfo.callback = callback;
  165. callbackInfo.deadTime = Utility.GetTimestamp() + 10 * 1000;
  166. mapper.Add(uuid, callbackInfo);
  167. }
  168. return uuid;
  169. }
  170. public static void handleCallback(Data data) {
  171. if (mapper.Count > 10) {
  172. long now = Utility.GetTimestamp();
  173. LinkedList<string> outKeys = new LinkedList<string>();
  174. foreach (var item in mapper) {
  175. if (now >= item.Value.deadTime) {
  176. outKeys.AddLast(item.Key);
  177. }
  178. }
  179. foreach (var item in outKeys) {
  180. mapper.Remove(item);
  181. }
  182. }
  183. CallbackInfo callbackInfo;
  184. mapper.TryGetValue(data.uuid, out callbackInfo);
  185. if (callbackInfo != null) {
  186. mapper.Remove(data.uuid);
  187. object target = callbackInfo.callback.Target;
  188. System.Reflection.MethodInfo method = callbackInfo.callback.Method;
  189. Utility.FormatArgsType(data.args, method.GetParameters());
  190. method.Invoke(target, data.args);
  191. }
  192. }
  193. }
  194. class Utility {
  195. public static long GetTimestamp() {
  196. TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
  197. return (long)ts.TotalMilliseconds;
  198. }
  199. public static void FormatArgsType(object[] args, System.Reflection.ParameterInfo[] parameters) {
  200. int i = 0;
  201. foreach (var param in parameters) {
  202. Type type = param.ParameterType;
  203. if (IsBaseType(type)) {
  204. args[i] = Convert.ChangeType(args[i], type);
  205. } else if (args[i].GetType().IsSubclassOf(typeof(JContainer))) {
  206. args[i] = ((JContainer)args[i]).ToObject(type);
  207. }
  208. i++;
  209. }
  210. }
  211. public static bool IsBaseType(Type type) {
  212. if (typeof(System.Int32).Equals(type)) return true;
  213. if (typeof(System.Int64).Equals(type)) return true;
  214. if (typeof(System.Single).Equals(type)) return true;
  215. if (typeof(System.Double).Equals(type)) return true;
  216. if (typeof(System.String).Equals(type)) return true;
  217. if (typeof(System.Boolean).Equals(type)) return true;
  218. return false;
  219. }
  220. }
  221. class CallbackInfo {
  222. public Delegate callback;
  223. public long deadTime;
  224. }
  225. class Data {
  226. public string uuid;
  227. public int type;
  228. public string func;
  229. public object[] args;
  230. }
  231. enum DataType {
  232. EVENT,
  233. FUNCTION,
  234. METHOD
  235. }
  236. }