HttpComponentSystem.cs 4.0 KB

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