HttpComponent.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. namespace Model
  5. {
  6. [ObjectSystem]
  7. public class HttpComponentComponentAwakeSystem : AwakeSystem<HttpComponent>
  8. {
  9. public override void Awake(HttpComponent self)
  10. {
  11. self.Awake();
  12. }
  13. }
  14. [ObjectSystem]
  15. public class HttpComponentComponentLoadSystem : LoadSystem<HttpComponent>
  16. {
  17. public override void Load(HttpComponent self)
  18. {
  19. self.Load();
  20. }
  21. }
  22. /// <summary>
  23. /// http请求分发器
  24. /// </summary>
  25. public class HttpComponent : Component
  26. {
  27. public AppType appType;
  28. public HttpListener listener;
  29. public HttpConfig HttpConfig;
  30. public Dictionary<string, IHttpHandler> dispatcher;
  31. public void Awake()
  32. {
  33. StartConfig startConfig = Game.Scene.GetComponent<StartConfigComponent>().StartConfig;
  34. this.appType = startConfig.AppType;
  35. this.HttpConfig = startConfig.GetComponent<HttpConfig>();
  36. this.Load();
  37. try
  38. {
  39. this.listener = new HttpListener();
  40. if (this.HttpConfig.Url == null)
  41. {
  42. this.HttpConfig.Url = "";
  43. }
  44. foreach (string s in this.HttpConfig.Url.Split(';'))
  45. {
  46. if (s.Trim() == "")
  47. {
  48. continue;
  49. }
  50. this.listener.Prefixes.Add(s);
  51. }
  52. this.listener.Start();
  53. this.Accept();
  54. }
  55. catch (HttpListenerException e)
  56. {
  57. throw new Exception($"http server error: {e.ErrorCode}", e);
  58. }
  59. }
  60. public void Load()
  61. {
  62. this.dispatcher = new Dictionary<string, IHttpHandler>();
  63. Type[] types = DllHelper.GetMonoTypes();
  64. foreach (Type type in types)
  65. {
  66. object[] attrs = type.GetCustomAttributes(typeof(HttpHandlerAttribute), false);
  67. if (attrs.Length == 0)
  68. {
  69. continue;
  70. }
  71. HttpHandlerAttribute httpHandlerAttribute = (HttpHandlerAttribute)attrs[0];
  72. if (!httpHandlerAttribute.AppType.Is(this.appType))
  73. {
  74. continue;
  75. }
  76. object obj = Activator.CreateInstance(type);
  77. IHttpHandler ihttpHandler = obj as IHttpHandler;
  78. if (ihttpHandler == null)
  79. {
  80. throw new Exception($"HttpHandler handler not inherit IHttpHandler class: {obj.GetType().FullName}");
  81. }
  82. this.dispatcher.Add(httpHandlerAttribute.Path, ihttpHandler);
  83. }
  84. }
  85. public async void Accept()
  86. {
  87. while (true)
  88. {
  89. if (this.IsDisposed)
  90. {
  91. return;
  92. }
  93. HttpListenerContext context = await this.listener.GetContextAsync();
  94. IHttpHandler handler;
  95. if (this.dispatcher.TryGetValue(context.Request.Url.AbsolutePath, out handler))
  96. {
  97. handler.Handle(context);
  98. }
  99. //Log.Debug($"{context.Request.Url.AbsolutePath} {context.Request.Url.AbsoluteUri} {context.Request.Url.LocalPath} {context.Request.Url.IdnHost} " +
  100. // $"{context.Request.Url.Host} {context.Request.Url.}");
  101. }
  102. }
  103. public override void Dispose()
  104. {
  105. if (this.IsDisposed)
  106. {
  107. return;
  108. }
  109. base.Dispose();
  110. this.listener.Stop();
  111. this.listener.Close();
  112. }
  113. }
  114. }