SocketAsyncEventArgs.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. IList <ArraySegment <byte>> _bufferList;
  57. public IList<ArraySegment<byte>> BufferList {
  58. get { return _bufferList; }
  59. set {
  60. if (Buffer != null && value != null)
  61. throw new ArgumentException ("Buffer and BufferList properties cannot both be non-null.");
  62. _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. #if !NET_2_1
  98. public TransmitFileOptions SendPacketsFlags {
  99. get;
  100. set;
  101. }
  102. #endif
  103. [MonoTODO ("unused property")]
  104. public int SendPacketsSendSize {
  105. get;
  106. set;
  107. }
  108. public SocketError SocketError {
  109. get;
  110. set;
  111. }
  112. public SocketFlags SocketFlags {
  113. get;
  114. set;
  115. }
  116. public object UserToken {
  117. get;
  118. set;
  119. }
  120. public Socket ConnectSocket {
  121. get {
  122. switch (SocketError) {
  123. case SocketError.AccessDenied:
  124. return null;
  125. default:
  126. return current_socket;
  127. }
  128. }
  129. }
  130. internal bool PolicyRestricted {
  131. get;
  132. private set;
  133. }
  134. public event EventHandler<SocketAsyncEventArgs> Completed;
  135. internal SocketAsyncEventArgs (bool policy)
  136. : this ()
  137. {
  138. PolicyRestricted = policy;
  139. }
  140. public SocketAsyncEventArgs ()
  141. {
  142. SendPacketsSendSize = -1;
  143. }
  144. ~SocketAsyncEventArgs ()
  145. {
  146. Dispose (false);
  147. }
  148. void Dispose (bool disposing)
  149. {
  150. disposed = true;
  151. if (disposing && in_progress != 0)
  152. return;
  153. AcceptSocket = null;
  154. Buffer = null;
  155. BufferList = null;
  156. RemoteEndPoint = null;
  157. UserToken = null;
  158. SendPacketsElements = null;
  159. }
  160. public void Dispose ()
  161. {
  162. Dispose (true);
  163. GC.SuppressFinalize (this);
  164. }
  165. internal void SetLastOperation (SocketAsyncOperation op)
  166. {
  167. if (disposed)
  168. throw new ObjectDisposedException ("System.Net.Sockets.SocketAsyncEventArgs");
  169. if (Interlocked.Exchange (ref in_progress, 1) != 0)
  170. throw new InvalidOperationException ("Operation already in progress");
  171. LastOperation = op;
  172. }
  173. internal void Complete ()
  174. {
  175. OnCompleted (this);
  176. }
  177. protected virtual void OnCompleted (SocketAsyncEventArgs e)
  178. {
  179. if (e == null)
  180. return;
  181. EventHandler<SocketAsyncEventArgs> handler = e.Completed;
  182. if (handler != null)
  183. handler (e.current_socket, e);
  184. }
  185. public void SetBuffer (int offset, int count)
  186. {
  187. SetBuffer (Buffer, offset, count);
  188. }
  189. public void SetBuffer (byte[] buffer, int offset, int count)
  190. {
  191. if (buffer != null) {
  192. if (BufferList != null)
  193. throw new ArgumentException ("Buffer and BufferList properties cannot both be non-null.");
  194. int buflen = buffer.Length;
  195. if (offset < 0 || (offset != 0 && offset >= buflen))
  196. throw new ArgumentOutOfRangeException ("offset");
  197. if (count < 0 || count > buflen - offset)
  198. throw new ArgumentOutOfRangeException ("count");
  199. Count = count;
  200. Offset = offset;
  201. }
  202. Buffer = buffer;
  203. }
  204. }
  205. }