[sgen] Untag the vtable during concurrent mark
[mono.git] / mcs / class / System / System.Net.Sockets / SocketAsyncEventArgs.cs
1 // System.Net.Sockets.SocketAsyncEventArgs.cs
2 //
3 // Authors:
4 //      Marek Habersack (mhabersack@novell.com)
5 //      Gonzalo Paniagua Javier (gonzalo@xamarin.com)
6 //
7 // Copyright (c) 2008,2010 Novell, Inc. (http://www.novell.com)
8 // Copyright (c) 2011 Xamarin, Inc. (http://xamarin.com)
9 //
10
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31 using System;
32 using System.Collections.Generic;
33 using System.Reflection;
34 using System.Security;
35 using System.Threading;
36
37 namespace System.Net.Sockets
38 {
39         public class SocketAsyncEventArgs : EventArgs, IDisposable
40         {
41                 bool disposed;
42
43                 internal volatile int in_progress;
44                 internal EndPoint remote_ep;
45                 internal Socket current_socket;
46
47                 internal SocketAsyncResult socket_async_result = new SocketAsyncResult ();
48
49                 public Exception ConnectByNameError {
50                         get;
51                         internal set;
52                 }
53
54                 public Socket AcceptSocket {
55                         get;
56                         set;
57                 }
58
59                 public byte[] Buffer {
60                         get;
61                         private set;
62                 }
63
64                 IList <ArraySegment <byte>> _bufferList;
65                 public IList<ArraySegment<byte>> BufferList {
66                         get { return _bufferList; }
67                         set {
68                                 if (Buffer != null && value != null)
69                                         throw new ArgumentException ("Buffer and BufferList properties cannot both be non-null.");
70                                 _bufferList = value;
71                         }
72                 }
73
74                 public int BytesTransferred {
75                         get;
76                         internal set;
77                 }
78
79                 public int Count {
80                         get;
81                         internal set;
82                 }
83
84                 public bool DisconnectReuseSocket {
85                         get;
86                         set;
87                 }
88
89                 public SocketAsyncOperation LastOperation {
90                         get;
91                         private set;
92                 }
93
94                 public int Offset {
95                         get;
96                         private set;
97                 }
98
99                 public EndPoint RemoteEndPoint {
100                         get { return remote_ep; }
101                         set { remote_ep = value; }
102                 }
103
104                 public IPPacketInformation ReceiveMessageFromPacketInfo {
105                         get;
106                         private set;
107                 }
108
109                 public SendPacketsElement[] SendPacketsElements {
110                         get;
111                         set;
112                 }
113
114 #if !MOBILE
115                 public TransmitFileOptions SendPacketsFlags {
116                         get;
117                         set;
118                 }
119 #endif
120
121                 [MonoTODO ("unused property")]
122                 public int SendPacketsSendSize {
123                         get;
124                         set;
125                 }
126
127                 public SocketError SocketError {
128                         get;
129                         set;
130                 }
131
132                 public SocketFlags SocketFlags {
133                         get;
134                         set;
135                 }
136
137                 public object UserToken {
138                         get;
139                         set;
140                 }
141
142                 public Socket ConnectSocket {
143                         get {
144                                 switch (SocketError) {
145                                 case SocketError.AccessDenied:
146                                         return null;
147                                 default:
148                                         return current_socket;
149                                 }
150                         }
151                 }
152
153                 internal bool PolicyRestricted {
154                         get;
155                         private set;
156                 }
157
158                 public event EventHandler<SocketAsyncEventArgs> Completed;
159
160                 internal SocketAsyncEventArgs (bool policy)
161                         : this ()
162                 {
163                         PolicyRestricted = policy;
164                 }
165
166                 public SocketAsyncEventArgs ()
167                 {
168                         SendPacketsSendSize = -1;
169                 }
170
171                 ~SocketAsyncEventArgs ()
172                 {
173                         Dispose (false);
174                 }
175
176                 void Dispose (bool disposing)
177                 {
178                         disposed = true;
179
180                         if (disposing && in_progress != 0)
181                                 return;
182
183                         AcceptSocket = null;
184                         Buffer = null;
185                         BufferList = null;
186                         RemoteEndPoint = null;
187                         UserToken = null;
188                         SendPacketsElements = null;
189                 }
190
191                 public void Dispose ()
192                 {
193                         Dispose (true);
194                         GC.SuppressFinalize (this);
195                 }
196
197                 internal void SetLastOperation (SocketAsyncOperation op)
198                 {
199                         if (disposed)
200                                 throw new ObjectDisposedException ("System.Net.Sockets.SocketAsyncEventArgs");
201                         if (Interlocked.Exchange (ref in_progress, 1) != 0)
202                                 throw new InvalidOperationException ("Operation already in progress");
203
204                         LastOperation = op;
205                 }
206
207                 internal void Complete ()
208                 {
209                         OnCompleted (this);
210                 }
211
212                 protected virtual void OnCompleted (SocketAsyncEventArgs e)
213                 {
214                         if (e == null)
215                                 return;
216                         
217                         EventHandler<SocketAsyncEventArgs> handler = e.Completed;
218                         if (handler != null)
219                                 handler (e.current_socket, e);
220                 }
221
222                 public void SetBuffer (int offset, int count)
223                 {
224                         SetBuffer (Buffer, offset, count);
225                 }
226
227                 public void SetBuffer (byte[] buffer, int offset, int count)
228                 {
229                         if (buffer != null) {
230                                 if (BufferList != null)
231                                         throw new ArgumentException ("Buffer and BufferList properties cannot both be non-null.");
232                                 
233                                 int buflen = buffer.Length;
234                                 if (offset < 0 || (offset != 0 && offset >= buflen))
235                                         throw new ArgumentOutOfRangeException ("offset");
236
237                                 if (count < 0 || count > buflen - offset)
238                                         throw new ArgumentOutOfRangeException ("count");
239
240                                 Count = count;
241                                 Offset = offset;
242                         }
243
244                         Buffer = buffer;
245                 }
246         }
247 }