Overview
Comment: | Support for non-blocking UDP sockets |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
7de9d67be9b28235b3ba29fa0dc33cf9 |
User & Date: | js on 2018-08-04 16:23:38 |
Other Links: | manifest | tags |
Context
2018-08-04
| ||
16:49 | OFDNSResolver: Set UDP sockets to non-blocking check-in: 2b72aa8c86 user: js tags: trunk | |
16:23 | Support for non-blocking UDP sockets check-in: 7de9d67be9 user: js tags: trunk | |
15:50 | OFDNSResolver: Have one shared IPv4/IPv6 socket check-in: 0b475d18d5 user: js tags: trunk | |
Changes
Modified src/OFStream.m from [33902d6225] to [7dfe0e045a].
︙ | ︙ | |||
21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <assert.h> #include <errno.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "platform.h" #if !defined(OF_WINDOWS) && !defined(OF_MORPHOS) # include <signal.h> #endif | > > > > | 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <assert.h> #include <errno.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef HAVE_FCNTL_H # include <fcntl.h> #endif #include "platform.h" #if !defined(OF_WINDOWS) && !defined(OF_MORPHOS) # include <signal.h> #endif |
︙ | ︙ | |||
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 | - (bool)isBlocking { return _blocking; } - (void)setBlocking: (bool)enable { OF_UNRECOGNIZED_SELECTOR } - (int)fileDescriptorForReading { OF_UNRECOGNIZED_SELECTOR } | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 | - (bool)isBlocking { return _blocking; } - (void)setBlocking: (bool)enable { #if defined(HAVE_FCNTL) bool readImplemented = false, writeImplemented = false; @try { int readFlags; readFlags = fcntl([self fileDescriptorForReading], F_GETFL); readImplemented = true; if (readFlags == -1) @throw [OFSetOptionFailedException exceptionWithObject: self errNo: errno]; if (enable) readFlags &= ~O_NONBLOCK; else readFlags |= O_NONBLOCK; if (fcntl([self fileDescriptorForReading], F_SETFL, readFlags) == -1) @throw [OFSetOptionFailedException exceptionWithObject: self errNo: errno]; } @catch (OFNotImplementedException *e) { } @try { int writeFlags; writeFlags = fcntl([self fileDescriptorForWriting], F_GETFL); writeImplemented = true; if (writeFlags == -1) @throw [OFSetOptionFailedException exceptionWithObject: self errNo: errno]; if (enable) writeFlags &= ~O_NONBLOCK; else writeFlags |= O_NONBLOCK; if (fcntl([self fileDescriptorForWriting], F_SETFL, writeFlags) == -1) @throw [OFSetOptionFailedException exceptionWithObject: self errNo: errno]; } @catch (OFNotImplementedException *e) { } if (!readImplemented && !writeImplemented) @throw [OFNotImplementedException exceptionWithSelector: _cmd object: self]; _blocking = enable; #else OF_UNRECOGNIZED_SELECTOR #endif } - (int)fileDescriptorForReading { OF_UNRECOGNIZED_SELECTOR } |
︙ | ︙ |
Modified src/OFStreamSocket.m from [0965d972e9] to [8a940fe4f3].
︙ | ︙ | |||
18 19 20 21 22 23 24 | #define __NO_EXT_QNX #include "config.h" #include <errno.h> #include <string.h> | < < < < | 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #define __NO_EXT_QNX #include "config.h" #include <errno.h> #include <string.h> #import "OFStreamSocket.h" #import "OFInitializationFailedException.h" #import "OFNotImplementedException.h" #import "OFNotOpenException.h" #import "OFOutOfRangeException.h" #import "OFReadFailedException.h" |
︙ | ︙ | |||
124 125 126 127 128 129 130 131 132 | bytesWritten: 0 errNo: of_socket_errno()]; #endif return (size_t)bytesWritten; } - (void)setBlocking: (bool)enable { | > < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < < < > | 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | bytesWritten: 0 errNo: of_socket_errno()]; #endif return (size_t)bytesWritten; } #ifdef OF_WINDOWS - (void)setBlocking: (bool)enable { u_long v = enable; if (ioctlsocket(_socket, FIONBIO, &v) == SOCKET_ERROR) @throw [OFSetOptionFailedException exceptionWithObject: self errNo: of_socket_errno()]; _blocking = enable; } #endif - (int)fileDescriptorForReading { #ifndef OF_WINDOWS return _socket; #else if (_socket == INVALID_SOCKET) |
︙ | ︙ |
Modified src/OFTCPSocket.m from [9f8d11c4a6] to [61b4b17037].
︙ | ︙ | |||
283 284 285 286 287 288 289 290 291 292 293 294 295 296 | if ((_socket = socket(result->family, result->type | SOCK_CLOEXEC, result->protocol)) == INVALID_SOCKET) { errNo = of_socket_errno(); continue; } #if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC) if ((flags = fcntl(_socket, F_GETFD, 0)) != -1) fcntl(_socket, F_SETFD, flags | FD_CLOEXEC); #endif if (connect(_socket, result->address, | > > | 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | if ((_socket = socket(result->family, result->type | SOCK_CLOEXEC, result->protocol)) == INVALID_SOCKET) { errNo = of_socket_errno(); continue; } _blocking = true; #if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC) if ((flags = fcntl(_socket, F_GETFD, 0)) != -1) fcntl(_socket, F_SETFD, flags | FD_CLOEXEC); #endif if (connect(_socket, result->address, |
︙ | ︙ | |||
392 393 394 395 396 397 398 399 400 401 402 403 404 405 | results[0]->type | SOCK_CLOEXEC, results[0]->protocol)) == INVALID_SOCKET) @throw [OFBindFailedException exceptionWithHost: host port: port socket: self errNo: of_socket_errno()]; #if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC) if ((flags = fcntl(_socket, F_GETFD, 0)) != -1) fcntl(_socket, F_SETFD, flags | FD_CLOEXEC); #endif setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, | > > | 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | results[0]->type | SOCK_CLOEXEC, results[0]->protocol)) == INVALID_SOCKET) @throw [OFBindFailedException exceptionWithHost: host port: port socket: self errNo: of_socket_errno()]; _blocking = true; #if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC) if ((flags = fcntl(_socket, F_GETFD, 0)) != -1) fcntl(_socket, F_SETFD, flags | FD_CLOEXEC); #endif setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, |
︙ | ︙ | |||
629 630 631 632 633 634 635 | - (void)setKeepAliveEnabled: (bool)enabled { int v = enabled; if (setsockopt(_socket, SOL_SOCKET, SO_KEEPALIVE, (char *)&v, (socklen_t)sizeof(v)) != 0) @throw [OFSetOptionFailedException | | | 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 | - (void)setKeepAliveEnabled: (bool)enabled { int v = enabled; if (setsockopt(_socket, SOL_SOCKET, SO_KEEPALIVE, (char *)&v, (socklen_t)sizeof(v)) != 0) @throw [OFSetOptionFailedException exceptionWithObject: self errNo: of_socket_errno()]; } - (bool)isKeepAliveEnabled { int v; socklen_t len = sizeof(v); |
︙ | ︙ | |||
656 657 658 659 660 661 662 | - (void)setTCPNoDelayEnabled: (bool)enabled { int v = enabled; if (setsockopt(_socket, IPPROTO_TCP, TCP_NODELAY, (char *)&v, (socklen_t)sizeof(v)) != 0) @throw [OFSetOptionFailedException | | | 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 | - (void)setTCPNoDelayEnabled: (bool)enabled { int v = enabled; if (setsockopt(_socket, IPPROTO_TCP, TCP_NODELAY, (char *)&v, (socklen_t)sizeof(v)) != 0) @throw [OFSetOptionFailedException exceptionWithObject: self errNo: of_socket_errno()]; } - (bool)isTCPNoDelayEnabled { int v; socklen_t len = sizeof(v); |
︙ | ︙ |
Modified src/OFUDPSocket.h from [362b51f3c0] to [ad5fd6fb30].
︙ | ︙ | |||
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | @interface OFUDPSocket: OFObject <OFCopying, OFReadyForReadingObserving, OFReadyForWritingObserving> { of_socket_t _socket; #ifdef OF_WII uint16_t _port; #endif } /*! * @brief Returns a new, autoreleased OFUDPSocket. * * @return A new, autoreleased OFUDPSocket */ + (instancetype)socket; | > > > > > > > > | 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | @interface OFUDPSocket: OFObject <OFCopying, OFReadyForReadingObserving, OFReadyForWritingObserving> { of_socket_t _socket; #ifdef OF_WII uint16_t _port; #endif bool _blocking; } /*! * @brief Whether the socket is in blocking mode. * * By default, a socket is in blocking mode. */ @property (nonatomic, getter=isBlocking) bool blocking; /*! * @brief Returns a new, autoreleased OFUDPSocket. * * @return A new, autoreleased OFUDPSocket */ + (instancetype)socket; |
︙ | ︙ |
Modified src/OFUDPSocket.m from [6754e57a6e] to [6cab00e4d5].
︙ | ︙ | |||
34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #import "OFBindFailedException.h" #import "OFInitializationFailedException.h" #import "OFInvalidArgumentException.h" #import "OFNotOpenException.h" #import "OFOutOfRangeException.h" #import "OFReadFailedException.h" #import "OFWriteFailedException.h" #import "socket.h" #import "socket_helpers.h" #import "resolver.h" #ifdef OF_HAVE_THREADS | > | 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #import "OFBindFailedException.h" #import "OFInitializationFailedException.h" #import "OFInvalidArgumentException.h" #import "OFNotOpenException.h" #import "OFOutOfRangeException.h" #import "OFReadFailedException.h" #import "OFSetOptionFailedException.h" #import "OFWriteFailedException.h" #import "socket.h" #import "socket_helpers.h" #import "resolver.h" #ifdef OF_HAVE_THREADS |
︙ | ︙ | |||
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | #endif - (instancetype)init { self = [super init]; _socket = INVALID_SOCKET; return self; } - (void)dealloc { if (_socket != INVALID_SOCKET) [self close]; [super dealloc]; } - (id)copy { return [self retain]; } - (uint16_t)bindToHost: (OFString *)host port: (uint16_t)port { of_resolver_result_t **results; #if !defined(OF_WII) && !defined(OF_NINTENDO_3DS) union { | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | #endif - (instancetype)init { self = [super init]; _socket = INVALID_SOCKET; _blocking = true; return self; } - (void)dealloc { if (_socket != INVALID_SOCKET) [self close]; [super dealloc]; } - (id)copy { return [self retain]; } - (bool)isBlocking { return _blocking; } - (void)setBlocking: (bool)enable { #if defined(HAVE_FCNTL) int flags = fcntl(_socket, F_GETFL); if (flags == -1) @throw [OFSetOptionFailedException exceptionWithObject: self errNo: errno]; if (enable) flags &= ~O_NONBLOCK; else flags |= O_NONBLOCK; if (fcntl(_socket, F_SETFL, flags) == -1) @throw [OFSetOptionFailedException exceptionWithObject: self errNo: errno]; _blocking = enable; #elif defined(OF_WINDOWS) u_long v = enable; if (ioctlsocket(_socket, FIONBIO, &v) == SOCKET_ERROR) @throw [OFSetOptionFailedException exceptionWithObject: self errNo: of_socket_errno()]; _blocking = enable; #else OF_UNRECOGNIZED_SELECTOR #endif } - (uint16_t)bindToHost: (OFString *)host port: (uint16_t)port { of_resolver_result_t **results; #if !defined(OF_WII) && !defined(OF_NINTENDO_3DS) union { |
︙ | ︙ | |||
299 300 301 302 303 304 305 306 307 308 309 310 311 312 | results[0]->type | SOCK_CLOEXEC, results[0]->protocol)) == INVALID_SOCKET) @throw [OFBindFailedException exceptionWithHost: host port: port socket: self errNo: of_socket_errno()]; #if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC) if ((flags = fcntl(_socket, F_GETFD, 0)) != -1) fcntl(_socket, F_SETFD, flags | FD_CLOEXEC); #endif #if defined(OF_WII) || defined(OF_NINTENDO_3DS) | > > | 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | results[0]->type | SOCK_CLOEXEC, results[0]->protocol)) == INVALID_SOCKET) @throw [OFBindFailedException exceptionWithHost: host port: port socket: self errNo: of_socket_errno()]; _blocking = true; #if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC) if ((flags = fcntl(_socket, F_GETFD, 0)) != -1) fcntl(_socket, F_SETFD, flags | FD_CLOEXEC); #endif #if defined(OF_WII) || defined(OF_NINTENDO_3DS) |
︙ | ︙ |
Modified src/exceptions/OFSetOptionFailedException.h from [ae0347307b] to [225ac67fef].
︙ | ︙ | |||
15 16 17 18 19 20 21 | * file. */ #import "OFException.h" OF_ASSUME_NONNULL_BEGIN | < < | | | | | | | | | 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | * file. */ #import "OFException.h" OF_ASSUME_NONNULL_BEGIN /*! * @class OFSetOptionFailedException \ * OFSetOptionFailedException.h ObjFW/OFSetOptionFailedException.h * * @brief An exception indicating that setting an option for an object failed. */ @interface OFSetOptionFailedException: OFException { id _object; int _errNo; } /*! * @brief The object for which the option could not be set. */ @property (readonly, nonatomic) id object; /*! * @brief The errno of the error that occurred. */ @property (readonly, nonatomic) int errNo; + (instancetype)exception OF_UNAVAILABLE; /*! * @brief Creates a new, autoreleased set option failed exception. * * @param object The object for which the option could not be set * @param errNo The errno of the error that occurred * @return A new, autoreleased set option failed exception */ + (instancetype)exceptionWithObject: (id)object errNo: (int)errNo; - (instancetype)init OF_UNAVAILABLE; /*! * @brief Initializes an already allocated set option failed exception. * * @param object The object for which the option could not be set * @param errNo The errno of the error that occurred * @return An initialized set option failed exception */ - (instancetype)initWithObject: (id)object errNo: (int)errNo OF_DESIGNATED_INITIALIZER; @end OF_ASSUME_NONNULL_END |
Modified src/exceptions/OFSetOptionFailedException.m from [0968265285] to [bc527db97a].
︙ | ︙ | |||
15 16 17 18 19 20 21 | * file. */ #include "config.h" #import "OFSetOptionFailedException.h" #import "OFString.h" | < | | | | | | | | | 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | * file. */ #include "config.h" #import "OFSetOptionFailedException.h" #import "OFString.h" @implementation OFSetOptionFailedException @synthesize object = _object, errNo = _errNo; + (instancetype)exception { OF_UNRECOGNIZED_SELECTOR } + (instancetype)exceptionWithObject: (id)object errNo: (int)errNo { return [[[self alloc] initWithObject: object errNo: errNo] autorelease]; } - (instancetype)init { OF_INVALID_INIT_METHOD } - (instancetype)initWithObject: (id)object errNo: (int)errNo { self = [super init]; _object = [object retain]; _errNo = errNo; return self; } - (void)dealloc { [_object release]; [super dealloc]; } - (OFString *)description { return [OFString stringWithFormat: @"Setting an option in an object of type %@ failed: %@", [_object class], of_strerror(_errNo)]; } @end |