TChannel.cs 3.3 KB

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