123 lines
4.8 KiB
C++
123 lines
4.8 KiB
C++
/*! \file pipacketextractor.h
|
|
* \brief Packets extractor
|
|
*/
|
|
/*
|
|
PIP - Platform Independent Primitives
|
|
Packets extractor
|
|
Copyright (C) 2014 Ivan Pelipenko peri4ko@gmail.com
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
|
|
#ifndef PIPACKETEXTRACTOR_H
|
|
#define PIPACKETEXTRACTOR_H
|
|
|
|
#include "piiodevice.h"
|
|
|
|
// Pass data, recHeaderPtr, received_data, recHeaderSize. Return true if packet is correct nor return false.
|
|
typedef bool (*HeaderCheckFunc)(void * , uchar * , uchar * , int );
|
|
|
|
class PIP_EXPORT PIPacketExtractor: public PIIODevice
|
|
{
|
|
PIIODEVICE(PIPacketExtractor)
|
|
public:
|
|
|
|
//! Contructs extractor with child device "device_", header content pointer "recHeaderPtr", header size "recHeaderSize" and payload size "recDataSize"
|
|
PIPacketExtractor(PIIODevice * device_ = 0, void * recHeaderPtr = 0, int recHeaderSize = 0, int recDataSize = 0);
|
|
|
|
virtual ~PIPacketExtractor() {stop();}
|
|
|
|
|
|
//! Returns child %device
|
|
PIIODevice * device() {return dev;}
|
|
|
|
//! Set child %device to "device_"
|
|
void setDevice(PIIODevice * device_);
|
|
|
|
|
|
//! Returns buffer size
|
|
int bufferSize() const {return buffer_size;}
|
|
|
|
//! Set buffer size to "new_size" bytes, should be at least greater than whole packet size
|
|
void setBufferSize(int new_size) {buffer_size = new_size; buffer.resize(buffer_size); sbuffer.resize(buffer_size); memset(buffer.data(), 0, buffer.size()); memset(sbuffer.data(), 0, sbuffer.size());}
|
|
|
|
void setHeaderCheckSlot(HeaderCheckFunc f) {ret_func_header = f;}
|
|
|
|
//! Set header content pointer "recHeaderPtr", header size "recHeaderSize" and payload size "recDataSize"
|
|
void setPacketData(void * recHeaderPtr, int recHeaderSize, int recDataSize) {headerPtr = recHeaderPtr; headerSize = recHeaderSize; dataSize = recDataSize; packetSize = headerSize + dataSize; if (headerSize > 0) mheader.resize(headerSize);}
|
|
|
|
|
|
//! Returns missed by validating functions bytes count
|
|
ullong missedBytes() const {return missed;}
|
|
|
|
//! Returns missed by validating functions packets count, = missedBytes() / packetSize
|
|
ullong missedPackets() const {if (packetSize == 0) return missed; return missed / packetSize;}
|
|
|
|
//! Returns pointer to \a missedBytes() count. Useful for output to PIConsole
|
|
const ullong * missedBytes_ptr() const {return &missed;}
|
|
|
|
//! Returns pointer to \a missedPackets() count. Useful for output to PIConsole
|
|
const ullong * missedPackets_ptr() const {return &missed_packets;}
|
|
|
|
|
|
//! Returns last successfully validated header as byte array
|
|
PIByteArray lastHeader() {return mheader;}
|
|
|
|
|
|
//! Directly call \a read() function of child %device
|
|
int read(void * read_to, int max_size) {if (dev == 0) return -1; return dev->read(read_to, max_size);}
|
|
|
|
//! Directly call \a write() function of child %device
|
|
int write(const void * data, int max_size) {if (dev == 0) return -1; return dev->write(data, max_size);}
|
|
|
|
EVENT2(packetReceived, uchar * , data, int, size)
|
|
|
|
//! \events
|
|
//! \{
|
|
|
|
//! \fn void packetReceived(uchar * data, int size)
|
|
//! \brief Raise on successfull \a packetValidate() function
|
|
|
|
//! \}
|
|
|
|
protected:
|
|
|
|
/** \brief Function to validate header
|
|
* \param src Your header content passed from constructor or with function \a setPacketData()
|
|
* \param rec Received header
|
|
* \param size Header size
|
|
* \details Default implementation returns by-byte "src" with "rec" compare result */
|
|
virtual bool packetHeaderValidate(uchar * src, uchar * rec, int size) {if (ret_func_header != 0) return ret_func_header(ret_data_, src, rec, size); for (int i = 0; i < size; ++i) if (src[i] != rec[i]) return false; return true;}
|
|
|
|
//! Function to validate packet paylod after successfully header validate. Default implementation returns \b true
|
|
virtual bool packetValidate(uchar * rec, int size) {if (ret_func_ != 0) return ret_func_(ret_data_, rec, size); return true;}
|
|
|
|
private:
|
|
bool threadedRead(uchar * readed, int size);
|
|
PIString fullPathPrefix() const {return "pckext";}
|
|
bool openDevice() {if (dev == 0) return false; return dev->open();}
|
|
|
|
PIIODevice * dev;
|
|
PIByteArray mheader, buffer, sbuffer;
|
|
HeaderCheckFunc ret_func_header;
|
|
void * headerPtr, * data;
|
|
int buffer_size, dataSize, headerSize, packetSize, allReaded, addSize, curInd;
|
|
ullong missed, missed_packets;
|
|
|
|
|
|
};
|
|
|
|
#endif // PIPACKETEXTRACTOR_H
|