I’m working on an NMDC client (p2p, DC++ and friends) with Qt. The protocol itself is pretty straightforward:
$command parameters|
Except for compression:
“ZPipe works by sending a command $ZOn| to the client. After $ZOn a ZLib compressed stream containing commands will follow. This stream will end with an EOF that ZLib defines. (there is no $ZOff in the compressed stream!)”
Here’s the relevant code:
QTcpSocket *conn;
bool compressed;
QByteArray zbuffer;
QByteArray buffer;
// ...
void NMDCConnection::on_conn_readyRead() {
// this gets called whenever we get new data from the hub
if(compressed) { // gets set when we receive $ZOn
zbuffer.append(conn->readAll());
// Magic happens here
if( stream_is_complete ) {
buffer.append(uncompressed_stream);
buffer.append(remainder_of_data);
compressed = false;
}
} else {
buffer.append(conn->readAll());
};
parse(buffer);
}
So, how do I get the values for stream_is_complete, uncompressed_stream, and remainder_of_data? I can’t look for the next ‘$’ because the stream can contain it. I tried looking for something resembling an EOF in the zlib documentation, but there is no such thing, in fact, every stream ends with a seemingly random character.
I also played around with qUncompress(), but that wants a complete stream, nothing less, nothing more.
Are you using zlib directly?
Totally untested…
This incrementally decompresses (inflates) from
qbuffertoqout, and stops wheninflatesays “no more”.Maybe it would be better to borrow from QuaZip instead.