HttpComponentSystem.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. namespace ET
  5. {
  6. public class HttpComponentAwakeSystem : AwakeSystem<HttpComponent, string>
  7. {
  8. public override void Awake(HttpComponent self, string address)
  9. {
  10. try
  11. {
  12. self.Load();
  13. self.Listener = new HttpListener();
  14. foreach (string s in address.Split(';'))
  15. {
  16. if (s.Trim() == "")
  17. {
  18. continue;
  19. }
  20. self.Listener.Prefixes.Add(s);
  21. }
  22. self.Listener.Start();
  23. self.Accept().Coroutine();
  24. }
  25. catch (HttpListenerException e)
  26. {
  27. throw new Exception($"请现在cmd中运行: netsh http add urlacl url=http://*:你的address中的端口/ user=Everyone, address: {address}", e);
  28. }
  29. }
  30. }
  31. [ObjectSystem]
  32. public class HttpComponentLoadSystem: LoadSystem<HttpComponent>
  33. {
  34. public override void Load(HttpComponent self)
  35. {
  36. self.Load();
  37. }
  38. }
  39. [ObjectSystem]
  40. public class HttpComponentDestroySystem: DestroySystem<HttpComponent>
  41. {
  42. public override void Destroy(HttpComponent self)
  43. {
  44. self.Listener.Stop();
  45. self.Listener.Close();
  46. }
  47. }
  48. public static class HttpComponentSystem
  49. {
  50. public static void Load(this HttpComponent self)
  51. {
  52. self.dispatcher = new Dictionary<string, IHttpHandler>();
  53. HashSet<Type> types = EventSystem.Instance.GetTypes(typeof (HttpHandlerAttribute));
  54. SceneType sceneType = self.GetParent<Scene>().SceneType;
  55. foreach (Type type in types)
  56. {
  57. object[] attrs = type.GetCustomAttributes(typeof(HttpHandlerAttribute), false);
  58. if (attrs.Length == 0)
  59. {
  60. continue;
  61. }
  62. HttpHandlerAttribute httpHandlerAttribute = (HttpHandlerAttribute)attrs[0];
  63. if (httpHandlerAttribute.SceneType != sceneType)
  64. {
  65. continue;
  66. }
  67. object obj = Activator.CreateInstance(type);
  68. IHttpHandler ihttpHandler = obj as IHttpHandler;
  69. if (ihttpHandler == null)
  70. {
  71. throw new Exception($"HttpHandler handler not inherit IHttpHandler class: {obj.GetType().FullName}");
  72. }
  73. self.dispatcher.Add(httpHandlerAttribute.Path, ihttpHandler);
  74. }
  75. }
  76. public static async ETTask Accept(this HttpComponent self)
  77. {
  78. long instanceId = self.InstanceId;
  79. while (self.InstanceId == instanceId)
  80. {
  81. try
  82. {
  83. HttpListenerContext context = await self.Listener.GetContextAsync();
  84. self.Handle(context).Coroutine();
  85. }
  86. catch (Exception e)
  87. {
  88. Log.Error(e);
  89. }
  90. }
  91. }
  92. public static async ETTask Handle(this HttpComponent self, HttpListenerContext context)
  93. {
  94. try
  95. {
  96. IHttpHandler handler;
  97. if (self.dispatcher.TryGetValue(context.Request.Url.AbsolutePath, out handler))
  98. {
  99. await handler.Handle(self.Domain, context);
  100. }
  101. }
  102. catch (Exception e)
  103. {
  104. Log.Error(e);
  105. }
  106. context.Request.InputStream.Dispose();
  107. context.Response.OutputStream.Dispose();
  108. }
  109. }
  110. }