HttpComponent.cs 2.7 KB

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