Index: src/OFStreamSocket.m ================================================================== --- src/OFStreamSocket.m +++ src/OFStreamSocket.m @@ -67,11 +67,12 @@ intoBuffer: (char*)buf { ssize_t ret; if (sock == INVALID_SOCKET) - @throw [OFNotConnectedException newWithClass: isa]; + @throw [OFNotConnectedException newWithClass: isa + socket: self]; if (eos) { OFReadFailedException *e; e = [OFReadFailedException newWithClass: isa @@ -99,11 +100,12 @@ fromBuffer: (const char*)buf { ssize_t ret; if (sock == INVALID_SOCKET) - @throw [OFNotConnectedException newWithClass: isa]; + @throw [OFNotConnectedException newWithClass: isa + socket: self]; if (eos) { OFWriteFailedException *e; e = [OFWriteFailedException newWithClass: isa @@ -142,14 +144,23 @@ } - (void)close { if (sock == INVALID_SOCKET) - @throw [OFNotConnectedException newWithClass: isa]; + @throw [OFNotConnectedException newWithClass: isa + socket: self]; close(sock); sock = INVALID_SOCKET; eos = NO; listening = NO; } + +- (void)dealloc +{ + if (sock != INVALID_SOCKET) + [self close]; + + [super dealloc]; +} @end Index: src/OFTCPSocket.m ================================================================== --- src/OFTCPSocket.m +++ src/OFTCPSocket.m @@ -80,11 +80,12 @@ - (void)connectToHost: (OFString*)host onPort: (uint16_t)port { if (sock != INVALID_SOCKET) - @throw [OFAlreadyConnectedException newWithClass: isa]; + @throw [OFAlreadyConnectedException newWithClass: isa + socket: self]; #ifdef HAVE_THREADSAFE_GETADDRINFO struct addrinfo hints, *res, *res0; char port_s[7]; @@ -93,10 +94,11 @@ hints.ai_socktype = SOCK_STREAM; snprintf(port_s, 7, "%" PRIu16, port); if (getaddrinfo([host cString], port_s, &hints, &res0)) @throw [OFAddressTranslationFailedException newWithClass: isa + socket: self host: host]; for (res = res0; res != NULL; res = res->ai_next) { if ((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == INVALID_SOCKET) @@ -142,10 +144,11 @@ # ifdef OF_THREADS [addrlist release]; [mutex unlock]; # endif @throw [OFConnectionFailedException newWithClass: isa + socket: self host: host port: port]; } # ifdef OF_THREADS @@ -185,19 +188,21 @@ } #endif if (sock == INVALID_SOCKET) @throw [OFConnectionFailedException newWithClass: isa + socket: self host: host port: port]; } - (void)bindToPort: (uint16_t)port onHost: (OFString*)host { if (sock != INVALID_SOCKET) - @throw [OFAlreadyConnectedException newWithClass: isa]; + @throw [OFAlreadyConnectedException newWithClass: isa + socket: self]; #ifdef HAVE_THREADSAFE_GETADDRINFO struct addrinfo hints, *res; char port_s[7]; @@ -206,22 +211,25 @@ hints.ai_socktype = SOCK_STREAM; snprintf(port_s, 7, "%" PRIu16, port); if (getaddrinfo([host cString], port_s, &hints, &res)) @throw [OFAddressTranslationFailedException newWithClass: isa + socket: self host: host]; if ((sock = socket(res->ai_family, SOCK_STREAM, 0)) == INVALID_SOCKET) @throw [OFBindFailedException newWithClass: isa + socket: self host: host port: port]; if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) { freeaddrinfo(res); close(sock); sock = INVALID_SOCKET; @throw [OFBindFailedException newWithClass: isa + socket: self host: host port: port]; } freeaddrinfo(res); @@ -274,26 +282,30 @@ } - (void)listenWithBackLog: (int)backlog { if (sock == INVALID_SOCKET) - @throw [OFNotConnectedException newWithClass: isa]; + @throw [OFNotConnectedException newWithClass: isa + socket: self]; if (listen(sock, backlog) == -1) @throw [OFListenFailedException newWithClass: isa + socket: self backLog: backlog]; listening = YES; } - (void)listen { if (sock == INVALID_SOCKET) - @throw [OFNotConnectedException newWithClass: isa]; + @throw [OFNotConnectedException newWithClass: isa + socket: self]; if (listen(sock, 5) == -1) @throw [OFListenFailedException newWithClass: isa + socket: self backLog: 5]; listening = YES; } @@ -314,11 +326,12 @@ @throw e; } if ((s = accept(sock, addr, &addrlen)) == INVALID_SOCKET) { [newsock release]; - @throw [OFAcceptFailedException newWithClass: isa]; + @throw [OFAcceptFailedException newWithClass: isa + socket: self]; } newsock->sock = s; newsock->sockAddr = addr; newsock->sockAddrLen = addrlen; @@ -385,14 +398,6 @@ [self freeMemory: sockAddr]; sockAddr = NULL; sockAddrLen = 0; } - -- (void)dealloc -{ - if (sock != INVALID_SOCKET) - [self close]; - - [super dealloc]; -} @end Index: src/exceptions/OFAcceptFailedException.h ================================================================== --- src/exceptions/OFAcceptFailedException.h +++ src/exceptions/OFAcceptFailedException.h @@ -14,22 +14,49 @@ * file. */ #import "OFException.h" +@class OFTCPSocket; + /** * \brief An exception indicating that accepting a connection failed. */ @interface OFAcceptFailedException: OFException { + OFTCPSocket *socket; int errNo; } #ifdef OF_HAVE_PROPERTIES +@property (readonly, nonatomic) OFTCPSocket *socket; @property (readonly) int errNo; #endif +/** + * \param class_ The class of the object which caused the exception + * \param socket The socket which could not accept a connection + * \return A new accept failed exception + */ ++ newWithClass: (Class)class_ + socket: (OFTCPSocket*)socket; + +/** + * Initializes an already allocated accept failed exception. + * + * \param class_ The class of the object which caused the exception + * \param socket The socket which could not accept a connection + * \return An initialized accept failed exception + */ +- initWithClass: (Class)class_ + socket: (OFTCPSocket*)socket; + +/** + * \return The socket which could not accept a connection + */ +- (OFTCPSocket*)socket; + /** * \return The errno from when the exception was created */ - (int)errNo; @end Index: src/exceptions/OFAcceptFailedException.m ================================================================== --- src/exceptions/OFAcceptFailedException.m +++ src/exceptions/OFAcceptFailedException.m @@ -20,18 +20,39 @@ #import "OFString.h" #import "common.h" @implementation OFAcceptFailedException ++ newWithClass: (Class)class_ + socket: (OFTCPSocket*)socket +{ + return [[self alloc] initWithClass: class_ + socket: socket]; +} + - initWithClass: (Class)class_ + socket: (OFTCPSocket*)socket_ { self = [super initWithClass: class_]; - errNo = GET_SOCK_ERRNO; + @try { + socket = [socket_ retain]; + errNo = GET_SOCK_ERRNO; + } @catch (id e) { + [self release]; + @throw e; + } return self; } + +- (void)dealloc +{ + [socket release]; + + [super dealloc]; +} - (OFString*)description { if (description != nil) return description; @@ -40,11 +61,16 @@ @"Failed to accept connection in socket of type %@! " ERRFMT, inClass, ERRPARAM]; return description; } + +- (OFTCPSocket*)socket +{ + return socket; +} - (int)errNo { return errNo; } @end Index: src/exceptions/OFAddressTranslationFailedException.h ================================================================== --- src/exceptions/OFAddressTranslationFailedException.h +++ src/exceptions/OFAddressTranslationFailedException.h @@ -14,47 +14,60 @@ * file. */ #import "OFException.h" +@class OFTCPSocket; + /** * \brief An exception indicating the translation of an address failed. */ @interface OFAddressTranslationFailedException: OFException { - OFString *host; - int errNo; + OFTCPSocket *socket; + OFString *host; + int errNo; } #ifdef OF_HAVE_PROPERTIES +@property (readonly, nonatomic) OFTCPSocket *socket; @property (readonly, nonatomic) OFString *host; @property (readonly) int errNo; #endif /** * \param class_ The class of the object which caused the exception + * \param socket The socket which could not translate the address * \param host The host for which translation was requested * \return A new address translation failed exception */ + newWithClass: (Class)class_ + socket: (OFTCPSocket*)socket host: (OFString*)host; /** * Initializes an already allocated address translation failed exception. * * \param class_ The class of the object which caused the exception + * \param socket The socket which could not translate the address * \param host The host for which translation was requested * \return An initialized address translation failed exception */ - initWithClass: (Class)class_ + socket: (OFTCPSocket*)socket host: (OFString*)host; /** - * \return The errno from when the exception was created + * \return The socket which could not translate the address */ -- (int)errNo; +- (OFTCPSocket*)socket; /** * /return The host for which translation was requested */ - (OFString*)host; + +/** + * \return The errno from when the exception was created + */ +- (int)errNo; @end Index: src/exceptions/OFAddressTranslationFailedException.m ================================================================== --- src/exceptions/OFAddressTranslationFailedException.m +++ src/exceptions/OFAddressTranslationFailedException.m @@ -21,13 +21,15 @@ #import "common.h" @implementation OFAddressTranslationFailedException + newWithClass: (Class)class_ + socket: (OFTCPSocket*)socket host: (OFString*)host { return [[self alloc] initWithClass: class_ + socket: socket host: host]; } - initWithClass: (Class)class_ { @@ -37,17 +39,19 @@ return self; } - initWithClass: (Class)class_ + socket: (OFTCPSocket*)socket_ host: (OFString*)host_ { self = [super initWithClass: class_]; @try { - host = [host_ copy]; - errNo = GET_AT_ERRNO; + socket = [socket_ retain]; + host = [host_ copy]; + errNo = GET_AT_ERRNO; } @catch (id e) { [self release]; @throw e; } @@ -54,10 +58,11 @@ return self; } - (void)dealloc { + [socket release]; [host release]; [super dealloc]; } @@ -79,15 +84,20 @@ inClass, AT_ERRPARAM]; return description; } -- (int)errNo +- (OFTCPSocket*)socket { - return errNo; + return socket; } - (OFString*)host { return host; } + +- (int)errNo +{ + return errNo; +} @end Index: src/exceptions/OFAlreadyConnectedException.h ================================================================== --- src/exceptions/OFAlreadyConnectedException.h +++ src/exceptions/OFAlreadyConnectedException.h @@ -14,11 +14,43 @@ * file. */ #import "OFException.h" +@class OFTCPSocket; + /** * \brief An exception indicating an attempt to connect or bind an already * connected or bound socket. */ @interface OFAlreadyConnectedException: OFException +{ + OFTCPSocket *socket; +} + +#ifdef OF_HAVE_PROPERTIES +@property (readonly, nonatomic) OFTCPSocket *socket; +#endif + +/** + * \param class_ The class of the object which caused the exception + * \param socket The socket which is already connected + * \return A new already connected exception + */ ++ newWithClass: (Class)class_ + socket: (OFTCPSocket*)socket; + +/** + * Initializes an already allocated already connected exception. + * + * \param class_ The class of the object which caused the exception + * \param socket The socket which is already connected + * \return An initialized already connected exception + */ +- initWithClass: (Class)class_ + socket: (OFTCPSocket*)socket; + +/** + * \return The socket which is already connected + */ +- (OFTCPSocket*)socket; @end Index: src/exceptions/OFAlreadyConnectedException.m ================================================================== --- src/exceptions/OFAlreadyConnectedException.m +++ src/exceptions/OFAlreadyConnectedException.m @@ -18,10 +18,39 @@ #import "OFAlreadyConnectedException.h" #import "OFString.h" @implementation OFAlreadyConnectedException ++ newWithClass: (Class)class_ + socket: (OFTCPSocket*)socket +{ + return [[self alloc] initWithClass: class_ + socket: socket]; +} + +- initWithClass: (Class)class_ + socket: (OFTCPSocket*)socket_ +{ + self = [super initWithClass: class_]; + + @try { + socket = [socket_ retain]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (void)dealloc +{ + [socket release]; + + [super dealloc]; +} + - (OFString*)description { if (description != nil) return description; @@ -29,6 +58,11 @@ @"The socket of type %@ is already connected or bound and thus " @"can't be connected or bound again!", inClass]; return description; } + +- (OFTCPSocket*)socket +{ + return socket; +} @end Index: src/exceptions/OFBindFailedException.h ================================================================== --- src/exceptions/OFBindFailedException.h +++ src/exceptions/OFBindFailedException.h @@ -14,52 +14,60 @@ * file. */ #import "OFException.h" +@class OFTCPSocket; + /** * \brief An exception indicating that binding a socket failed. */ @interface OFBindFailedException: OFException { - OFString *host; - uint16_t port; - int errNo; + OFTCPSocket *socket; + OFString *host; + uint16_t port; + int errNo; } #ifdef OF_HAVE_PROPERTIES +@property (readonly, nonatomic) OFTCPSocket *socket; @property (readonly, nonatomic) OFString *host; @property (readonly) uint16_t port; @property (readonly) int errNo; #endif /** * \param class_ The class of the object which caused the exception + * \param socket The socket which could not be bound * \param host The host on which binding failed * \param port The port on which binding failed * \return A new bind failed exception */ + newWithClass: (Class)class_ + socket: (OFTCPSocket*)socket host: (OFString*)host port: (uint16_t)port; /** * Initializes an already allocated bind failed exception. * * \param class_ The class of the object which caused the exception + * \param socket The socket which could not be bound * \param host The host on which binding failed * \param port The port on which binding failed * \return An initialized bind failed exception */ - initWithClass: (Class)class_ + socket: (OFTCPSocket*)socket host: (OFString*)host port: (uint16_t)port; /** - * \return The errno from when the exception was created + * \return The socket which could not be bound */ -- (int)errNo; +- (OFTCPSocket*)socket; /** * \return The host on which binding failed */ - (OFString*)host; @@ -66,6 +74,11 @@ /** * \return The port on which binding failed */ - (uint16_t)port; + +/** + * \return The errno from when the exception was created + */ +- (int)errNo; @end Index: src/exceptions/OFBindFailedException.m ================================================================== --- src/exceptions/OFBindFailedException.m +++ src/exceptions/OFBindFailedException.m @@ -23,14 +23,16 @@ #import "common.h" @implementation OFBindFailedException + newWithClass: (Class)class_ + socket: (OFTCPSocket*)socket host: (OFString*)host port: (uint16_t)port { return [[self alloc] initWithClass: class_ + socket: socket host: host port: port]; } - initWithClass: (Class)class_ @@ -40,19 +42,21 @@ @throw [OFNotImplementedException newWithClass: c selector: _cmd]; } - initWithClass: (Class)class_ + socket: (OFTCPSocket*)socket_ host: (OFString*)host_ port: (uint16_t)port_ { self = [super initWithClass: class_]; @try { - host = [host_ copy]; - port = port_; - errNo = GET_SOCK_ERRNO; + socket = [socket_ retain]; + host = [host_ copy]; + port = port_; + errNo = GET_SOCK_ERRNO; } @catch (id e) { [self release]; @throw e; } @@ -59,10 +63,11 @@ return self; } - (void)dealloc { + [socket release]; [host release]; [super dealloc]; } @@ -76,13 +81,13 @@ ERRFMT, port, host, inClass, ERRPARAM]; return description; } -- (int)errNo +- (OFTCPSocket*)socket { - return errNo; + return socket; } - (OFString*)host { return host; @@ -90,6 +95,11 @@ - (uint16_t)port { return port; } + +- (int)errNo +{ + return errNo; +} @end Index: src/exceptions/OFConnectionFailedException.h ================================================================== --- src/exceptions/OFConnectionFailedException.h +++ src/exceptions/OFConnectionFailedException.h @@ -14,52 +14,60 @@ * file. */ #import "OFException.h" +@class OFTCPSocket; + /** * \brief An exception indicating that a connection could not be established. */ @interface OFConnectionFailedException: OFException { - OFString *host; - uint16_t port; - int errNo; + OFTCPSocket *socket; + OFString *host; + uint16_t port; + int errNo; } #ifdef OF_HAVE_PROPERTIES +@property (readonly, nonatomic) OFTCPSocket *socket; @property (readonly, nonatomic) OFString *host; @property (readonly) uint16_t port; @property (readonly) int errNo; #endif /** * \param class_ The class of the object which caused the exception + * \param socket The socket which could not connect * \param host The host to which the connection failed * \param port The port on the host to which the connection failed * \return A new connection failed exception */ + newWithClass: (Class)class_ + socket: (OFTCPSocket*)socket host: (OFString*)host port: (uint16_t)port; /** * Initializes an already allocated connection failed exception. * * \param class_ The class of the object which caused the exception + * \param socket The socket which could not connect * \param host The host to which the connection failed * \param port The port on the host to which the connection failed * \return An initialized connection failed exception */ - initWithClass: (Class)class_ + socket: (OFTCPSocket*)socket host: (OFString*)host port: (uint16_t)port; /** - * \return The errno from when the exception was created + * \return The socket which could not connect */ -- (int)errNo; +- (OFTCPSocket*)socket; /** * \return The host to which the connection failed */ - (OFString*)host; @@ -66,6 +74,11 @@ /** * \return The port on the host to which the connection failed */ - (uint16_t)port; + +/** + * \return The errno from when the exception was created + */ +- (int)errNo; @end Index: src/exceptions/OFConnectionFailedException.m ================================================================== --- src/exceptions/OFConnectionFailedException.m +++ src/exceptions/OFConnectionFailedException.m @@ -23,14 +23,16 @@ #import "common.h" @implementation OFConnectionFailedException + newWithClass: (Class)class_ + socket: (OFTCPSocket*)socket host: (OFString*)host port: (uint16_t)port { return [[self alloc] initWithClass: class_ + socket: socket host: host port: port]; } - initWithClass: (Class)class_ @@ -40,19 +42,21 @@ @throw [OFNotImplementedException newWithClass: c selector: _cmd]; } - initWithClass: (Class)class_ + socket: (OFTCPSocket*)socket_ host: (OFString*)host_ port: (uint16_t)port_ { self = [super initWithClass: class_]; @try { - host = [host_ copy]; - port = port_; - errNo = GET_SOCK_ERRNO; + socket = [socket_ retain]; + host = [host_ copy]; + port = port_; + errNo = GET_SOCK_ERRNO; } @catch (id e) { [self release]; @throw e; } @@ -59,10 +63,11 @@ return self; } - (void)dealloc { + [socket release]; [host release]; [super dealloc]; } @@ -76,13 +81,13 @@ @"in class %@! " ERRFMT, host, port, inClass, ERRPARAM]; return description; } -- (int)errNo +- (OFTCPSocket*)socket { - return errNo; + return socket; } - (OFString*)host { return host; @@ -90,6 +95,11 @@ - (uint16_t)port { return port; } + +- (int)errNo +{ + return errNo; +} @end Index: src/exceptions/OFListenFailedException.h ================================================================== --- src/exceptions/OFListenFailedException.h +++ src/exceptions/OFListenFailedException.h @@ -14,47 +14,60 @@ * file. */ #import "OFException.h" +@class OFTCPSocket; + /** * \brief An exception indicating that listening on the socket failed. */ @interface OFListenFailedException: OFException { - int backLog; - int errNo; + OFTCPSocket *socket; + int backLog; + int errNo; } #ifdef OF_HAVE_PROPERTIES +@property (readonly, nonatomic) OFTCPSocket *socket; @property (readonly) int backLog; @property (readonly) int errNo; #endif /** * \param class_ The class of the object which caused the exception + * \param socket The socket which failed to listen * \param backlog The requested size of the back log * \return A new listen failed exception */ + newWithClass: (Class)class_ + socket: (OFTCPSocket*)socket backLog: (int)backlog; /** * Initializes an already allocated listen failed exception * * \param class_ The class of the object which caused the exception + * \param socket The socket which failed to listen * \param backlog The requested size of the back log * \return An initialized listen failed exception */ - initWithClass: (Class)class_ + socket: (OFTCPSocket*)socket backLog: (int)backlog; /** - * \return The errno from when the exception was created + * \return The socket which failed to listen */ -- (int)errNo; +- (OFTCPSocket*)socket; /** * \return The requested back log. */ - (int)backLog; + +/** + * \return The errno from when the exception was created + */ +- (int)errNo; @end Index: src/exceptions/OFListenFailedException.m ================================================================== --- src/exceptions/OFListenFailedException.m +++ src/exceptions/OFListenFailedException.m @@ -23,13 +23,15 @@ #import "common.h" @implementation OFListenFailedException + newWithClass: (Class)class_ + socket: (OFTCPSocket*)socket backLog: (int)backlog { return [[self alloc] initWithClass: class_ + socket: socket backLog: backlog]; } - initWithClass: (Class)class_ { @@ -38,19 +40,33 @@ @throw [OFNotImplementedException newWithClass: c selector: _cmd]; } - initWithClass: (Class)class_ + socket: (OFTCPSocket*)socket_ backLog: (int)backlog { self = [super initWithClass: class_]; - backLog = backlog; - errNo = GET_SOCK_ERRNO; + @try { + socket = [socket_ retain]; + backLog = backlog; + errNo = GET_SOCK_ERRNO; + } @catch (id e) { + [self release]; + @throw e; + } return self; } + +- (void)dealloc +{ + [socket release]; + + [super dealloc]; +} - (OFString*)description { if (description != nil) return description; @@ -60,15 +76,20 @@ ERRFMT, inClass, backLog, ERRPARAM]; return description; } -- (int)errNo +- (OFTCPSocket*)socket { - return errNo; + return socket; } - (int)backLog { return backLog; } + +- (int)errNo +{ + return errNo; +} @end Index: src/exceptions/OFNotConnectedException.h ================================================================== --- src/exceptions/OFNotConnectedException.h +++ src/exceptions/OFNotConnectedException.h @@ -14,10 +14,42 @@ * file. */ #import "OFException.h" +@class OFStreamSocket; + /** * \brief An exception indicating a socket is not connected or bound. */ @interface OFNotConnectedException: OFException +{ + OFStreamSocket *socket; +} + +#ifdef OF_HAVE_PROPERTIES +@property (readonly, nonatomic) OFStreamSocket *socket; +#endif + +/** + * \param class_ The class of the object which caused the exception + * \param socket The socket which is not connected + * \return A new not connected exception + */ ++ newWithClass: (Class)class_ + socket: (OFStreamSocket*)socket; + +/** + * Initializes an already allocated not connected exception. + * + * \param class_ The class of the object which caused the exception + * \param socket The socket which is not connected + * \return An initialized not connected exception + */ +- initWithClass: (Class)class_ + socket: (OFStreamSocket*)socket; + +/** + * \return The socket which is not connected + */ +- (OFStreamSocket*)socket; @end Index: src/exceptions/OFNotConnectedException.m ================================================================== --- src/exceptions/OFNotConnectedException.m +++ src/exceptions/OFNotConnectedException.m @@ -18,10 +18,39 @@ #import "OFNotConnectedException.h" #import "OFString.h" @implementation OFNotConnectedException ++ newWithClass: (Class)class_ + socket: (OFStreamSocket*)socket +{ + return [[self alloc] initWithClass: class_ + socket: socket]; +} + +- initWithClass: (Class)class_ + socket: (OFStreamSocket*)socket_ +{ + self = [super initWithClass: class_]; + + @try { + socket = [socket_ retain]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (void)dealloc +{ + [socket release]; + + [super dealloc]; +} + - (OFString*)description { if (description != nil) return description; @@ -28,6 +57,11 @@ description = [[OFString alloc] initWithFormat: @"The socket of type %@ is not connected or bound!", inClass]; return description; } + +- (OFStreamSocket*)socket +{ + return socket; +} @end