@@ -13,20 +13,27 @@ * file. */ #include "config.h" +#ifndef _XOPEN_SOURCE_EXTENDED +# define _XOPEN_SOURCE_EXTENDED +#endif +#define _HPUX_ALT_XOPEN_SOCKET_API + #include #ifdef HAVE_FCNTL_H # include #endif #import "OFDatagramSocket.h" #import "OFData.h" -#import "OFRunLoop+Private.h" #import "OFRunLoop.h" +#import "OFRunLoop+Private.h" +#import "OFSocket.h" +#import "OFSocket+Private.h" #import "OFGetOptionFailedException.h" #import "OFInitializationFailedException.h" #import "OFNotOpenException.h" #import "OFOutOfRangeException.h" @@ -33,22 +40,19 @@ #import "OFReadFailedException.h" #import "OFSetOptionFailedException.h" #import "OFSetOptionFailedException.h" #import "OFWriteFailedException.h" -#import "socket.h" -#import "socket_helpers.h" - @implementation OFDatagramSocket @synthesize delegate = _delegate; + (void)initialize { if (self != [OFDatagramSocket class]) return; - if (!of_socket_init()) + if (!OFSocketInit()) @throw [OFInitializationFailedException exceptionWithClass: self]; } + (instancetype)socket @@ -64,11 +68,11 @@ if (self.class == [OFDatagramSocket class]) { [self doesNotRecognizeSelector: _cmd]; abort(); } - _socket = INVALID_SOCKET; + _socket = OFInvalidSocketHandle; _canBlock = true; } @catch (id e) { [self release]; @throw e; } @@ -76,11 +80,11 @@ return self; } - (void)dealloc { - if (_socket != INVALID_SOCKET) + if (_socket != OFInvalidSocketHandle) [self close]; [super dealloc]; } @@ -112,16 +116,16 @@ @throw [OFSetOptionFailedException exceptionWithObject: self errNo: errno]; _canBlock = canBlock; #elif defined(OF_WINDOWS) - u_long v = canBlock; + u_long v = !canBlock; if (ioctlsocket(_socket, FIONBIO, &v) == SOCKET_ERROR) @throw [OFSetOptionFailedException exceptionWithObject: self - errNo: of_socket_errno()]; + errNo: OFSocketErrNo()]; _canBlock = canBlock; #else OF_UNRECOGNIZED_SELECTOR #endif @@ -133,11 +137,11 @@ if (setsockopt(_socket, SOL_SOCKET, SO_BROADCAST, (char *)&v, (socklen_t)sizeof(v)) != 0) @throw [OFSetOptionFailedException exceptionWithObject: self - errNo: of_socket_errno()]; + errNo: OFSocketErrNo()]; #ifdef OF_WII _canSendToBroadcastAddresses = canSendToBroadcastAddresses; #endif } @@ -150,25 +154,25 @@ if (getsockopt(_socket, SOL_SOCKET, SO_BROADCAST, (char *)&v, &len) != 0 || len != sizeof(v)) @throw [OFGetOptionFailedException exceptionWithObject: self - errNo: of_socket_errno()]; + errNo: OFSocketErrNo()]; return v; #else return _canSendToBroadcastAddresses; #endif } - (size_t)receiveIntoBuffer: (void *)buffer length: (size_t)length - sender: (of_socket_address_t *)sender + sender: (OFSocketAddress *)sender { ssize_t ret; - if (_socket == INVALID_SOCKET) + if (_socket == OFInvalidSocketHandle) @throw [OFNotOpenException exceptionWithObject: self]; sender->length = (socklen_t)sizeof(sender->sockaddr); #ifndef OF_WINDOWS @@ -175,56 +179,55 @@ if ((ret = recvfrom(_socket, buffer, length, 0, &sender->sockaddr.sockaddr, &sender->length)) < 0) @throw [OFReadFailedException exceptionWithObject: self requestedLength: length - errNo: of_socket_errno()]; + errNo: OFSocketErrNo()]; #else if (length > INT_MAX) @throw [OFOutOfRangeException exception]; if ((ret = recvfrom(_socket, buffer, (int)length, 0, &sender->sockaddr.sockaddr, &sender->length)) < 0) @throw [OFReadFailedException exceptionWithObject: self requestedLength: length - errNo: of_socket_errno()]; + errNo: OFSocketErrNo()]; #endif switch (sender->sockaddr.sockaddr.sa_family) { case AF_INET: - sender->family = OF_SOCKET_ADDRESS_FAMILY_IPV4; + sender->family = OFSocketAddressFamilyIPv4; break; #ifdef OF_HAVE_IPV6 case AF_INET6: - sender->family = OF_SOCKET_ADDRESS_FAMILY_IPV6; + sender->family = OFSocketAddressFamilyIPv6; break; #endif #ifdef OF_HAVE_IPX case AF_IPX: - sender->family = OF_SOCKET_ADDRESS_FAMILY_IPX; + sender->family = OFSocketAddressFamilyIPX; break; #endif default: - sender->family = OF_SOCKET_ADDRESS_FAMILY_UNKNOWN; + sender->family = OFSocketAddressFamilyUnknown; break; } return ret; } -- (void)asyncReceiveIntoBuffer: (void *)buffer - length: (size_t)length +- (void)asyncReceiveIntoBuffer: (void *)buffer length: (size_t)length { [self asyncReceiveIntoBuffer: buffer length: length - runLoopMode: of_run_loop_mode_default]; + runLoopMode: OFDefaultRunLoopMode]; } - (void)asyncReceiveIntoBuffer: (void *)buffer length: (size_t)length - runLoopMode: (of_run_loop_mode_t)runLoopMode + runLoopMode: (OFRunLoopMode)runLoopMode { [OFRunLoop of_addAsyncReceiveForDatagramSocket: self buffer: buffer length: length mode: runLoopMode @@ -235,22 +238,22 @@ } #ifdef OF_HAVE_BLOCKS - (void)asyncReceiveIntoBuffer: (void *)buffer length: (size_t)length - block: (of_datagram_socket_async_receive_block_t)block + block: (OFDatagramSocketAsyncReceiveBlock)block { [self asyncReceiveIntoBuffer: buffer length: length - runLoopMode: of_run_loop_mode_default + runLoopMode: OFDefaultRunLoopMode block: block]; } - (void)asyncReceiveIntoBuffer: (void *)buffer length: (size_t)length - runLoopMode: (of_run_loop_mode_t)runLoopMode - block: (of_datagram_socket_async_receive_block_t)block + runLoopMode: (OFRunLoopMode)runLoopMode + block: (OFDatagramSocketAsyncReceiveBlock)block { [OFRunLoop of_addAsyncReceiveForDatagramSocket: self buffer: buffer length: length mode: runLoopMode @@ -259,13 +262,13 @@ } #endif - (void)sendBuffer: (const void *)buffer length: (size_t)length - receiver: (const of_socket_address_t *)receiver + receiver: (const OFSocketAddress *)receiver { - if (_socket == INVALID_SOCKET) + if (_socket == OFInvalidSocketHandle) @throw [OFNotOpenException exceptionWithObject: self]; #ifndef OF_WINDOWS ssize_t bytesWritten; @@ -277,11 +280,11 @@ receiver->length)) < 0) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length bytesWritten: 0 - errNo: of_socket_errno()]; + errNo: OFSocketErrNo()]; #else int bytesWritten; if (length > INT_MAX) @throw [OFOutOfRangeException exception]; @@ -290,11 +293,11 @@ &receiver->sockaddr.sockaddr, receiver->length)) < 0) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length bytesWritten: 0 - errNo: of_socket_errno()]; + errNo: OFSocketErrNo()]; #endif if ((size_t)bytesWritten != length) @throw [OFWriteFailedException exceptionWithObject: self requestedLength: length @@ -301,20 +304,20 @@ bytesWritten: bytesWritten errNo: 0]; } - (void)asyncSendData: (OFData *)data - receiver: (const of_socket_address_t *)receiver + receiver: (const OFSocketAddress *)receiver { [self asyncSendData: data receiver: receiver - runLoopMode: of_run_loop_mode_default]; + runLoopMode: OFDefaultRunLoopMode]; } - (void)asyncSendData: (OFData *)data - receiver: (const of_socket_address_t *)receiver - runLoopMode: (of_run_loop_mode_t)runLoopMode + receiver: (const OFSocketAddress *)receiver + runLoopMode: (OFRunLoopMode)runLoopMode { [OFRunLoop of_addAsyncSendForDatagramSocket: self data: data receiver: receiver mode: runLoopMode @@ -324,23 +327,23 @@ delegate: _delegate]; } #ifdef OF_HAVE_BLOCKS - (void)asyncSendData: (OFData *)data - receiver: (const of_socket_address_t *)receiver - block: (of_datagram_socket_async_send_data_block_t)block + receiver: (const OFSocketAddress *)receiver + block: (OFDatagramSocketAsyncSendDataBlock)block { [self asyncSendData: data receiver: receiver - runLoopMode: of_run_loop_mode_default + runLoopMode: OFDefaultRunLoopMode block: block]; } - (void)asyncSendData: (OFData *)data - receiver: (const of_socket_address_t *)receiver - runLoopMode: (of_run_loop_mode_t)runLoopMode - block: (of_datagram_socket_async_send_data_block_t)block + receiver: (const OFSocketAddress *)receiver + runLoopMode: (OFRunLoopMode)runLoopMode + block: (OFDatagramSocketAsyncSendDataBlock)block { [OFRunLoop of_addAsyncSendForDatagramSocket: self data: data receiver: receiver mode: runLoopMode @@ -350,19 +353,19 @@ #endif - (void)cancelAsyncRequests { [OFRunLoop of_cancelAsyncRequestsForObject: self - mode: of_run_loop_mode_default]; + mode: OFDefaultRunLoopMode]; } - (int)fileDescriptorForReading { #ifndef OF_WINDOWS return _socket; #else - if (_socket == INVALID_SOCKET) + if (_socket == OFInvalidSocketHandle) return -1; if (_socket > INT_MAX) @throw [OFOutOfRangeException exception]; @@ -373,11 +376,11 @@ - (int)fileDescriptorForWriting { #ifndef OF_WINDOWS return _socket; #else - if (_socket == INVALID_SOCKET) + if (_socket == OFInvalidSocketHandle) return -1; if (_socket > INT_MAX) @throw [OFOutOfRangeException exception]; @@ -385,12 +388,12 @@ #endif } - (void)close { - if (_socket == INVALID_SOCKET) + if (_socket == OFInvalidSocketHandle) @throw [OFNotOpenException exceptionWithObject: self]; closesocket(_socket); - _socket = INVALID_SOCKET; + _socket = OFInvalidSocketHandle; } @end