TService.cs 5.5 KB

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