TChannel.cs 3.6 KB

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