From 725ed2a06c5e376168dc1552d52cf0b77ed5c073 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 25 Jun 2008 03:16:01 +0200 Subject: Neuer Send-Stack --- src/Net/Connection.cpp | 32 ++++++++++++++++++++++++-------- src/Net/Connection.h | 7 ++++++- src/Net/ServerConnection.cpp | 2 +- src/mad-core.cpp | 2 +- src/madc.cpp | 3 +++ 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/Net/Connection.cpp b/src/Net/Connection.cpp index 768d827..b0f505b 100644 --- a/src/Net/Connection.cpp +++ b/src/Net/Connection.cpp @@ -18,6 +18,7 @@ */ #include "Connection.h" +#include namespace Mad { namespace Net { @@ -94,23 +95,38 @@ bool Connection::rawReceive(unsigned long length, return true; } -bool Connection::rawSend(const unsigned char *data, unsigned long length) { +void Connection::doSend() { if(!isConnected()) - return false; + return; - while(length > 0) { - ssize_t ret = gnutls_record_send(getSession(), data, length); + while(!sendQueueEmpty()) { + ssize_t ret = gnutls_record_send(getSession(), transS.front().data+transS.front().transmitted, + transS.front().length-transS.front().transmitted); if(ret < 0) { if(ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN) - continue; + return; - return false; + // Error... disconnect? + return; } - data += ret; - length -= ret; + transS.front().transmitted += ret; + + if(transS.front().transmitted == transS.front().length) { + delete [] transS.front().data; + transS.pop(); + } } +} + +bool Connection::rawSend(const unsigned char *data, unsigned long length) { + if(!isConnected()) + return false; + + Transmission trans = {length, 0, new unsigned char[length], sigc::slot()}; + std::memcpy(trans.data, data, length); + transS.push(trans); return true; } diff --git a/src/Net/Connection.h b/src/Net/Connection.h index 7e73955..d13c2f6 100644 --- a/src/Net/Connection.h +++ b/src/Net/Connection.h @@ -43,6 +43,7 @@ class Connection { }; Transmission transR; + std::queue transS; Packet::Data header; @@ -52,6 +53,7 @@ class Connection { void packetDataReceiveHandler(const void *data, unsigned long length); void doReceive(); + void doSend(); bool receiveComplete() const { return (transR.length == transR.transmitted); @@ -84,10 +86,13 @@ class Connection { return rawSend(reinterpret_cast(packet.getRawData()), packet.getRawDataLength()); } - void sendreceive() { + void sendReceive() { doReceive(); + doSend(); } + bool sendQueueEmpty() {return transS.empty();} + sigc::signal signalReceive() const {return signal;} static void init() { diff --git a/src/Net/ServerConnection.cpp b/src/Net/ServerConnection.cpp index 66274b4..afbfcbb 100644 --- a/src/Net/ServerConnection.cpp +++ b/src/Net/ServerConnection.cpp @@ -21,8 +21,8 @@ #include "IPAddress.h" #include #include -#include #include +#include namespace Mad { namespace Net { diff --git a/src/mad-core.cpp b/src/mad-core.cpp index 04b18cc..830f26a 100644 --- a/src/mad-core.cpp +++ b/src/mad-core.cpp @@ -42,7 +42,7 @@ int main() { try { connection.listen(Mad::Net::IPAddress("0.0.0.0", 6666)); - while(running) connection.sendreceive(); + while(running) connection.sendReceive(); } catch(Mad::Net::Exception &e) { std::cerr << "Connection error: " << e.what() << std::endl; diff --git a/src/madc.cpp b/src/madc.cpp index 9d525b2..0c28793 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -30,6 +30,9 @@ int main() { try { connection.connect(Mad::Net::IPAddress("127.0.0.1", 6666)); connection.send(Mad::Net::Packet(0x0001, 0xABCD)); + + while(!connection.sendQueueEmpty()) + connection.sendReceive(); } catch(Mad::Net::Exception &e) { std::cerr << "Connection error: " << e.what() << std::endl; -- cgit v1.2.3