Finished making the changes.
[mono.git] / mcs / class / System / System.Net.Sockets / Socket.cs
index 53ed16251f97221b2153653d6d9fd1917bf0dcc8..78aae7ddeaf83a3ab62e040a2645788c783bed5d 100644 (file)
@@ -523,7 +523,13 @@ namespace System.Net.Sockets
                        e.curSocket = this;
                        Worker w = e.Worker;
                        w.Init (this, e, SocketOperation.Accept);
-                       socket_pool_queue (Worker.Dispatcher, w.result);
+                       int count;
+                       lock (readQ) {
+                               readQ.Enqueue (e.Worker);
+                               count = readQ.Count;
+                       }
+                       if (count == 1)
+                               socket_pool_queue (Worker.Dispatcher, w.result);
                        return true;
                }
 #endif
@@ -605,7 +611,13 @@ namespace System.Net.Sockets
                                throw new InvalidOperationException ();
 
                        SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.Accept);
-                       socket_pool_queue (Worker.Dispatcher, req);
+                       int count;
+                       lock (readQ) {
+                               readQ.Enqueue (req.Worker);
+                               count = readQ.Count;
+                       }
+                       if (count == 1)
+                               socket_pool_queue (Worker.Dispatcher, req);
                        return req;
                }
 
@@ -624,7 +636,13 @@ namespace System.Net.Sockets
                        req.Offset = 0;
                        req.Size = receiveSize;
                        req.SockFlags = SocketFlags.None;
-                       socket_pool_queue (Worker.Dispatcher, req);
+                       int count;
+                       lock (readQ) {
+                               readQ.Enqueue (req.Worker);
+                               count = readQ.Count;
+                       }
+                       if (count == 1)
+                               socket_pool_queue (Worker.Dispatcher, req);
                        return req;
                }
 
@@ -661,7 +679,13 @@ namespace System.Net.Sockets
                        req.Size = receiveSize;
                        req.SockFlags = SocketFlags.None;
                        req.AcceptSocket = acceptSocket;
-                       socket_pool_queue (Worker.Dispatcher, req);
+                       int count;
+                       lock (readQ) {
+                               readQ.Enqueue (req.Worker);
+                               count = readQ.Count;
+                       }
+                       if (count == 1)
+                               socket_pool_queue (Worker.Dispatcher, req);
                        return(req);
                }
 
@@ -723,6 +747,15 @@ namespace System.Net.Sockets
                        socket_pool_queue (Worker.Dispatcher, req);
                        return(req);
                }
