SocketAsyncEventArgs.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // System.Net.Sockets.SocketAsyncEventArgs.cs
  2. //
  3. // Authors:
  4. // Marek Habersack ([email protected])
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // Copyright (c) 2008,2010 Novell, Inc. (http://www.novell.com)
  8. // Copyright (c) 2011 Xamarin, Inc. (http://xamarin.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Reflection;
  33. using System.Security;
  34. using System.Threading;
  35. namespace System.Net.Sockets
  36. {
  37. public class SocketAsyncEventArgs : EventArgs, IDisposable
  38. {
  39. bool disposed;
  40. internal volatile int in_progress;
  41. internal EndPoint remote_ep;
  42. internal Socket current_socket;
  43. internal SocketAsyncResult socket_async_result = new SocketAsyncResult ();
  44. public Exception ConnectByNameError {
  45. get;
  46. internal set;
  47. }
  48. public Socket AcceptSocket {
  49. get;
  50. set;
  51. }
  52. public byte[] Buffer {
  53. get;
  54. private set;
  55. }
  56. public Memory<byte> MemoryBuffer => Buffer;
  57. internal IList<ArraySegment<byte>> m_BufferList;
  58. public IList<ArraySegment<byte>> BufferList {
  59. get { return m_BufferList; }
  60. set {
  61. if (Buffer != null && value != null)
  62. throw new ArgumentException ("Buffer and BufferList properties cannot both be non-null.");
  63. m_BufferList = value;
  64. }
  65. }
  66. public int BytesTransferred {
  67. get;
  68. internal set;
  69. }
  70. public int Count {
  71. get;
  72. internal set;
  73. }
  74. public bool DisconnectReuseSocket {
  75. get;
  76. set;
  77. }
  78. public SocketAsyncOperation LastOperation {
  79. get;
  80. private set;
  81. }
  82. public int Offset {
  83. get;
  84. private set;
  85. }
  86. public EndPoint RemoteEndPoint {
  87. get { return remote_ep; }
  88. set { remote_ep = value; }
  89. }
  90. public IPPacketInformation ReceiveMessageFromPacketInfo {
  91. get;
  92. private set;
  93. }
  94. public SendPacketsElement[] SendPacketsElements {
  95. get;
  96. set;
  97. }
  98. public TransmitFileOptions SendPacketsFlags {
  99. get;
  100. set;
  101. }
  102. [MonoTODO ("unused property")]
  103. public int SendPacketsSendSize {
  104. get;
  105. set;
  106. }
  107. public SocketError SocketError {
  108. get;
  109. set;
  110. }
  111. public SocketFlags SocketFlags {
  112. get;
  113. set;
  114. }
  115. public object UserToken {
  116. get;
  117. set;
  118. }
  119. public Socket ConnectSocket {
  120. get {
  121. switch (SocketError) {
  122. case SocketError.AccessDenied:
  123. return null;
  124. default:
  125. return current_socket;
  126. }
  127. }
  128. }
  129. internal bool PolicyRestricted {
  130. get;
  131. private set;
  132. }
  133. public event EventHandler<SocketAsyncEventArgs> Completed;
  134. internal SocketAsyncEventArgs (bool policy)
  135. : this ()
  136. {
  137. PolicyRestricted = policy;
  138. }
  139. public SocketAsyncEventArgs ()
  140. {
  141. SendPacketsSendSize = -1;
  142. }
  143. ~SocketAsyncEventArgs ()
  144. {
  145. Dispose (false);
  146. }
  147. void Dispose (bool disposing)
  148. {
  149. disposed = true;
  150. if (disposing && in_progress != 0)
  151. return;
  152. }
  153. public void Dispose ()
  154. {
  155. Dispose (true);
  156. GC.SuppressFinalize (this);
  157. }
  158. internal void SetLastOperation (SocketAsyncOperation op)
  159. {
  160. if (disposed)
  161. throw new ObjectDisposedException ("System.Net.Sockets.SocketAsyncEventArgs");
  162. if (Interlocked.Exchange (ref in_progress, 1) != 0)
  163. throw new InvalidOperationException ("Operation already in progress");
  164. LastOperation = op;
  165. }
  166. internal void Complete ()
  167. {
  168. in_progress = 0;
  169. OnCompleted (this);
  170. }
  171. protected virtual void OnCompleted (SocketAsyncEventArgs e)
  172. {
  173. if (e == null)
  174. return;
  175. EventHandler<SocketAsyncEventArgs> handler = e.Completed;
  176. if (handler != null)
  177. handler (e.current_socket, e);
  178. }
  179. public void SetBuffer (int offset, int count)
  180. {
  181. SetBuffer (Buffer, offset, count);
  182. }
  183. public void SetBuffer (byte[] buffer, int offset, int count)
  184. {
  185. if (buffer != null) {
  186. if (BufferList != null)
  187. throw new ArgumentException ("Buffer and BufferList properties cannot both be non-null.");
  188. int buflen = buffer.Length;
  189. if (offset < 0 || (offset != 0 && offset >= buflen))
  190. throw new ArgumentOutOfRangeException ("offset");
  191. if (count < 0 || count > buflen - offset)
  192. throw new ArgumentOutOfRangeException ("count");
  193. Count = count;
  194. Offset = offset;
  195. }
  196. Buffer = buffer;
  197. }
  198. public void SetBuffer(Memory<byte> buffer)
  199. {
  200. SetBuffer(buffer.ToArray(), 0, buffer.Length);
  201. }
  202. internal void StartOperationCommon (Socket socket)
  203. {
  204. current_socket = socket;
  205. }
  206. internal void StartOperationWrapperConnect (MultipleConnectAsync args)
  207. {
  208. SetLastOperation (SocketAsyncOperation.Connect);
  209. //m_MultipleConnect = args;
  210. }
  211. internal void FinishConnectByNameSyncFailure (Exception exception, int bytesTransferred, SocketFlags flags)
  212. {
  213. SetResults (exception, bytesTransferred, flags);
  214. if (current_socket != null)
  215. current_socket.is_connected = false;
  216. Complete ();
  217. }
  218. internal void FinishOperationAsyncFailure (Exception exception, int bytesTransferred, SocketFlags flags)
  219. {
  220. SetResults (exception, bytesTransferred, flags);
  221. if (current_socket != null)
  222. current_socket.is_connected = false;
  223. Complete ();
  224. }
  225. internal void FinishWrapperConnectSuccess (Socket connectSocket, int bytesTransferred, SocketFlags flags)
  226. {
  227. SetResults(SocketError.Success, bytesTransferred, flags);
  228. current_socket = connectSocket;
  229. Complete ();
  230. }
  231. internal void SetResults (SocketError socketError, int bytesTransferred, SocketFlags flags)
  232. {
  233. SocketError = socketError;
  234. ConnectByNameError = null;
  235. BytesTransferred = bytesTransferred;
  236. SocketFlags = flags;
  237. }
  238. internal void SetResults (Exception exception, int bytesTransferred, SocketFlags flags)
  239. {
  240. ConnectByNameError = exception;
  241. BytesTransferred = bytesTransferred;
  242. SocketFlags = flags;
  243. if (exception == null)
  244. {
  245. SocketError = SocketError.Success;
  246. }
  247. else
  248. {
  249. var socketException = exception as SocketException;
  250. if (socketException != null)
  251. SocketError = socketException.SocketErrorCode;
  252. else
  253. SocketError = SocketError.SocketError;
  254. }
  255. }
  256. }
  257. }