summaryrefslogtreecommitdiffstats
path: root/src/Net/Connection.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Net/Connection.cpp')
-rw-r--r--src/Net/Connection.cpp62
1 files changed, 58 insertions, 4 deletions
diff --git a/src/Net/Connection.cpp b/src/Net/Connection.cpp
index 22adeaf..da1ff3f 100644
--- a/src/Net/Connection.cpp
+++ b/src/Net/Connection.cpp
@@ -22,11 +22,42 @@
#include <cstring>
#include <sys/socket.h>
-#include <iostream>
-
namespace Mad {
namespace Net {
+Connection::~Connection() {
+ if(isConnected())
+ doDisconnect();
+
+ if(transR.data)
+ delete [] transR.data;
+
+ while(!sendQueueEmpty()) {
+ delete [] transS.front().data;
+ transS.pop();
+ }
+
+ gnutls_certificate_free_credentials(x509_cred);
+}
+
+void Connection::handshake() {
+ if(isConnected())
+ return;
+
+ state = HANDSHAKE;
+
+ doHandshake();
+}
+
+void Connection::bye() {
+ if(state != DISCONNECT)
+ return;
+
+ state = BYE;
+
+ doBye();
+}
+
void Connection::doHandshake() {
if(state != HANDSHAKE)
return;
@@ -36,8 +67,6 @@ void Connection::doHandshake() {
if(ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN)
return;
- std::cerr << "Handshake error: " << gnutls_strerror(ret) << std::endl;
-
// TODO: Error
doDisconnect();
return;
@@ -64,6 +93,15 @@ void Connection::doBye() {
doDisconnect();
}
+bool Connection::enterReceiveLoop() {
+ if(!isConnected() || isDisconnecting())
+ return false;
+
+ state = PACKET_HEADER;
+
+ return rawReceive(sizeof(Packet::Data), sigc::mem_fun(this, &Connection::packetHeaderReceiveHandler));
+}
+
void Connection::packetHeaderReceiveHandler(const void *data, unsigned long length) {
if(state != PACKET_HEADER)
return;
@@ -213,6 +251,22 @@ void Connection::sendReceive(short events) {
bye();
}
+bool Connection::send(const Packet &packet) {
+ if(!isConnected() || isConnecting() || isDisconnecting())
+ return false;
+
+ return rawSend((const uint8_t*)packet.getRawData(), packet.getRawDataLength());
+}
+
+void Connection::disconnect() {
+ if(isConnected() && !isDisconnecting()) {
+ state = DISCONNECT;
+
+ if(sendQueueEmpty())
+ bye();
+ }
+}
+
void Connection::doDisconnect() {
if(!isConnected())
return;