Files
pip/libs/main/io_utils/pistreampacker.h
2021-04-07 14:38:32 +03:00

136 lines
3.7 KiB
C++

/*! \file pistreampacker.h
* \brief Simple packet wrap aroud any PIIODevice
*/
/*
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/>.
*/
#ifndef PISTREAMPACKER_H
#define PISTREAMPACKER_H
#include "pip_io_utils_export.h"
#include "piobject.h"
#include "piethutilbase.h"
class PIIODevice;
class PIP_IO_UTILS_EXPORT PIStreamPacker: public PIObject, public PIEthUtilBase {
PIOBJECT(PIStreamPacker)
public:
//! Contructs packer and try to assign \"dev\"
PIStreamPacker(PIIODevice * dev = 0);
//! Progress info
struct PIP_IO_UTILS_EXPORT 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;
};
//! Set maximum size of single packet
void setMaxPacketSize(int max_size) {max_packet_size = max_size;}
//! Returns maximum size of single packet, default 1400 bytes
int maxPacketSize() {return max_packet_size;}
//! Set packet sinature
void setPacketSign(ushort sign_) {packet_sign = sign_;}
//! Returns packet sinature, default 0xAFBE
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);
//! Receive data part. If packet is ready, raise \a received() event
void received(const PIByteArray & data);
EVENT_HANDLER2(void, received, uchar * , readed, int, size);
//! Connect \"dev\" \a PIIODevice::threadedReadEvent() event to \a received() handler
//! 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)
EVENT1(sendRequest, PIByteArray, data)
//! \handlers
//! \{
//! \fn void received(uchar * readed, int size)
//! \brief Handler to receive data. \a PIIODevice::threadedReadEvent()
//! can be connected to this handler
//! \}
//! \events
//! \{
//! \fn void packetReceiveEvent(PIByteArray data)
//! \brief Raise on packet successfully received
//! \fn void sendRequest(PIByteArray data)
//! \brief Raise from \a send() function. This data should
//! be directly sended to your device. You can
//! connect this event to \a PIIODevice::write() handler
//! \}
protected:
//! Packet successfully received, by default does nothing
virtual void packetReceived(PIByteArray data) {}
private:
PIByteArray stream, packet;
bool crypt_frag;
int packet_size, crypt_frag_size;
ushort packet_sign;
int max_packet_size;
Progress prog_s, prog_r;
mutable PIMutex prog_s_mutex, prog_r_mutex;
};
#endif // PISTREAMPACKER_H