Skip to content

Commit

Permalink
libcore: Block should be serializable; added more utility methods
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Feb 12, 2017
1 parent a8bf1b7 commit 1316dea
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 46 deletions.
14 changes: 8 additions & 6 deletions doomsday/apps/client/src/network/serverlink.cpp
Expand Up @@ -682,18 +682,20 @@ void ServerLink::handleIncomingPackets()
QScopedPointer<BlockPacket> packet(static_cast<BlockPacket *>(nextPacket()));
if (packet.isNull()) break;

Block const &packetData = packet->block();

switch (d->state)
{
case WaitingForInfoResponse:
if (!d->handleInfoResponse(*packet)) return;
if (!d->handleInfoResponse(packetData)) return;
break;

case WaitingForJoinResponse:
if (!d->handleJoinResponse(*packet)) return;
if (!d->handleJoinResponse(packetData)) return;
break;

case WaitingForPong:
if (packet->size() == 4 && *packet == "Pong" &&
if (packetData.size() == 4 && packetData == "Pong" &&
d->pingCounter-- > 0)
{
d->pings.append(TimeDelta::fromMilliSeconds(d->pingTimer.elapsed()));
Expand Down Expand Up @@ -728,9 +730,9 @@ void ServerLink::handleIncomingPackets()
netmessage_t *msg = reinterpret_cast<netmessage_t *>(M_Calloc(sizeof(netmessage_t)));

msg->sender = 0; // the server
msg->data = new byte[packet->size()];
memcpy(msg->data, packet->data(), packet->size());
msg->size = packet->size();
msg->data = new byte[packetData.size()];
memcpy(msg->data, packetData.data(), packetData.size());
msg->size = packetData.size();
msg->handle = msg->data; // needs delete[]

// The message queue will handle the message from now on.
Expand Down
21 changes: 20 additions & 1 deletion doomsday/sdk/libcore/include/de/data/block.h
Expand Up @@ -22,11 +22,13 @@

#include "../IByteArray"
#include "../IBlock"
#include "../ISerializable"

#include <QByteArray>

namespace de {

class String;
class IIStream;

/**
Expand All @@ -39,7 +41,8 @@ class IIStream;
*
* @ingroup data
*/
class DENG2_PUBLIC Block : public QByteArray, public IByteArray, public IBlock
class DENG2_PUBLIC Block : public QByteArray, public IByteArray, public IBlock,
public ISerializable
{
public:
Block(Size initialSize = 0);
Expand Down Expand Up @@ -89,6 +92,20 @@ class DENG2_PUBLIC Block : public QByteArray, public IByteArray, public IBlock
void resize(Size size);
Byte const *data() const;

// Implements ISerializable.
/**
* Writes @a block into the destination buffer. Writes the size of the
* block in addition to its contents, so a Reader will not need to know
* beforehand how large the block is.
*
* @param block Block to write.
*
* @return Reference to the Writer.
*/
void operator >> (Writer &to) const;

void operator << (Reader &from);

Byte *data();
Byte const *dataConst() const;

Expand All @@ -111,6 +128,8 @@ class DENG2_PUBLIC Block : public QByteArray, public IByteArray, public IBlock

Block compressed() const;
Block decompressed() const;
Block md5Hash() const;
String asHexadecimalText() const;

public:
static Block join(QList<Block> const &blocks, Block const &sep = Block());
Expand Down
5 changes: 2 additions & 3 deletions doomsday/sdk/libcore/include/de/data/reader.h
Expand Up @@ -143,6 +143,8 @@ class DENG2_PUBLIC Reader
/// Reads a string from the source buffer.
Reader &operator >> (String &text);

Reader &operator >> (Block &block);

/// Reads a sequence bytes from the source buffer.
Reader &operator >> (IByteArray &byteArray);

Expand Down Expand Up @@ -179,9 +181,6 @@ class DENG2_PUBLIC Reader
*/
Reader &readBytesFixedSize(IByteArray &destination);

/// Reads a Block from the source buffer.
Reader &operator >> (Block &block);

/// Reads a serializable object from the source buffer.
Reader &operator >> (IReadable &readable);

Expand Down
13 changes: 2 additions & 11 deletions doomsday/sdk/libcore/include/de/data/writer.h
Expand Up @@ -149,6 +149,8 @@ class DENG2_PUBLIC Writer
*/
Writer &operator << (String const &text);

Writer &operator << (Block const &block);

/**
* Writes a string to the destination buffer.
*
Expand Down Expand Up @@ -203,17 +205,6 @@ class DENG2_PUBLIC Writer
*/
Writer &writeBytes(IByteArray const &array);

/**
* Writes @a block into the destination buffer. Writes the size of the
* block in addition to its contents, so a Reader will not need to know
* beforehand how large the block is.
*
* @param block Block to write.
*
* @return Reference to the Writer.
*/
Writer &operator << (Block const &block);

/// Writes a writable object into the destination buffer.
Writer &operator << (IWritable const &writable);

Expand Down
7 changes: 5 additions & 2 deletions doomsday/sdk/libcore/include/de/net/blockpacket.h
Expand Up @@ -13,7 +13,7 @@
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBDENG2_BLOCKPACKET_H
Expand All @@ -29,7 +29,7 @@ namespace de {
*
* @ingroup protocol
*/
class DENG2_PUBLIC BlockPacket : public Packet, public Block
class DENG2_PUBLIC BlockPacket : public Packet, private Block
{
public:
BlockPacket();
Expand All @@ -40,6 +40,9 @@ class DENG2_PUBLIC BlockPacket : public Packet, public Block
void operator >> (Writer &to) const;
void operator << (Reader &from);

Block &block();
Block const &block() const;

public:
/// Constructor for a Protocol.
static Packet *fromBlock(Block const &block);
Expand Down
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/include/de/net/packet.h
Expand Up @@ -109,7 +109,7 @@ class DENG2_PUBLIC Packet : public ISerializable
if (checkType(from, packetTypeIdentifier))
{
std::unique_ptr<PacketType> p(new PacketType);
from >> *static_cast<IReadable *>(p.get());
*p << from;
return p.release();
}
return 0;
Expand Down
44 changes: 44 additions & 0 deletions doomsday/sdk/libcore/src/data/block.cpp
Expand Up @@ -20,6 +20,7 @@
#include "de/Block"
#include "de/File"

#include <QCryptographicHash>
#include <cstring>

namespace de {
Expand Down Expand Up @@ -149,6 +150,23 @@ Block::Byte const *Block::data() const
return reinterpret_cast<Byte const *>(QByteArray::data());
}

void Block::operator >> (Writer &to) const
{
// First write the length of the block.
to << duint32(size());

to.writeBytes(size(), *this);
}

void Block::operator << (Reader &from)
{
duint32 size = 0;
from >> size;

resize(size);
from.readBytes(size, *this);
}

Block &Block::operator += (Block const &other)
{
QByteArray::append(other);
Expand Down Expand Up @@ -185,6 +203,32 @@ Block Block::decompressed() const
return qUncompress(*this);
}

Block Block::md5Hash() const
{
QCryptographicHash hash(QCryptographicHash::Md5);
hash.addData(*this);
return hash.result();
}

static char asciiHexNumber(char n)
{
if (n < 0 || n > 15) return ' ';
return n <= 9? ('0' + n) : ('a' + n - 10);
}

String Block::asHexadecimalText() const
{
int const count = QByteArray::size();
String hex(count * 2, QChar('0'));
for (int i = 0; i < count; i++)
{
auto const ch = duint8(at(i));
hex[i*2] = QChar(asciiHexNumber(ch >> 4));
hex[i*2 + 1] = QChar(asciiHexNumber(ch & 0xf));
}
return hex;
}

Block Block::join(QList<Block> const &blocks, Block const &sep) // static
{
if (blocks.isEmpty()) return Block();
Expand Down
16 changes: 5 additions & 11 deletions doomsday/sdk/libcore/src/data/reader.cpp
Expand Up @@ -322,6 +322,11 @@ Reader &Reader::operator >> (String &text)
return *this;
}

Reader &Reader::operator >> (Block &block)
{
return *this >> static_cast<IReadable &>(block);
}

Reader &Reader::operator >> (IByteArray &byteArray)
{
duint size = 0;
Expand Down Expand Up @@ -364,17 +369,6 @@ Reader &Reader::readBytesFixedSize(IByteArray &destination)
return *this >> dest;
}

Reader &Reader::operator >> (Block &block)
{
duint size = 0;
*this >> size;

block.resize(size);
d->readBytes(block.data(), size);

return *this;
}

Reader &Reader::operator >> (IReadable &readable)
{
readable << *this;
Expand Down
15 changes: 5 additions & 10 deletions doomsday/sdk/libcore/src/data/writer.cpp
Expand Up @@ -192,6 +192,11 @@ Writer &Writer::operator << (String const &text)
return *this;
}

Writer &Writer::operator << (Block const &block)
{
return *this << static_cast<IWritable const &>(block);
}

Writer &Writer::writeText(String const &text)
{
Block const utf8 = text.toUtf8();
Expand Down Expand Up @@ -229,16 +234,6 @@ Writer &Writer::writeBytes(IByteArray const &array)
return *this << FixedByteArray(array);
}

Writer &Writer::operator << (Block const &block)
{
// First write the length of the block.
duint32 size = block.size();
*this << size;

d->write(block.data(), size);
return *this;
}

Writer &Writer::operator << (IWritable const &writable)
{
writable >> *this;
Expand Down
12 changes: 11 additions & 1 deletion doomsday/sdk/libcore/src/net/blockpacket.cpp
Expand Up @@ -13,7 +13,7 @@
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
* http://www.gnu.org/licenses</small>
*/

#include "de/BlockPacket"
Expand Down Expand Up @@ -43,6 +43,16 @@ void BlockPacket::operator << (Reader &from)
from >> *static_cast<Block *>(this);
}

Block &BlockPacket::block()
{
return *this;
}

Block const &BlockPacket::block() const
{
return *this;
}

Packet *BlockPacket::fromBlock(Block const &block)
{
// Attempts to deserialize from the data in block.
Expand Down

0 comments on commit 1316dea

Please sign in to comment.