mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-22 16:33:29 +00:00
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:
parent
bbb5b5db2f
commit
0a27433553
6 changed files with 268 additions and 144 deletions
|
@ -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>
|
||||
|
||||
* Source/NSAssertionHandler.m: Use the correct key to store the
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
#include <wininet.h>
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#define SOCKET int
|
||||
#endif
|
||||
|
||||
@class NSMutableArray;
|
||||
|
@ -85,12 +86,9 @@ GS_EXPORT NSString * const NSPortTimeoutException; /* OPENSTEP */
|
|||
|
||||
#ifndef NO_GNUSTEP
|
||||
@interface NSPort (GNUstep)
|
||||
|
||||
- (void) close;
|
||||
|
||||
+ (Class) outPacketClass;
|
||||
- (Class) outPacketClass;
|
||||
|
||||
@end
|
||||
#endif
|
||||
|
||||
|
@ -98,8 +96,9 @@ GS_EXPORT NSString* NSPortDidBecomeInvalidNotification;
|
|||
|
||||
#define PortBecameInvalidNotification NSPortDidBecomeInvalidNotification
|
||||
|
||||
#ifndef STRICT_OPENSTEP
|
||||
typedef int NSSocketNativeHandle;
|
||||
#ifndef STRICT_OPENSTEP
|
||||
|
||||
typedef SOCKET NSSocketNativeHandle;
|
||||
|
||||
@interface NSSocketPort : NSPort <NSCoding, NSCopying>
|
||||
{
|
||||
|
|
|
@ -67,6 +67,10 @@
|
|||
#include <sys/filio.h>
|
||||
#endif
|
||||
#include <netdb.h>
|
||||
|
||||
#define SOCKET int
|
||||
#define SOCKET_ERROR -1
|
||||
#define INVALID_SOCKET -1
|
||||
#endif /* __MINGW__ */
|
||||
|
||||
#include <string.h>
|
||||
|
@ -123,7 +127,11 @@ static NSString* NotificationKey = @"NSFileHandleNotificationKey";
|
|||
#endif
|
||||
if (isSocket)
|
||||
{
|
||||
#if defined(__MINGW__)
|
||||
len = recv((SOCKET)_get_osfhandle(descriptor), buf, len, 0);
|
||||
#else
|
||||
len = recv(descriptor, buf, len, 0);
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -147,7 +155,11 @@ static NSString* NotificationKey = @"NSFileHandleNotificationKey";
|
|||
#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 +310,14 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
|
|||
[self setNonBlocking: wasNonBlocking];
|
||||
if (closeOnDealloc == YES)
|
||||
{
|
||||
#if defined(__MINGW__)
|
||||
if (isSocket)
|
||||
closesocket((SOCKET)_get_osfhandle(descriptor));
|
||||
else
|
||||
close(descriptor);
|
||||
#else
|
||||
close(descriptor);
|
||||
#endif
|
||||
descriptor = -1;
|
||||
}
|
||||
}
|
||||
|
@ -679,7 +698,7 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
static NSString *esocks = nil;
|
||||
static NSString *dsocks = nil;
|
||||
BOOL beenHere = NO;
|
||||
int net;
|
||||
SOCKET net;
|
||||
struct sockaddr_in sin;
|
||||
NSString *shost = 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));
|
||||
RELEASE(self);
|
||||
return nil;
|
||||
}
|
||||
|
||||
#if defined(__MINGW__)
|
||||
self = [self initWithNativeHandle: (void*)net closeOnDealloc: YES];
|
||||
#else
|
||||
self = [self initWithFileDescriptor: net closeOnDealloc: YES];
|
||||
#endif
|
||||
if (self)
|
||||
{
|
||||
NSMutableDictionary* info;
|
||||
|
||||
isSocket = 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 (WSAGetLastError() != WSAEWOULDBLOCK)
|
||||
|
@ -858,11 +881,11 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
protocol: (NSString*)p
|
||||
{
|
||||
#ifndef BROKEN_SO_REUSEADDR
|
||||
int status = 1;
|
||||
int status = 1;
|
||||
#endif
|
||||
int net;
|
||||
SOCKET net;
|
||||
struct sockaddr_in sin;
|
||||
int size = sizeof(sin);
|
||||
int size = sizeof(sin);
|
||||
|
||||
if (getAddr(a, s, p, &sin) == NO)
|
||||
{
|
||||
|
@ -871,7 +894,7 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
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));
|
||||
RELEASE(self);
|
||||
|
@ -888,32 +911,48 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
setsockopt(net, SOL_SOCKET, SO_REUSEADDR, (char *)&status, sizeof(status));
|
||||
#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),
|
||||
GSSwapBigI16ToHost(sin.sin_port), GSLastErrorStr(errno));
|
||||
#if defined(__MINGW__)
|
||||
(void) closesocket(net);
|
||||
#else
|
||||
(void) close(net);
|
||||
#endif
|
||||
RELEASE(self);
|
||||
return nil;
|
||||
}
|
||||
|
||||
if (listen(net, 5) < 0)
|
||||
if (listen(net, 5) == SOCKET_ERROR)
|
||||
{
|
||||
NSLog(@"unable to listen on port - %s", GSLastErrorStr(errno));
|
||||
#if defined(__MINGW__)
|
||||
(void) closesocket(net);
|
||||
#else
|
||||
(void) close(net);
|
||||
#endif
|
||||
RELEASE(self);
|
||||
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));
|
||||
#if defined(__MINGW__)
|
||||
(void) closesocket(net);
|
||||
#else
|
||||
(void) close(net);
|
||||
#endif
|
||||
RELEASE(self);
|
||||
return nil;
|
||||
}
|
||||
|
||||
#if defined(__MINGW__)
|
||||
self = [self initWithNativeHandle: (void*)net closeOnDealloc: YES];
|
||||
#else
|
||||
self = [self initWithFileDescriptor: net closeOnDealloc: YES];
|
||||
#endif
|
||||
if (self)
|
||||
{
|
||||
isSocket = YES;
|
||||
|
@ -1081,7 +1120,7 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
#endif
|
||||
|
||||
#if defined(__MINGW__)
|
||||
if (_fstat(desc, &sbuf) < 0)
|
||||
if (_fstat(desc, &sbuf) != 0)
|
||||
#else
|
||||
if (fstat(desc, &sbuf) < 0)
|
||||
#endif
|
||||
|
@ -1111,10 +1150,14 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
* This is probably a socket ... try
|
||||
* 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;
|
||||
}
|
||||
else
|
||||
{
|
||||
isSocket = NO; // maybe special file desc. like std in/out/err?
|
||||
}
|
||||
}
|
||||
#else
|
||||
if ((e = fcntl(desc, F_GETFL, 0)) >= 0)
|
||||
|
@ -1148,7 +1191,7 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
- (id) initWithNativeHandle: (void*)hdl
|
||||
{
|
||||
#if defined(__MINGW__)
|
||||
return [self initWithFileDescriptor: _open_osfhandle((int)hdl, 0)
|
||||
return [self initWithFileDescriptor: _open_osfhandle((SOCKET)hdl, 0)
|
||||
closeOnDealloc: NO];
|
||||
#else
|
||||
return [self initWithFileDescriptor: (gsaddr)hdl closeOnDealloc: NO];
|
||||
|
@ -1158,7 +1201,7 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
- (id) initWithNativeHandle: (void*)hdl closeOnDealloc: (BOOL)flag
|
||||
{
|
||||
#if defined(__MINGW__)
|
||||
return [self initWithFileDescriptor: _open_osfhandle((int)hdl, 0)
|
||||
return [self initWithFileDescriptor: _open_osfhandle((SOCKET)hdl, 0)
|
||||
closeOnDealloc: flag];
|
||||
#else
|
||||
return [self initWithFileDescriptor: (gsaddr)hdl closeOnDealloc: flag];
|
||||
|
@ -1268,7 +1311,7 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
- (void*) nativeHandle
|
||||
{
|
||||
#if defined(__MINGW__)
|
||||
return (void*)_get_osfhandle(descriptor);
|
||||
return (void*)(SOCKET)_get_osfhandle(descriptor);
|
||||
#else
|
||||
return (void*)descriptor;
|
||||
#endif
|
||||
|
@ -1625,7 +1668,7 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
}
|
||||
else
|
||||
{
|
||||
(void)closesocket(descriptor);
|
||||
(void)closesocket((SOCKET)_get_osfhandle(descriptor));
|
||||
}
|
||||
#else
|
||||
(void)close(descriptor);
|
||||
|
@ -1660,12 +1703,14 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
|
||||
- (void) synchronizeFile
|
||||
{
|
||||
#if !defined(__MINGW__)
|
||||
if (isStandardFile)
|
||||
{
|
||||
#if defined(__MINGW__)
|
||||
(void)_commit(descriptor);
|
||||
#else
|
||||
(void)sync();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
- (void) truncateFileAtOffset: (unsigned long long)pos
|
||||
|
@ -1800,18 +1845,32 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1840,18 +1899,32 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1871,19 +1944,33 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1908,18 +1995,32 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1944,11 +2045,19 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
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);
|
||||
if (desc < 0)
|
||||
#endif
|
||||
if (desc == INVALID_SOCKET)
|
||||
{
|
||||
NSString *s;
|
||||
|
||||
|
@ -1962,8 +2071,13 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
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];
|
||||
|
@ -2041,8 +2155,13 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
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;
|
||||
|
||||
|
@ -2132,10 +2251,14 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
#if defined(__MINGW__)
|
||||
unsigned long dummy;
|
||||
|
||||
if (isSocket != YES)
|
||||
return;
|
||||
|
||||
if (flag)
|
||||
{
|
||||
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",
|
||||
GSLastErrorStr(errno));
|
||||
|
@ -2144,7 +2267,8 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
|
|||
else
|
||||
{
|
||||
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",
|
||||
GSLastErrorStr(errno));
|
||||
|
|
|
@ -51,7 +51,9 @@
|
|||
#include <unistd.h> /* for gethostname() */
|
||||
#endif
|
||||
|
||||
#ifndef __MINGW__
|
||||
#ifdef __MINGW__
|
||||
#define close closesocket
|
||||
#else
|
||||
#include <sys/param.h> /* for MAXHOSTNAMELEN */
|
||||
#include <sys/types.h>
|
||||
#include <netinet/in.h>
|
||||
|
@ -94,11 +96,12 @@
|
|||
#if defined(__svr4__)
|
||||
#include <sys/stropts.h>
|
||||
#endif
|
||||
#endif /* !__MINGW__ */
|
||||
|
||||
#ifdef __MINGW__
|
||||
#define close closesocket
|
||||
#endif
|
||||
#define SOCKET int
|
||||
#define SOCKET_ERROR -1
|
||||
#define INVALID_SOCKET -1
|
||||
|
||||
#endif /* !__MINGW__ */
|
||||
|
||||
static BOOL multi_threaded = NO;
|
||||
|
||||
|
@ -189,7 +192,7 @@ typedef enum {
|
|||
|
||||
@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. */
|
||||
NSMutableData *wData; /* Data object being written. */
|
||||
unsigned wLength; /* Ammount written so far. */
|
||||
|
@ -213,7 +216,7 @@ typedef enum {
|
|||
NSString *defaultAddress;
|
||||
}
|
||||
|
||||
+ (GSTcpHandle*) handleWithDescriptor: (int)d;
|
||||
+ (GSTcpHandle*) handleWithDescriptor: (SOCKET)d;
|
||||
- (BOOL) connectToPort: (GSTcpPort*)aPort beforeDate: (NSDate*)when;
|
||||
- (int) descriptor;
|
||||
- (void) invalidate;
|
||||
|
@ -238,7 +241,7 @@ typedef enum {
|
|||
NSHost *host; /* OpenStep host for this port. */
|
||||
NSString *address; /* Forced internet address. */
|
||||
gsu16 portNum; /* TCP port in host byte order. */
|
||||
int listener; /* Descriptor to listen on. */
|
||||
SOCKET listener;
|
||||
NSMapTable *handles; /* Handles indexed by socket. */
|
||||
}
|
||||
|
||||
|
@ -251,7 +254,7 @@ typedef enum {
|
|||
|
||||
- (void) addHandle: (GSTcpHandle*)handle forSend: (BOOL)send;
|
||||
- (NSString*) address;
|
||||
- (void) getFds: (int*)fds count: (int*)count;
|
||||
- (void) getFds: (SOCKET*)fds count: (int*)count;
|
||||
- (GSTcpHandle*) handleForPort: (GSTcpPort*)recvPort beforeDate: (NSDate*)when;
|
||||
- (void) handlePortMessage: (NSPortMessage*)m;
|
||||
- (NSHost*) host;
|
||||
|
@ -391,7 +394,7 @@ static Class runLoopClass;
|
|||
return nil;
|
||||
}
|
||||
|
||||
+ (GSTcpHandle*) handleWithDescriptor: (int)d
|
||||
+ (GSTcpHandle*) handleWithDescriptor: (SOCKET)d
|
||||
{
|
||||
GSTcpHandle *handle;
|
||||
#ifdef __MINGW__
|
||||
|
@ -400,14 +403,14 @@ static Class runLoopClass;
|
|||
int e;
|
||||
#endif /* __MINGW__ */
|
||||
|
||||
if (d < 0)
|
||||
if (d == INVALID_SOCKET)
|
||||
{
|
||||
NSLog(@"illegal descriptor (%d) for Tcp Handle", d);
|
||||
return nil;
|
||||
}
|
||||
#ifdef __MINGW__
|
||||
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",
|
||||
d, GSLastErrorStr(errno));
|
||||
|
@ -551,7 +554,8 @@ static Class runLoopClass;
|
|||
}
|
||||
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__
|
||||
if (WSAGetLastError() != WSAEWOULDBLOCK)
|
||||
|
@ -655,7 +659,7 @@ static Class runLoopClass;
|
|||
desc, inet_ntoa(sockAddr.sin_addr), ntohs(sockAddr.sin_port)];
|
||||
}
|
||||
|
||||
- (int) descriptor
|
||||
- (SOCKET) descriptor
|
||||
{
|
||||
return desc;
|
||||
}
|
||||
|
@ -723,7 +727,7 @@ static Class runLoopClass;
|
|||
* If we have been invalidated (desc < 0) then we should ignore this
|
||||
* event and remove ourself from the runloop.
|
||||
*/
|
||||
if (desc < 0)
|
||||
if (desc == INVALID_SOCKET)
|
||||
{
|
||||
NSRunLoop *l = [runLoopClass currentRunLoop];
|
||||
|
||||
|
@ -776,11 +780,7 @@ static Class runLoopClass;
|
|||
* Now try to fill the buffer with data.
|
||||
*/
|
||||
bytes = [rData mutableBytes];
|
||||
#ifdef __MINGW__
|
||||
res = recv(desc, bytes + rLength, want - rLength, 0);
|
||||
#else
|
||||
res = read(desc, bytes + rLength, want - rLength);
|
||||
#endif
|
||||
if (res <= 0)
|
||||
{
|
||||
if (res == 0)
|
||||
|
@ -1100,11 +1100,7 @@ static Class runLoopClass;
|
|||
{
|
||||
NSData *d = newDataWithEncodedPort([self recvPort]);
|
||||
|
||||
#ifdef __MINGW__
|
||||
len = send(desc, [d bytes], [d length], 0);
|
||||
#else
|
||||
len = write(desc, [d bytes], [d length]);
|
||||
#endif
|
||||
if (len == (int)[d length])
|
||||
{
|
||||
RELEASE(defaultAddress);
|
||||
|
@ -1147,11 +1143,7 @@ static Class runLoopClass;
|
|||
}
|
||||
b = [wData bytes];
|
||||
l = [wData length];
|
||||
#ifdef __MINGW__
|
||||
res = send(desc, b + wLength, l - wLength, 0);
|
||||
#else
|
||||
res = write(desc, b + wLength, l - wLength);
|
||||
#endif
|
||||
if (res < 0)
|
||||
{
|
||||
if (errno != EINTR && errno != EAGAIN)
|
||||
|
@ -1483,8 +1475,10 @@ static unsigned wordAlign;
|
|||
|
||||
if (shouldListen == YES && [thisHost isEqual: aHost])
|
||||
{
|
||||
#ifndef BROKEN_SO_REUSEADDR
|
||||
int reuse = 1; /* Should we re-use ports? */
|
||||
int desc;
|
||||
#endif
|
||||
SOCKET desc;
|
||||
BOOL addrOk = YES;
|
||||
struct sockaddr_in sockaddr;
|
||||
|
||||
|
@ -1521,7 +1515,8 @@ static unsigned wordAlign;
|
|||
NSLog(@"Bad address (%@) specified for listening port", addr);
|
||||
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));
|
||||
DESTROY(port);
|
||||
|
@ -1543,20 +1538,21 @@ static unsigned wordAlign;
|
|||
}
|
||||
#endif
|
||||
else if (bind(desc, (struct sockaddr *)&sockaddr,
|
||||
sizeof(sockaddr)) < 0)
|
||||
sizeof(sockaddr)) == SOCKET_ERROR)
|
||||
{
|
||||
NSLog(@"unable to bind to port %s:%d - %s",
|
||||
inet_ntoa(sockaddr.sin_addr), number, GSLastErrorStr(errno));
|
||||
(void) close(desc);
|
||||
DESTROY(port);
|
||||
}
|
||||
else if (listen(desc, 5) < 0)
|
||||
else if (listen(desc, 5) == SOCKET_ERROR)
|
||||
{
|
||||
NSLog(@"unable to listen on port - %s", GSLastErrorStr(errno));
|
||||
(void) close(desc);
|
||||
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));
|
||||
(void) close(desc);
|
||||
|
@ -1693,10 +1689,10 @@ static unsigned wordAlign;
|
|||
* This is a callback method used by the NSRunLoop class to determine which
|
||||
* descriptors to watch for the port.
|
||||
*/
|
||||
- (void) getFds: (int*)fds count: (int*)count
|
||||
- (void) getFds: (SOCKET*)fds count: (int*)count
|
||||
{
|
||||
NSMapEnumerator me;
|
||||
int sock;
|
||||
SOCKET sock;
|
||||
GSTcpHandle *handle;
|
||||
id recvSelf;
|
||||
|
||||
|
@ -1737,8 +1733,10 @@ static unsigned wordAlign;
|
|||
- (GSTcpHandle*) handleForPort: (GSTcpPort*)recvPort beforeDate: (NSDate*)when
|
||||
{
|
||||
NSMapEnumerator me;
|
||||
int sock;
|
||||
SOCKET sock;
|
||||
#ifndef BROKEN_SO_REUSEADDR
|
||||
int opt = 1;
|
||||
#endif
|
||||
GSTcpHandle *handle = nil;
|
||||
|
||||
M_LOCK(myLock);
|
||||
|
@ -1761,7 +1759,7 @@ static unsigned wordAlign;
|
|||
* Not found ... create a new handle.
|
||||
*/
|
||||
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));
|
||||
}
|
||||
|
@ -1916,7 +1914,7 @@ static unsigned wordAlign;
|
|||
extra: (void*)extra
|
||||
forMode: (NSString*)mode
|
||||
{
|
||||
int desc = (int)(gsaddr)extra;
|
||||
SOCKET desc = (SOCKET)(gsaddr)extra;
|
||||
GSTcpHandle *handle;
|
||||
|
||||
if (desc == listener)
|
||||
|
@ -1925,7 +1923,7 @@ static unsigned wordAlign;
|
|||
int size = sizeof(sockAddr);
|
||||
|
||||
desc = accept(listener, (struct sockaddr*)&sockAddr, &size);
|
||||
if (desc < 0)
|
||||
if (desc == INVALID_SOCKET)
|
||||
{
|
||||
NSDebugMLLog(@"NSPort", @"accept failed - handled in other thread?");
|
||||
}
|
||||
|
|
|
@ -702,13 +702,11 @@ static void setPollfd(int fd, int event, GSRunLoopCtxt *ctxt)
|
|||
GSCheckTasks();
|
||||
poll_return = 0;
|
||||
}
|
||||
#ifdef __MINGW__
|
||||
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;
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
/* 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_SET (port_fd_array[port_fd_count], &read_fds);
|
||||
if (fd > fdEnd)
|
||||
if (fd > fdEnd)
|
||||
fdEnd = fd;
|
||||
NSMapInsert(_rfdMap,
|
||||
(void*)port_fd_array[port_fd_count], info);
|
||||
|
@ -1023,13 +1021,11 @@ static void setPollfd(int fd, int event, GSRunLoopCtxt *ctxt)
|
|||
GSCheckTasks();
|
||||
select_return = 0;
|
||||
}
|
||||
#ifdef __MINGW__
|
||||
else if (errno == 0)
|
||||
{
|
||||
/* MinGW often returns an errno == 0. Not sure why */
|
||||
select_return = 0;
|
||||
/* Some systems return an errno == 0. Not sure why */
|
||||
select_return = 0;
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
/* Some exceptional condition happened. */
|
||||
|
|
|
@ -49,6 +49,9 @@
|
|||
#include <winsock2.h>
|
||||
#include <wininet.h>
|
||||
#define close closesocket
|
||||
#define SOCKET int
|
||||
#define SOCKET_ERROR -1
|
||||
#define INVALID_SOCKET -1
|
||||
#endif /* __MINGW__ */
|
||||
|
||||
|
||||
|
@ -58,17 +61,11 @@
|
|||
* NSPort which implements Distributed Objects communication between
|
||||
* hosts on a network. However, the GNUstep distributed objects system's
|
||||
* NSPort class uses TCP/IP for all of its communication. The GNUstep
|
||||
* <code>NSSocketPort</code>, then, is useful as a convenient method to
|
||||
* create and encapsulate BSD sockets:
|
||||
* <code>NSSocketPort</code>, then, is useful only as a method to
|
||||
* 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>
|
||||
* <example>
|
||||
* int fileDesc;
|
||||
* NSFileHandle *smtpHandle;
|
||||
*
|
||||
* fileDesc = [[NSSocketPort alloc] initRemoteWithTCPPort: 25
|
||||
* host: @"mail.example.com"];
|
||||
* smtpHandle = [[NSFileHandle alloc] initWithFileDescriptor: fileDesc];
|
||||
* </example>
|
||||
*/
|
||||
@implementation NSSocketPort
|
||||
|
||||
|
@ -143,29 +140,30 @@
|
|||
int s = -1;
|
||||
|
||||
if (addrData == nil)
|
||||
{
|
||||
NSDebugMLLog(@"NSSocketPort", @"Nil value passed for address.");
|
||||
goto iWPFAFailed;
|
||||
}
|
||||
{
|
||||
NSDebugMLLog(@"NSSocketPort", @"Nil value passed for address.");
|
||||
goto iWPFAFailed;
|
||||
}
|
||||
|
||||
s = socket(family, type, protocol);
|
||||
if (s == -1)
|
||||
{
|
||||
NSLog(@"socket: %s", GSLastErrorStr(errno));
|
||||
goto iWPFAFailed;
|
||||
}
|
||||
if (s == INVALID_SOCKET)
|
||||
{
|
||||
NSLog(@"socket: %s", GSLastErrorStr(errno));
|
||||
goto iWPFAFailed;
|
||||
}
|
||||
|
||||
if (bind(s, (struct sockaddr *)[addrData bytes], [addrData length]) == -1)
|
||||
{
|
||||
NSLog(@"bind: %s", GSLastErrorStr(errno));
|
||||
goto iWPFAFailed;
|
||||
}
|
||||
if (bind(s, (struct sockaddr *)[addrData bytes], [addrData length])
|
||||
== SOCKET_ERROR)
|
||||
{
|
||||
NSLog(@"bind: %s", GSLastErrorStr(errno));
|
||||
goto iWPFAFailed;
|
||||
}
|
||||
|
||||
if (listen(s, SOMAXCONN) == -1)
|
||||
{
|
||||
NSLog(@"listen: %s", GSLastErrorStr(errno));
|
||||
goto iWPFAFailed;
|
||||
}
|
||||
if (listen(s, SOMAXCONN) == SOCKET_ERROR)
|
||||
{
|
||||
NSLog(@"listen: %s", GSLastErrorStr(errno));
|
||||
goto iWPFAFailed;
|
||||
}
|
||||
|
||||
return [self initWithProtocolFamily: family
|
||||
socketType: type
|
||||
|
@ -214,10 +212,10 @@ iWPFAFailed:
|
|||
|
||||
address = [[[NSHost hostWithName: hostname] address] cString];
|
||||
if (address == NULL)
|
||||
{
|
||||
RELEASE(self);
|
||||
return nil;
|
||||
}
|
||||
{
|
||||
RELEASE(self);
|
||||
return nil;
|
||||
}
|
||||
|
||||
/* Clear memory, as recommended. */
|
||||
memset(&sa, 0, sizeof(struct sockaddr_in));
|
||||
|
@ -229,10 +227,10 @@ iWPFAFailed:
|
|||
|
||||
addrData = [NSData dataWithBytes: &sa length: sizeof(struct sockaddr_in)];
|
||||
if (addrData == nil)
|
||||
{
|
||||
RELEASE(self);
|
||||
return nil;
|
||||
}
|
||||
{
|
||||
RELEASE(self);
|
||||
return nil;
|
||||
}
|
||||
|
||||
return [self initRemoteWithProtocolFamily: PF_INET
|
||||
socketType: SOCK_STREAM
|
||||
|
@ -252,19 +250,19 @@ iWPFAFailed:
|
|||
address: (NSData *)addrData
|
||||
{
|
||||
if (addrData == nil)
|
||||
{
|
||||
NSDebugMLLog(@"NSSocketPort", @"Nil value passed for address.");
|
||||
RELEASE(self);
|
||||
return nil;
|
||||
}
|
||||
{
|
||||
NSDebugMLLog(@"NSSocketPort", @"Nil value passed for address.");
|
||||
RELEASE(self);
|
||||
return nil;
|
||||
}
|
||||
|
||||
_socket = socket(family, type, protocol);
|
||||
if (_socket == -1)
|
||||
{
|
||||
NSLog(@"socket: %s", GSLastErrorStr(errno));
|
||||
RELEASE(self);
|
||||
return nil;
|
||||
}
|
||||
if (_socket == INVALID_SOCKET)
|
||||
{
|
||||
NSLog(@"socket: %s", GSLastErrorStr(errno));
|
||||
RELEASE(self);
|
||||
return nil;
|
||||
}
|
||||
|
||||
_protocolFamily = family;
|
||||
_socketType = type;
|
||||
|
@ -283,18 +281,18 @@ iWPFAFailed:
|
|||
int len = SOCK_MAXADDRLEN;
|
||||
|
||||
if (_remoteAddrData != nil)
|
||||
{
|
||||
return _remoteAddrData;
|
||||
}
|
||||
{
|
||||
return _remoteAddrData;
|
||||
}
|
||||
else if (getsockname(_socket, (struct sockaddr *)&sa, &len) == 0)
|
||||
{
|
||||
return [NSData dataWithBytes: &sa length: len];
|
||||
}
|
||||
{
|
||||
return [NSData dataWithBytes: &sa length: len];
|
||||
}
|
||||
else
|
||||
{
|
||||
NSLog(@"getsockname: %s", GSLastErrorStr(errno));
|
||||
return nil;
|
||||
}
|
||||
{
|
||||
NSLog(@"getsockname: %s", GSLastErrorStr(errno));
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -357,23 +355,23 @@ iWPFAFailed:
|
|||
if (NSShouldRetainWithZone(self, zone) == YES)
|
||||
return RETAIN(self);
|
||||
else
|
||||
{
|
||||
NSSocketPort *copy = NSAllocateObject([self class], 0, zone);
|
||||
|
||||
if (copy != nil)
|
||||
{
|
||||
/* Insulate against NSPort changes. */
|
||||
[copy setDelegate: [self delegate]];
|
||||
NSSocketPort *copy = NSAllocateObject([self class], 0, zone);
|
||||
|
||||
copy->_socket = dup(_socket);
|
||||
copy->_protocolFamily = _protocolFamily;
|
||||
copy->_socketType = _socketType;
|
||||
copy->_protocol = _protocol;
|
||||
_remoteAddrData = [_remoteAddrData copyWithZone: zone];
|
||||
if (copy != nil)
|
||||
{
|
||||
/* Insulate against NSPort changes. */
|
||||
[copy setDelegate: [self delegate]];
|
||||
|
||||
copy->_socket = dup(_socket);
|
||||
copy->_protocolFamily = _protocolFamily;
|
||||
copy->_socketType = _socketType;
|
||||
copy->_protocol = _protocol;
|
||||
_remoteAddrData = [_remoteAddrData copyWithZone: zone];
|
||||
}
|
||||
|
||||
return RETAIN(copy);
|
||||
}
|
||||
|
||||
return RETAIN(copy);
|
||||
}
|
||||
}
|
||||
|
||||
/* NSCoding */
|
||||
|
|
Loading…
Reference in a new issue