TService.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. namespace ET
  8. {
  9. public sealed class TService : AService
  10. {
  11. private readonly Dictionary<long, TChannel> idChannels = new Dictionary<long, TChannel>();
  12. private readonly SocketAsyncEventArgs innArgs = new SocketAsyncEventArgs();
  13. private Socket acceptor;
  14. public HashSet<long> NeedStartSend = new HashSet<long>();
  15. public TService(ThreadSynchronizationContext threadSynchronizationContext, ServiceType serviceType)
  16. {
  17. this.foreachAction = channelId =>
  18. {
  19. TChannel tChannel = this.Get(channelId);
  20. tChannel?.Update();
  21. };
  22. this.ServiceType = serviceType;
  23. this.ThreadSynchronizationContext = threadSynchronizationContext;
  24. }
  25. public TService(ThreadSynchronizationContext threadSynchronizationContext, IPEndPoint ipEndPoint, ServiceType serviceType)
  26. {
  27. this.foreachAction = channelId =>
  28. {
  29. TChannel tChannel = this.Get(channelId);
  30. tChannel?.Update();
  31. };
  32. this.ServiceType = serviceType;
  33. this.ThreadSynchronizationContext = threadSynchronizationContext;
  34. this.acceptor = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  35. this.acceptor.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  36. this.innArgs.Completed += this.OnComplete;
  37. this.acceptor.Bind(ipEndPoint);
  38. this.acceptor.Listen(1000);
  39. this.ThreadSynchronizationContext.PostNext(this.AcceptAsync);
  40. }
  41. private void OnComplete(object sender, SocketAsyncEventArgs e)
  42. {
  43. switch (e.LastOperation)
  44. {
  45. case SocketAsyncOperation.Accept:
  46. SocketError socketError = e.SocketError;
  47. Socket acceptSocket = e.AcceptSocket;
  48. this.ThreadSynchronizationContext.Post(()=>{this.OnAcceptComplete(socketError, acceptSocket);});
  49. break;
  50. default:
  51. throw new Exception($"socket error: {e.LastOperation}");
  52. }
  53. }
  54. #region 网络线程
  55. private void OnAcceptComplete(SocketError socketError, Socket acceptSocket)
  56. {
  57. if (this.acceptor == null)
  58. {
  59. return;
  60. }
  61. if (socketError != SocketError.Success)
  62. {
  63. Log.Error($"accept error {socketError}");
  64. this.AcceptAsync();
  65. return;
  66. }
  67. try
  68. {
  69. long id = this.CreateAcceptChannelId(0);
  70. TChannel channel = new TChannel(id, acceptSocket, this);
  71. this.idChannels.Add(channel.Id, channel);
  72. long channelId = channel.Id;
  73. this.OnAccept(channelId, channel.RemoteAddress);
  74. }
  75. catch (Exception exception)
  76. {
  77. Log.Error(exception);
  78. }
  79. // 开始新的accept
  80. this.AcceptAsync();
  81. }
  82. private void AcceptAsync()
  83. {
  84. this.innArgs.AcceptSocket = null;
  85. if (this.acceptor.AcceptAsync(this.innArgs))
  86. {
  87. return;
  88. }
  89. OnAcceptComplete(this.innArgs.SocketError, this.innArgs.AcceptSocket);
  90. }
  91. private TChannel Create(IPEndPoint ipEndPoint, long id)
  92. {
  93. TChannel channel = new TChannel(id, ipEndPoint, this);
  94. this.idChannels.Add(channel.Id, channel);
  95. return channel;
  96. }
  97. protected override void Get(long id, IPEndPoint address)
  98. {
  99. if (this.idChannels.TryGetValue(id, out TChannel _))
  100. {
  101. return;
  102. }
  103. this.Create(address, id);
  104. }
  105. private TChannel Get(long id)
  106. {
  107. TChannel channel = null;
  108. this.idChannels.TryGetValue(id, out channel);
  109. return channel;
  110. }
  111. public override void Dispose()
  112. {
  113. this.acceptor?.Close();
  114. this.acceptor = null;
  115. this.innArgs.Dispose();
  116. ThreadSynchronizationContext = null;
  117. foreach (long id in this.idChannels.Keys.ToArray())
  118. {
  119. TChannel channel = this.idChannels[id];
  120. channel.Dispose();
  121. }
  122. this.idChannels.Clear();
  123. }
  124. public override void Remove(long id)
  125. {
  126. if (this.idChannels.TryGetValue(id, out TChannel channel))
  127. {
  128. channel.Dispose();
  129. }
  130. this.idChannels.Remove(id);
  131. }
  132. protected override void Send(long channelId, long actorId, MemoryStream stream)
  133. {
  134. try
  135. {
  136. TChannel aChannel = this.Get(channelId);
  137. if (aChannel == null)
  138. {
  139. this.OnError(channelId, ErrorCore.ERR_SendMessageNotFoundTChannel);
  140. return;
  141. }
  142. aChannel.Send(actorId, stream);
  143. }
  144. catch (Exception e)
  145. {
  146. Log.Error(e);
  147. }
  148. }
  149. private readonly Action<long> foreachAction;
  150. public override void Update()
  151. {
  152. this.NeedStartSend.Foreach(this.foreachAction);
  153. this.NeedStartSend.Clear();
  154. }
  155. public override bool IsDispose()
  156. {
  157. return this.ThreadSynchronizationContext == null;
  158. }
  159. #endregion
  160. }
  161. }