Mingw updates

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@17187 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2003-07-11 11:31:26 +00:00
parent 8e70ff7b10
commit 07a55ace86
6 changed files with 268 additions and 144 deletions

View file

@ -1,3 +1,12 @@
2003-07-11 Richard Frith-Macdonald <rfm@gnu.org>
* Headers/Foundation/NSPort.h: Tweak for mingw and correct conditional
compilation to only include NSSocketPort for MacOS-X
* Source/GSFileHandle.m: Incorporate S.J.Chuns mingw fixes with huge
simplification to avoid lots of conditional compilation.
* Source/GSTcpPort.m: ditto.
* Source/NSSocketPort.m: ditto.
2003-07-11 02:33 Alexander Malmberg <alexander@malmberg.org> 2003-07-11 02:33 Alexander Malmberg <alexander@malmberg.org>
* Source/NSAssertionHandler.m: Use the correct key to store the * Source/NSAssertionHandler.m: Use the correct key to store the

View file

@ -34,6 +34,7 @@
#include <wininet.h> #include <wininet.h>
#else #else
#include <sys/socket.h> #include <sys/socket.h>
#define SOCKET int
#endif #endif
@class NSMutableArray; @class NSMutableArray;
@ -85,12 +86,9 @@ GS_EXPORT NSString * const NSPortTimeoutException; /* OPENSTEP */
#ifndef NO_GNUSTEP #ifndef NO_GNUSTEP
@interface NSPort (GNUstep) @interface NSPort (GNUstep)
- (void) close; - (void) close;
+ (Class) outPacketClass; + (Class) outPacketClass;
- (Class) outPacketClass; - (Class) outPacketClass;
@end @end
#endif #endif
@ -98,8 +96,9 @@ GS_EXPORT NSString* NSPortDidBecomeInvalidNotification;
#define PortBecameInvalidNotification NSPortDidBecomeInvalidNotification #define PortBecameInvalidNotification NSPortDidBecomeInvalidNotification
#ifndef STRICT_OPENSTEP #ifndef STRICT_OPENSTEP
typedef int NSSocketNativeHandle;
typedef SOCKET NSSocketNativeHandle;
@interface NSSocketPort : NSPort <NSCoding, NSCopying> @interface NSSocketPort : NSPort <NSCoding, NSCopying>
{ {

View file

@ -67,6 +67,10 @@
#include <sys/filio.h> #include <sys/filio.h>
#endif #endif
#include <netdb.h> #include <netdb.h>
#define SOCKET int
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
#endif /* __MINGW__ */ #endif /* __MINGW__ */
#include <string.h> #include <string.h>
@ -123,7 +127,11 @@ static NSString* NotificationKey = @"NSFileHandleNotificationKey";
#endif #endif
if (isSocket) if (isSocket)
{ {
#if defined(__MINGW__)
len = recv((SOCKET)_get_osfhandle(descriptor), buf, len, 0);
#else
len = recv(descriptor, buf, len, 0); len = recv(descriptor, buf, len, 0);
#endif
} }
else else
{ {
@ -147,7 +155,11 @@ static NSString* NotificationKey = @"NSFileHandleNotificationKey";
#endif #endif
if (isSocket) if (isSocket)
{ {
#if defined(__MINGW__)
len = send((SOCKET)_get_osfhandle(descriptor), buf, len, 0);
#else
len = send(descriptor, buf, len, 0); len = send(descriptor, buf, len, 0);
#endif
} }
else else
{ {
@ -298,7 +310,14 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
[self setNonBlocking: wasNonBlocking]; [self setNonBlocking: wasNonBlocking];
if (closeOnDealloc == YES) if (closeOnDealloc == YES)
{ {
#if defined(__MINGW__)
if (isSocket)
closesocket((SOCKET)_get_osfhandle(descriptor));
else
close(descriptor);
#else
close(descriptor); close(descriptor);
#endif
descriptor = -1; descriptor = -1;
} }
} }
@ -679,7 +698,7 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
static NSString *esocks = nil; static NSString *esocks = nil;
static NSString *dsocks = nil; static NSString *dsocks = nil;
BOOL beenHere = NO; BOOL beenHere = NO;
int net; SOCKET net;
struct sockaddr_in sin; struct sockaddr_in sin;
NSString *shost = nil; NSString *shost = nil;
NSString *sport = nil; NSString *sport = nil;
@ -785,21 +804,25 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
} }
} }
if ((net = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) if ((net = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) == INVALID_SOCKET)
{ {
NSLog(@"unable to create socket - %s", GSLastErrorStr(errno)); NSLog(@"unable to create socket - %s", GSLastErrorStr(errno));
RELEASE(self); RELEASE(self);
return nil; return nil;
} }
#if defined(__MINGW__)
self = [self initWithNativeHandle: (void*)net closeOnDealloc: YES];
#else
self = [self initWithFileDescriptor: net closeOnDealloc: YES]; self = [self initWithFileDescriptor: net closeOnDealloc: YES];
#endif
if (self) if (self)
{ {
NSMutableDictionary* info; NSMutableDictionary* info;
isSocket = YES; isSocket = YES;
[self setNonBlocking: YES]; [self setNonBlocking: YES];
if (connect(net, (struct sockaddr*)&sin, sizeof(sin)) < 0) if (connect(net, (struct sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
{ {
#if defined(__MINGW__) #if defined(__MINGW__)
if (WSAGetLastError() != WSAEWOULDBLOCK) if (WSAGetLastError() != WSAEWOULDBLOCK)
@ -858,11 +881,11 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
protocol: (NSString*)p protocol: (NSString*)p
{ {
#ifndef BROKEN_SO_REUSEADDR #ifndef BROKEN_SO_REUSEADDR
int status = 1; int status = 1;
#endif #endif
int net; SOCKET net;
struct sockaddr_in sin; struct sockaddr_in sin;
int size = sizeof(sin); int size = sizeof(sin);
if (getAddr(a, s, p, &sin) == NO) if (getAddr(a, s, p, &sin) == NO)
{ {
@ -871,7 +894,7 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
return nil; return nil;
} }
if ((net = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) if ((net = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) == INVALID_SOCKET)
{ {
NSLog(@"unable to create socket - %s", GSLastErrorStr(errno)); NSLog(@"unable to create socket - %s", GSLastErrorStr(errno));
RELEASE(self); RELEASE(self);
@ -888,32 +911,48 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
setsockopt(net, SOL_SOCKET, SO_REUSEADDR, (char *)&status, sizeof(status)); setsockopt(net, SOL_SOCKET, SO_REUSEADDR, (char *)&status, sizeof(status));
#endif #endif
if (bind(net, (struct sockaddr *)&sin, sizeof(sin)) < 0) if (bind(net, (struct sockaddr *)&sin, sizeof(sin)) == SOCKET_ERROR)
{ {
NSLog(@"unable to bind to port %s:%d - %s", inet_ntoa(sin.sin_addr), NSLog(@"unable to bind to port %s:%d - %s", inet_ntoa(sin.sin_addr),
GSSwapBigI16ToHost(sin.sin_port), GSLastErrorStr(errno)); GSSwapBigI16ToHost(sin.sin_port), GSLastErrorStr(errno));
#if defined(__MINGW__)
(void) closesocket(net);
#else
(void) close(net); (void) close(net);
#endif
RELEASE(self); RELEASE(self);
return nil; return nil;
} }
if (listen(net, 5) < 0) if (listen(net, 5) == SOCKET_ERROR)
{ {
NSLog(@"unable to listen on port - %s", GSLastErrorStr(errno)); NSLog(@"unable to listen on port - %s", GSLastErrorStr(errno));
#if defined(__MINGW__)
(void) closesocket(net);
#else
(void) close(net); (void) close(net);
#endif
RELEASE(self); RELEASE(self);
return nil; return nil;
} }
if (getsockname(net, (struct sockaddr*)&sin, &size) < 0) if (getsockname(net, (struct sockaddr*)&sin, &size) == SOCKET_ERROR)
{ {
NSLog(@"unable to get socket name - %s", GSLastErrorStr(errno)); NSLog(@"unable to get socket name - %s", GSLastErrorStr(errno));
#if defined(__MINGW__)
(void) closesocket(net);
#else
(void) close(net); (void) close(net);
#endif
RELEASE(self); RELEASE(self);
return nil; return nil;
} }
#if defined(__MINGW__)
self = [self initWithNativeHandle: (void*)net closeOnDealloc: YES];
#else
self = [self initWithFileDescriptor: net closeOnDealloc: YES]; self = [self initWithFileDescriptor: net closeOnDealloc: YES];
#endif
if (self) if (self)
{ {
isSocket = YES; isSocket = YES;
@ -1081,7 +1120,7 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
#endif #endif
#if defined(__MINGW__) #if defined(__MINGW__)
if (_fstat(desc, &sbuf) < 0) if (_fstat(desc, &sbuf) != 0)
#else #else
if (fstat(desc, &sbuf) < 0) if (fstat(desc, &sbuf) < 0)
#endif #endif
@ -1111,10 +1150,14 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
* This is probably a socket ... try * This is probably a socket ... try
* using a socket specific call and see if that fails. * using a socket specific call and see if that fails.
*/ */
if (ioctlsocket(desc, FIONBIO, &nbio) == 0) if (ioctlsocket((SOCKET)_get_osfhandle(desc), FIONBIO, &nbio) == 0)
{ {
wasNonBlocking = (nbio == 0) ? NO : YES; wasNonBlocking = (nbio == 0) ? NO : YES;
} }
else
{
isSocket = NO; // maybe special file desc. like std in/out/err?
}
} }
#else #else
if ((e = fcntl(desc, F_GETFL, 0)) >= 0) if ((e = fcntl(desc, F_GETFL, 0)) >= 0)
@ -1148,7 +1191,7 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
- (id) initWithNativeHandle: (void*)hdl - (id) initWithNativeHandle: (void*)hdl
{ {
#if defined(__MINGW__) #if defined(__MINGW__)
return [self initWithFileDescriptor: _open_osfhandle((int)hdl, 0) return [self initWithFileDescriptor: _open_osfhandle((SOCKET)hdl, 0)
closeOnDealloc: NO]; closeOnDealloc: NO];
#else #else
return [self initWithFileDescriptor: (gsaddr)hdl closeOnDealloc: NO]; return [self initWithFileDescriptor: (gsaddr)hdl closeOnDealloc: NO];
@ -1158,7 +1201,7 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
- (id) initWithNativeHandle: (void*)hdl closeOnDealloc: (BOOL)flag - (id) initWithNativeHandle: (void*)hdl closeOnDealloc: (BOOL)flag
{ {
#if defined(__MINGW__) #if defined(__MINGW__)
return [self initWithFileDescriptor: _open_osfhandle((int)hdl, 0) return [self initWithFileDescriptor: _open_osfhandle((SOCKET)hdl, 0)
closeOnDealloc: flag]; closeOnDealloc: flag];
#else #else
return [self initWithFileDescriptor: (gsaddr)hdl closeOnDealloc: flag]; return [self initWithFileDescriptor: (gsaddr)hdl closeOnDealloc: flag];
@ -1268,7 +1311,7 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
- (void*) nativeHandle - (void*) nativeHandle
{ {
#if defined(__MINGW__) #if defined(__MINGW__)
return (void*)_get_osfhandle(descriptor); return (void*)(SOCKET)_get_osfhandle(descriptor);
#else #else
return (void*)descriptor; return (void*)descriptor;
#endif #endif
@ -1625,7 +1668,7 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
} }
else else
{ {
(void)closesocket(descriptor); (void)closesocket((SOCKET)_get_osfhandle(descriptor));
} }
#else #else
(void)close(descriptor); (void)close(descriptor);
@ -1660,12 +1703,14 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
- (void) synchronizeFile - (void) synchronizeFile
{ {
#if !defined(__MINGW__)
if (isStandardFile) if (isStandardFile)
{ {
#if defined(__MINGW__)
(void)_commit(descriptor);
#else
(void)sync(); (void)sync();
}
#endif #endif
}
} }
- (void) truncateFileAtOffset: (unsigned long long)pos - (void) truncateFileAtOffset: (unsigned long long)pos
@ -1800,18 +1845,32 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
for (i = 0; i < [modes count]; i++) 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 [l removeEvent: (void*)(gsaddr)descriptor
type: ET_RDESC type: ET_RDESC
forMode: [modes objectAtIndex: i] forMode: [modes objectAtIndex: i]
all: YES]; all: YES];
#endif
} }
} }
else 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 [l removeEvent: (void*)(gsaddr)descriptor
type: ET_RDESC type: ET_RDESC
forMode: NSDefaultRunLoopMode forMode: NSDefaultRunLoopMode
all: YES]; all: YES];
#endif
} }
} }
@ -1840,18 +1899,32 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
for (i = 0; i < [modes count]; i++) 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 [l removeEvent: (void*)(gsaddr)descriptor
type: ET_WDESC type: ET_WDESC
forMode: [modes objectAtIndex: i] forMode: [modes objectAtIndex: i]
all: YES]; all: YES];
#endif
} }
} }
else 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 [l removeEvent: (void*)(gsaddr)descriptor
type: ET_WDESC type: ET_WDESC
forMode: NSDefaultRunLoopMode forMode: NSDefaultRunLoopMode
all: YES]; all: YES];
#endif
} }
} }
@ -1871,19 +1944,33 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
for (i = 0; i < [modes count]; i++) 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 [l addEvent: (void*)(gsaddr)descriptor
type: ET_RDESC type: ET_RDESC
watcher: self watcher: self
forMode: [modes objectAtIndex: i]]; forMode: [modes objectAtIndex: i]];
#endif
} }
[readInfo setObject: modes forKey: NSFileHandleNotificationMonitorModes]; [readInfo setObject: modes forKey: NSFileHandleNotificationMonitorModes];
} }
else 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 [l addEvent: (void*)(gsaddr)descriptor
type: ET_RDESC type: ET_RDESC
watcher: self watcher: self
forMode: NSDefaultRunLoopMode]; forMode: NSDefaultRunLoopMode];
#endif
} }
} }
@ -1908,18 +1995,32 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
for (i = 0; i < [modes count]; i++) 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 [l addEvent: (void*)(gsaddr)descriptor
type: ET_WDESC type: ET_WDESC
watcher: self watcher: self
forMode: [modes objectAtIndex: i]]; forMode: [modes objectAtIndex: i]];
#endif
} }
} }
else 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 [l addEvent: (void*)(gsaddr)descriptor
type: ET_WDESC type: ET_WDESC
watcher: self watcher: self
forMode: NSDefaultRunLoopMode]; forMode: NSDefaultRunLoopMode];
#endif
} }
} }
} }
@ -1944,11 +2045,19 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
if (operation == NSFileHandleConnectionAcceptedNotification) if (operation == NSFileHandleConnectionAcceptedNotification)
{ {
struct sockaddr_in buf; struct sockaddr_in buf;
#if defined(__MINGW__)
SOCKET desc;
#else
int desc; int desc;
#endif
int blen = sizeof(buf); 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); desc = accept(descriptor, (struct sockaddr*)&buf, &blen);
if (desc < 0) #endif
if (desc == INVALID_SOCKET)
{ {
NSString *s; NSString *s;
@ -1962,8 +2071,13 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
struct sockaddr_in sin; struct sockaddr_in sin;
int size = sizeof(sin); int size = sizeof(sin);
#if defined(__MINGW__)
h = [[[self class] alloc] initWithNativeHandle: (void*)desc
closeOnDealloc: YES];
#else
h = [[[self class] alloc] initWithFileDescriptor: desc h = [[[self class] alloc] initWithFileDescriptor: desc
closeOnDealloc: YES]; closeOnDealloc: YES];
#endif
h->isSocket = YES; h->isSocket = YES;
getpeername(desc, (struct sockaddr*)&sin, &size); getpeername(desc, (struct sockaddr*)&sin, &size);
[h setAddr: &sin]; [h setAddr: &sin];
@ -2041,8 +2155,13 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
int result; int result;
int len = sizeof(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, if (getsockopt(descriptor, SOL_SOCKET, SO_ERROR,
(char*)&result, &len) == 0 && result != 0) (char*)&result, &len) == 0 && result != 0)
#endif
{ {
NSString *s; NSString *s;
@ -2132,10 +2251,14 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
#if defined(__MINGW__) #if defined(__MINGW__)
unsigned long dummy; unsigned long dummy;
if (isSocket != YES)
return;
if (flag) if (flag)
{ {
dummy = 1; dummy = 1;
if (ioctlsocket(descriptor, FIONBIO, &dummy) < 0) if (ioctlsocket((SOCKET)_get_osfhandle(descriptor), FIONBIO, &dummy)
== SOCKET_ERROR)
{ {
NSLog(@"unable to set non-blocking mode - %s", NSLog(@"unable to set non-blocking mode - %s",
GSLastErrorStr(errno)); GSLastErrorStr(errno));
@ -2144,7 +2267,8 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
else else
{ {
dummy = 0; dummy = 0;
if (ioctlsocket(descriptor, FIONBIO, &dummy) < 0) if (ioctlsocket((SOCKET)_get_osfhandle(descriptor), FIONBIO, &dummy)
== SOCKET_ERROR)
{ {
NSLog(@"unable to set blocking mode - %s", NSLog(@"unable to set blocking mode - %s",
GSLastErrorStr(errno)); GSLastErrorStr(errno));

View file

@ -51,7 +51,9 @@
#include <unistd.h> /* for gethostname() */ #include <unistd.h> /* for gethostname() */
#endif #endif
#ifndef __MINGW__ #ifdef __MINGW__
#define close closesocket
#else
#include <sys/param.h> /* for MAXHOSTNAMELEN */ #include <sys/param.h> /* for MAXHOSTNAMELEN */
#include <sys/types.h> #include <sys/types.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -94,11 +96,12 @@
#if defined(__svr4__) #if defined(__svr4__)
#include <sys/stropts.h> #include <sys/stropts.h>
#endif #endif
#endif /* !__MINGW__ */
#ifdef __MINGW__ #define SOCKET int
#define close closesocket #define SOCKET_ERROR -1
#endif #define INVALID_SOCKET -1
#endif /* !__MINGW__ */
static BOOL multi_threaded = NO; static BOOL multi_threaded = NO;
@ -189,7 +192,7 @@ typedef enum {
@interface GSTcpHandle : NSObject <GCFinalization, RunLoopEvents> @interface GSTcpHandle : NSObject <GCFinalization, RunLoopEvents>
{ {
int desc; /* File descriptor for I/O. */ SOCKET desc; /* File descriptor for I/O. */
unsigned wItem; /* Index of item being written. */ unsigned wItem; /* Index of item being written. */
NSMutableData *wData; /* Data object being written. */ NSMutableData *wData; /* Data object being written. */
unsigned wLength; /* Ammount written so far. */ unsigned wLength; /* Ammount written so far. */
@ -213,7 +216,7 @@ typedef enum {
NSString *defaultAddress; NSString *defaultAddress;
} }
+ (GSTcpHandle*) handleWithDescriptor: (int)d; + (GSTcpHandle*) handleWithDescriptor: (SOCKET)d;
- (BOOL) connectToPort: (GSTcpPort*)aPort beforeDate: (NSDate*)when; - (BOOL) connectToPort: (GSTcpPort*)aPort beforeDate: (NSDate*)when;
- (int) descriptor; - (int) descriptor;
- (void) invalidate; - (void) invalidate;
@ -238,7 +241,7 @@ typedef enum {
NSHost *host; /* OpenStep host for this port. */ NSHost *host; /* OpenStep host for this port. */
NSString *address; /* Forced internet address. */ NSString *address; /* Forced internet address. */
gsu16 portNum; /* TCP port in host byte order. */ gsu16 portNum; /* TCP port in host byte order. */
int listener; /* Descriptor to listen on. */ SOCKET listener;
NSMapTable *handles; /* Handles indexed by socket. */ NSMapTable *handles; /* Handles indexed by socket. */
} }
@ -251,7 +254,7 @@ typedef enum {
- (void) addHandle: (GSTcpHandle*)handle forSend: (BOOL)send; - (void) addHandle: (GSTcpHandle*)handle forSend: (BOOL)send;
- (NSString*) address; - (NSString*) address;
- (void) getFds: (int*)fds count: (int*)count; - (void) getFds: (SOCKET*)fds count: (int*)count;
- (GSTcpHandle*) handleForPort: (GSTcpPort*)recvPort beforeDate: (NSDate*)when; - (GSTcpHandle*) handleForPort: (GSTcpPort*)recvPort beforeDate: (NSDate*)when;
- (void) handlePortMessage: (NSPortMessage*)m; - (void) handlePortMessage: (NSPortMessage*)m;
- (NSHost*) host; - (NSHost*) host;
@ -391,7 +394,7 @@ static Class runLoopClass;
return nil; return nil;
} }
+ (GSTcpHandle*) handleWithDescriptor: (int)d + (GSTcpHandle*) handleWithDescriptor: (SOCKET)d
{ {
GSTcpHandle *handle; GSTcpHandle *handle;
#ifdef __MINGW__ #ifdef __MINGW__
@ -400,14 +403,14 @@ static Class runLoopClass;
int e; int e;
#endif /* __MINGW__ */ #endif /* __MINGW__ */
if (d < 0) if (d == INVALID_SOCKET)
{ {
NSLog(@"illegal descriptor (%d) for Tcp Handle", d); NSLog(@"illegal descriptor (%d) for Tcp Handle", d);
return nil; return nil;
} }
#ifdef __MINGW__ #ifdef __MINGW__
dummy = 1; dummy = 1;
if (ioctlsocket(d, FIONBIO, &dummy) < 0) if (ioctlsocket(d, FIONBIO, &dummy) == SOCKET_ERROR)
{ {
NSLog(@"unable to set non-blocking mode on %d - %s", NSLog(@"unable to set non-blocking mode on %d - %s",
d, GSLastErrorStr(errno)); d, GSLastErrorStr(errno));
@ -551,7 +554,8 @@ static Class runLoopClass;
} }
sockAddr.sin_port = GSSwapHostI16ToBig([aPort portNumber]); sockAddr.sin_port = GSSwapHostI16ToBig([aPort portNumber]);
if (connect(desc, (struct sockaddr*)&sockAddr, sizeof(sockAddr)) < 0) if (connect(desc, (struct sockaddr*)&sockAddr, sizeof(sockAddr))
== SOCKET_ERROR)
{ {
#ifdef __MINGW__ #ifdef __MINGW__
if (WSAGetLastError() != WSAEWOULDBLOCK) if (WSAGetLastError() != WSAEWOULDBLOCK)
@ -655,7 +659,7 @@ static Class runLoopClass;
desc, inet_ntoa(sockAddr.sin_addr), ntohs(sockAddr.sin_port)]; desc, inet_ntoa(sockAddr.sin_addr), ntohs(sockAddr.sin_port)];
} }
- (int) descriptor - (SOCKET) descriptor
{ {
return desc; return desc;
} }
@ -723,7 +727,7 @@ static Class runLoopClass;
* If we have been invalidated (desc < 0) then we should ignore this * If we have been invalidated (desc < 0) then we should ignore this
* event and remove ourself from the runloop. * event and remove ourself from the runloop.
*/ */
if (desc < 0) if (desc == INVALID_SOCKET)
{ {
NSRunLoop *l = [runLoopClass currentRunLoop]; NSRunLoop *l = [runLoopClass currentRunLoop];
@ -776,11 +780,7 @@ static Class runLoopClass;
* Now try to fill the buffer with data. * Now try to fill the buffer with data.
*/ */
bytes = [rData mutableBytes]; bytes = [rData mutableBytes];
#ifdef __MINGW__
res = recv(desc, bytes + rLength, want - rLength, 0); res = recv(desc, bytes + rLength, want - rLength, 0);
#else
res = read(desc, bytes + rLength, want - rLength);
#endif
if (res <= 0) if (res <= 0)
{ {
if (res == 0) if (res == 0)
@ -1100,11 +1100,7 @@ static Class runLoopClass;
{ {
NSData *d = newDataWithEncodedPort([self recvPort]); NSData *d = newDataWithEncodedPort([self recvPort]);
#ifdef __MINGW__
len = send(desc, [d bytes], [d length], 0); len = send(desc, [d bytes], [d length], 0);
#else
len = write(desc, [d bytes], [d length]);
#endif
if (len == (int)[d length]) if (len == (int)[d length])
{ {
RELEASE(defaultAddress); RELEASE(defaultAddress);
@ -1147,11 +1143,7 @@ static Class runLoopClass;
} }
b = [wData bytes]; b = [wData bytes];
l = [wData length]; l = [wData length];
#ifdef __MINGW__
res = send(desc, b + wLength, l - wLength, 0); res = send(desc, b + wLength, l - wLength, 0);
#else
res = write(desc, b + wLength, l - wLength);
#endif
if (res < 0) if (res < 0)
{ {
if (errno != EINTR && errno != EAGAIN) if (errno != EINTR && errno != EAGAIN)
@ -1483,8 +1475,10 @@ static unsigned wordAlign;
if (shouldListen == YES && [thisHost isEqual: aHost]) if (shouldListen == YES && [thisHost isEqual: aHost])
{ {
#ifndef BROKEN_SO_REUSEADDR
int reuse = 1; /* Should we re-use ports? */ int reuse = 1; /* Should we re-use ports? */
int desc; #endif
SOCKET desc;
BOOL addrOk = YES; BOOL addrOk = YES;
struct sockaddr_in sockaddr; struct sockaddr_in sockaddr;
@ -1521,7 +1515,8 @@ static unsigned wordAlign;
NSLog(@"Bad address (%@) specified for listening port", addr); NSLog(@"Bad address (%@) specified for listening port", addr);
DESTROY(port); DESTROY(port);
} }
else if ((desc = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) else if ((desc = socket(AF_INET, SOCK_STREAM, PF_UNSPEC))
== INVALID_SOCKET)
{ {
NSLog(@"unable to create socket - %s", GSLastErrorStr(errno)); NSLog(@"unable to create socket - %s", GSLastErrorStr(errno));
DESTROY(port); DESTROY(port);
@ -1543,20 +1538,21 @@ static unsigned wordAlign;
} }
#endif #endif
else if (bind(desc, (struct sockaddr *)&sockaddr, else if (bind(desc, (struct sockaddr *)&sockaddr,
sizeof(sockaddr)) < 0) sizeof(sockaddr)) == SOCKET_ERROR)
{ {
NSLog(@"unable to bind to port %s:%d - %s", NSLog(@"unable to bind to port %s:%d - %s",
inet_ntoa(sockaddr.sin_addr), number, GSLastErrorStr(errno)); inet_ntoa(sockaddr.sin_addr), number, GSLastErrorStr(errno));
(void) close(desc); (void) close(desc);
DESTROY(port); DESTROY(port);
} }
else if (listen(desc, 5) < 0) else if (listen(desc, 5) == SOCKET_ERROR)
{ {
NSLog(@"unable to listen on port - %s", GSLastErrorStr(errno)); NSLog(@"unable to listen on port - %s", GSLastErrorStr(errno));
(void) close(desc); (void) close(desc);
DESTROY(port); DESTROY(port);
} }
else if (getsockname(desc, (struct sockaddr*)&sockaddr, &i) < 0) else if (getsockname(desc, (struct sockaddr*)&sockaddr, &i)
== SOCKET_ERROR)
{ {
NSLog(@"unable to get socket name - %s", GSLastErrorStr(errno)); NSLog(@"unable to get socket name - %s", GSLastErrorStr(errno));
(void) close(desc); (void) close(desc);
@ -1693,10 +1689,10 @@ static unsigned wordAlign;
* This is a callback method used by the NSRunLoop class to determine which * This is a callback method used by the NSRunLoop class to determine which
* descriptors to watch for the port. * descriptors to watch for the port.
*/ */
- (void) getFds: (int*)fds count: (int*)count - (void) getFds: (SOCKET*)fds count: (int*)count
{ {
NSMapEnumerator me; NSMapEnumerator me;
int sock; SOCKET sock;
GSTcpHandle *handle; GSTcpHandle *handle;
id recvSelf; id recvSelf;
@ -1737,8 +1733,10 @@ static unsigned wordAlign;
- (GSTcpHandle*) handleForPort: (GSTcpPort*)recvPort beforeDate: (NSDate*)when - (GSTcpHandle*) handleForPort: (GSTcpPort*)recvPort beforeDate: (NSDate*)when
{ {
NSMapEnumerator me; NSMapEnumerator me;
int sock; SOCKET sock;
#ifndef BROKEN_SO_REUSEADDR
int opt = 1; int opt = 1;
#endif
GSTcpHandle *handle = nil; GSTcpHandle *handle = nil;
M_LOCK(myLock); M_LOCK(myLock);
@ -1761,7 +1759,7 @@ static unsigned wordAlign;
* Not found ... create a new handle. * Not found ... create a new handle.
*/ */
handle = nil; handle = nil;
if ((sock = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) if ((sock = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) == INVALID_SOCKET)
{ {
NSLog(@"unable to create socket - %s", GSLastErrorStr(errno)); NSLog(@"unable to create socket - %s", GSLastErrorStr(errno));
} }
@ -1916,7 +1914,7 @@ static unsigned wordAlign;
extra: (void*)extra extra: (void*)extra
forMode: (NSString*)mode forMode: (NSString*)mode
{ {
int desc = (int)(gsaddr)extra; SOCKET desc = (SOCKET)(gsaddr)extra;
GSTcpHandle *handle; GSTcpHandle *handle;
if (desc == listener) if (desc == listener)
@ -1925,7 +1923,7 @@ static unsigned wordAlign;
int size = sizeof(sockAddr); int size = sizeof(sockAddr);
desc = accept(listener, (struct sockaddr*)&sockAddr, &size); desc = accept(listener, (struct sockaddr*)&sockAddr, &size);
if (desc < 0) if (desc == INVALID_SOCKET)
{ {
NSDebugMLLog(@"NSPort", @"accept failed - handled in other thread?"); NSDebugMLLog(@"NSPort", @"accept failed - handled in other thread?");
} }

View file

@ -702,13 +702,11 @@ static void setPollfd(int fd, int event, GSRunLoopCtxt *ctxt)
GSCheckTasks(); GSCheckTasks();
poll_return = 0; poll_return = 0;
} }
#ifdef __MINGW__
else if (errno == 0) else if (errno == 0)
{ {
/* MinGW often returns an errno == 0. Not sure why */ /* Some systems returns an errno == 0. Not sure why */
poll_return = 0; poll_return = 0;
} }
#endif
else else
{ {
/* Some exceptional condition happened. */ /* Some exceptional condition happened. */
@ -984,7 +982,7 @@ static void setPollfd(int fd, int event, GSRunLoopCtxt *ctxt)
{ {
fd = port_fd_array[port_fd_count]; fd = port_fd_array[port_fd_count];
FD_SET (port_fd_array[port_fd_count], &read_fds); FD_SET (port_fd_array[port_fd_count], &read_fds);
if (fd > fdEnd) if (fd > fdEnd)
fdEnd = fd; fdEnd = fd;
NSMapInsert(_rfdMap, NSMapInsert(_rfdMap,
(void*)port_fd_array[port_fd_count], info); (void*)port_fd_array[port_fd_count], info);
@ -1023,13 +1021,11 @@ static void setPollfd(int fd, int event, GSRunLoopCtxt *ctxt)
GSCheckTasks(); GSCheckTasks();
select_return = 0; select_return = 0;
} }
#ifdef __MINGW__
else if (errno == 0) else if (errno == 0)
{ {
/* MinGW often returns an errno == 0. Not sure why */ /* Some systems return an errno == 0. Not sure why */
select_return = 0; select_return = 0;
} }
#endif
else else
{ {
/* Some exceptional condition happened. */ /* Some exceptional condition happened. */

View file

@ -49,6 +49,9 @@
#include <winsock2.h> #include <winsock2.h>
#include <wininet.h> #include <wininet.h>
#define close closesocket #define close closesocket
#define SOCKET int
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
#endif /* __MINGW__ */ #endif /* __MINGW__ */
@ -58,17 +61,11 @@
* NSPort which implements Distributed Objects communication between * NSPort which implements Distributed Objects communication between
* hosts on a network. However, the GNUstep distributed objects system's * hosts on a network. However, the GNUstep distributed objects system's
* NSPort class uses TCP/IP for all of its communication. The GNUstep * NSPort class uses TCP/IP for all of its communication. The GNUstep
* <code>NSSocketPort</code>, then, is useful as a convenient method to * <code>NSSocketPort</code>, then, is useful only as a method to
* create and encapsulate BSD sockets: * create and encapsulate BSD sockets. However, the GNUstep [NSFileHandle]
* extensions for networking provide a richer and easier to use mechanism
* than that provided by the NSSocketPort class.
* </p> * </p>
* <example>
* int fileDesc;
* NSFileHandle *smtpHandle;
*
* fileDesc = [[NSSocketPort alloc] initRemoteWithTCPPort: 25
* host: @"mail.example.com"];
* smtpHandle = [[NSFileHandle alloc] initWithFileDescriptor: fileDesc];
* </example>
*/ */
@implementation NSSocketPort @implementation NSSocketPort
@ -143,29 +140,30 @@
int s = -1; int s = -1;
if (addrData == nil) if (addrData == nil)
{ {
NSDebugMLLog(@"NSSocketPort", @"Nil value passed for address."); NSDebugMLLog(@"NSSocketPort", @"Nil value passed for address.");
goto iWPFAFailed; goto iWPFAFailed;
} }
s = socket(family, type, protocol); s = socket(family, type, protocol);
if (s == -1) if (s == INVALID_SOCKET)
{ {
NSLog(@"socket: %s", GSLastErrorStr(errno)); NSLog(@"socket: %s", GSLastErrorStr(errno));
goto iWPFAFailed; goto iWPFAFailed;
} }
if (bind(s, (struct sockaddr *)[addrData bytes], [addrData length]) == -1) if (bind(s, (struct sockaddr *)[addrData bytes], [addrData length])
{ == SOCKET_ERROR)
NSLog(@"bind: %s", GSLastErrorStr(errno)); {
goto iWPFAFailed; NSLog(@"bind: %s", GSLastErrorStr(errno));
} goto iWPFAFailed;
}
if (listen(s, SOMAXCONN) == -1) if (listen(s, SOMAXCONN) == SOCKET_ERROR)
{ {
NSLog(@"listen: %s", GSLastErrorStr(errno)); NSLog(@"listen: %s", GSLastErrorStr(errno));
goto iWPFAFailed; goto iWPFAFailed;
} }
return [self initWithProtocolFamily: family return [self initWithProtocolFamily: family
socketType: type socketType: type
@ -214,10 +212,10 @@ iWPFAFailed:
address = [[[NSHost hostWithName: hostname] address] cString]; address = [[[NSHost hostWithName: hostname] address] cString];
if (address == NULL) if (address == NULL)
{ {
RELEASE(self); RELEASE(self);
return nil; return nil;
} }
/* Clear memory, as recommended. */ /* Clear memory, as recommended. */
memset(&sa, 0, sizeof(struct sockaddr_in)); memset(&sa, 0, sizeof(struct sockaddr_in));
@ -229,10 +227,10 @@ iWPFAFailed:
addrData = [NSData dataWithBytes: &sa length: sizeof(struct sockaddr_in)]; addrData = [NSData dataWithBytes: &sa length: sizeof(struct sockaddr_in)];
if (addrData == nil) if (addrData == nil)
{ {
RELEASE(self); RELEASE(self);
return nil; return nil;
} }
return [self initRemoteWithProtocolFamily: PF_INET return [self initRemoteWithProtocolFamily: PF_INET
socketType: SOCK_STREAM socketType: SOCK_STREAM
@ -252,19 +250,19 @@ iWPFAFailed:
address: (NSData *)addrData address: (NSData *)addrData
{ {
if (addrData == nil) if (addrData == nil)
{ {
NSDebugMLLog(@"NSSocketPort", @"Nil value passed for address."); NSDebugMLLog(@"NSSocketPort", @"Nil value passed for address.");
RELEASE(self); RELEASE(self);
return nil; return nil;
} }
_socket = socket(family, type, protocol); _socket = socket(family, type, protocol);
if (_socket == -1) if (_socket == INVALID_SOCKET)
{ {
NSLog(@"socket: %s", GSLastErrorStr(errno)); NSLog(@"socket: %s", GSLastErrorStr(errno));
RELEASE(self); RELEASE(self);
return nil; return nil;
} }
_protocolFamily = family; _protocolFamily = family;
_socketType = type; _socketType = type;
@ -283,18 +281,18 @@ iWPFAFailed:
int len = SOCK_MAXADDRLEN; int len = SOCK_MAXADDRLEN;
if (_remoteAddrData != nil) if (_remoteAddrData != nil)
{ {
return _remoteAddrData; return _remoteAddrData;
} }
else if (getsockname(_socket, (struct sockaddr *)&sa, &len) == 0) else if (getsockname(_socket, (struct sockaddr *)&sa, &len) == 0)
{ {
return [NSData dataWithBytes: &sa length: len]; return [NSData dataWithBytes: &sa length: len];
} }
else else
{ {
NSLog(@"getsockname: %s", GSLastErrorStr(errno)); NSLog(@"getsockname: %s", GSLastErrorStr(errno));
return nil; return nil;
} }
} }
/** /**
@ -357,23 +355,23 @@ iWPFAFailed:
if (NSShouldRetainWithZone(self, zone) == YES) if (NSShouldRetainWithZone(self, zone) == YES)
return RETAIN(self); return RETAIN(self);
else else
{
NSSocketPort *copy = NSAllocateObject([self class], 0, zone);
if (copy != nil)
{ {
/* Insulate against NSPort changes. */ NSSocketPort *copy = NSAllocateObject([self class], 0, zone);
[copy setDelegate: [self delegate]];
copy->_socket = dup(_socket); if (copy != nil)
copy->_protocolFamily = _protocolFamily; {
copy->_socketType = _socketType; /* Insulate against NSPort changes. */
copy->_protocol = _protocol; [copy setDelegate: [self delegate]];
_remoteAddrData = [_remoteAddrData copyWithZone: zone];
copy->_socket = dup(_socket);
copy->_protocolFamily = _protocolFamily;
copy->_socketType = _socketType;
copy->_protocol = _protocol;
_remoteAddrData = [_remoteAddrData copyWithZone: zone];
}
return RETAIN(copy);
} }
return RETAIN(copy);
}
} }
/* NSCoding */ /* NSCoding */