72 lines
2.9 KiB
C++
72 lines
2.9 KiB
C++
/*
|
|
PIP - Platform Independent Primitives
|
|
Packets extractor
|
|
Copyright (C) 2012 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 PIPacketExtractor: public PIIODevice
|
|
{
|
|
public:
|
|
PIPacketExtractor(PIIODevice * device_ = 0, void * recHeaderPtr = 0, int recHeaderSize = 0, int recDataSize = 0);
|
|
|
|
PIIODevice * device() {return dev;}
|
|
void setDevice(PIIODevice * device_);
|
|
|
|
int bufferSize() const {return buffer_size;}
|
|
void setBufferSize(int new_size) {buffer_size = new_size; buffer.resize(buffer_size); sbuffer.resize(buffer_size);}
|
|
|
|
void setHeaderCheckSlot(HeaderCheckFunc f) {ret_func_header = f;}
|
|
void setPacketData(void * recHeaderPtr, int recHeaderSize, int recDataSize) {headerPtr = recHeaderPtr; headerSize = recHeaderSize; dataSize = recDataSize; packetSize = headerSize + dataSize; if (headerSize > 0) mheader.resize(headerSize);}
|
|
|
|
ullong missedBytes() const {return missed;}
|
|
ullong missedPackets() const {return missed / packetSize;}
|
|
const ullong * missedBytes_ptr() const {return &missed;}
|
|
const ullong * missedPackets_ptr() const {return &missed_packets;}
|
|
|
|
PIByteArray lastHeader() {return mheader;}
|
|
|
|
int read(void * read_to, int max_size) {if (dev == 0) return -1; return dev->read(read_to, max_size);}
|
|
int write(const void * data, int max_size) {if (dev == 0) return -1; return dev->write(data, max_size);}
|
|
|
|
protected:
|
|
virtual bool packetValidate(uchar * rec, int size) {if (ret_func_ != 0) return ret_func_(ret_data_, rec, size); return true;}
|
|
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;}
|
|
|
|
private:
|
|
bool threadedRead(uchar * readed, int size);
|
|
bool openDevice() {if (dev == 0) return false; return dev->isOpened();}
|
|
|
|
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
|