@@ -18,29 +18,28 @@ #include #import "OFSPXSocket.h" #import "OFRunLoop.h" #import "OFRunLoop+Private.h" +#import "OFSocket.h" +#import "OFSocket+Private.h" #import "OFAlreadyConnectedException.h" #import "OFBindFailedException.h" #import "OFConnectionFailedException.h" #import "OFNotOpenException.h" -#import "socket.h" -#import "socket_helpers.h" - #ifndef NSPROTO_SPX # define NSPROTO_SPX 0 #endif -#define SPX_PACKET_TYPE 5 +static const uint8_t SPXPacketType = 5; @interface OFSPXSocket () -- (int)of_createSocketForAddress: (const of_socket_address_t *)address +- (int)of_createSocketForAddress: (const OFSocketAddress *)address errNo: (int *)errNo; -- (bool)of_connectSocketToAddress: (const of_socket_address_t *)address +- (bool)of_connectSocketToAddress: (const OFSocketAddress *)address errNo: (int *)errNo; - (void)of_closeSocket; @end OF_DIRECT_MEMBERS @@ -49,32 +48,32 @@ OFSPXSocket *_socket; unsigned char _node[IPX_NODE_LEN]; uint32_t _network; uint16_t _port; #ifdef OF_HAVE_BLOCKS - of_spx_socket_async_connect_block_t _block; + OFSPXSocketAsyncConnectBlock _block; #endif } - (instancetype)initWithSocket: (OFSPXSocket *)socket node: (unsigned char [IPX_NODE_LEN])node network: (uint32_t)network port: (uint16_t)port #ifdef OF_HAVE_BLOCKS - block: (of_spx_socket_async_connect_block_t)block + block: (OFSPXSocketAsyncConnectBlock)block #endif ; -- (void)startWithRunLoopMode: (of_run_loop_mode_t)runLoopMode; +- (void)startWithRunLoopMode: (OFRunLoopMode)runLoopMode; @end @implementation OFSPXSocketAsyncConnectDelegate - (instancetype)initWithSocket: (OFSPXSocket *)sock node: (unsigned char [IPX_NODE_LEN])node network: (uint32_t)network port: (uint16_t)port #ifdef OF_HAVE_BLOCKS - block: (of_spx_socket_async_connect_block_t)block + block: (OFSPXSocketAsyncConnectBlock)block #endif { self = [super init]; @try { @@ -101,28 +100,30 @@ #endif [super dealloc]; } -- (void)startWithRunLoopMode: (of_run_loop_mode_t)runLoopMode +- (void)startWithRunLoopMode: (OFRunLoopMode)runLoopMode { - of_socket_address_t address = - of_socket_address_ipx(_node, _network, _port); + OFSocketAddress address = + OFSocketAddressMakeIPX(_node, _network, _port); id exception = nil; int errNo; - if (![_socket of_createSocketForAddress: &address - errNo: &errNo]) { + if (![_socket of_createSocketForAddress: &address errNo: &errNo]) { exception = [self of_connectionFailedExceptionForErrNo: errNo]; goto inform_delegate; } _socket.canBlock = false; - if (![_socket of_connectSocketToAddress: &address - errNo: &errNo]) { + if (![_socket of_connectSocketToAddress: &address errNo: &errNo]) { +#ifdef OF_WINDOWS + if (errNo == EINPROGRESS || errNo == EWOULDBLOCK) { +#else if (errNo == EINPROGRESS) { +#endif [OFRunLoop of_addAsyncConnectForSocket: _socket mode: runLoopMode delegate: self]; return; } @@ -137,12 +138,11 @@ withObject: _socket withObject: exception afterDelay: 0]; } -- (void)of_socketDidConnect: (id)sock - exception: (id)exception +- (void)of_socketDidConnect: (id)sock exception: (id)exception { id delegate = ((OFSPXSocket *)sock).delegate; if (exception == nil) ((OFSPXSocket *)sock).canBlock = true; @@ -175,23 +175,24 @@ @end @implementation OFSPXSocket @dynamic delegate; -- (int)of_createSocketForAddress: (const of_socket_address_t *)address +- (int)of_createSocketForAddress: (const OFSocketAddress *)address errNo: (int *)errNo { #if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC) int flags; #endif - if (_socket != INVALID_SOCKET) + if (_socket != OFInvalidSocketHandle) @throw [OFAlreadyConnectedException exceptionWithSocket: self]; if ((_socket = socket(address->sockaddr.ipx.sipx_family, - SOCK_SEQPACKET | SOCK_CLOEXEC, NSPROTO_SPX)) == INVALID_SOCKET) { - *errNo = of_socket_errno(); + SOCK_SEQPACKET | SOCK_CLOEXEC, NSPROTO_SPX)) == + OFInvalidSocketHandle) { + *errNo = OFSocketErrNo(); return false; } #if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC) if ((flags = fcntl(_socket, F_GETFD, 0)) != -1) @@ -199,50 +200,47 @@ #endif return true; } -- (bool)of_connectSocketToAddress: (const of_socket_address_t *)address +- (bool)of_connectSocketToAddress: (const OFSocketAddress *)address errNo: (int *)errNo { - if (_socket == INVALID_SOCKET) + if (_socket == OFInvalidSocketHandle) @throw [OFNotOpenException exceptionWithObject: self]; if (connect(_socket, &address->sockaddr.sockaddr, address->length) != 0) { - *errNo = of_socket_errno(); + *errNo = OFSocketErrNo(); return false; } return true; } - (void)of_closeSocket { closesocket(_socket); - _socket = INVALID_SOCKET; + _socket = OFInvalidSocketHandle; } - (void)connectToNode: (unsigned char [_Nonnull IPX_NODE_LEN])node network: (uint32_t)network port: (uint16_t)port { - of_socket_address_t address = - of_socket_address_ipx(node, network, port); + OFSocketAddress address = OFSocketAddressMakeIPX(node, network, port); int errNo; - if (![self of_createSocketForAddress: &address - errNo: &errNo]) + if (![self of_createSocketForAddress: &address errNo: &errNo]) @throw [OFConnectionFailedException exceptionWithNode: node network: network port: port socket: self errNo: errNo]; - if (![self of_connectSocketToAddress: &address - errNo: &errNo]) { + if (![self of_connectSocketToAddress: &address errNo: &errNo]) { [self of_closeSocket]; @throw [OFConnectionFailedException exceptionWithNode: node network: network @@ -257,17 +255,17 @@ port: (uint16_t)port { [self asyncConnectToNode: node network: network port: port - runLoopMode: of_run_loop_mode_default]; + runLoopMode: OFDefaultRunLoopMode]; } - (void)asyncConnectToNode: (unsigned char [_Nonnull IPX_NODE_LEN])node network: (uint32_t)network port: (uint16_t)port - runLoopMode: (of_run_loop_mode_t)runLoopMode + runLoopMode: (OFRunLoopMode)runLoopMode { void *pool = objc_autoreleasePoolPush(); [[[[OFSPXSocketAsyncConnectDelegate alloc] initWithSocket: self @@ -284,24 +282,24 @@ #ifdef OF_HAVE_BLOCKS - (void)asyncConnectToNode: (unsigned char [_Nonnull IPX_NODE_LEN])node network: (uint32_t)network port: (uint16_t)port - block: (of_spx_socket_async_connect_block_t)block + block: (OFSPXSocketAsyncConnectBlock)block { [self asyncConnectToNode: node network: network port: port - runLoopMode: of_run_loop_mode_default + runLoopMode: OFDefaultRunLoopMode block: block]; } - (void)asyncConnectToNode: (unsigned char [_Nonnull IPX_NODE_LEN])node network: (uint32_t)network port: (uint16_t)port - runLoopMode: (of_run_loop_mode_t)runLoopMode - block: (of_spx_socket_async_connect_block_t)block + runLoopMode: (OFRunLoopMode)runLoopMode + block: (OFSPXSocketAsyncConnectBlock)block { void *pool = objc_autoreleasePoolPush(); [[[[OFSPXSocketAsyncConnectDelegate alloc] initWithSocket: self @@ -313,75 +311,76 @@ objc_autoreleasePoolPop(pool); } #endif -- (of_socket_address_t)bindToPort: (uint16_t)port +- (OFSocketAddress)bindToPort: (uint16_t)port { const unsigned char zeroNode[IPX_NODE_LEN] = { 0 }; - of_socket_address_t address; + OFSocketAddress address; #if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL_H) && defined(FD_CLOEXEC) int flags; #endif - if (_socket != INVALID_SOCKET) + if (_socket != OFInvalidSocketHandle) @throw [OFAlreadyConnectedException exceptionWithSocket: self]; - address = of_socket_address_ipx(zeroNode, 0, port); + address = OFSocketAddressMakeIPX(zeroNode, 0, port); if ((_socket = socket(address.sockaddr.sockaddr.sa_family, - SOCK_SEQPACKET | SOCK_CLOEXEC, NSPROTO_SPX)) == INVALID_SOCKET) + SOCK_SEQPACKET | SOCK_CLOEXEC, NSPROTO_SPX)) == + OFInvalidSocketHandle) @throw [OFBindFailedException exceptionWithPort: port - packetType: SPX_PACKET_TYPE + packetType: SPXPacketType socket: self - errNo: of_socket_errno()]; + errNo: OFSocketErrNo()]; _canBlock = true; #if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL_H) && defined(FD_CLOEXEC) if ((flags = fcntl(_socket, F_GETFD, 0)) != -1) fcntl(_socket, F_SETFD, flags | FD_CLOEXEC); #endif if (bind(_socket, &address.sockaddr.sockaddr, address.length) != 0) { - int errNo = of_socket_errno(); + int errNo = OFSocketErrNo(); closesocket(_socket); - _socket = INVALID_SOCKET; + _socket = OFInvalidSocketHandle; @throw [OFBindFailedException exceptionWithPort: port - packetType: SPX_PACKET_TYPE + packetType: SPXPacketType socket: self errNo: errNo]; } memset(&address, 0, sizeof(address)); - address.family = OF_SOCKET_ADDRESS_FAMILY_IPX; + address.family = OFSocketAddressFamilyIPX; address.length = (socklen_t)sizeof(address.sockaddr); - if (of_getsockname(_socket, &address.sockaddr.sockaddr, + if (OFGetSockName(_socket, &address.sockaddr.sockaddr, &address.length) != 0) { - int errNo = of_socket_errno(); + int errNo = OFSocketErrNo(); closesocket(_socket); - _socket = INVALID_SOCKET; + _socket = OFInvalidSocketHandle; @throw [OFBindFailedException exceptionWithPort: port - packetType: SPX_PACKET_TYPE + packetType: SPXPacketType socket: self errNo: errNo]; } if (address.sockaddr.sockaddr.sa_family != AF_IPX) { closesocket(_socket); - _socket = INVALID_SOCKET; + _socket = OFInvalidSocketHandle; @throw [OFBindFailedException exceptionWithPort: port - packetType: SPX_PACKET_TYPE + packetType: SPXPacketType socket: self errNo: EAFNOSUPPORT]; } return address; } @end