TChannel.cs 4.0 KB

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