TChannel.cs 3.3 KB

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