TSession.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using Common.Helper;
  3. using Common.Logger;
  4. using MongoDB.Bson;
  5. namespace TNet
  6. {
  7. public class TSession: IDisposable
  8. {
  9. private const int RecvSendInterval = 100;
  10. private readonly TServer server;
  11. private TSocket socket;
  12. private readonly TBuffer recvBuffer = new TBuffer();
  13. private readonly TBuffer sendBuffer = new TBuffer();
  14. public ObjectId SendTimer = ObjectId.Empty;
  15. public ObjectId RecvTimer = ObjectId.Empty;
  16. private event Action onRecv = () => { };
  17. private event Action onSend = () => { };
  18. public event Action OnRecv
  19. {
  20. add
  21. {
  22. this.onRecv += value;
  23. }
  24. remove
  25. {
  26. this.onRecv -= value;
  27. }
  28. }
  29. public event Action OnSend
  30. {
  31. add
  32. {
  33. this.onSend += value;
  34. }
  35. remove
  36. {
  37. this.onSend -= value;
  38. }
  39. }
  40. public TSession(TSocket socket, TServer server)
  41. {
  42. this.socket = socket;
  43. this.server = server;
  44. }
  45. public void Dispose()
  46. {
  47. if (this.socket == null)
  48. {
  49. return;
  50. }
  51. this.server.Remove(socket.RemoteAddress);
  52. this.socket.Dispose();
  53. this.socket = null;
  54. }
  55. public void Send(byte[] buffer)
  56. {
  57. this.sendBuffer.SendTo(buffer);
  58. if (this.SendTimer == ObjectId.Empty)
  59. {
  60. this.SendTimer = this.server.Timer.Add(TimeHelper.Now() + RecvSendInterval, this.SendTimerCallback);
  61. }
  62. }
  63. private async void SendTimerCallback()
  64. {
  65. try
  66. {
  67. while (true)
  68. {
  69. if (this.sendBuffer.Count == 0)
  70. {
  71. break;
  72. }
  73. int sendSize = TBuffer.ChunkSize - this.sendBuffer.FirstIndex;
  74. if (sendSize > this.sendBuffer.Count)
  75. {
  76. sendSize = this.sendBuffer.Count;
  77. }
  78. int n = await this.socket.SendAsync(
  79. this.sendBuffer.First, this.sendBuffer.FirstIndex, sendSize);
  80. this.sendBuffer.FirstIndex += n;
  81. if (this.sendBuffer.FirstIndex == TBuffer.ChunkSize)
  82. {
  83. this.sendBuffer.FirstIndex = 0;
  84. this.sendBuffer.RemoveFirst();
  85. }
  86. }
  87. }
  88. catch (Exception e)
  89. {
  90. Log.Trace(e.ToString());
  91. }
  92. this.onSend();
  93. this.SendTimer = ObjectId.Empty;
  94. }
  95. public int RecvSize
  96. {
  97. get
  98. {
  99. return this.recvBuffer.Count;
  100. }
  101. }
  102. public void Recv(byte[] buffer)
  103. {
  104. this.recvBuffer.RecvFrom(buffer);
  105. }
  106. public async void Start()
  107. {
  108. try
  109. {
  110. while (true)
  111. {
  112. int n = await this.socket.RecvAsync(
  113. this.recvBuffer.Last, this.recvBuffer.LastIndex, TBuffer.ChunkSize - this.recvBuffer.LastIndex);
  114. if (n == 0)
  115. {
  116. break;
  117. }
  118. this.recvBuffer.LastIndex += n;
  119. if (this.recvBuffer.LastIndex == TBuffer.ChunkSize)
  120. {
  121. this.recvBuffer.AddLast();
  122. this.recvBuffer.LastIndex = 0;
  123. }
  124. if (this.RecvTimer == ObjectId.Empty)
  125. {
  126. this.RecvTimer = this.server.Timer.Add(TimeHelper.Now() + RecvSendInterval, this.RecvTimerCallback);
  127. }
  128. }
  129. }
  130. catch (Exception e)
  131. {
  132. Log.Trace(e.ToString());
  133. }
  134. }
  135. private void RecvTimerCallback()
  136. {
  137. this.onRecv();
  138. this.RecvTimer = ObjectId.Empty;
  139. }
  140. }
  141. }