This allow compile check event for CONNECT and use EVENT as CONNECT target, also raise event now is simple execute EVENT function.
85 lines
2.8 KiB
C++
85 lines
2.8 KiB
C++
/*
|
|
PIP - Platform Independent Primitives
|
|
Packets extractor
|
|
Copyright (C) 2013 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/>.
|
|
*/
|
|
|
|
#include "pipacketextractor.h"
|
|
|
|
|
|
PIPacketExtractor::PIPacketExtractor(PIIODevice * device_, void * recHeaderPtr, int recHeaderSize, int recDataSize) {
|
|
ret_func_header = 0;
|
|
setPacketData(recHeaderPtr, recHeaderSize, recDataSize);
|
|
setThreadedReadBufferSize(65536);
|
|
setBufferSize(65536);
|
|
setDevice(device_);
|
|
allReaded = addSize = curInd = missed = 0;
|
|
}
|
|
|
|
|
|
void PIPacketExtractor::setDevice(PIIODevice * device_) {
|
|
dev = device_;
|
|
if (dev == 0) return;
|
|
}
|
|
|
|
|
|
bool PIPacketExtractor::threadedRead(uchar * readed, int size_) {
|
|
//cout << "extractor readed " << size_ << endl;
|
|
memcpy(buffer.data(allReaded), readed, size_);
|
|
allReaded += size_;
|
|
if (allReaded < packetSize + addSize) return true;
|
|
if (headerSize > 0) {
|
|
if (allReaded + curInd >= buffer_size) {
|
|
memcpy(sbuffer.data(), buffer.data(), buffer_size);
|
|
memcpy(buffer.data(), sbuffer.data(buffer_size - packetSize), allReaded);
|
|
allReaded = packetSize;
|
|
addSize = curInd = 0;
|
|
}
|
|
while (!packetHeaderValidate((uchar * )headerPtr, buffer.data(curInd), headerSize)) {
|
|
curInd++; missed++;
|
|
if (packetSize > 0) missed_packets = missed / packetSize;
|
|
if (curInd > addSize) {
|
|
addSize += packetSize;
|
|
return true;
|
|
}
|
|
}
|
|
memcpy(mheader.data(), buffer.data(curInd), headerSize);
|
|
if (headerPtr != 0) memcpy(headerPtr, buffer.data(curInd), headerSize);
|
|
if (!packetValidate(buffer.data(curInd + headerSize), dataSize)) {
|
|
curInd++; missed++;
|
|
if (packetSize > 0) missed_packets = missed / packetSize;
|
|
return true;
|
|
}
|
|
memcpy(sbuffer.data(), buffer.data(), allReaded);
|
|
memcpy(buffer.data(), sbuffer.data(packetSize + curInd), allReaded);
|
|
allReaded -= packetSize + curInd;
|
|
curInd = addSize = 0;
|
|
} else {
|
|
if (dataSize == 0) {
|
|
packetValidate(buffer.data(), size_);
|
|
memcpy(sbuffer.data(), buffer.data(), allReaded);
|
|
memcpy(buffer.data(), sbuffer.data(size_), allReaded);
|
|
allReaded -= size_;
|
|
} else {
|
|
packetValidate(buffer.data(), dataSize);
|
|
memcpy(sbuffer.data(), buffer.data(), allReaded);
|
|
memcpy(buffer.data(), sbuffer.data(packetSize), allReaded);
|
|
allReaded -= packetSize;
|
|
}
|
|
}
|
|
return true;
|
|
}
|