summaryrefslogtreecommitdiffstats
path: root/signet
diff options
context:
space:
mode:
Diffstat (limited to 'signet')
-rw-r--r--signet/protocol/Packet.cpp91
-rw-r--r--signet/protocol/Packet.h16
-rw-r--r--signet/protocol/PacketMaker.cpp101
-rw-r--r--signet/protocol/PacketMaker.h10
4 files changed, 99 insertions, 119 deletions
diff --git a/signet/protocol/Packet.cpp b/signet/protocol/Packet.cpp
index e4d5eef5..74c785e6 100644
--- a/signet/protocol/Packet.cpp
+++ b/signet/protocol/Packet.cpp
@@ -23,59 +23,19 @@
#include <QtCore/QBuffer>
#include <QtCore/QIODevice>
-Signet::Protocol::Packet::Packet(const Type type) :
+Signet::Protocol::Packet::Packet(const qint16 type) :
m_type(type)
{
}
-bool Signet::Protocol::Packet::determineType()
+qint16 Signet::Protocol::Packet::type() const
{
- QByteArray temp = m_rawData;
- QBuffer buffer(&temp);
- int size = m_rawData.size();
- union
- {
- char packetType_raw[2];
- qint16 packetType;
- };
- union
- {
- char dataSize_raw[8];
- qint64 dataSize;
- };
- if ((m_type == Invalid) && (10 <= size))
- {
- buffer.read(packetType_raw, 2);
- buffer.read(dataSize_raw, 8);
- packetType = qFromBigEndian(packetType);
- dataSize = qFromBigEndian(dataSize);
- switch (packetType)
- {
- case RawData:
- m_type = RawData;
- break;
- case Acceptance:
- m_type = Acceptance;
- break;
- case Denial:
- m_type = Denial;
- break;
- case Challenge:
- m_type = Challenge;
- break;
- default:
- m_type = Invalid;
- break;
- }
- }
- if (m_type == Invalid)
- return false;
- // TODO: verify the size
+ return m_type;
}
-qint16 Signet::Protocol::Packet::type() const
+QStringList Signet::Protocol::Packet::receivers() const
{
- return m_type;
+ return m_receivers;
}
bool Signet::Protocol::Packet::isValid() const
@@ -83,6 +43,16 @@ bool Signet::Protocol::Packet::isValid() const
return m_type == Invalid;
}
+void Signet::Protocol::Packet::addReceiver(const QString& receiver)
+{
+ m_receivers << receiver;
+}
+
+void Signet::Protocol::Packet::addReceivers(const QStringList& receivers)
+{
+ m_receivers << receivers;
+}
+
void Signet::Protocol::Packet::write(const QByteArray& data)
{
if ((m_type == RawData) || (m_type == Invalid))
@@ -103,16 +73,27 @@ void Signet::Protocol::Packet::dump(QIODevice* device) const
}
union
{
- char header[10];
- struct
- {
- qint16 type;
- qint64 size;
- }headerParts;
+ char raw[4];
+ qint16 type;
+ qint32 size;
};
- QByteArray data = m_rawData;
- headerParts.type = qToBigEndian(m_type);
- headerParts.size = qToBigEndian(m_rawData.size());
- data.prepend(header);
+ QByteArray data;
+ QBuffer buffer(&data);
+ type = qToBigEndian(m_type);
+ buffer.write(raw, 2);
+ foreach (const QString& receiver, m_receivers)
+ {
+ QByteArray utf8Data = receiver.toUtf8();
+ size = receiver.size();
+ size = qToBigEndian(size);
+ buffer.putChar('\0');
+ buffer.write(raw, 4);
+ buffer.write(utf8Data.constData(), utf8Data.size());
+ }
+ size = m_rawData.size();
+ size = qToBigEndian(size);
+ buffer.putChar('\x1');
+ buffer.write(raw, 4);
+ buffer.write(m_rawData);
device->write(data);
}
diff --git a/signet/protocol/Packet.h b/signet/protocol/Packet.h
index 2d15c115..93034789 100644
--- a/signet/protocol/Packet.h
+++ b/signet/protocol/Packet.h
@@ -23,6 +23,7 @@
// Qt includes
#include <QtCore/QByteArray>
+#include <QtCore/QStringList>
// Forward declarations
class QIODevice;
@@ -41,21 +42,30 @@ class SIGNET_EXPORT Packet
Acceptance = 0x0001,
Denial = 0x0002,
Challenge = 0x0003,
+ Message = 0x0004
+ };
+ enum SectionType
+ {
+ Receiver = 0,
+ Payload = 1
};
- Packet(const Type type = Invalid);
+ Packet(const qint16 type = Invalid);
// TODO: Add other ctors for other data packages
- bool determineType();
-
qint16 type() const;
+ QStringList receivers() const;
bool isValid() const;
+ void addReceiver(const QString& receiver);
+ void addReceivers(const QStringList& receivers);
+
void write(const QByteArray& data);
void rawDump(QIODevice* device) const;
void dump(QIODevice* device) const;
private:
qint16 m_type;
+ QStringList m_receivers;
QByteArray m_rawData;
};
}
diff --git a/signet/protocol/PacketMaker.cpp b/signet/protocol/PacketMaker.cpp
index 2435942d..3c398ffe 100644
--- a/signet/protocol/PacketMaker.cpp
+++ b/signet/protocol/PacketMaker.cpp
@@ -23,104 +23,91 @@
#include <QtCore/QBuffer>
#include <QtCore/QIODevice>
-bool readIntoBuffer(QIODevice* dest, QIODevice* src, const int size)
+bool readFromBuffer(char* dest, QIODevice* src, const int size)
{
- char* tbuffer = new char[size];
int totalRead = 0;
while ((totalRead < size) && src->waitForReadyRead(30000))
{
- int read = src->read(tbuffer + totalRead, 10 - totalRead);
- int written = 0;
+ int read = src->read(dest + totalRead, size - totalRead);
if (read < 0)
- {
- delete [] tbuffer;
return false;
- }
- while (written < read)
- {
- int writtenThisTime = dest->write(tbuffer + written, read - written);
- if (writtenThisTime < 0)
- {
- delete [] tbuffer;
- return false;
- }
- written += writtenThisTime;
- }
totalRead += read;
}
- delete [] tbuffer;
return true;
}
-void Signet::Protocol::PacketMaker::rawData(QIODevice* device, const QByteArray& data)
+void Signet::Protocol::PacketMaker::rawData(QIODevice* device, const QByteArray& data, const QStringList& receivers)
{
Packet packet(Packet::RawData);
+ packet.addReceivers(receivers);
packet.write(data);
packet.dump(device);
}
-void Signet::Protocol::PacketMaker::deny(QIODevice* device)
+void Signet::Protocol::PacketMaker::deny(QIODevice* device, const QStringList& receivers)
{
Packet packet(Packet::Denial);
+ packet.addReceivers(receivers);
packet.dump(device);
}
-void Signet::Protocol::PacketMaker::accept(QIODevice* device)
+void Signet::Protocol::PacketMaker::accept(QIODevice* device, const QStringList& receivers)
{
Packet packet(Packet::Acceptance);
+ packet.addReceivers(receivers);
packet.dump(device);
}
-void Signet::Protocol::PacketMaker::challenge(QIODevice* device, const QByteArray& id)
+void Signet::Protocol::PacketMaker::challenge(QIODevice* device, const QByteArray& id, const QStringList& receivers)
{
Packet packet(Packet::Challenge);
+ packet.addReceivers(receivers);
packet.write(id);
packet.dump(device);
}
Signet::Protocol::Packet Signet::Protocol::PacketMaker::unwrap(QIODevice* device)
{
- QByteArray temp;
- QBuffer buffer(&temp);
union
{
- char expectedType_raw[2];
- qint16 expectedType;
+ char raw[4];
+ qint32 sectionSize;
+ qint16 packetType;
+ qint8 sectionType;
};
- union
+ if (!readFromBuffer(raw, device, 2))
+ return Packet();
+ packetType = qFromBigEndian(packetType);
+ Packet packet(packetType);
+ if (!readFromBuffer(raw, device, 1))
+ return Packet();
+ while (sectionType == Packet::Receiver)
{
- char expectedSize_raw[8];
- qint64 expectedSize;
- };
- if (!readIntoBuffer(&buffer, device, 10))
+ if (!readFromBuffer(raw, device, 4))
+ return Packet();
+ char* buffer = new char[sectionSize + 1];
+ buffer[sectionSize] = '\0';
+ sectionSize = qFromBigEndian(sectionSize);
+ if (!readFromBuffer(buffer, device, sectionSize))
+ {
+ delete [] buffer;
+ return Packet();
+ }
+ packet.addReceiver(QString::fromUtf8(buffer));
+ delete [] buffer;
+ if (!readFromBuffer(raw, device, 1))
+ return Packet();
+ }
+ if ((sectionType != Packet::Payload) || !readFromBuffer(raw, device, 4))
return Packet();
- buffer.write(expectedType_raw, 2);
- expectedType = qFromBigEndian(expectedType);
- buffer.write(expectedSize_raw, 8);
- expectedSize = qFromBigEndian(expectedSize);
- Packet packet;
- switch (expectedType)
+ char* buffer = new char[sectionSize];
+ sectionSize = qFromBigEndian(sectionSize);
+ if (!readFromBuffer(buffer, device, sectionSize))
{
- case Packet::RawData:
- packet = Packet(Packet::RawData);
- if (readIntoBuffer(&buffer, device, expectedSize))
- packet.write(buffer.buffer());
- break;
- case Packet::Acceptance:
- if (!expectedSize)
- packet = Packet(Packet::Acceptance);
- break;
- case Packet::Denial:
- if (!expectedSize)
- packet = Packet(Packet::Denial);
- break;
- case Packet::Challenge:
- packet = Packet(Packet::Challenge);
- if (readIntoBuffer(&buffer, device, expectedSize))
- packet.write(buffer.buffer());
- break;
- default:
- break;
+ delete [] buffer;
+ return Packet();
}
+ packet.write(QByteArray(buffer, sectionSize));
+ delete [] buffer;
return packet;
}
diff --git a/signet/protocol/PacketMaker.h b/signet/protocol/PacketMaker.h
index 4fca41a8..24ab1b17 100644
--- a/signet/protocol/PacketMaker.h
+++ b/signet/protocol/PacketMaker.h
@@ -34,12 +34,14 @@ namespace Protocol
class SIGNET_EXPORT PacketMaker
{
public:
- static void rawData(QIODevice* device, const QByteArray& data);
+ static void rawData(QIODevice* device, const QByteArray& data, const QStringList& receivers = QStringList());
- static void deny(QIODevice* device);
- static void accept(QIODevice* device);
+ static void deny(QIODevice* device, const QStringList& receivers = QStringList());
+ static void accept(QIODevice* device, const QStringList& receivers = QStringList());
- static void challenge(QIODevice* device, const QByteArray& id);
+ static void challenge(QIODevice* device, const QByteArray& id, const QStringList& receivers = QStringList());
+
+ static void message(QIODevice* device, const QString& message, const QStringList& receivers = QStringList());
static Packet unwrap(QIODevice* device);
};