Author: rfm
Date: Fri Mar  4 19:53:32 2016
New Revision: 39455

URL: http://svn.gna.org/viewcvs/gnustep?rev=39455&view=rev
Log:
various windows networking fixes

Modified:
    libs/base/trunk/ChangeLog
    libs/base/trunk/Source/win32/GSFileHandle.m

Modified: libs/base/trunk/ChangeLog
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/ChangeLog?rev=39455&r1=39454&r2=39455&view=diff
==============================================================================
--- libs/base/trunk/ChangeLog   (original)
+++ libs/base/trunk/ChangeLog   Fri Mar  4 19:53:32 2016
@@ -8,6 +8,7 @@
        * macosx\GNUstepBase\preface.h:
        Standardise on using _WIN32 and _WIN64 defines, following the
        informal convention used by all the compilers we might be compiled with.
+       * Source/win32/GSFileHandle.m: Various network/file bugfixes.
 
 2016-03-01  Richard Frith-Macdonald <[email protected]>
 

Modified: libs/base/trunk/Source/win32/GSFileHandle.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Source/win32/GSFileHandle.m?rev=39455&r1=39454&r2=39455&view=diff
==============================================================================
--- libs/base/trunk/Source/win32/GSFileHandle.m (original)
+++ libs/base/trunk/Source/win32/GSFileHandle.m Fri Mar  4 19:53:32 2016
@@ -300,7 +300,10 @@
              WSACloseEvent(event);
              event = WSA_INVALID_EVENT;
             }
-         close(descriptor);
+         else
+           {
+             close(descriptor);
+           }
          descriptor = -1;
        }
     }
@@ -1288,10 +1291,11 @@
 
 // Synchronous I/O operations
 
+#if 0
 - (NSData*) availableData
 {
   char                 buf[READ_SIZE];
-  NSMutableData*       d;
+  SMutableData*        d;
   int                  len;
 
   [self checkRead];
@@ -1310,6 +1314,76 @@
   else
     {
       len = [self read: buf length: sizeof(buf)];
+      if (len > 0)
+       {
+         [d appendBytes: buf length: len];
+       }
+    }
+  if (len < 0)
+    {
+      [NSException raise: NSFileHandleOperationException
+                  format: @"unable to read from descriptor - %@",
+                  [NSError _last]];
+    }
+  return d;
+}
+#endif
+- (NSData*) availableData
+{
+  char                 buf[READ_SIZE];
+  NSMutableData*       d;
+  int                  len;
+
+  [self checkRead];
+  d = [NSMutableData dataWithCapacity: 0];
+  if (isStandardFile)
+    {
+      if (isNonBlocking == YES)
+       {
+         [self setNonBlocking: NO];
+       }
+      while ((len = [self read: buf length: sizeof(buf)]) > 0)
+        {
+         [d appendBytes: buf length: len];
+        }
+    }
+  else
+    {
+      if (isNonBlocking == NO)
+       {
+         [self setNonBlocking: YES];
+       }
+      len = [self read: buf length: sizeof(buf)];
+
+      if (len <= 0)
+       {
+          if (WSAGetLastError()== WSAEINTR
+           || WSAGetLastError()== WSAEWOULDBLOCK)
+           {
+             /*
+              * Read would have blocked ... so try to get a single character
+              * in non-blocking mode (to ensure we wait until data arrives)
+              * and then try again.
+              * This ensures that we block for *some* data as we should.
+              */
+             [self setNonBlocking: NO];
+             len = [self read: buf length: 1];
+             [self setNonBlocking: YES];
+             if (len == 1)
+               {
+                 len = [self read: &buf[1] length: sizeof(buf) - 1];
+                 if (len <= 0)
+                   {
+                     len = 1;
+                   }
+                 else
+                   {
+                     len = len + 1;
+                   }
+               }
+           }
+       }
+
       if (len > 0)
        {
          [d appendBytes: buf length: len];
@@ -2330,6 +2404,10 @@
 
 - (void) setNonBlocking: (BOOL)flag
 {
+  if (flag == isNonBlocking)
+    {
+      return;
+    }
   if (descriptor < 0)
     {
       return;
@@ -2350,7 +2428,7 @@
         {                        // Not a file and not a socket, must be a pipe
           DWORD mode;
 
-          if (flag)
+          if (YES == flag)
             mode = PIPE_NOWAIT;
           else
             mode = PIPE_WAIT;
@@ -2361,34 +2439,39 @@
             }
           else
             {
-             NSLog(@"unable to set pipe non-blocking mode - %d",
-               GetLastError());
+             NSLog(@"unable to set pipe non-blocking mode to %s - %d",
+               (YES  == flag ? "YES" : "NO"), GetLastError());
             }
           return;
         }
 
-      if (flag)
-       {
+      if (YES == flag)
+       {
+         WSAEventSelect((SOCKET)_get_osfhandle(descriptor), event,
+           FD_ALL_EVENTS);
          dummy = 1;
          if (ioctlsocket((SOCKET)_get_osfhandle(descriptor), FIONBIO, &dummy)
            == SOCKET_ERROR)
            {
-             NSLog(@"unable to set non-blocking mode - %@",
+             NSLog(@"unable to set non-blocking mode to YES - %@",
                [NSError _last]);
            }
          else
-           isNonBlocking = flag;
+           {
+             isNonBlocking = flag;
+           }
        }
       else
        {
+         WSAEventSelect((SOCKET)_get_osfhandle(descriptor), event, 0);
          dummy = 0;
          if (ioctlsocket((SOCKET)_get_osfhandle(descriptor), FIONBIO, &dummy)
            == SOCKET_ERROR)
            {
-             NSLog(@"unable to set blocking mode - %@", [NSError _last]);
-           }
-         else
-           isNonBlocking = flag;
+             NSLog(@"unable to set blocking mode to NO - %@",
+               [NSError _last]);
+           }
+          isNonBlocking = flag;
        }
     }
 }


_______________________________________________
Gnustep-cvs mailing list
[email protected]
https://mail.gna.org/listinfo/gnustep-cvs

Reply via email to