diff options
Diffstat (limited to 'src/Net/ClientConnection.cpp')
-rw-r--r-- | src/Net/ClientConnection.cpp | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/src/Net/ClientConnection.cpp b/src/Net/ClientConnection.cpp index 66df2ea..ea2e10e 100644 --- a/src/Net/ClientConnection.cpp +++ b/src/Net/ClientConnection.cpp @@ -32,8 +32,6 @@ void ClientConnection::connect(const IPAddress &address) throw(ConnectionExcepti if(connected) disconnect(); - peer = new IPAddress(address); - sock = socket(PF_INET, SOCK_STREAM, 0); if(sock < 0) throw ConnectionException("socket()", std::strerror(errno)); @@ -41,6 +39,10 @@ void ClientConnection::connect(const IPAddress &address) throw(ConnectionExcepti if(::connect(sock, peer->getSockAddr(), peer->getSockAddrLength()) < 0) throw ConnectionException("connect()", std::strerror(errno)); + peer = new IPAddress(address); + + connected = true; + gnutls_anon_allocate_client_credentials(&anoncred); gnutls_init(&session, GNUTLS_CLIENT); @@ -52,10 +54,11 @@ void ClientConnection::connect(const IPAddress &address) throw(ConnectionExcepti gnutls_transport_set_ptr(session, reinterpret_cast<gnutls_transport_ptr_t>(sock)); int ret = gnutls_handshake(session); - if(ret < 0) + if(ret < 0) { + disconnect(); + throw ConnectionException("gnutls_handshake()", gnutls_strerror(ret)); - - connected = true; + } } void ClientConnection::disconnect() { @@ -64,19 +67,14 @@ void ClientConnection::disconnect() { gnutls_bye(session, GNUTLS_SHUT_RDWR); - if(sock >= 0) { - shutdown(sock, SHUT_RDWR); - close(sock); - sock = -1; - } + shutdown(sock, SHUT_RDWR); + close(sock); + sock = -1; gnutls_deinit(session); gnutls_anon_free_client_credentials(anoncred); - if(peer) { - delete peer; - peer = 0; - } + delete peer; connected = false; } |