Merge pull request #3647 from BrzVlad/fix-sgen-internal-alloc
[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                 public TransmitFileOptions SendPacketsFlags {
115                         get;
116                         set;
117                 }
118
119                 [MonoTODO ("unused property")]
120                 public int SendPacketsSendSize {
121                         get;
122                         set;
123                 }
124
125                 public SocketError SocketError {
126                         get;
127                         set;
128                 }
129
130                 public SocketFlags SocketFlags {
131                         get;
132                         set;
133                 }
134
135                 public object UserToken {
136                         get;
137                         set;
138                 }
139
140                 public Socket ConnectSocket {
141                         get {
142                                 switch (SocketError) {
143                                 case SocketError.AccessDenied:
144                                         return null;
145                                 default:
146                                         return current_socket;
147                                 }
148                         }
149                 }
150
151                 internal bool PolicyRestricted {
152                         get;
153                         private set;
154                 }
155
156                 public event EventHandler<SocketAsyncEventArgs> Completed;
157
158                 internal SocketAsyncEventArgs (bool policy)
159                         : this ()
160                 {
161                         PolicyRestricted = policy;
162                 }
163
164                 public SocketAsyncEventArgs ()
165                 {
166                         SendPacketsSendSize = -1;
167                 }
168
169                 ~SocketAsyncEventArgs ()
170                 {
171                         Dispose (false);
172                 }
173
174                 void Dispose (bool disposing)
175                 {
176                         disposed = true;
177
178                         if (disposing && in_progress != 0)
179                                 return;
180
181                         AcceptSocket = null;
182                         Buffer = null;
183                         BufferList = null;
184                         RemoteEndPoint = null;
185                         UserToken = null;
186                         SendPacketsElements = null;
187                 }
188
189                 public void Dispose ()
190                 {
191                         Dispose (true);
192                         GC.SuppressFinalize (this);
193                 }
194
195                 internal void SetLastOperation (SocketAsyncOperation op)
196                 {
197                         if (disposed)
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");
201
202                         LastOperation = op;
203                 }
204
205                 internal void Complete ()
206                 {
207                         OnCompleted (this);
208                 }
209
210                 protected virtual void OnCompleted (SocketAsyncEventArgs e)
211                 {
212                         if (e == null)
213                                 return;
214                         
215                         EventHandler<SocketAsyncEventArgs> handler = e.Completed;
216                         if (handler != null)
217                                 handler (e.current_socket, e);
218                 }
219
220                 public void SetBuffer (int offset, int count)
221                 {
222                         SetBuffer (Buffer, offset, count);
223                 }
224
225                 public void SetBuffer (byte[] buffer, int offset, int count)
226                 {
227                         if (buffer != null) {
228                                 if (BufferList != null)
229                                         throw new ArgumentException ("Buffer and BufferList properties cannot both be non-null.");
230                                 
231                                 int buflen = buffer.Length;
232                                 if (offset < 0 || (offset != 0 && offset >= buflen))
233                                         throw new ArgumentOutOfRangeException ("offset");
234
235                                 if (count < 0 || count > buflen - offset)
236                                         throw new ArgumentOutOfRangeException ("count");
237
238                                 Count = count;
239                                 Offset = offset;
240                         }
241
242                         Buffer = buffer;
243                 }
244         }
245 }