Index: src/OFExceptions.h ================================================================== --- src/OFExceptions.h +++ src/OFExceptions.h @@ -38,10 +38,15 @@ */ - initWithObject: (id)obj; - free; +/** + * \return The object that caused the exception + */ +- (id)object; + /** * \return An error message for the exception as a C String */ - (char*)cString; @end @@ -127,24 +132,13 @@ /** * An OFException indicating the given value is out of range. */ @interface OFOutOfRangeException: OFException {} /** - * Creates a new out of range exception. - * - * \param obj The object which caused the exception - * \return A new out of range exception - */ -+ newWithObject: (id)obj; - -/** - * Initializes an already allocated out of range exception. - * - * \param obj The object which caused the exception - * \return An initialized out of range exception - */ -- initWithObject: (id)obj; + * \return An error message for the exception as a C String + */ +- (char*)cString; @end /** * An OFException indicating the file couldn't be opened. */ @@ -255,7 +249,28 @@ */ @interface OFWriteFailedException: OFReadOrWriteFailedException {} /** * \return An error message for the exception as a C String */ +- (char*)cString; +@end + +/** + * An OFException indicating a socket is not connected or bound. + */ +@interface OFNotConnectedException: OFException {} +/** + * \return An error message for the exception as a C string. + */ +- (char*)cString; +@end + +/** + * An OFException indicating an attempt to connect or bind an already connected + * or bound socket + */ +@interface OFAlreadyConnectedException: OFException {} +/** + * \return An error message for the exception as a C string. + */ - (char*)cString; @end Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -49,10 +49,15 @@ if (string != NULL) free(string); return [super free]; } + +- (id)object +{ + return object; +} - (char*)cString { return string; } @@ -78,12 +83,12 @@ - (char*)cString { if (string != NULL) return string; - asprintf(&string, "ERROR: Could not allocate %zu bytes for object of " - "class %s!\n", req_size, object != nil ? [object name] : "(null)"); + asprintf(&string, "Could not allocate %zu bytes for object of class " + "%s!", req_size, object != nil ? [object name] : "(null)"); return string; } - (size_t)requestedSize @@ -112,16 +117,14 @@ - (char*)cString { if (string != NULL) return string; - asprintf(&string, "ERROR: Memory at %p was not allocated as part of " - "object of class\n" - "ERROR: %s!\n" - "ERROR: -> Not changing memory allocation!\n" - "ERROR: (Hint: It is also possible that you tried to free the same " - "memory twice!)\n", pointer, [object name]); + asprintf(&string, "Memory at %p was not allocated as part of object " + "of class %s, thus the memory allocation was not changed! It is " + "also possible that there was an attempt to free the same memory " + "twice.", pointer, [object name]); return string; } - (void*)pointer @@ -129,26 +132,16 @@ return pointer; } @end @implementation OFOutOfRangeException -+ newWithObject: (id)obj -{ - return [[self alloc] initWithObject: obj]; -} - -- initWithObject: (id)obj -{ - return (self = [super initWithObject: obj]); -} - - (char*)cString { if (string != NULL) return string; - asprintf(&string, "ERROR: Value out of range in object of class %s!\n", + asprintf(&string, "Value out of range in object of class %s!", object != nil ? [object name] : "(null)"); return string; } @end @@ -188,12 +181,12 @@ - (char*)cString { if (string != NULL) return string; - asprintf(&string, "ERROR: Failed to open file %s with mode %s " - "in object of class %s!\n", path, mode, [self name]); + asprintf(&string, "Failed to open file %s with mode %s in object of " + "class %s!", path, mode, [self name]); return string; } - (char*)path @@ -244,12 +237,12 @@ - (char*)cString { if (string != NULL) return string;; - asprintf(&string, "ERROR: Failed to read %zu items of size %zu in " - "object of class %s!\n", req_items, req_size, [object name]); + asprintf(&string, "Failed to read %zu items of size %zu in object of " + "class %s!", req_items, req_size, [object name]); return string; } @end @@ -257,11 +250,37 @@ - (char*)cString { if (string != NULL) return string; - asprintf(&string, "ERROR: Failed to write %zu items of size %zu in " - "object of class %s!\n", req_items, req_size, [object name]); + asprintf(&string, "Failed to write %zu items of size %zu in object of " + "class %s!", req_items, req_size, [object name]); + + return string; +} +@end + +@implementation OFNotConnectedException +- (char*)cString +{ + if (string != NULL) + return string; + + asprintf(&string, "The socket of type %s is not connected or bound!", + [object name]); + + return string; +} +@end + +@implementation OFAlreadyConnectedException +- (char*)cString +{ + if (string != NULL) + return string; + + asprintf(&string, "The socket of type %s is already connected or bound " + "and thus can't be connected or bound again!", [object name]); return string; } @end Index: src/OFTCPSocket.m ================================================================== --- src/OFTCPSocket.m +++ src/OFTCPSocket.m @@ -64,14 +64,12 @@ if (!port) { /* FIXME: Throw exception */ return nil; } - if (sock >= 0) { - /* FIXME: Throw exception */ - return nil; - } + if (sock >= 0) + @throw [OFAlreadyConnectedException newWithObject: self]; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; @@ -116,14 +114,12 @@ if (!port) { /* FIXME: Throw exception */ return nil; } - if (sock >= 0) { - /* FIXME: Throw exception */ - return nil; - } + if (sock >= 0) + @throw [OFAlreadyConnectedException newWithObject: self]; if ((sock = socket(family, SOCK_STREAM, 0)) < 0) { /* FIXME: Throw exception */ return nil; } @@ -150,14 +146,12 @@ return self; } - listenWithBackLog: (int)backlog { - if (sock < 0) { - /* FIXME: Throw exception */ - return nil; - } + if (sock < 0) + @throw [OFNotConnectedException newWithObject: self]; if (listen(sock, backlog) < 0 ) { /* FIXME: Throw exception */ return nil; } @@ -165,14 +159,12 @@ return self; } - listen { - if (sock < 0) { - /* FIXME: Throw exception */ - return nil; - } + if (sock < 0) + @throw [OFNotConnectedException newWithObject: self]; if (listen(sock, 5) < 0 ) { /* FIXME: Throw exception */ return nil; } @@ -212,14 +204,12 @@ - (size_t)readNBytes: (size_t)size intoBuffer: (uint8_t*)buf { ssize_t ret; - if (sock < 0) { - /* FIXME: Throw exception */ - return 0; - } + if (sock < 0) + @throw [OFNotConnectedException newWithObject: self]; if ((ret = recv(sock, buf, size, 0)) < 0) { /* FIXME: Throw exception */ return 0; } @@ -230,14 +220,12 @@ - (uint8_t*)readNBytes: (size_t)size { uint8_t *ret; - if (sock < 0) { - /* FIXME: Throw exception */ - return NULL; - } + if (sock < 0) + @throw [OFNotConnectedException newWithObject: self]; ret = [self getMemWithSize: size]; @try { [self readNBytes: size @@ -253,14 +241,12 @@ - (size_t)writeNBytes: (size_t)size fromBuffer: (const uint8_t*)buf { ssize_t ret; - if (sock < 0) { - /* FIXME: Throw exception */ - return 0; - } + if (sock < 0) + @throw [OFNotConnectedException newWithObject: self]; if ((ret = send(sock, buf, size, 0)) < 0) { /* FIXME: Throw exception */ return 0; } @@ -269,25 +255,21 @@ return ret; } - (size_t)writeCString: (const char*)str { - if (sock < 0) { - /* FIXME: Throw exception */ - return 0; - } + if (sock < 0) + @throw [OFNotConnectedException newWithObject: self]; return [self writeNBytes: strlen(str) fromBuffer: (const uint8_t*)str]; } - close { - if (sock < 0) { - /* FIXME: Throw exception */ - return nil; - } + if (sock < 0) + @throw [OFNotConnectedException newWithObject: self]; sock = -1; if (saddr != NULL) [self freeMem: saddr]; Index: tests/OFArray/OFArray.m ================================================================== --- tests/OFArray/OFArray.m +++ tests/OFArray/OFArray.m @@ -23,11 +23,11 @@ @try { \ code; \ } @catch (exception *e) { \ caught = YES; \ puts("CAUGHT! Error string was:"); \ - fputs([e cString], stdout); \ + puts([e cString]); \ puts("Resuming..."); \ } \ if (!caught) { \ puts("NOT CAUGHT!"); \ return 1; \ Index: tests/OFObject/OFObject.m ================================================================== --- tests/OFObject/OFObject.m +++ tests/OFObject/OFObject.m @@ -22,11 +22,11 @@ @try { \ code; \ } @catch (exception *e) { \ caught = YES; \ puts("CAUGHT! Error string was:"); \ - fputs([e cString], stdout); \ + puts([e cString]); \ puts("Resuming..."); \ } \ if (!caught) { \ puts("NOT CAUGHT!"); \ return 1; \ Index: tests/OFTCPSocket/OFTCPSocket.m ================================================================== --- tests/OFTCPSocket/OFTCPSocket.m +++ tests/OFTCPSocket/OFTCPSocket.m @@ -102,9 +102,10 @@ [accepted free]; [client close]; [server close]; } @catch(OFException *e) { printf("EXCEPTION: %s\n", [e cString]); + return 1; } return 0; }