@@ -17,21 +17,30 @@ #include "config.h" #import "OFConnectionFailedException.h" #import "OFString.h" -#import "common.h" - @implementation OFConnectionFailedException + (instancetype)exceptionWithHost: (OFString*)host port: (uint16_t)port socket: (id)socket { return [[[self alloc] initWithHost: host port: port socket: socket] autorelease]; } + ++ (instancetype)exceptionWithHost: (OFString*)host + port: (uint16_t)port + socket: (id)socket + errNo: (int)errNo +{ + return [[[self alloc] initWithHost: host + port: port + socket: socket + errNo: errNo] autorelease]; +} - init { OF_INVALID_INIT_METHOD } @@ -41,14 +50,33 @@ socket: (id)socket { self = [super init]; @try { - _host = [host copy]; + _host = [host copy]; + _socket = [socket retain]; + _port = port; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- initWithHost: (OFString*)host + port: (uint16_t)port + socket: (id)socket + errNo: (int)errNo +{ + self = [super init]; + + @try { + _host = [host copy]; _socket = [socket retain]; - _port = port; - _errNo = GET_SOCK_ERRNO; + _port = port; + _errNo = errNo; } @catch (id e) { [self release]; @throw e; } @@ -63,14 +91,20 @@ [super dealloc]; } - (OFString*)description { - return [OFString stringWithFormat: - @"A connection to %@ on port %" @PRIu16 @" could not be " - @"established in socket of type %@! " ERRFMT, _host, _port, - [_socket class], ERRPARAM]; + if (_errNo != 0) + return [OFString stringWithFormat: + @"A connection to %@ on port %" @PRIu16 @" could not be " + @"established in socket of type %@: %@", + _host, _port, [_socket class], of_strerror(_errNo)]; + else + return [OFString stringWithFormat: + @"A connection to %@ on port %" @PRIu16 @" could not be " + @"established in socket of type %@!", + _host, _port, [_socket class]]; } - (OFString*)host { OF_GETTER(_host, true) @@ -86,12 +120,8 @@ OF_GETTER(_socket, true) } - (int)errNo { -#ifdef _WIN32 - return of_wsaerr_to_errno(_errNo); -#else return _errNo; -#endif } @end