From 93b62ba1b88b478153a4d2a5ced97e4d5919b3ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B5=D0=BB=D0=B8=D0=BF=D0=B5=D0=BD=D0=BA=D0=BE=20?= =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD?= Date: Thu, 18 Apr 2019 06:24:26 +0000 Subject: [PATCH] git-svn-id: svn://db.shs.com.ru/pip@780 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5 --- src_io_utils/pistreampacker.cpp | 29 +++++++++++++++++++++++++++-- src_main/core/pibytearray.h | 12 +++++++++++- src_main/io_utils/pistreampacker.h | 21 ++++++++++++++++++++- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src_io_utils/pistreampacker.cpp b/src_io_utils/pistreampacker.cpp index 89d2f04a..71a73b48 100644 --- a/src_io_utils/pistreampacker.cpp +++ b/src_io_utils/pistreampacker.cpp @@ -40,7 +40,9 @@ 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); @@ -49,7 +51,20 @@ PIStreamPacker::PIStreamPacker(PIIODevice * dev): PIObject() { void PIStreamPacker::send(const PIByteArray & data) { if (data.isEmpty()) return; - PIByteArray cd = cryptData(data); + 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()); @@ -124,7 +139,17 @@ void PIStreamPacker::received(const PIByteArray & data) { prog_r_mutex.unlock(); stream.remove(0, ps); if (packet.size_s() == packet_size) { - PIByteArray cd = decryptData(packet); + PIByteArray cd; + if (crypt_frag) { + while (packet.size_s() >= 4) { + PIByteArray frag; + packet >> frag; + cd.append(decryptData(frag)); + piCout << "crypt_frag receive add frag" << frag.size_s(); + } + } else { + cd = decryptData(packet); + } //piCout << "decrypt" << packet.size() << "->" << cd.size() << key().size(); if (!cd.isEmpty()) { packetReceived(cd); diff --git a/src_main/core/pibytearray.h b/src_main/core/pibytearray.h index 5de379f8..c00fd650 100755 --- a/src_main/core/pibytearray.h +++ b/src_main/core/pibytearray.h @@ -209,7 +209,17 @@ inline PIByteArray & operator >>(PIByteArray & s, ldouble & v) {assert(s.size() //! \relatesalso PIByteArray \brief Restore operator template inline PIByteArray & operator >>(PIByteArray & s, PIFlags & v) {PBA_OPERATOR_FROM return s;} //! \relatesalso PIByteArray \brief Restore operator, see \ref PIByteArray_sec1 for details -inline PIByteArray & operator >>(PIByteArray & s, PIByteArray & v) {assert(s.size_s() >= 4); int sz; s >> sz; v.resize(sz); if (sz > 0) memcpy(v.data(), s.data(), v.size()); s.remove(0, v.size()); return s;} +inline PIByteArray & operator >>(PIByteArray & s, PIByteArray & v) { + assert(s.size_s() >= 4); + int sz; s >> sz; + if (sz > s.size_s()) { + piCout << "[PIByteArray] Warning: operator >> want too much data!"; + sz = s.size_s(); + } + v.resize(sz); + if (sz > 0) memcpy(v.data(), s.data(), v.size()); + s.remove(0, v.size()); + return s;} //! \relatesalso PIByteArray \brief Restore operator, see \ref PIByteArray_sec1 for details inline PIByteArray & operator >>(PIByteArray & s, PIByteArray::RawData v) {assert(s.size_s() >= v.s); if (v.s > 0) memcpy((void*)(v.d), s.data(), v.s); s.remove(0, v.s); return s;} diff --git a/src_main/io_utils/pistreampacker.h b/src_main/io_utils/pistreampacker.h index 5cad2534..dec59134 100644 --- a/src_main/io_utils/pistreampacker.h +++ b/src_main/io_utils/pistreampacker.h @@ -35,11 +35,20 @@ public: //! Contructs packer and try to assign \"dev\" PIStreamPacker(PIIODevice * dev = 0); + //! Progress info struct Progress { Progress(); + + //! Is send/receive in progress bool active; + + //! Overall send/receive packet size int bytes_all; + + //! Current send/receive size int bytes_current; + + //! Current send/receive progress from 0 to 1 double progress; }; @@ -57,6 +66,12 @@ public: ushort packetSign() {return packet_sign;} + bool cryptFragmentationEnabled() const {return crypt_frag;} + void setCryptFragmentationEnabled(bool on) {crypt_frag = on;} + int cryptFragmentationSize() const {return crypt_frag_size;} + void setCryptFragmentationSize(int size_) {crypt_frag_size = size_;} + + //! Prepare data for send and raise \a sendRequest() events void send(const PIByteArray & data); @@ -69,7 +84,10 @@ public: //! and \a sendRequest() event to \"dev\" \a PIIODevice::write() handler void assignDevice(PIIODevice * dev); + //! Returns \a Progress info about sending Progress progressSend() const; + + //! Returns \a Progress info about receiving Progress progressReceive() const; EVENT1(packetReceiveEvent, PIByteArray, data) @@ -104,7 +122,8 @@ protected: private: PIByteArray stream, packet; - int packet_size; + bool crypt_frag; + int packet_size, crypt_frag_size; ushort packet_sign; int max_packet_size; Progress prog_s, prog_r;