tree changes
This commit is contained in:
221
libs/io_utils/pistreampacker.cpp
Normal file
221
libs/io_utils/pistreampacker.cpp
Normal file
@@ -0,0 +1,221 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Simple packet wrap aroud any PIIODevice
|
||||
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty 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/>.
|
||||
*/
|
||||
|
||||
#ifdef __GNUC__
|
||||
# pragma GCC diagnostic push
|
||||
# pragma GCC diagnostic ignored "-Wnonnull"
|
||||
#endif
|
||||
#include "pistreampacker.h"
|
||||
#include "piiodevice.h"
|
||||
#ifdef __GNUC__
|
||||
# pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
/** \class PIStreamPacker
|
||||
* \brief Simple packet wrap aroud any PIIODevice
|
||||
*
|
||||
* \section PIStreamPacker_synopsis Synopsis
|
||||
* %PIStreamPacker provides simple pack/unpack logic for any data packets.
|
||||
*
|
||||
* When you call \a send() function data splited into several
|
||||
* parts, \a packetSign() prepended to first part and \a sendRequest()
|
||||
* event raised several times.
|
||||
*
|
||||
* When your device receive some data, call \a received() function.
|
||||
* \a packetReceiveEvent() event will be raised when packet will be
|
||||
* collected.
|
||||
*
|
||||
* Use \a assignDevice() to connect device to this %PIStreamPacker.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
PIStreamPacker::PIStreamPacker(PIIODevice * dev): PIObject() {
|
||||
crypt_frag = false;
|
||||
packet_size = -1;
|
||||
crypt_frag_size = 1024*1024;
|
||||
max_packet_size = 1400;
|
||||
packet_sign = 0xAFBE;
|
||||
assignDevice(dev);
|
||||
}
|
||||
|
||||
|
||||
void PIStreamPacker::send(const PIByteArray & data) {
|
||||
if (data.isEmpty()) return;
|
||||
PIByteArray cd;
|
||||
if (crypt_frag) {
|
||||
int fcnt = (data.size_s() - 1) / crypt_frag_size + 1, fst = 0;
|
||||
//piCout << "crypt_frag send" << fcnt << "frags";
|
||||
PIByteArray frag;
|
||||
for (int i = 0; i < fcnt; ++i) {
|
||||
if (i == fcnt - 1) frag = PIByteArray(data.data(fst), data.size_s() - fst);
|
||||
else frag = PIByteArray(data.data(fst), crypt_frag_size);
|
||||
fst += crypt_frag_size;
|
||||
cd << cryptData(frag);
|
||||
}
|
||||
} else {
|
||||
cd = cryptData(data);
|
||||
}
|
||||
//piCout << "crypt" << data.size() << "->" << cd.size() << key().size();
|
||||
PIByteArray hdr, part;
|
||||
hdr << packet_sign << int(cd.size_s());
|
||||
cd.insert(0, hdr);
|
||||
int pcnt = (cd.size_s() - 1) / max_packet_size + 1, pst = 0;
|
||||
if (pcnt > 1) {
|
||||
prog_s_mutex.lock();
|
||||
prog_s.active = true;
|
||||
prog_s.bytes_all = data.size_s();
|
||||
prog_s.bytes_current = 0;
|
||||
prog_s.progress = 0.;
|
||||
prog_s_mutex.unlock();
|
||||
}
|
||||
for (int i = 0; i < pcnt; ++i) {
|
||||
if (i == pcnt - 1) part = PIByteArray(cd.data(pst), cd.size_s() - pst);
|
||||
else part = PIByteArray(cd.data(pst), max_packet_size);
|
||||
//piCout << "send" << part.size();
|
||||
sendRequest(part);
|
||||
pst += max_packet_size;
|
||||
if (pcnt > 1) {
|
||||
prog_s_mutex.lock();
|
||||
prog_s.bytes_current += part.size_s();
|
||||
prog_s.progress = (double)prog_s.bytes_current / prog_s.bytes_all;
|
||||
prog_s_mutex.unlock();
|
||||
}
|
||||
}
|
||||
if (pcnt > 1) {
|
||||
prog_s_mutex.lock();
|
||||
prog_s.active = false;
|
||||
prog_s_mutex.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void PIStreamPacker::received(uchar * readed, int size) {
|
||||
received(PIByteArray(readed, size));
|
||||
}
|
||||
|
||||
|
||||
void PIStreamPacker::received(const PIByteArray & data) {
|
||||
stream.append(data);
|
||||
//piCout << "rec" << data.size();
|
||||
while (stream.size_s() >= 6) {
|
||||
if (packet_size < 0) {
|
||||
ushort sign(0);
|
||||
memcpy(&sign, stream.data(), 2);
|
||||
if (sign != packet_sign) {
|
||||
stream.pop_front();
|
||||
continue;
|
||||
}
|
||||
int sz = -1;
|
||||
memcpy(&sz, stream.data(2), 4);
|
||||
if (sz < 0) {
|
||||
stream.pop_front();
|
||||
continue;
|
||||
}
|
||||
stream.remove(0, 6);
|
||||
packet.clear();
|
||||
packet_size = sz;
|
||||
if (packet_size == 0)
|
||||
packet_size = -1;
|
||||
else {
|
||||
prog_r_mutex.lock();
|
||||
prog_r.active = true;
|
||||
prog_r.bytes_all = packet_size;
|
||||
prog_r.bytes_current = 0;
|
||||
prog_r.progress = 0.;
|
||||
prog_r_mutex.unlock();
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
int ps = piMini(stream.size_s(), packet_size - packet.size_s());
|
||||
packet.append(stream.data(), ps);
|
||||
prog_r_mutex.lock();
|
||||
prog_r.bytes_current = packet.size_s();
|
||||
prog_r.progress = (double)prog_r.bytes_current / piMaxi(1, prog_r.bytes_all);
|
||||
prog_r_mutex.unlock();
|
||||
stream.remove(0, ps);
|
||||
if (packet.size_s() == packet_size) {
|
||||
PIByteArray cd;
|
||||
if (crypt_frag) {
|
||||
//piCout << "decrypt frags ..." << packet_size;
|
||||
while (packet.size_s() >= 4) {
|
||||
//piCout << "decrypt frags take data ...";
|
||||
PIByteArray frag;
|
||||
//piCout << "decrypt frags take data done" << frag.size_s();
|
||||
packet >> frag;
|
||||
if (frag.isEmpty()) {
|
||||
//piCout << "decrypt frags corrupt, break";
|
||||
cd.clear();
|
||||
break;
|
||||
}
|
||||
cd.append(decryptData(frag));
|
||||
//piCout << "decrypt frags add" << frag.size_s();
|
||||
}
|
||||
//piCout << "decrypt frags done" << cd.size();
|
||||
} else {
|
||||
cd = decryptData(packet);
|
||||
}
|
||||
//piCout << "decrypt" << packet.size() << "->" << cd.size() << key().size();
|
||||
if (!cd.isEmpty()) {
|
||||
packetReceived(cd);
|
||||
packetReceiveEvent(cd);
|
||||
}
|
||||
packet.clear();
|
||||
packet_size = -1;
|
||||
prog_r_mutex.lock();
|
||||
prog_r.active = false;
|
||||
prog_r_mutex.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void PIStreamPacker::assignDevice(PIIODevice * dev) {
|
||||
if (!dev) return;
|
||||
CONNECTU(dev, threadedReadEvent, this, received);
|
||||
CONNECTU(this, sendRequest, dev, write);
|
||||
}
|
||||
|
||||
|
||||
PIStreamPacker::Progress PIStreamPacker::progressSend() const {
|
||||
PIStreamPacker::Progress ret;
|
||||
prog_s_mutex.lock();
|
||||
ret = prog_s;
|
||||
prog_s_mutex.unlock();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIStreamPacker::Progress PIStreamPacker::progressReceive() const {
|
||||
PIStreamPacker::Progress ret;
|
||||
prog_r_mutex.lock();
|
||||
ret = prog_r;
|
||||
prog_r_mutex.unlock();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
PIStreamPacker::Progress::Progress() {
|
||||
active = false;
|
||||
bytes_all = bytes_current = 0;
|
||||
progress = 0.;
|
||||
}
|
||||
Reference in New Issue
Block a user