1 // System.Net.Sockets.SocketAsyncEventArgs.cs
4 // Marek Habersack (mhabersack@novell.com)
5 // Gonzalo Paniagua Javier (gonzalo@xamarin.com)
7 // Copyright (c) 2008,2010 Novell, Inc. (http://www.novell.com)
8 // Copyright (c) 2011 Xamarin, Inc. (http://xamarin.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections.Generic;
33 using System.Reflection;
34 using System.Security;
35 using System.Threading;
37 namespace System.Net.Sockets
39 public class SocketAsyncEventArgs : EventArgs, IDisposable
43 internal volatile int in_progress;
44 internal EndPoint remote_ep;
45 internal Socket current_socket;
47 internal SocketAsyncResult socket_async_result = new SocketAsyncResult ();
49 public Exception ConnectByNameError {
54 public Socket AcceptSocket {
59 public byte[] Buffer {
64 IList <ArraySegment <byte>> _bufferList;
65 public IList<ArraySegment<byte>> BufferList {
66 get { return _bufferList; }
68 if (Buffer != null && value != null)
69 throw new ArgumentException ("Buffer and BufferList properties cannot both be non-null.");
74 public int BytesTransferred {
84 public bool DisconnectReuseSocket {
89 public SocketAsyncOperation LastOperation {
99 public EndPoint RemoteEndPoint {
100 get { return remote_ep; }
101 set { remote_ep = value; }
104 public IPPacketInformation ReceiveMessageFromPacketInfo {
109 public SendPacketsElement[] SendPacketsElements {
114 public TransmitFileOptions SendPacketsFlags {
119 [MonoTODO ("unused property")]
120 public int SendPacketsSendSize {
125 public SocketError SocketError {
130 public SocketFlags SocketFlags {
135 public object UserToken {
140 public Socket ConnectSocket {
142 switch (SocketError) {
143 case SocketError.AccessDenied:
146 return current_socket;
151 internal bool PolicyRestricted {
156 public event EventHandler<SocketAsyncEventArgs> Completed;
158 internal SocketAsyncEventArgs (bool policy)
161 PolicyRestricted = policy;
164 public SocketAsyncEventArgs ()
166 SendPacketsSendSize = -1;
169 ~SocketAsyncEventArgs ()
174 void Dispose (bool disposing)
178 if (disposing && in_progress != 0)
184 RemoteEndPoint = null;
186 SendPacketsElements = null;
189 public void Dispose ()
192 GC.SuppressFinalize (this);
195 internal void SetLastOperation (SocketAsyncOperation op)
198 throw new ObjectDisposedException ("System.Net.Sockets.SocketAsyncEventArgs");
199 if (Interlocked.Exchange (ref in_progress, 1) != 0)
200 throw new InvalidOperationException ("Operation already in progress");
205 internal void Complete ()
210 protected virtual void OnCompleted (SocketAsyncEventArgs e)
215 EventHandler<SocketAsyncEventArgs> handler = e.Completed;
217 handler (e.current_socket, e);
220 public void SetBuffer (int offset, int count)
222 SetBuffer (Buffer, offset, count);
225 public void SetBuffer (byte[] buffer, int offset, int count)
227 if (buffer != null) {
228 if (BufferList != null)
229 throw new ArgumentException ("Buffer and BufferList properties cannot both be non-null.");
231 int buflen = buffer.Length;
232 if (offset < 0 || (offset != 0 && offset >= buflen))
233 throw new ArgumentOutOfRangeException ("offset");
235 if (count < 0 || count > buflen - offset)
236 throw new ArgumentOutOfRangeException ("count");