TChannel.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Sockets;
  5. using System.Threading.Tasks;
  6. namespace Base
  7. {
  8. public class TChannel : AChannel
  9. {
  10. private readonly TSocket socket;
  11. private readonly TBuffer recvBuffer = new TBuffer();
  12. private readonly TBuffer sendBuffer = new TBuffer();
  13. private bool isSending;
  14. private readonly PacketParser parser;
  15. private bool isConnected;
  16. public Action<long, SocketError> OnError;
  17. public string RemoteAddress { get; }
  18. private TaskCompletionSource<byte[]> recvTcs;
  19. /// <summary>
  20. /// connect
  21. /// </summary>
  22. public TChannel(TSocket socket, string host, int port, TService service) : base(service)
  23. {
  24. this.socket = socket;
  25. this.parser = new PacketParser(this.recvBuffer);
  26. this.RemoteAddress = host + ":" + port;
  27. bool result = this.socket.ConnectAsync(host, port);
  28. if (!result)
  29. {
  30. this.OnConnected(this.Id, SocketError.Success);
  31. return;
  32. }
  33. this.socket.OnConn += e => OnConnected(this.Id, e);
  34. }
  35. /// <summary>
  36. /// accept
  37. /// </summary>
  38. public TChannel(TSocket socket, TService service) : base(service)
  39. {
  40. this.socket = socket;
  41. this.parser = new PacketParser(this.recvBuffer);
  42. this.RemoteAddress = socket.RemoteAddress;
  43. }
  44. public override void Dispose()
  45. {
  46. if (this.Id == 0)
  47. {
  48. return;
  49. }
  50. long id = this.Id;
  51. base.Dispose();
  52. this.socket.Dispose();
  53. this.service.Remove(id);
  54. }
  55. private void OnConnected(long channelId, SocketError error)
  56. {
  57. if (this.service.GetChannel(channelId) == null)
  58. {
  59. return;
  60. }
  61. if (error != SocketError.Success)
  62. {
  63. Log.Error($"connect error: {error}");
  64. return;
  65. }
  66. this.isConnected = true;
  67. this.StartSend();
  68. this.StartRecv();
  69. }
  70. public override void Send(byte[] buffer, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
  71. {
  72. byte[] size = BitConverter.GetBytes(buffer.Length);
  73. this.sendBuffer.SendTo(size);
  74. this.sendBuffer.SendTo(buffer);
  75. if (!this.isSending && this.isConnected)
  76. {
  77. this.StartSend();
  78. }
  79. }
  80. public override void Send(List<byte[]> buffers, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
  81. {
  82. int size = buffers.Select(b => b.Length).Sum();
  83. byte[] sizeBuffer = BitConverter.GetBytes(size);
  84. this.sendBuffer.SendTo(sizeBuffer);
  85. foreach (byte[] buffer in buffers)
  86. {
  87. this.sendBuffer.SendTo(buffer);
  88. }
  89. if (!this.isSending && this.isConnected)
  90. {
  91. this.StartSend();
  92. }
  93. }
  94. private void StartSend()
  95. {
  96. // 没有数据需要发送
  97. if (this.sendBuffer.Count == 0)
  98. {
  99. this.isSending = false;
  100. return;
  101. }
  102. this.isSending = true;
  103. int sendSize = TBuffer.ChunkSize - this.sendBuffer.FirstIndex;
  104. if (sendSize > this.sendBuffer.Count)
  105. {
  106. sendSize = this.sendBuffer.Count;
  107. }
  108. if (!this.socket.SendAsync(this.sendBuffer.First, this.sendBuffer.FirstIndex, sendSize))
  109. {
  110. this.OnSend(sendSize, SocketError.Success);
  111. return;
  112. }
  113. this.socket.OnSend = this.OnSend;
  114. }
  115. private void OnSend(int n, SocketError error)
  116. {
  117. if (this.Id == 0)
  118. {
  119. return;
  120. }
  121. this.socket.OnSend = null;
  122. if (error != SocketError.Success)
  123. {
  124. Log.Info($"socket send fail, error: {error}, n: {n}");
  125. this.OnError(this.Id, error);
  126. return;
  127. }
  128. this.sendBuffer.FirstIndex += n;
  129. if (this.sendBuffer.FirstIndex == TBuffer.ChunkSize)
  130. {
  131. this.sendBuffer.FirstIndex = 0;
  132. this.sendBuffer.RemoveFirst();
  133. }
  134. this.StartSend();
  135. }
  136. private void StartRecv()
  137. {
  138. int size = TBuffer.ChunkSize - this.recvBuffer.LastIndex;
  139. if (!this.socket.RecvAsync(this.recvBuffer.Last, this.recvBuffer.LastIndex, size))
  140. {
  141. this.OnRecv(size, SocketError.Success);
  142. }
  143. this.socket.OnRecv = this.OnRecv;
  144. }
  145. private void OnRecv(int n, SocketError error)
  146. {
  147. if (this.Id == 0)
  148. {
  149. return;
  150. }
  151. this.socket.OnRecv = null;
  152. if (error != SocketError.Success)
  153. {
  154. Log.Info($"socket recv fail, error: {error}, {n}");
  155. this.OnError(this.Id, error);
  156. return;
  157. }
  158. this.recvBuffer.LastIndex += n;
  159. if (this.recvBuffer.LastIndex == TBuffer.ChunkSize)
  160. {
  161. this.recvBuffer.AddLast();
  162. this.recvBuffer.LastIndex = 0;
  163. }
  164. if (this.recvTcs != null)
  165. {
  166. byte[] packet = this.parser.GetPacket();
  167. if (packet != null)
  168. {
  169. this.recvTcs.SetResult(packet);
  170. this.recvTcs = null;
  171. }
  172. }
  173. StartRecv();
  174. }
  175. public override Task<byte[]> Recv()
  176. {
  177. TaskCompletionSource<byte[]> tcs = new TaskCompletionSource<byte[]>();
  178. byte[] packet = this.parser.GetPacket();
  179. if (packet != null)
  180. {
  181. tcs.SetResult(packet);
  182. }
  183. else
  184. {
  185. recvTcs = tcs;
  186. }
  187. return tcs.Task;
  188. }
  189. }
  190. }