Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -22,13 +22,13 @@ OFNumber.m \ OFObject.m \ ${OFPLUGIN_M} \ OFSeekableStream.m \ OFSHA1Hash.m \ - OFSocket.m \ OFStream.m \ OFStreamObserver.m \ + OFStreamSocket.m \ OFString.m \ OFString+Hashing.m \ OFString+URLEncoding.m \ OFString+XMLEscaping.m \ OFString+XMLUnescaping.m \ Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -448,11 +448,11 @@ { self = [super initWithClass: class_]; requestedSize = size; - if ([class_ isSubclassOfClass: [OFSocket class]]) + if ([class_ isSubclassOfClass: [OFStreamSocket class]]) errNo = GET_SOCK_ERRNO; else errNo = GET_ERRNO; return self; DELETED src/OFSocket.h Index: src/OFSocket.h ================================================================== --- src/OFSocket.h +++ src/OFSocket.h @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2008 - 2010 - * Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE included in - * the packaging of this file. - */ - -#import "OFStream.h" - -#ifdef _WIN32 -# define _WIN32_WINNT 0x0501 -# include -#endif - -/** - * \brief A class which provides functions to create and use sockets. - */ -@interface OFSocket: OFStream -{ -@public -#ifndef _WIN32 - int sock; -#else - SOCKET sock; -#endif - BOOL listening; -@protected - BOOL eos; -} - -/** - * \return A new autoreleased OFTCPSocket - */ -+ socket; - -/** - * Enables/disables non-blocking I/O. - */ -- (void)setBlocking: (BOOL)enable; -@end DELETED src/OFSocket.m Index: src/OFSocket.m ================================================================== --- src/OFSocket.m +++ src/OFSocket.m @@ -1,127 +0,0 @@ -/* - * Copyright (c) 2008 - 2010 - * Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE included in - * the packaging of this file. - */ - -#include "config.h" - -#include -#include -#include - -#ifndef _WIN32 -# include -# include -#endif - -#import "OFSocket.h" -#import "OFExceptions.h" - -#ifndef INVALID_SOCKET -# define INVALID_SOCKET -1 -#endif - -@implementation OFSocket -#ifdef _WIN32 -+ (void)initialize -{ - WSADATA wsa; - - if (self != [OFSocket class]) - return; - - if (WSAStartup(MAKEWORD(2, 0), &wsa)) - @throw [OFInitializationFailedException newWithClass: self]; -} -#endif - -+ socket -{ - return [[[self alloc] init] autorelease]; -} - -- (BOOL)_atEndOfStream -{ - return eos; -} - -- (size_t)_readNBytes: (size_t)size - intoBuffer: (char*)buf -{ - ssize_t ret; - - if (sock == INVALID_SOCKET) - @throw [OFNotConnectedException newWithClass: isa]; - -#ifndef _WIN32 - /* FIXME: We want a sane error message on Win32 as well */ - if (eos) - errno = ENOTCONN; -#endif - - if (eos || (ret = recv(sock, buf, size, 0)) < 0) - @throw [OFReadFailedException newWithClass: isa - size: size]; - - if (ret == 0) - eos = YES; - - return ret; -} - -- (size_t)_writeNBytes: (size_t)size - fromBuffer: (const char*)buf -{ - ssize_t ret; - - if (sock == INVALID_SOCKET) - @throw [OFNotConnectedException newWithClass: isa]; - -#ifndef _WIN32 - /* FIXME: We want a sane error message on Win32 as well */ - if (eos) - errno = ENOTCONN; -#endif - - if (eos || (ret = send(sock, buf, size, 0)) == -1) - @throw [OFWriteFailedException newWithClass: isa - size: size]; - - /* This is safe, as we already checked for -1 */ - return ret; -} - -- (void)setBlocking: (BOOL)enable -{ -#ifndef _WIN32 - int flags; - - if ((flags = fcntl(sock, F_GETFL)) == -1) - @throw [OFSetOptionFailedException newWithClass: isa]; - - if (enable) - flags &= ~O_NONBLOCK; - else - flags |= O_NONBLOCK; - - if (fcntl(sock, F_SETFL, flags) == -1) - @throw [OFSetOptionFailedException newWithClass: isa]; -#else - u_long v = enable; - - if (ioctlsocket(sock, FIONBIO, &v) == SOCKET_ERROR) - @throw [OFSetOptionFailedException newWithClass: isa]; -#endif -} - -- (int)fileDescriptor -{ - return sock; -} -@end ADDED src/OFStreamSocket.h Index: src/OFStreamSocket.h ================================================================== --- src/OFStreamSocket.h +++ src/OFStreamSocket.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2008 - 2010 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +#import "OFStream.h" + +#ifdef _WIN32 +# define _WIN32_WINNT 0x0501 +# include +#endif + +/** + * \brief A class which provides functions to create and use stream sockets. + */ +@interface OFStreamSocket: OFStream +{ +@public +#ifndef _WIN32 + int sock; +#else + SOCKET sock; +#endif + BOOL listening; +@protected + BOOL eos; +} + +/** + * \return A new autoreleased OFTCPSocket + */ ++ socket; + +/** + * Enables/disables non-blocking I/O. + */ +- (void)setBlocking: (BOOL)enable; +@end ADDED src/OFStreamSocket.m Index: src/OFStreamSocket.m ================================================================== --- src/OFStreamSocket.m +++ src/OFStreamSocket.m @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2008 - 2010 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +#include "config.h" + +#include +#include +#include + +#ifndef _WIN32 +# include +# include +#endif + +#import "OFStreamSocket.h" +#import "OFExceptions.h" + +#ifndef INVALID_SOCKET +# define INVALID_SOCKET -1 +#endif + +@implementation OFStreamSocket +#ifdef _WIN32 ++ (void)initialize +{ + WSADATA wsa; + + if (self != [OFStreamSocket class]) + return; + + if (WSAStartup(MAKEWORD(2, 0), &wsa)) + @throw [OFInitializationFailedException newWithClass: self]; +} +#endif + ++ socket +{ + return [[[self alloc] init] autorelease]; +} + +- (BOOL)_atEndOfStream +{ + return eos; +} + +- (size_t)_readNBytes: (size_t)size + intoBuffer: (char*)buf +{ + ssize_t ret; + + if (sock == INVALID_SOCKET) + @throw [OFNotConnectedException newWithClass: isa]; + +#ifndef _WIN32 + /* FIXME: We want a sane error message on Win32 as well */ + if (eos) + errno = ENOTCONN; +#endif + + if (eos || (ret = recv(sock, buf, size, 0)) < 0) + @throw [OFReadFailedException newWithClass: isa + size: size]; + + if (ret == 0) + eos = YES; + + return ret; +} + +- (size_t)_writeNBytes: (size_t)size + fromBuffer: (const char*)buf +{ + ssize_t ret; + + if (sock == INVALID_SOCKET) + @throw [OFNotConnectedException newWithClass: isa]; + +#ifndef _WIN32 + /* FIXME: We want a sane error message on Win32 as well */ + if (eos) + errno = ENOTCONN; +#endif + + if (eos || (ret = send(sock, buf, size, 0)) == -1) + @throw [OFWriteFailedException newWithClass: isa + size: size]; + + /* This is safe, as we already checked for -1 */ + return ret; +} + +- (void)setBlocking: (BOOL)enable +{ +#ifndef _WIN32 + int flags; + + if ((flags = fcntl(sock, F_GETFL)) == -1) + @throw [OFSetOptionFailedException newWithClass: isa]; + + if (enable) + flags &= ~O_NONBLOCK; + else + flags |= O_NONBLOCK; + + if (fcntl(sock, F_SETFL, flags) == -1) + @throw [OFSetOptionFailedException newWithClass: isa]; +#else + u_long v = enable; + + if (ioctlsocket(sock, FIONBIO, &v) == SOCKET_ERROR) + @throw [OFSetOptionFailedException newWithClass: isa]; +#endif +} + +- (int)fileDescriptor +{ + return sock; +} +@end Index: src/OFTCPSocket.h ================================================================== --- src/OFTCPSocket.h +++ src/OFTCPSocket.h @@ -13,11 +13,11 @@ # include # include # include #endif -#import "OFSocket.h" +#import "OFStreamSocket.h" #ifdef _WIN32 # include #endif @@ -24,11 +24,11 @@ @class OFString; /** * \brief A class which provides functions to create and use TCP sockets. */ -@interface OFTCPSocket: OFSocket +@interface OFTCPSocket: OFStreamSocket { struct sockaddr *sockAddr; socklen_t sockAddrLen; } Index: src/ObjFW.h ================================================================== --- src/ObjFW.h +++ src/ObjFW.h @@ -26,16 +26,16 @@ #import "OFEnumerator.h" #import "OFNumber.h" #import "OFStream.h" +#import "OFStreamObserver.h" #import "OFFile.h" -#import "OFSocket.h" +#import "OFStreamSocket.h" #import "OFTCPSocket.h" -#import "OFSocketObserver.h" #import "OFHash.h" #import "OFMD5Hash.h" #import "OFSHA1Hash.h"