TSocket.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. namespace TNet
  5. {
  6. public class TSocket: IDisposable
  7. {
  8. private Socket socket;
  9. private readonly TPoller poller;
  10. private readonly SocketAsyncEventArgs innSocketAsyncEventArgs = new SocketAsyncEventArgs();
  11. private readonly SocketAsyncEventArgs outSocketAsyncEventArgs = new SocketAsyncEventArgs();
  12. private readonly TBuffer recvBuffer = new TBuffer();
  13. private readonly TBuffer sendBuffer = new TBuffer();
  14. public Action RecvAction { get; set; }
  15. public Action<TSocket> AcceptAction { get; set; }
  16. public TSocket(TPoller poller)
  17. {
  18. this.poller = poller;
  19. this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  20. this.outSocketAsyncEventArgs.Completed += this.OnComplete;
  21. this.innSocketAsyncEventArgs.Completed += this.OnComplete;
  22. }
  23. public TSocket(TPoller poller, Socket socket)
  24. {
  25. this.poller = poller;
  26. this.socket = socket;
  27. this.outSocketAsyncEventArgs.Completed += this.OnComplete;
  28. this.innSocketAsyncEventArgs.Completed += this.OnComplete;
  29. }
  30. public void Dispose()
  31. {
  32. if (this.socket == null)
  33. {
  34. return;
  35. }
  36. socket.Dispose();
  37. this.socket = null;
  38. }
  39. public void Connect(string host, int port)
  40. {
  41. if (socket.ConnectAsync(this.innSocketAsyncEventArgs))
  42. {
  43. return;
  44. }
  45. this.poller.Add(this.OnConnComplete);
  46. }
  47. public void Accept(int port)
  48. {
  49. this.socket.Bind(new IPEndPoint(IPAddress.Any, port));
  50. this.socket.Listen(100);
  51. this.BeginAccept();
  52. }
  53. public bool Recv(byte[] buffer)
  54. {
  55. if (buffer.Length > this.RecvSize)
  56. {
  57. return false;
  58. }
  59. this.recvBuffer.RecvFrom(buffer);
  60. return true;
  61. }
  62. public void Send(byte[] buffer)
  63. {
  64. bool needBeginSend = this.sendBuffer.Count == 0;
  65. this.sendBuffer.SendTo(buffer);
  66. if (needBeginSend)
  67. {
  68. this.BeginSend();
  69. }
  70. }
  71. public int RecvSize
  72. {
  73. get
  74. {
  75. return this.recvBuffer.Count;
  76. }
  77. }
  78. private void OnComplete(object sender, SocketAsyncEventArgs e)
  79. {
  80. Action action;
  81. switch (e.LastOperation)
  82. {
  83. case SocketAsyncOperation.Accept:
  84. action = () => this.OnAcceptComplete(e.AcceptSocket);
  85. e.AcceptSocket = null;
  86. break;
  87. case SocketAsyncOperation.Connect:
  88. action = this.OnConnComplete;
  89. break;
  90. case SocketAsyncOperation.Disconnect:
  91. action = this.OnDisconnect;
  92. break;
  93. case SocketAsyncOperation.Receive:
  94. action = () => this.OnRecvComplete(e.BytesTransferred);
  95. break;
  96. case SocketAsyncOperation.Send:
  97. action = () => this.OnSendComplete(e.BytesTransferred);
  98. break;
  99. default:
  100. throw new ArgumentOutOfRangeException();
  101. }
  102. this.poller.Add(action);
  103. }
  104. private void OnDisconnect()
  105. {
  106. this.Dispose();
  107. }
  108. private void OnAcceptComplete(Socket sock)
  109. {
  110. if (this.socket == null)
  111. {
  112. return;
  113. }
  114. TSocket newSocket = new TSocket(poller, sock);
  115. if (this.AcceptAction != null)
  116. {
  117. this.AcceptAction(newSocket);
  118. }
  119. this.BeginAccept();
  120. }
  121. private void OnConnComplete()
  122. {
  123. if (this.socket == null)
  124. {
  125. return;
  126. }
  127. this.BeginRecv();
  128. }
  129. private void OnRecvComplete(int bytesTransferred)
  130. {
  131. if (this.socket == null)
  132. {
  133. return;
  134. }
  135. this.recvBuffer.LastIndex += bytesTransferred;
  136. if (this.recvBuffer.LastIndex == TBuffer.ChunkSize)
  137. {
  138. this.recvBuffer.LastIndex = 0;
  139. this.recvBuffer.AddLast();
  140. }
  141. this.BeginRecv();
  142. if (this.RecvAction != null)
  143. {
  144. this.RecvAction();
  145. }
  146. }
  147. private void OnSendComplete(int bytesTransferred)
  148. {
  149. if (this.socket == null)
  150. {
  151. return;
  152. }
  153. this.sendBuffer.FirstIndex += bytesTransferred;
  154. if (this.sendBuffer.FirstIndex == TBuffer.ChunkSize)
  155. {
  156. this.sendBuffer.FirstIndex = 0;
  157. this.sendBuffer.RemoveFirst();
  158. }
  159. // 如果没有数据可以发送,则返回
  160. if (this.sendBuffer.Count == 0)
  161. {
  162. return;
  163. }
  164. // 继续发送数据
  165. this.BeginSend();
  166. }
  167. private void BeginAccept()
  168. {
  169. if (this.socket == null)
  170. {
  171. return;
  172. }
  173. if (this.socket.AcceptAsync(this.innSocketAsyncEventArgs))
  174. {
  175. return;
  176. }
  177. Action action = () => this.OnAcceptComplete(this.innSocketAsyncEventArgs.AcceptSocket);
  178. this.poller.Add(action);
  179. }
  180. private void BeginRecv()
  181. {
  182. if (this.socket == null)
  183. {
  184. return;
  185. }
  186. this.innSocketAsyncEventArgs.SetBuffer(this.recvBuffer.Last, this.recvBuffer.LastIndex, TBuffer.ChunkSize - this.recvBuffer.LastIndex);
  187. if (this.socket.ReceiveAsync(this.innSocketAsyncEventArgs))
  188. {
  189. return;
  190. }
  191. Action action = () => this.OnRecvComplete(this.innSocketAsyncEventArgs.BytesTransferred);
  192. this.poller.Add(action);
  193. }
  194. private void BeginSend()
  195. {
  196. if (this.socket == null)
  197. {
  198. return;
  199. }
  200. int count = 0;
  201. if (TBuffer.ChunkSize - this.sendBuffer.FirstIndex < this.sendBuffer.Count)
  202. {
  203. count = TBuffer.ChunkSize - this.sendBuffer.FirstIndex;
  204. }
  205. else
  206. {
  207. count = this.sendBuffer.Count;
  208. }
  209. this.outSocketAsyncEventArgs.SetBuffer(this.sendBuffer.First, this.sendBuffer.FirstIndex, count);
  210. if (this.socket.SendAsync(outSocketAsyncEventArgs))
  211. {
  212. return;
  213. }
  214. Action action = () => this.OnSendComplete(this.outSocketAsyncEventArgs.BytesTransferred);
  215. this.poller.Add(action);
  216. }
  217. }
  218. }