SocketAsyncEventArgs.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. 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. AcceptSocket = null;
  152. Buffer = null;
  153. BufferList = null;
  154. RemoteEndPoint = null;
  155. UserToken = null;
  156. SendPacketsElements = null;
  157. }
  158. public void Dispose ()
  159. {
  160. Dispose (true);
  161. GC.SuppressFinalize (this);
  162. }
  163. internal void SetLastOperation (SocketAsyncOperation op)
  164. {
  165. if (disposed)
  166. throw new ObjectDisposedException ("System.Net.Sockets.SocketAsyncEventArgs");
  167. if (Interlocked.Exchange (ref in_progress, 1) != 0)
  168. throw new InvalidOperationException ("Operation already in progress");
  169. LastOperation = op;
  170. }
  171. internal void Complete ()
  172. {
  173. OnCompleted (this);
  174. }
  175. protected virtual void OnCompleted (SocketAsyncEventArgs e)
  176. {
  177. if (e == null)
  178. return;
  179. EventHandler<SocketAsyncEventArgs> handler = e.Completed;
  180. if (handler != null)
  181. handler (e.current_socket, e);
  182. }
  183. public void SetBuffer (int offset, int count)
  184. {
  185. SetBuffer (Buffer, offset, count);
  186. }
  187. public void SetBuffer (byte[] buffer, int offset, int count)
  188. {
  189. if (buffer != null) {
  190. if (BufferList != null)
  191. throw new ArgumentException ("Buffer and BufferList properties cannot both be non-null.");
  192. int buflen = buffer.Length;
  193. if (offset < 0 || (offset != 0 && offset >= buflen))
  194. throw new ArgumentOutOfRangeException ("offset");
  195. if (count < 0 || count > buflen - offset)
  196. throw new ArgumentOutOfRangeException ("count");
  197. Count = count;
  198. Offset = offset;
  199. }
  200. Buffer = buffer;
  201. }
  202. }
  203. }