TService.cs 4.2 KB

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