TChannel.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Threading.Tasks;
  3. using Common.Helper;
  4. using Common.Logger;
  5. using MongoDB.Bson;
  6. using Network;
  7. namespace TNet
  8. {
  9. internal class TChannel: IChannel
  10. {
  11. private const int SendInterval = 50;
  12. private readonly TService service;
  13. private TSocket socket;
  14. private readonly TBuffer recvBuffer = new TBuffer();
  15. private readonly TBuffer sendBuffer = new TBuffer();
  16. private ObjectId sendTimer = ObjectId.Empty;
  17. private Action onParseComplete = () => { };
  18. private readonly PacketParser parser;
  19. private readonly string remoteAddress;
  20. public TChannel(TSocket socket, TService service)
  21. {
  22. this.socket = socket;
  23. this.service = service;
  24. this.parser = new PacketParser(recvBuffer);
  25. this.remoteAddress = this.socket.RemoteAddress;
  26. }
  27. protected virtual void Dispose(bool disposing)
  28. {
  29. if (socket == null)
  30. {
  31. return;
  32. }
  33. if (disposing)
  34. {
  35. // 释放托管的资源
  36. socket.Dispose();
  37. }
  38. // 释放非托管资源
  39. this.service.Remove(this);
  40. this.socket = null;
  41. }
  42. ~TChannel()
  43. {
  44. this.Dispose(false);
  45. }
  46. public void Dispose()
  47. {
  48. Dispose(true);
  49. GC.SuppressFinalize(this);
  50. }
  51. public void SendAsync(byte[] buffer, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
  52. {
  53. byte[] size = BitConverter.GetBytes(buffer.Length);
  54. this.sendBuffer.SendTo(size);
  55. this.sendBuffer.SendTo(buffer);
  56. if (this.sendTimer == ObjectId.Empty)
  57. {
  58. this.sendTimer = this.service.Timer.Add(TimeHelper.Now() + SendInterval, this.SendTimerCallback);
  59. }
  60. }
  61. public ObjectId SendTimer
  62. {
  63. get
  64. {
  65. return this.sendTimer;
  66. }
  67. }
  68. private async void SendTimerCallback()
  69. {
  70. try
  71. {
  72. while (true)
  73. {
  74. if (this.sendBuffer.Count == 0)
  75. {
  76. break;
  77. }
  78. int sendSize = TBuffer.ChunkSize - this.sendBuffer.FirstIndex;
  79. if (sendSize > this.sendBuffer.Count)
  80. {
  81. sendSize = this.sendBuffer.Count;
  82. }
  83. int n = await this.socket.SendAsync(
  84. this.sendBuffer.First, this.sendBuffer.FirstIndex, sendSize);
  85. this.sendBuffer.FirstIndex += n;
  86. if (this.sendBuffer.FirstIndex == TBuffer.ChunkSize)
  87. {
  88. this.sendBuffer.FirstIndex = 0;
  89. this.sendBuffer.RemoveFirst();
  90. }
  91. }
  92. }
  93. catch (Exception e)
  94. {
  95. Log.Trace(e.ToString());
  96. }
  97. this.sendTimer = ObjectId.Empty;
  98. }
  99. public Task<byte[]> RecvAsync()
  100. {
  101. var tcs = new TaskCompletionSource<byte[]>();
  102. if (parser.Parse())
  103. {
  104. tcs.SetResult(parser.GetPacket());
  105. }
  106. else
  107. {
  108. this.onParseComplete = () => this.ParseComplete(tcs);
  109. }
  110. return tcs.Task;
  111. }
  112. public async Task<bool> DisconnnectAsync()
  113. {
  114. return await this.socket.DisconnectAsync();
  115. }
  116. public string RemoteAddress
  117. {
  118. get
  119. {
  120. return remoteAddress;
  121. }
  122. }
  123. private void ParseComplete(TaskCompletionSource<byte[]> tcs)
  124. {
  125. byte[] packet = parser.GetPacket();
  126. this.onParseComplete = () => { };
  127. tcs.SetResult(packet);
  128. }
  129. public async void Start()
  130. {
  131. try
  132. {
  133. while (true)
  134. {
  135. int n = await this.socket.RecvAsync(
  136. this.recvBuffer.Last, this.recvBuffer.LastIndex, TBuffer.ChunkSize - this.recvBuffer.LastIndex);
  137. if (n == 0)
  138. {
  139. break;
  140. }
  141. this.recvBuffer.LastIndex += n;
  142. if (this.recvBuffer.LastIndex == TBuffer.ChunkSize)
  143. {
  144. this.recvBuffer.AddLast();
  145. this.recvBuffer.LastIndex = 0;
  146. }
  147. // 解析封包
  148. if (parser.Parse())
  149. {
  150. this.onParseComplete();
  151. }
  152. }
  153. }
  154. catch (Exception e)
  155. {
  156. Log.Trace(e.ToString());
  157. }
  158. }
  159. }
  160. }