+
+               void CheckRange(byte[] buffer, int offset, int size)
+               {
+                       if (offset < 0 || offset > buffer.Length)
+                               throw new ArgumentOutOfRangeException ("offset");
+
+                       if (size < 0 || size > buffer.Length - offset)
+                               throw new ArgumentOutOfRangeException ("size");
+               }
                
                public IAsyncResult BeginReceive(byte[] buffer, int offset,
                                                 int size,
@@ -736,11 +769,7 @@ namespace System.Net.Sockets
                        if (buffer == null)
                                throw new ArgumentNullException ("buffer");
 
-                       if (offset < 0 || offset > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("offset");
-
-                       if (size < 0 || offset + size > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("size");
+                       CheckRange(buffer, offset, size);
 
                        SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.Receive);
                        req.Buffer = buffer;
@@ -828,14 +857,7 @@ namespace System.Net.Sockets
                        if (remote_end == null)
                                throw new ArgumentNullException ("remote_end");
 
-                       if (offset < 0)
-                               throw new ArgumentOutOfRangeException ("offset", "offset must be >= 0");
-
-                       if (size < 0)
-                               throw new ArgumentOutOfRangeException ("size", "size must be >= 0");
-
-                       if (offset + size > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("offset, size", "offset + size exceeds the buffer length");
+                       CheckRange (buffer, offset, size);
 
                        SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.ReceiveFrom);
                        req.Buffer = buffer;
@@ -868,11 +890,7 @@ namespace System.Net.Sockets
                        if (remoteEP == null)
                                throw new ArgumentNullException ("remoteEP");
 
-                       if (offset < 0 || offset > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("offset");
-
-                       if (size < 0 || offset + size > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("size");
+                       CheckRange (bufer, offset, size);
 
                        throw new NotImplementedException ();
                }
@@ -886,14 +904,7 @@ namespace System.Net.Sockets
                        if (buffer == null)
                                throw new ArgumentNullException ("buffer");
 
-                       if (offset < 0)
-                               throw new ArgumentOutOfRangeException ("offset", "offset must be >= 0");
-
-                       if (size < 0)
-                               throw new ArgumentOutOfRangeException ("size", "size must be >= 0");
-
-                       if (offset + size > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("offset, size", "offset + size exceeds the buffer length");
+                       CheckRange (buffer, offset, size);
 
                        if (!connected)
                                throw new SocketException ((int)SocketError.NotConnected);
@@ -1059,14 +1070,7 @@ namespace System.Net.Sockets
                        if (buffer == null)
                                throw new ArgumentNullException ("buffer");
 
-                       if (offset < 0)
-                               throw new ArgumentOutOfRangeException ("offset", "offset must be >= 0");
-
-                       if (size < 0)
-                               throw new ArgumentOutOfRangeException ("size", "size must be >= 0");
-
-                       if (offset + size > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("offset, size", "offset + size exceeds the buffer length");
+                       CheckRange (buffer, offset, size);
 
                        SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.SendTo);
                        req.Buffer = buffer;
@@ -1539,8 +1543,7 @@ namespace System.Net.Sockets
                        if (buffer == null)
                                throw new ArgumentNullException ("buffer");
 
-                       if (size < 0 || size > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("size");
+                       CheckRange (buffer, 0, size);
 
                        SocketError error;
 
@@ -1563,11 +1566,7 @@ namespace System.Net.Sockets
                        if (buffer == null)
                                throw new ArgumentNullException ("buffer");
 
-                       if (offset < 0 || offset > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("offset");
-
-                       if (size < 0 || offset + size > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("size");
+                       CheckRange (buffer, offset, size);
                        
                        SocketError error;
 
@@ -1590,11 +1589,7 @@ namespace System.Net.Sockets
                        if (buffer == null)
                                throw new ArgumentNullException ("buffer");
 
-                       if (offset < 0 || offset > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("offset");
-
-                       if (size < 0 || offset + size > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("size");
+                       CheckRange (buffer, offset, size);
                        
                        return Receive_nochecks (buffer, offset, size, flags, out error);
                }
@@ -1698,11 +1693,7 @@ namespace System.Net.Sockets
                        if (remoteEP == null)
                                throw new ArgumentNullException ("remoteEP");
 
-                       if (offset < 0 || offset > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("offset");
-
-                       if (size < 0 || offset + size > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("size");
+                       CheckRange (buffer, offset, size);
 
                        return ReceiveFrom_nochecks (buffer, offset, size, flags, ref remoteEP);
                }
@@ -1780,11 +1771,7 @@ namespace System.Net.Sockets
                        if (remoteEP == null)
                                throw new ArgumentNullException ("remoteEP");
 
-                       if (offset < 0 || offset > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("offset");
-
-                       if (size < 0 || offset + size > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("size");
+                       CheckRange (buffer, offset, size);
 
                        /* FIXME: figure out how we get hold of the
                         * IPPacketInformation
@@ -1847,8 +1834,7 @@ namespace System.Net.Sockets
                        if (buf == null)
                                throw new ArgumentNullException ("buf");
 
-                       if (size < 0 || size > buf.Length)
-                               throw new ArgumentOutOfRangeException ("size");
+                       CheckRange (buf, 0, size);
 
                        SocketError error;
 
@@ -1868,11 +1854,7 @@ namespace System.Net.Sockets
                        if (buf == null)
                                throw new ArgumentNullException ("buffer");
 
-                       if (offset < 0 || offset > buf.Length)
-                               throw new ArgumentOutOfRangeException ("offset");
-
-                       if (size < 0 || offset + size > buf.Length)
-                               throw new ArgumentOutOfRangeException ("size");
+                       CheckRange (buf, offset, size);
 
                        SocketError error;
 
@@ -1892,11 +1874,7 @@ namespace System.Net.Sockets
                        if (buf == null)
                                throw new ArgumentNullException ("buffer");
 
-                       if (offset < 0 || offset > buf.Length)
-                               throw new ArgumentOutOfRangeException ("offset");
-
-                       if (size < 0 || offset + size > buf.Length)
-                               throw new ArgumentOutOfRangeException ("size");
+                       CheckRange (buf, offset, size);
 
                        return Send_nochecks (buf, offset, size, flags, out error);
                }
@@ -2008,8 +1986,7 @@ namespace System.Net.Sockets
                        if (remote_end == null)
                                throw new ArgumentNullException ("remote_end");
 
-                       if (size < 0 || size > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("size");
+                       CheckRange (buffer, 0, size);
 
                        return SendTo_nochecks (buffer, 0, size, flags, remote_end);
                }
@@ -2035,11 +2012,7 @@ namespace System.Net.Sockets
                        if (remote_end == null)
                                throw new ArgumentNullException("remote_end");
 
-                       if (offset < 0 || offset > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("offset");
-
-                       if (size < 0 || offset + size > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("size");
+                       CheckRange (buffer, offset, size);
 
                        return SendTo_nochecks (buffer, offset, size, flags, remote_end);
                }