diff options
author | Thorvald Natvig <slicer@users.sourceforge.net> | 2007-09-07 06:09:40 +0400 |
---|---|---|
committer | Thorvald Natvig <slicer@users.sourceforge.net> | 2007-09-07 06:09:40 +0400 |
commit | 568fdeb28297266364d5f7faf38b839fabed9937 (patch) | |
tree | 12b88d718d9581c568155fef989b5ad98045ce55 /src | |
parent | 2f046d8f845d3b24bf4a9110f4396eecd74571a1 (diff) |
More gracefully handle oversized outgoing packets from murmur
git-svn-id: https://mumble.svn.sourceforge.net/svnroot/mumble/trunk@763 05730e5d-ab1b-0410-a4ac-84af385074fa
Diffstat (limited to 'src')
-rw-r--r-- | src/Message.cpp | 19 | ||||
-rw-r--r-- | src/PacketDataStream.h | 14 | ||||
-rw-r--r-- | src/tests/TestPacketDataStream.cpp | 19 |
3 files changed, 47 insertions, 5 deletions
diff --git a/src/Message.cpp b/src/Message.cpp index 7162bf2e8..3cc32db66 100644 --- a/src/Message.cpp +++ b/src/Message.cpp @@ -39,13 +39,24 @@ Message::~Message() { } void Message::messageToNetwork(QByteArray &qbaOut) const { - char buffer[65535]; - PacketDataStream qdsOut(buffer, 65535); + char buffer[8192]; + PacketDataStream qdsOut(buffer, 8192); qdsOut << messageType(); qdsOut << uiSession; saveStream(qdsOut); - - qbaOut = QByteArray(buffer, qdsOut.size()); + + if (qdsOut.undersize() == 0) { + qbaOut = QByteArray(buffer, qdsOut.size()); + return; + } + + unsigned int size = 8192 + qdsOut.undersize(); + char b[size]; + PacketDataStream pdsResized(b, size); + pdsResized << messageType(); + pdsResized << uiSession; + saveStream(pdsResized); + qbaOut = QByteArray(b, pdsResized.size()); } void Message::messageToNetwork(PacketDataStream &pds) const { diff --git a/src/PacketDataStream.h b/src/PacketDataStream.h index 07ed08e15..043080260 100644 --- a/src/PacketDataStream.h +++ b/src/PacketDataStream.h @@ -40,6 +40,7 @@ class PacketDataStream { unsigned char *data; quint32 maxsize; quint32 offset; + quint32 overshoot; bool ok; public: quint32 size() const { @@ -57,12 +58,18 @@ class PacketDataStream { quint32 left() const { return maxsize - offset; } + + quint32 undersize() const { + return overshoot; + } void append(const quint32 v) { if (offset < maxsize) data[offset++] = v; - else + else { ok = false; + overshoot++; + } }; void append(const char *d, quint32 len) { @@ -70,6 +77,10 @@ class PacketDataStream { memcpy(& data[offset], d, len); offset += len; } else { + int l = left(); + memset(& data[offset], 0, l); + offset += l; + overshoot += len - l; ok = false; } } @@ -121,6 +132,7 @@ class PacketDataStream { void setup(unsigned char *d, int msize) { data = d; offset = 0; + overshoot = 0; maxsize = msize; ok = true; } diff --git a/src/tests/TestPacketDataStream.cpp b/src/tests/TestPacketDataStream.cpp index 7aad73ceb..b062c2713 100644 --- a/src/tests/TestPacketDataStream.cpp +++ b/src/tests/TestPacketDataStream.cpp @@ -15,6 +15,7 @@ class TestPacketDataStream : public QObject { void space(); void floating(); void floating_data(); + void undersize(); }; void TestPacketDataStream::floating_data() { @@ -41,6 +42,24 @@ void TestPacketDataStream::floating() { QVERIFY(in.left() == 0); } +void TestPacketDataStream::undersize() { + QByteArray qba(32, 'Z'); + char buff[256]; + + for(unsigned int i=0;i<32;i++) { + PacketDataStream out(buff, i); + out << qba; + QCOMPARE(33-i, out.undersize()); + QVERIFY(! out.isValid()); + QVERIFY(out.left() == 0); + } + PacketDataStream out(buff, 33); + out << qba; + QCOMPARE(out.undersize(), 0U); + QVERIFY(out.isValid()); + QVERIFY(out.left() == 0); +} + void TestPacketDataStream::integer_data() { QTest::addColumn<quint64>("value"); for (int i=0;i<64;i++) { |