TService.cs 4.2 KB

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