Add support on Windows to install atexit handler(s) early in application’s startup.
[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
182                 public void Dispose ()
183                 {
184                         Dispose (true);
185                         GC.SuppressFinalize (this);
186                 }
187
188                 internal void SetLastOperation (SocketAsyncOperation op)
189                 {
190                         if (disposed)
191                                 throw new ObjectDisposedException ("System.Net.Sockets.SocketAsyncEventArgs");
192                         if (Interlocked.Exchange (ref in_progress, 1) != 0)
193                                 throw new InvalidOperationException ("Operation already in progress");
194
195                         LastOperation = op;
196                 }
197
198                 internal void Complete ()
199                 {
200                         OnCompleted (this);
201                 }
202
203                 protected virtual void OnCompleted (SocketAsyncEventArgs e)
204                 {
205                         if (e == null)
206                                 return;
207                         
208                         EventHandler<SocketAsyncEventArgs> handler = e.Completed;
209                         if (handler != null)
210                                 handler (e.current_socket, e);
211                 }
212
213                 public void SetBuffer (int offset, int count)
214                 {
215                         SetBuffer (Buffer, offset, count);
216                 }
217
218                 public void SetBuffer (byte[] buffer, int offset, int count)
219                 {
220                         if (buffer != null) {
221                                 if (BufferList != null)
222                                         throw new ArgumentException ("Buffer and BufferList properties cannot both be non-null.");
223                                 
224                                 int buflen = buffer.Length;
225                                 if (offset < 0 || (offset != 0 && offset >= buflen))
226                                         throw new ArgumentOutOfRangeException ("offset");
227
228                                 if (count < 0 || count > buflen - offset)
229                                         throw new ArgumentOutOfRangeException ("count");
230
231                                 Count = count;
232                                 Offset = offset;
233                         }
234
235                         Buffer = buffer;
236                 }
237         }
238 }