git-svn-id: svn://db.shs.com.ru/pip@780 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
@@ -40,7 +40,9 @@
|
|||||||
|
|
||||||
|
|
||||||
PIStreamPacker::PIStreamPacker(PIIODevice * dev): PIObject() {
|
PIStreamPacker::PIStreamPacker(PIIODevice * dev): PIObject() {
|
||||||
|
crypt_frag = false;
|
||||||
packet_size = -1;
|
packet_size = -1;
|
||||||
|
crypt_frag_size = 1024*1024;
|
||||||
max_packet_size = 1400;
|
max_packet_size = 1400;
|
||||||
packet_sign = 0xAFBE;
|
packet_sign = 0xAFBE;
|
||||||
assignDevice(dev);
|
assignDevice(dev);
|
||||||
@@ -49,7 +51,20 @@ PIStreamPacker::PIStreamPacker(PIIODevice * dev): PIObject() {
|
|||||||
|
|
||||||
void PIStreamPacker::send(const PIByteArray & data) {
|
void PIStreamPacker::send(const PIByteArray & data) {
|
||||||
if (data.isEmpty()) return;
|
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();
|
//piCout << "crypt" << data.size() << "->" << cd.size() << key().size();
|
||||||
PIByteArray hdr, part;
|
PIByteArray hdr, part;
|
||||||
hdr << packet_sign << int(cd.size_s());
|
hdr << packet_sign << int(cd.size_s());
|
||||||
@@ -124,7 +139,17 @@ void PIStreamPacker::received(const PIByteArray & data) {
|
|||||||
prog_r_mutex.unlock();
|
prog_r_mutex.unlock();
|
||||||
stream.remove(0, ps);
|
stream.remove(0, ps);
|
||||||
if (packet.size_s() == packet_size) {
|
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();
|
//piCout << "decrypt" << packet.size() << "->" << cd.size() << key().size();
|
||||||
if (!cd.isEmpty()) {
|
if (!cd.isEmpty()) {
|
||||||
packetReceived(cd);
|
packetReceived(cd);
|
||||||
|
|||||||
@@ -209,7 +209,17 @@ inline PIByteArray & operator >>(PIByteArray & s, ldouble & v) {assert(s.size()
|
|||||||
//! \relatesalso PIByteArray \brief Restore operator
|
//! \relatesalso PIByteArray \brief Restore operator
|
||||||
template<typename T> inline PIByteArray & operator >>(PIByteArray & s, PIFlags<T> & v) {PBA_OPERATOR_FROM return s;}
|
template<typename T> inline PIByteArray & operator >>(PIByteArray & s, PIFlags<T> & v) {PBA_OPERATOR_FROM return s;}
|
||||||
//! \relatesalso PIByteArray \brief Restore operator, see \ref PIByteArray_sec1 for details
|
//! \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
|
//! \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;}
|
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;}
|
||||||
|
|
||||||
|
|||||||
@@ -35,11 +35,20 @@ public:
|
|||||||
//! Contructs packer and try to assign \"dev\"
|
//! Contructs packer and try to assign \"dev\"
|
||||||
PIStreamPacker(PIIODevice * dev = 0);
|
PIStreamPacker(PIIODevice * dev = 0);
|
||||||
|
|
||||||
|
//! Progress info
|
||||||
struct Progress {
|
struct Progress {
|
||||||
Progress();
|
Progress();
|
||||||
|
|
||||||
|
//! Is send/receive in progress
|
||||||
bool active;
|
bool active;
|
||||||
|
|
||||||
|
//! Overall send/receive packet size
|
||||||
int bytes_all;
|
int bytes_all;
|
||||||
|
|
||||||
|
//! Current send/receive size
|
||||||
int bytes_current;
|
int bytes_current;
|
||||||
|
|
||||||
|
//! Current send/receive progress from 0 to 1
|
||||||
double progress;
|
double progress;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -57,6 +66,12 @@ public:
|
|||||||
ushort packetSign() {return packet_sign;}
|
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
|
//! Prepare data for send and raise \a sendRequest() events
|
||||||
void send(const PIByteArray & data);
|
void send(const PIByteArray & data);
|
||||||
|
|
||||||
@@ -69,7 +84,10 @@ public:
|
|||||||
//! and \a sendRequest() event to \"dev\" \a PIIODevice::write() handler
|
//! and \a sendRequest() event to \"dev\" \a PIIODevice::write() handler
|
||||||
void assignDevice(PIIODevice * dev);
|
void assignDevice(PIIODevice * dev);
|
||||||
|
|
||||||
|
//! Returns \a Progress info about sending
|
||||||
Progress progressSend() const;
|
Progress progressSend() const;
|
||||||
|
|
||||||
|
//! Returns \a Progress info about receiving
|
||||||
Progress progressReceive() const;
|
Progress progressReceive() const;
|
||||||
|
|
||||||
EVENT1(packetReceiveEvent, PIByteArray, data)
|
EVENT1(packetReceiveEvent, PIByteArray, data)
|
||||||
@@ -104,7 +122,8 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
PIByteArray stream, packet;
|
PIByteArray stream, packet;
|
||||||
int packet_size;
|
bool crypt_frag;
|
||||||
|
int packet_size, crypt_frag_size;
|
||||||
ushort packet_sign;
|
ushort packet_sign;
|
||||||
int max_packet_size;
|
int max_packet_size;
|
||||||
Progress prog_s, prog_r;
|
Progress prog_s, prog_r;
|
||||||
|
|||||||
Reference in New Issue
Block a user