2003-02-16 Gonzalo Paniagua Javier <gonzalo@ximian.com>
authorGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Sun, 16 Feb 2003 22:13:26 +0000 (22:13 -0000)
committerGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Sun, 16 Feb 2003 22:13:26 +0000 (22:13 -0000)
* Socket.cs: make Worker.Connect and Receive work with non-blocking
sockets. May be Receive* and Send* in Worker need to do the same. I'll
wait for bug reports. Set IsCompleted to true before invoking the end
callback. Fixes bug #38136.

svn path=/trunk/mcs/; revision=11629

mcs/class/System/System.Net.Sockets/ChangeLog
mcs/class/System/System.Net.Sockets/Socket.cs

index 22960f131ab2fa6b68058f243c432f21f9417847..b34f3138f960026418e71aec7201c6b15f01948d 100644 (file)
@@ -1,3 +1,10 @@
+2003-02-16  Gonzalo Paniagua Javier <gonzalo@ximian.com>
+
+       * Socket.cs: make Worker.Connect and Receive work with non-blocking
+       sockets. May be Receive* and Send* in Worker need to do the same. I'll
+       wait for bug reports. Set IsCompleted to true before invoking the end
+       callback. Fixes bug #38136.
+
 2003-01-23  Gonzalo Paniagua Javier <gonzalo@ximian.com>
 
        * NetworkStream.cs: the check for disposed should not be done in the
index 36086ef24b50af0decdcb861831ce62cf53a6943..f430be427ac8ea5e7a6284900caa868da13a8cfd 100644 (file)
@@ -133,8 +133,8 @@ namespace System.Net.Sockets
 
                        private void End() {
                                ((ManualResetEvent)result.AsyncWaitHandle).Set();
-                               callback(result);
                                result.IsCompleted=true;
+                               callback(result);
                        }
                        
                        public void Accept() {
@@ -146,16 +146,60 @@ namespace System.Net.Sockets
 
                        public void Connect() {
                                lock(result) {
-                                       socket.Connect(endpoint);
-                                       End();
+                                       if (socket.Blocking) {
+                                               socket.Connect(endpoint);
+                                               End ();
+                                               return;
+                                       }
+
+                                       SocketException rethrow = null;
+                                       try {
+                                               socket.Connect (endpoint);
+                                       } catch (SocketException e) {
+                                               //WSAEINPROGRESS
+                                               if (e.NativeErrorCode != 10036)
+                                                       throw;
+
+                                               socket.Poll (-1, SelectMode.SelectWrite);
+                                               try {
+                                                       socket.Connect (endpoint);
+                                               } catch (SocketException e2) {
+                                                       rethrow = e2;
+                                               }
+                                       }
+                                       End ();
+                                       if (rethrow != null)
+                                               throw rethrow;
                                }
                        }
 
                        public void Receive() {
                                lock(result) {
-                                       total=socket.Receive(buffer, offset,
-                                                            size, sockflags);
-                                       End();
+                                       if (socket.Blocking) {
+                                               total=socket.Receive(buffer, offset,
+                                                                    size, sockflags);
+                                               End();
+                                               return;
+                                       }
+
+                                       SocketException rethrow = null;
+                                       try {
+                                               total = socket.Receive (buffer, offset, size, sockflags);
+                                       } catch (SocketException e) {
+                                               //WSAEWOULDBLOCK
+                                               if (e.NativeErrorCode != 10035)
+                                                       throw;
+
+                                               socket.Poll (-1, SelectMode.SelectRead);
+                                               try {
+                                                       total = socket.Receive (buffer, offset, size, sockflags);
+                                               } catch (SocketException e2) {
+                                                       rethrow = e2;
+                                               }
+                                       }
+                                       End ();
+                                       if (rethrow != null)
+                                               throw rethrow;
                                }
                        }