diff options
Diffstat (limited to 'src/Net/ClientConnection.cpp')
-rw-r--r-- | src/Net/ClientConnection.cpp | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/src/Net/ClientConnection.cpp b/src/Net/ClientConnection.cpp index 0162a86..e4de735 100644 --- a/src/Net/ClientConnection.cpp +++ b/src/Net/ClientConnection.cpp @@ -29,6 +29,7 @@ namespace Mad { namespace Net { +// TODO Error handling void ClientConnection::connectionHeaderReceiveHandler(const void *data, unsigned long length) { if(length != sizeof(ConnectionHeader)) // Error... disconnect @@ -55,15 +56,21 @@ void ClientConnection::connectionHeader() { } void ClientConnection::connect(const IPAddress &address, bool daemon0) throw(Exception) { + gl_rwlock_wrlock(stateLock); + daemon = daemon0; - if(isConnected()) + if(_isConnected()) { + gl_rwlock_unlock(stateLock); return; // TODO Error + } sock = socket(PF_INET, SOCK_STREAM, 0); - if(sock < 0) + if(sock < 0) { + gl_rwlock_unlock(stateLock); throw Exception("socket()", Exception::INTERNAL_ERRNO, errno); + } if(peer) delete peer; @@ -73,6 +80,8 @@ void ClientConnection::connect(const IPAddress &address, bool daemon0) throw(Exc close(sock); delete peer; peer = 0; + + gl_rwlock_unlock(stateLock); throw Exception("connect()", Exception::INTERNAL_ERRNO, errno); } @@ -82,6 +91,7 @@ void ClientConnection::connect(const IPAddress &address, bool daemon0) throw(Exc if(flags < 0) { close(sock); + gl_rwlock_unlock(stateLock); throw Exception("fcntl()", Exception::INTERNAL_ERRNO, errno); } @@ -96,9 +106,13 @@ void ClientConnection::connect(const IPAddress &address, bool daemon0) throw(Exc gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred); gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t)sock); - FdManager::get()->registerFd(sock, sigc::mem_fun(this, &Connection::sendReceive)); + FdManager::get()->registerFd(sock, sigc::mem_fun(this, &ClientConnection::sendReceive)); + + state = CONNECT; + + gl_rwlock_unlock(stateLock); - handshake(); + updateEvents(); } } |