@@ -1,7 +1,7 @@ /* - * Copyright (c) 2008-2022 Jonathan Schleifer + * Copyright (c) 2008-2024 Jonathan Schleifer * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in @@ -18,11 +18,11 @@ #include #import "OFGnuTLSTLSStream.h" #import "OFData.h" -#import "OFAlreadyConnectedException.h" +#import "OFAlreadyOpenException.h" #import "OFInitializationFailedException.h" #import "OFNotOpenException.h" #import "OFReadFailedException.h" #import "OFTLSHandshakeFailedException.h" #import "OFWriteFailedException.h" @@ -125,11 +125,11 @@ if (_handshakeDone) gnutls_bye(_session, GNUTLS_SHUT_WR); gnutls_deinit(_session); - _initialized = false; + _initialized = _handshakeDone = false; [_host release]; _host = nil; [super close]; @@ -183,16 +183,14 @@ } return ret; } -- (bool)hasDataInReadBuffer +- (bool)lowlevelHasDataInReadBuffer { - if (gnutls_record_check_pending(_session) > 0) - return true; - - return super.hasDataInReadBuffer; + return (_underlyingStream.hasDataInReadBuffer || + gnutls_record_check_pending(_session) > 0); } - (void)asyncPerformClientHandshakeWithHost: (OFString *)host runLoopMode: (OFRunLoopMode)runLoopMode { @@ -200,11 +198,11 @@ OFTLSStreamErrorCodeInitializationFailed; id exception = nil; int status; if (_initialized) - @throw [OFAlreadyConnectedException exceptionWithSocket: self]; + @throw [OFAlreadyOpenException exceptionWithObject: self]; if (gnutls_init(&_session, GNUTLS_CLIENT | GNUTLS_NONBLOCK | GNUTLS_SAFE_PADDING_CHECK) != GNUTLS_E_SUCCESS) @throw [OFTLSHandshakeFailedException exceptionWithStream: self @@ -239,13 +237,12 @@ status = gnutls_handshake(_session); if (status == GNUTLS_E_INTERRUPTED || status == GNUTLS_E_AGAIN) { if (gnutls_record_get_direction(_session) == 1) - [_underlyingStream - asyncWriteData: [OFData dataWithItems: "" count: 0] - runLoopMode: runLoopMode]; + [_underlyingStream asyncWriteData: [OFData data] + runLoopMode: runLoopMode]; else [_underlyingStream asyncReadIntoBuffer: (void *)"" length: 0 runLoopMode: runLoopMode]; @@ -270,23 +267,21 @@ } - (bool)stream: (OFStream *)stream didReadIntoBuffer: (void *)buffer length: (size_t)length - exception: (nullable id)exception + exception: (id)exception { if (exception == nil) { int status = gnutls_handshake(_session); if (status == GNUTLS_E_INTERRUPTED || status == GNUTLS_E_AGAIN) { if (gnutls_record_get_direction(_session) == 1) { - OFData *data = [OFData dataWithItems: "" - count: 0]; OFRunLoopMode runLoopMode = [OFRunLoop currentRunLoop].currentMode; - [_underlyingStream asyncWriteData: data + [_underlyingStream asyncWriteData: [OFData data] runLoopMode: runLoopMode]; return false; } else return true; }