WService.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.WebSockets;
  6. namespace ET
  7. {
  8. public class WService: AService
  9. {
  10. private long idGenerater = 200000000;
  11. private HttpListener httpListener;
  12. private readonly Dictionary<long, WChannel> channels = new Dictionary<long, WChannel>();
  13. public WService(ThreadSynchronizationContext threadSynchronizationContext, IEnumerable<string> prefixs)
  14. {
  15. this.ThreadSynchronizationContext = threadSynchronizationContext;
  16. this.httpListener = new HttpListener();
  17. StartAccept(prefixs).Coroutine();
  18. }
  19. public WService(ThreadSynchronizationContext threadSynchronizationContext)
  20. {
  21. this.ThreadSynchronizationContext = threadSynchronizationContext;
  22. }
  23. private long GetId
  24. {
  25. get
  26. {
  27. return ++this.idGenerater;
  28. }
  29. }
  30. public WChannel Create(string address, long id)
  31. {
  32. ClientWebSocket webSocket = new ClientWebSocket();
  33. WChannel channel = new WChannel(id, webSocket, address, this);
  34. this.channels[channel.Id] = channel;
  35. return channel;
  36. }
  37. public override void Remove(long id)
  38. {
  39. WChannel channel;
  40. if (!this.channels.TryGetValue(id, out channel))
  41. {
  42. return;
  43. }
  44. this.channels.Remove(id);
  45. channel.Dispose();
  46. }
  47. public override bool IsDispose()
  48. {
  49. return this.ThreadSynchronizationContext == null;
  50. }
  51. protected void Get(long id, string address)
  52. {
  53. if (!this.channels.TryGetValue(id, out _))
  54. {
  55. this.Create(address, id);
  56. }
  57. }
  58. public override void Dispose()
  59. {
  60. this.ThreadSynchronizationContext = null;
  61. this.httpListener?.Close();
  62. this.httpListener = null;
  63. }
  64. private async ETTask StartAccept(IEnumerable<string> prefixs)
  65. {
  66. try
  67. {
  68. foreach (string prefix in prefixs)
  69. {
  70. this.httpListener.Prefixes.Add(prefix);
  71. }
  72. httpListener.Start();
  73. while (true)
  74. {
  75. try
  76. {
  77. HttpListenerContext httpListenerContext = await this.httpListener.GetContextAsync();
  78. HttpListenerWebSocketContext webSocketContext = await httpListenerContext.AcceptWebSocketAsync(null);
  79. WChannel channel = new WChannel(this.GetId, webSocketContext, this);
  80. this.channels[channel.Id] = channel;
  81. this.OnAccept(channel.Id, channel.RemoteAddress);
  82. }
  83. catch (Exception e)
  84. {
  85. Log.Error(e);
  86. }
  87. }
  88. }
  89. catch (HttpListenerException e)
  90. {
  91. if (e.ErrorCode == 5)
  92. {
  93. throw new Exception($"CMD管理员中输入: netsh http add urlacl url=http://*:8080/ user=Everyone", e);
  94. }
  95. Log.Error(e);
  96. }
  97. catch (Exception e)
  98. {
  99. Log.Error(e);
  100. }
  101. }
  102. protected override void Get(long id, IPEndPoint address)
  103. {
  104. throw new NotImplementedException();
  105. }
  106. protected override void Send(long channelId, long actorId, MemoryStream stream)
  107. {
  108. this.channels.TryGetValue(channelId, out WChannel channel);
  109. if (channel == null)
  110. {
  111. return;
  112. }
  113. channel.Send(stream);
  114. }
  115. public override void Update()
  116. {
  117. }
  118. }
  119. }