TSocket.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. namespace Base
  5. {
  6. /// <summary>
  7. /// 封装Socket,将回调push到主线程处理
  8. /// </summary>
  9. public class TSocket: IDisposable
  10. {
  11. private readonly TPoller poller;
  12. private Socket socket;
  13. private readonly SocketAsyncEventArgs innArgs = new SocketAsyncEventArgs();
  14. private readonly SocketAsyncEventArgs outArgs = new SocketAsyncEventArgs();
  15. public Action<SocketError> OnConn;
  16. public Action<int, SocketError> OnRecv;
  17. public Action<int, SocketError> OnSend;
  18. public Action<SocketError> OnDisconnect;
  19. private string remoteAddress;
  20. public TSocket(TPoller poller)
  21. {
  22. this.poller = poller;
  23. this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  24. this.innArgs.Completed += this.OnComplete;
  25. this.outArgs.Completed += this.OnComplete;
  26. }
  27. public string RemoteAddress
  28. {
  29. get
  30. {
  31. return remoteAddress;
  32. }
  33. }
  34. public Socket Socket
  35. {
  36. get
  37. {
  38. return this.socket;
  39. }
  40. }
  41. protected void Dispose(bool disposing)
  42. {
  43. if (this.socket == null)
  44. {
  45. return;
  46. }
  47. if (disposing)
  48. {
  49. this.socket.Close();
  50. }
  51. this.socket = null;
  52. }
  53. ~TSocket()
  54. {
  55. this.Dispose(false);
  56. }
  57. public void Dispose()
  58. {
  59. this.Dispose(true);
  60. GC.SuppressFinalize(this);
  61. }
  62. private void OnComplete(object sender, SocketAsyncEventArgs e)
  63. {
  64. Action action;
  65. switch (e.LastOperation)
  66. {
  67. case SocketAsyncOperation.Connect:
  68. action = () => OnConnectComplete(e);
  69. break;
  70. case SocketAsyncOperation.Receive:
  71. action = () => OnRecvComplete(e);
  72. break;
  73. case SocketAsyncOperation.Send:
  74. action = () => OnSendComplete(e);
  75. break;
  76. case SocketAsyncOperation.Disconnect:
  77. action = () => OnDisconnectComplete(e);
  78. break;
  79. default:
  80. throw new Exception($"socket error: {e.LastOperation}");
  81. }
  82. // 回调到主线程处理
  83. this.poller.Add(action);
  84. }
  85. public bool ConnectAsync(string host, int port)
  86. {
  87. remoteAddress = $"{host}:{port}";
  88. this.outArgs.RemoteEndPoint = new IPEndPoint(IPAddress.Parse(host), port);
  89. if (this.socket.ConnectAsync(this.outArgs))
  90. {
  91. return true;
  92. }
  93. OnConnectComplete(this.outArgs);
  94. return false;
  95. }
  96. private void OnConnectComplete(SocketAsyncEventArgs e)
  97. {
  98. if (this.OnConn == null)
  99. {
  100. return;
  101. }
  102. this.OnConn(e.SocketError);
  103. }
  104. public bool RecvAsync(byte[] buffer, int offset, int count)
  105. {
  106. try
  107. {
  108. this.innArgs.SetBuffer(buffer, offset, count);
  109. }
  110. catch (Exception e)
  111. {
  112. throw new Exception($"socket set buffer error: {buffer.Length}, {offset}, {count}", e);
  113. }
  114. if (this.socket.ReceiveAsync(this.innArgs))
  115. {
  116. return true;
  117. }
  118. OnRecvComplete(this.innArgs);
  119. return false;
  120. }
  121. private void OnRecvComplete(SocketAsyncEventArgs e)
  122. {
  123. if (this.OnRecv == null)
  124. {
  125. return;
  126. }
  127. this.OnRecv(e.BytesTransferred, e.SocketError);
  128. }
  129. public bool SendAsync(byte[] buffer, int offset, int count)
  130. {
  131. try
  132. {
  133. this.outArgs.SetBuffer(buffer, offset, count);
  134. }
  135. catch (Exception e)
  136. {
  137. throw new Exception($"socket set buffer error: {buffer.Length}, {offset}, {count}", e);
  138. }
  139. if (this.socket.SendAsync(this.outArgs))
  140. {
  141. return true;
  142. }
  143. OnSendComplete(this.outArgs);
  144. return false;
  145. }
  146. private void OnSendComplete(SocketAsyncEventArgs e)
  147. {
  148. if (this.OnSend == null)
  149. {
  150. return;
  151. }
  152. this.OnSend(e.BytesTransferred, e.SocketError);
  153. }
  154. private void OnDisconnectComplete(SocketAsyncEventArgs e)
  155. {
  156. if (this.OnDisconnect == null)
  157. {
  158. return;
  159. }
  160. this.OnDisconnect(e.SocketError);
  161. }
  162. }
  163. }