From 1307b25994cd81179fb7216bf74e045ed318ee81 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: Fri, 5 Apr 2019 20:11:43 +0000 Subject: [PATCH] git-svn-id: svn://db.shs.com.ru/pip@774 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5 --- src_io_utils/pistreampacker.cpp | 62 +++++++++++++++++++++++++++++- src_main/io_utils/pistreampacker.h | 15 +++++++- 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src_io_utils/pistreampacker.cpp b/src_io_utils/pistreampacker.cpp index edfab765..89d2f04a 100644 --- a/src_io_utils/pistreampacker.cpp +++ b/src_io_utils/pistreampacker.cpp @@ -47,7 +47,7 @@ PIStreamPacker::PIStreamPacker(PIIODevice * dev): PIObject() { } -void PIStreamPacker::send(const PIByteArray &data) { +void PIStreamPacker::send(const PIByteArray & data) { if (data.isEmpty()) return; PIByteArray cd = cryptData(data); //piCout << "crypt" << data.size() << "->" << cd.size() << key().size(); @@ -55,12 +55,31 @@ void PIStreamPacker::send(const PIByteArray &data) { 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(); } } @@ -87,10 +106,22 @@ void PIStreamPacker::received(const PIByteArray & data) { 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 = decryptData(packet); @@ -101,6 +132,9 @@ void PIStreamPacker::received(const PIByteArray & data) { } packet.clear(); packet_size = -1; + prog_r_mutex.lock(); + prog_r.active = false; + prog_r_mutex.unlock(); } } } @@ -113,3 +147,29 @@ void PIStreamPacker::assignDevice(PIIODevice * dev) { 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.; +} diff --git a/src_main/io_utils/pistreampacker.h b/src_main/io_utils/pistreampacker.h index 2c1bd6ae..5cad2534 100644 --- a/src_main/io_utils/pistreampacker.h +++ b/src_main/io_utils/pistreampacker.h @@ -29,12 +29,20 @@ class PIIODevice; -class PIStreamPacker: public PIObject, public PIEthUtilBase { +class PIP_EXPORT PIStreamPacker: public PIObject, public PIEthUtilBase { PIOBJECT(PIStreamPacker) public: //! Contructs packer and try to assign \"dev\" PIStreamPacker(PIIODevice * dev = 0); + struct Progress { + Progress(); + bool active; + int bytes_all; + int bytes_current; + double progress; + }; + //! Set maximum size of single packet void setMaxPacketSize(int max_size) {max_packet_size = max_size;} @@ -61,6 +69,9 @@ public: //! and \a sendRequest() event to \"dev\" \a PIIODevice::write() handler void assignDevice(PIIODevice * dev); + Progress progressSend() const; + Progress progressReceive() const; + EVENT1(packetReceiveEvent, PIByteArray, data) EVENT1(sendRequest, PIByteArray, data) @@ -96,6 +107,8 @@ private: int packet_size; ushort packet_sign; int max_packet_size; + Progress prog_s, prog_r; + mutable PIMutex prog_s_mutex, prog_r_mutex; };