Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -20,11 +20,11 @@ OFMacros.h \ OFStream.h include ../buildsys.mk -CPPFLAGS += -I.. +CPPFLAGS += -I.. -pthread CFLAGS += ${LIB_CFLAGS} OBJCFLAGS += ${LIB_CFLAGS} LD = ${OBJC} -LDFLAGS += ${LIB_LDFLAGS} +LDFLAGS += ${LIB_LDFLAGS} -pthread LIBS += -lobjc Index: src/OFExceptions.h ================================================================== --- src/OFExceptions.h +++ src/OFExceptions.h @@ -148,10 +148,11 @@ */ @interface OFOpenFileFailedException: OFException { char *path; char *mode; + int err; } /** * \param obj The object which caused the exception * \param path A C string of the path to the file tried to open @@ -179,10 +180,15 @@ /** * \return An error message for the exception as a C String */ - (const char*)cString; +/** + * \return The errno from when the exception was created + */ +- (int)errNo; + /** * \return A C string of the path to the file which couldn't be opened */ - (char*)path; @@ -197,11 +203,12 @@ */ @interface OFReadOrWriteFailedException: OFException { size_t req_size; size_t req_items; - BOOL has_items; + BOOL has_items; + int err; } /** * \param obj The object which caused the exception * \param size The requested size of the data that couldn't be read / written @@ -240,10 +247,15 @@ * \return A new open file failed exception */ - initWithObject: (id)obj andSize: (size_t)size; +/** + * \return The errno from when the exception was created + */ +- (int)errNo; + /** * \return The requested size of the data that couldn't be read / written */ - (size_t)requestedSize; @@ -324,10 +336,11 @@ */ @interface OFAddressTranslationFailedException: OFException { char *node; char *service; + int err; } /** * \param obj The object which caused the exception * \param node The node for which translation was requested @@ -355,10 +368,15 @@ /** * \return An error message for the exception as a C string. */ - (const char*)cString; +/** + * \return The errno from when the exception was created + */ +- (int)errNo; + /** * /return The node for which translation was requested */ - (const char*)node; @@ -371,12 +389,13 @@ /** * An OFException indicating that the connection could not be established. */ @interface OFConnectionFailedException: OFException { - char *host; + char *host; uint16_t port; + int err; } /** * \param obj The object which caused the exception * \param host The host to which the connection failed @@ -404,10 +423,15 @@ /** * \return An error message for the exception as a C string. */ - (const char*)cString; +/** + * \return The errno from when the exception was created + */ +- (int)errNo; + /** * \return The host to which the connection failed */ - (const char*)host; @@ -420,13 +444,14 @@ /** * An OFException indicating that binding the socket failed. */ @interface OFBindFailedException: OFException { - char *host; + char *host; uint16_t port; - int family; + int family; + int err; } /** * \param obj The object which caused the exception * \param host The host on which binding failed @@ -458,10 +483,15 @@ /** * \return An error message for the exception as a C string. */ - (const char*)cString; +/** + * \return The errno from when the exception was created + */ +- (int)errNo; + /** * \return The host on which binding failed */ - (const char*)host; @@ -480,10 +510,11 @@ * An OFException indicating that listening on the socket failed. */ @interface OFListenFailedException: OFException { int backlog; + int err; } /** * \param obj The object which caused the exception * \param backlog The requested size of the back log @@ -505,20 +536,42 @@ /** * \return An error message for the exception as a C string. */ - (const char*)cString; +/** + * \return The errno from when the exception was created + */ +- (int)errNo; + /** * \return The requested back log. */ - (int)backLog; @end /** * An OFException indicating that accepting a connection failed. */ -@interface OFAcceptFailedException: OFException {} +@interface OFAcceptFailedException: OFException +{ + int err; +} + +/** + * Initializes an already allocated accept failed exception. + * + * \param obj The object which caused the exception + * \return An initialized accept failed exception + */ +- initWithObject: (id)obj; + /** * \return An error message for the exception as a C string. */ - (const char*)cString; + +/** + * \return The errno from when the exception was created + */ +- (int)errNo; @end Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -13,10 +13,11 @@ #define _GNU_SOURCE #import #import #import +#import #import "OFExceptions.h" #ifndef HAVE_ASPRINTF #import "asprintf.h" @@ -168,10 +169,11 @@ andMode: (const char*)mode_ { if ((self = [super initWithObject: obj])) { path = (path_ != NULL ? strdup(path_) : NULL); mode = (mode_ != NULL ? strdup(mode_) : NULL); + err = errno; } return self; } @@ -189,14 +191,20 @@ { if (string != NULL) return string; asprintf(&string, "Failed to open file %s with mode %s in object of " - "class %s!", path, mode, [self name]); + "class %s! Error string was: %s", path, mode, [self name], + strerror(err)); return string; } + +- (int)errNo +{ + return err; +} - (char*)path { return path; } @@ -230,10 +238,11 @@ { if ((self = [super initWithObject: obj])) { req_size = size; req_items = nitems; has_items = YES; + err = errno; } return self; } @@ -242,14 +251,20 @@ { if ((self = [super initWithObject: obj])) { req_size = size; req_items = 0; has_items = NO; + err = errno; } return self; } + +- (int)errNo +{ + return err; +} - (size_t)requestedSize { return req_size; } @@ -271,14 +286,16 @@ if (string != NULL) return string;; if (has_items) asprintf(&string, "Failed to read %zu items of size %zu in " - "object of class %s!", req_items, req_size, [object name]); + "object of class %s! Error string was: %s", req_items, + req_size, [object name], strerror(err)); else asprintf(&string, "Failed to read %zu bytes in object of class " - "%s!", req_size, [object name]); + "%s! Error string was: %s", req_size, [object name], + strerror(err)); return string; } @end @@ -288,14 +305,16 @@ if (string != NULL) return string; if (has_items) asprintf(&string, "Failed to write %zu items of size %zu in " - "object of class %s!", req_items, req_size, [object name]); + "object of class %s! Error string was: %s", req_items, + req_size, [object name], strerror(err)); else asprintf(&string, "Failed to write %zu bytes in object of " - "class %s!", req_size, [object name]); + "class %s! Error string was: %s", req_size, [object name], + strerror(err)); return string; } @end @@ -367,10 +386,11 @@ andService: (const char*)service_ { if ((self = [super initWithObject: obj])) { node = (node_ != NULL ? strdup(node_) : NULL); service = (service_ != NULL ? strdup(service_) : NULL); + err = errno; } return self; } @@ -391,15 +411,21 @@ asprintf(&string, "The service %s on %s could not be translated to an " "address for an object of type %s. This means that either the node " "was not found, there is no such service on the node, there was a " "problem with the name server, there was a problem with your " - "network connection or you specified an invalid node or service.", - service, node, [object name]); + "network connection or you specified an invalid node or service. " + "Error string was: %s", service, node, [object name], + strerror(err)); return string; } + +- (int)errNo +{ + return err; +} - (const char*)node { return node; } @@ -425,10 +451,11 @@ andPort: (uint16_t)port_ { if ((self = [super initWithObject: obj])) { host = (host_ != NULL ? strdup(host_) : NULL); port = port_; + err = errno; } return self; } @@ -444,14 +471,20 @@ { if (string != NULL) return string; asprintf(&string, "A connection to %s:%d could not be established in " - "object of type %s!", host, port, [object name]); + "object of type %s! Error string was: %s", host, port, + [object name], strerror(err)); return string; } + +- (int)errNo +{ + return err; +} - (const char*)host { return host; } @@ -481,10 +514,11 @@ { if ((self = [super initWithObject: obj])) { host = (host_ != NULL ? strdup(host_) : NULL); port = port_; family = family_; + err = errno; } return self; } @@ -500,14 +534,20 @@ { if (string != NULL) return string; asprintf(&string, "Binding to port %d on %s using family %d failed in " - "object of type %s!", port, host, family, [object name]); + "object of type %s! Error string was: %s", port, host, family, + [object name], strerror(err)); return string; } + +- (int)errNo +{ + return err; +} - (const char*)host { return host; } @@ -532,12 +572,14 @@ } - initWithObject: (id)obj andBackLog: (int)backlog_ { - if ((self = [super initWithObject: obj])) + if ((self = [super initWithObject: obj])) { backlog = backlog_; + err = errno; + } return self; } - (const char*)cString @@ -544,28 +586,47 @@ { if (string != NULL) return string; asprintf(&string, "Failed to listen in socket of type %s with a back " - "log of %d!", [object name], backlog); + "log of %d! Error string was: %s", [object name], backlog, + strerror(err)); return string; } + +- (int)errNo +{ + return err; +} - (int)backLog { return backlog; } @end @implementation OFAcceptFailedException +- initWithObject: (id)obj +{ + if ((self = [super initWithObject: obj])) + err = errno; + + return self; +} + - (const char*)cString { if (string != NULL) return string; - asprintf(&string, "Failed to accept connection in socket of type %s!", - [object name]); + asprintf(&string, "Failed to accept connection in socket of type %s! " + "Error string was: %s", [object name], strerror(err)); return string; } + +- (int)errNo +{ + return err; +} @end Index: src/OFObject.h ================================================================== --- src/OFObject.h +++ src/OFObject.h @@ -16,11 +16,11 @@ /** * The OFObject class is the base class for all other classes inside ObjFW. */ @interface OFObject: Object { - void **__memchunks; + void **__memchunks; size_t __memchunks_size; } /** * Initialize the already allocated object. Index: src/OFTCPSocket.h ================================================================== --- src/OFTCPSocket.h +++ src/OFTCPSocket.h @@ -37,12 +37,12 @@ /** * The OFTCPSocket class provides functions to create and use sockets. */ @interface OFTCPSocket: OFObject { - int sock; - struct sockaddr *saddr; + int sock; + struct sockaddr *saddr; socklen_t saddr_len; } /** * Initializes an already allocated OFTCPSocket.