SocketAsyncEventArgs.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. internal IList<ArraySegment<byte>> m_BufferList;
  57. public IList<ArraySegment<byte>> BufferList {
  58. get { return m_BufferList; }
  59. set {
  60. if (Buffer != null && value != null)
  61. throw new ArgumentException ("Buffer and BufferList properties cannot both be non-null.");
  62. m_BufferList = value;
  63. }
  64. }
  65. public int BytesTransferred {
  66. get;
  67. internal set;
  68. }
  69. public int Count {
  70. get;
  71. internal set;
  72. }
  73. public bool DisconnectReuseSocket {
  74. get;
  75. set;
  76. }
  77. public SocketAsyncOperation LastOperation {
  78. get;
  79. private set;
  80. }
  81. public int Offset {
  82. get;
  83. private set;
  84. }
  85. public EndPoint RemoteEndPoint {
  86. get { return remote_ep; }
  87. set { remote_ep = value; }
  88. }
  89. public IPPacketInformation ReceiveMessageFromPacketInfo {
  90. get;
  91. private set;
  92. }
  93. public SendPacketsElement[] SendPacketsElements {
  94. get;
  95. set;
  96. }
  97. public TransmitFileOptions SendPacketsFlags {
  98. get;
  99. set;
  100. }
  101. [MonoTODO ("unused property")]
  102. public int SendPacketsSendSize {
  103. get;
  104. set;
  105. }
  106. public SocketError SocketError {
  107. get;
  108. set;
  109. }
  110. public SocketFlags SocketFlags {
  111. get;
  112. set;
  113. }
  114. public object UserToken {
  115. get;
  116. set;
  117. }
  118. public Socket ConnectSocket {
  119. get {
  120. switch (SocketError) {
  121. case SocketError.AccessDenied:
  122. return null;
  123. default:
  124. return current_socket;
  125. }
  126. }
  127. }
  128. internal bool PolicyRestricted {
  129. get;
  130. private set;
  131. }
  132. public event EventHandler<SocketAsyncEventArgs> Completed;
  133. internal SocketAsyncEventArgs (bool policy)
  134. : this ()
  135. {
  136. PolicyRestricted = policy;
  137. }
  138. public SocketAsyncEventArgs ()
  139. {
  140. SendPacketsSendSize = -1;
  141. }
  142. ~SocketAsyncEventArgs ()
  143. {
  144. Dispose (false);
  145. }
  146. void Dispose (bool disposing)
  147. {
  148. disposed = true;
  149. if (disposing && in_progress != 0)
  150. return;
  151. }
  152. public void Dispose ()
  153. {
  154. Dispose (true);
  155. GC.SuppressFinalize (this);
  156. }
  157. internal void SetLastOperation (SocketAsyncOperation op)
  158. {
  159. if (disposed)
  160. throw new ObjectDisposedException ("System.Net.Sockets.SocketAsyncEventArgs");
  161. if (Interlocked.Exchange (ref in_progress, 1) != 0)
  162. throw new InvalidOperationException ("Operation already in progress");
  163. LastOperation = op;
  164. }
  165. internal void Complete ()
  166. {
  167. OnCompleted (this);
  168. }
  169. protected virtual void OnCompleted (SocketAsyncEventArgs e)
  170. {
  171. if (e == null)
  172. return;
  173. EventHandler<SocketAsyncEventArgs> handler = e.Completed;
  174. if (handler != null)
  175. handler (e.current_socket, e);
  176. }
  177. public void SetBuffer (int offset, int count)
  178. {
  179. SetBuffer (Buffer, offset, count);
  180. }
  181. public void SetBuffer (byte[] buffer, int offset, int count)
  182. {
  183. if (buffer != null) {
  184. if (BufferList != null)
  185. throw new ArgumentException ("Buffer and BufferList properties cannot both be non-null.");
  186. int buflen = buffer.Length;
  187. if (offset < 0 || (offset != 0 && offset >= buflen))
  188. throw new ArgumentOutOfRangeException ("offset");
  189. if (count < 0 || count > buflen - offset)
  190. throw new ArgumentOutOfRangeException ("count");
  191. Count = count;
  192. Offset = offset;
  193. }
  194. Buffer = buffer;
  195. }
  196. internal void StartOperationCommon (Socket socket)
  197. {
  198. current_socket = socket;
  199. }
  200. internal void StartOperationWrapperConnect (MultipleConnectAsync args)
  201. {
  202. SetLastOperation (SocketAsyncOperation.Connect);
  203. //m_MultipleConnect = args;
  204. }
  205. internal void FinishConnectByNameSyncFailure (Exception exception, int bytesTransferred, SocketFlags flags)
  206. {
  207. throw new NotImplementedException ();
  208. }
  209. internal void FinishOperationAsyncFailure (Exception exception, int bytesTransferred, SocketFlags flags)
  210. {
  211. throw new NotImplementedException ();
  212. }
  213. internal void FinishWrapperConnectSuccess (Socket connectSocket, int bytesTransferred, SocketFlags flags)
  214. {
  215. SetResults(SocketError.Success, bytesTransferred, flags);
  216. current_socket = connectSocket;
  217. OnCompleted (this);
  218. }
  219. internal void SetResults (SocketError socketError, int bytesTransferred, SocketFlags flags)
  220. {
  221. SocketError = socketError;
  222. BytesTransferred = bytesTransferred;
  223. SocketFlags = flags;
  224. }
  225. }
  226. }