[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
NSFileHandle/socket don't work for windows in gnustep-base 1.10.0
From: |
andre levy |
Subject: |
NSFileHandle/socket don't work for windows in gnustep-base 1.10.0 |
Date: |
Mon, 13 Sep 2004 18:17:15 +0200 |
User-agent: |
Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.6) Gecko/20040113 |
trying to use socket with NSFileHandle in windows, NSFileHandle
repeatedly told me that the sockets I created are not valid.
the same piece of code used to work with gnustep-base-1.3.2.
looking at the code of GSFileHandle.m I discovered that it goes a long
way to turn file descriptors created with a 'socket' call into a "file
handle" (whatever that means in windows) using '_get_osfhandle'.
looking at windows documentation, it seems to me that there is no common
set of API between files and sockets at least at the libc layer.
therefore every call related to sockets expect a file descriptor and the
API is almost the same as BSD sockets. (with the notable exception of
closesocket and ioctlsocket which could not be shared with file handling
obviously!)
there is a discussion on this point in
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/porting_socket_applications_to_winsock.asp
and following. (do I need to say that it's easier to look at using IE?
you get the whole discussion at a glance...)
However, I did a simplification on the code which works for me. It is
not fully tested yet but the beginning is encouraging.
I don't know which format is preferred for the modification, therefore I
send a diff and a patch. (and if this is not enough, i am willing to
send the source file!)
Thanks a lot for your comments.
--andre
Index: Source/GSFileHandle.m
===================================================================
RCS file: /cvsroot/gnustep/gnustep/core/base/Source/GSFileHandle.m,v
retrieving revision 1.16
diff -u -r1.16 GSFileHandle.m
--- Source/GSFileHandle.m 18 Aug 2004 10:12:13 -0000 1.16
+++ Source/GSFileHandle.m 13 Sep 2004 16:08:02 -0000
@@ -126,11 +126,7 @@
#endif
if (isSocket)
{
-#if defined(__MINGW__)
- len = recv((SOCKET)_get_osfhandle(descriptor), buf, len, 0);
-#else
len = recv(descriptor, buf, len, 0);
-#endif
}
else
{
@@ -154,11 +150,7 @@
#endif
if (isSocket)
{
-#if defined(__MINGW__)
- len = send((SOCKET)_get_osfhandle(descriptor), buf, len, 0);
-#else
len = send(descriptor, buf, len, 0);
-#endif
}
else
{
@@ -298,7 +290,7 @@
#if defined(__MINGW__)
if (isSocket)
{
- closesocket((SOCKET)_get_osfhandle(descriptor));
+ closesocket(descriptor);
}
#endif
close(descriptor);
@@ -836,11 +828,7 @@
}
}
-#if defined(__MINGW__)
- self = [self initWithNativeHandle: (void*)net closeOnDealloc: YES];
-#else
self = [self initWithFileDescriptor: net closeOnDealloc: YES];
-#endif
if (self)
{
NSMutableDictionary* info;
@@ -973,11 +961,7 @@
return nil;
}
-#if defined(__MINGW__)
- self = [self initWithNativeHandle: (void*)net closeOnDealloc: YES];
-#else
self = [self initWithFileDescriptor: net closeOnDealloc: YES];
-#endif
if (self)
{
isSocket = YES;
@@ -1145,14 +1129,20 @@
#endif
#if defined(__MINGW__)
- if (_fstat(desc, &sbuf) != 0)
+ // _fstat only knows about files.
+ // socket descriptor is beyond its grasp.
+ // see:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_file_handling.asp
+ if (_fstat(desc, &sbuf) < 0)
+ {
+ isStandardFile = NO;
+ }
#else
if (fstat(desc, &sbuf) < 0)
-#endif
{
NSLog(@"unable to get status of descriptor %d - %s",
desc, GSLastErrorStr(errno));
}
+#endif
else
{
if (S_ISREG(sbuf.st_mode))
@@ -1175,7 +1165,7 @@
* This is probably a socket ... try
* using a socket specific call and see if that fails.
*/
- if (ioctlsocket((SOCKET)_get_osfhandle(desc), FIONBIO, &nbio) == 0)
+ if (ioctlsocket((SOCKET)desc, FIONBIO, &nbio) == 0)
{
wasNonBlocking = (nbio == 0) ? NO : YES;
}
@@ -1689,7 +1679,7 @@
#if defined(__MINGW__)
if (isSocket)
{
- (void)closesocket((SOCKET)_get_osfhandle(descriptor));
+ (void)closesocket(descriptor);
}
#endif
(void)close(descriptor);
@@ -1735,14 +1725,15 @@
- (void) truncateFileAtOffset: (unsigned long long)pos
{
-#if defined(__MINGW__)
- _chsize(descriptor, pos);
-#else
if (isStandardFile && descriptor >= 0)
{
+#if defined(__MINGW__)
+ _chsize(descriptor, pos);
+#else
(void)ftruncate(descriptor, pos);
- }
#endif
+ }
+
[self seekToFileOffset: pos];
}
@@ -1865,32 +1856,18 @@
for (i = 0; i < [modes count]; i++)
{
-#if defined(__MINGW__)
- [l removeEvent: (void*)(gsaddr)(SOCKET)_get_osfhandle(descriptor)
- type: ET_RDESC
- forMode: [modes objectAtIndex: i]
- all: YES];
-#else
[l removeEvent: (void*)(gsaddr)descriptor
type: ET_RDESC
forMode: [modes objectAtIndex: i]
all: YES];
-#endif
}
}
else
{
-#if defined(__MINGW__)
- [l removeEvent: (void*)(gsaddr)(SOCKET)_get_osfhandle(descriptor)
- type: ET_RDESC
- forMode: NSDefaultRunLoopMode
- all: YES];
-#else
[l removeEvent: (void*)(gsaddr)descriptor
type: ET_RDESC
forMode: NSDefaultRunLoopMode
all: YES];
-#endif
}
}
@@ -1919,32 +1896,18 @@
for (i = 0; i < [modes count]; i++)
{
-#if defined(__MINGW__)
- [l removeEvent: (void*)(gsaddr)(SOCKET)_get_osfhandle(descriptor)
- type: ET_WDESC
- forMode: [modes objectAtIndex: i]
- all: YES];
-#else
[l removeEvent: (void*)(gsaddr)descriptor
type: ET_WDESC
forMode: [modes objectAtIndex: i]
all: YES];
-#endif
}
}
else
{
-#if defined(__MINGW__)
- [l removeEvent: (void*)(gsaddr)(SOCKET)_get_osfhandle(descriptor)
- type: ET_WDESC
- forMode: NSDefaultRunLoopMode
- all: YES];
-#else
[l removeEvent: (void*)(gsaddr)descriptor
type: ET_WDESC
forMode: NSDefaultRunLoopMode
all: YES];
-#endif
}
}
@@ -1964,33 +1927,19 @@
for (i = 0; i < [modes count]; i++)
{
-#if defined(__MINGW__)
- [l addEvent: (void*)(gsaddr)(SOCKET)_get_osfhandle(descriptor)
- type: ET_RDESC
- watcher: self
- forMode: [modes objectAtIndex: i]];
-#else
[l addEvent: (void*)(gsaddr)descriptor
type: ET_RDESC
watcher: self
forMode: [modes objectAtIndex: i]];
-#endif
}
[readInfo setObject: modes forKey: NSFileHandleNotificationMonitorModes];
}
else
{
-#if defined(__MINGW__)
- [l addEvent: (void*)(gsaddr)(SOCKET)_get_osfhandle(descriptor)
- type: ET_RDESC
- watcher: self
- forMode: NSDefaultRunLoopMode];
-#else
[l addEvent: (void*)(gsaddr)descriptor
type: ET_RDESC
watcher: self
forMode: NSDefaultRunLoopMode];
-#endif
}
}
@@ -2015,32 +1964,18 @@
for (i = 0; i < [modes count]; i++)
{
-#if defined(__MINGW__)
- [l addEvent: (void*)(gsaddr)(SOCKET)_get_osfhandle(descriptor)
- type: ET_WDESC
- watcher: self
- forMode: [modes objectAtIndex: i]];
-#else
[l addEvent: (void*)(gsaddr)descriptor
type: ET_WDESC
watcher: self
forMode: [modes objectAtIndex: i]];
-#endif
}
}
else
{
-#if defined(__MINGW__)
- [l addEvent: (void*)(gsaddr)(SOCKET)_get_osfhandle(descriptor)
- type: ET_WDESC
- watcher: self
- forMode: NSDefaultRunLoopMode];
-#else
[l addEvent: (void*)(gsaddr)descriptor
type: ET_WDESC
watcher: self
forMode: NSDefaultRunLoopMode];
-#endif
}
}
}
@@ -2065,18 +2000,10 @@
if (operation == NSFileHandleConnectionAcceptedNotification)
{
struct sockaddr_in buf;
-#if defined(__MINGW__)
SOCKET desc;
-#else
- int desc;
-#endif
int blen = sizeof(buf);
-#if defined(__MINGW__)
- desc = accept((SOCKET)_get_osfhandle(descriptor), (struct
sockaddr*)&buf, &blen);
-#else
desc = accept(descriptor, (struct sockaddr*)&buf, &blen);
-#endif
if (desc == INVALID_SOCKET)
{
NSString *s;
@@ -2091,13 +2018,8 @@
struct sockaddr_in sin;
int size = sizeof(sin);
-#if defined(__MINGW__)
- h = [[[self class] alloc] initWithNativeHandle: (void*)desc
- closeOnDealloc: YES];
-#else
h = [[[self class] alloc] initWithFileDescriptor: desc
closeOnDealloc: YES];
-#endif
h->isSocket = YES;
getpeername(desc, (struct sockaddr*)&sin, &size);
[h setAddr: &sin];
@@ -2175,13 +2097,8 @@
int result;
int len = sizeof(result);
-#if defined(__MINGW__)
- if (getsockopt((SOCKET)_get_osfhandle(descriptor), SOL_SOCKET,
SO_ERROR,
- (char*)&result, &len) == 0 && result != 0)
-#else
if (getsockopt(descriptor, SOL_SOCKET, SO_ERROR,
(char*)&result, &len) == 0 && result != 0)
-#endif
{
NSString *s;
@@ -2277,7 +2194,7 @@
if (flag)
{
dummy = 1;
- if (ioctlsocket((SOCKET)_get_osfhandle(descriptor), FIONBIO, &dummy)
+ if (ioctlsocket(descriptor, FIONBIO, &dummy)
== SOCKET_ERROR)
{
NSLog(@"unable to set non-blocking mode - %s",
@@ -2287,7 +2204,7 @@
else
{
dummy = 0;
- if (ioctlsocket((SOCKET)_get_osfhandle(descriptor), FIONBIO, &dummy)
+ if (ioctlsocket(descriptor, FIONBIO, &dummy)
== SOCKET_ERROR)
{
NSLog(@"unable to set blocking mode - %s",
129,131d128
< #if defined(__MINGW__)
< len = recv((SOCKET)_get_osfhandle(descriptor), buf, len, 0);
< #else
133d129
< #endif
157,159d152
< #if defined(__MINGW__)
< len = send((SOCKET)_get_osfhandle(descriptor), buf, len, 0);
< #else
161d153
< #endif
301c293
< closesocket((SOCKET)_get_osfhandle(descriptor));
---
> closesocket(descriptor);
839,841d830
< #if defined(__MINGW__)
< self = [self initWithNativeHandle: (void*)net closeOnDealloc: YES];
< #else
843d831
< #endif
976,978d963
< #if defined(__MINGW__)
< self = [self initWithNativeHandle: (void*)net closeOnDealloc: YES];
< #else
980d964
< #endif
1148c1132,1138
< if (_fstat(desc, &sbuf) != 0)
---
> // _fstat only knows about files.
> // socket descriptor is beyond its grasp.
> // see:
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_file_handling.asp
> if (_fstat(desc, &sbuf) < 0)
> {
> isStandardFile = NO;
> }
1151d1140
< #endif
1155a1145
> #endif
1178c1168
< if (ioctlsocket((SOCKET)_get_osfhandle(desc), FIONBIO, &nbio) == 0)
---
> if (ioctlsocket((SOCKET)desc, FIONBIO, &nbio) == 0)
1692c1682
< (void)closesocket((SOCKET)_get_osfhandle(descriptor));
---
> (void)closesocket(descriptor);
1738,1740d1727
< #if defined(__MINGW__)
< _chsize(descriptor, pos);
< #else
1742a1730,1732
> #if defined(__MINGW__)
> _chsize(descriptor, pos);
> #else
1744d1733
< }
1745a1735,1736
> }
>
1868,1873d1858
< #if defined(__MINGW__)
< [l removeEvent: (void*)(gsaddr)(SOCKET)_get_osfhandle(descriptor)
< type: ET_RDESC
< forMode: [modes objectAtIndex: i]
< all: YES];
< #else
1878d1862
< #endif
1883,1888d1866
< #if defined(__MINGW__)
< [l removeEvent: (void*)(gsaddr)(SOCKET)_get_osfhandle(descriptor)
< type: ET_RDESC
< forMode: NSDefaultRunLoopMode
< all: YES];
< #else
1893d1870
< #endif
1922,1927d1898
< #if defined(__MINGW__)
< [l removeEvent: (void*)(gsaddr)(SOCKET)_get_osfhandle(descriptor)
< type: ET_WDESC
< forMode: [modes objectAtIndex: i]
< all: YES];
< #else
1932d1902
< #endif
1937,1942d1906
< #if defined(__MINGW__)
< [l removeEvent: (void*)(gsaddr)(SOCKET)_get_osfhandle(descriptor)
< type: ET_WDESC
< forMode: NSDefaultRunLoopMode
< all: YES];
< #else
1947d1910
< #endif
1967,1972d1929
< #if defined(__MINGW__)
< [l addEvent: (void*)(gsaddr)(SOCKET)_get_osfhandle(descriptor)
< type: ET_RDESC
< watcher: self
< forMode: [modes objectAtIndex: i]];
< #else
1977d1933
< #endif
1983,1988d1938
< #if defined(__MINGW__)
< [l addEvent: (void*)(gsaddr)(SOCKET)_get_osfhandle(descriptor)
< type: ET_RDESC
< watcher: self
< forMode: NSDefaultRunLoopMode];
< #else
1993d1942
< #endif
2018,2023d1966
< #if defined(__MINGW__)
< [l addEvent: (void*)(gsaddr)(SOCKET)_get_osfhandle(descriptor)
< type: ET_WDESC
< watcher: self
< forMode: [modes objectAtIndex: i]];
< #else
2028d1970
< #endif
2033,2038d1974
< #if defined(__MINGW__)
< [l addEvent: (void*)(gsaddr)(SOCKET)_get_osfhandle(descriptor)
< type: ET_WDESC
< watcher: self
< forMode: NSDefaultRunLoopMode];
< #else
2043d1978
< #endif
2068d2002
< #if defined(__MINGW__)
2070,2072d2003
< #else
< int desc;
< #endif
2075,2077d2005
< #if defined(__MINGW__)
< desc = accept((SOCKET)_get_osfhandle(descriptor), (struct
sockaddr*)&buf, &blen);
< #else
2079d2006
< #endif
2094,2097d2020
< #if defined(__MINGW__)
< h = [[[self class] alloc] initWithNativeHandle: (void*)desc
< closeOnDealloc: YES];
< #else
2100d2022
< #endif
2178,2181d2099
< #if defined(__MINGW__)
< if (getsockopt((SOCKET)_get_osfhandle(descriptor), SOL_SOCKET,
SO_ERROR,
< (char*)&result, &len) == 0 && result != 0)
< #else
2184d2101
< #endif
2280c2197
< if (ioctlsocket((SOCKET)_get_osfhandle(descriptor), FIONBIO, &dummy)
---
> if (ioctlsocket(descriptor, FIONBIO, &dummy)
2290c2207
< if (ioctlsocket((SOCKET)_get_osfhandle(descriptor), FIONBIO, &dummy)
---
> if (ioctlsocket(descriptor, FIONBIO, &dummy)
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- NSFileHandle/socket don't work for windows in gnustep-base 1.10.0,
andre levy <=