git-svn-id: svn://db.shs.com.ru/pip@251 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2016-09-01 15:15:51 +00:00
parent 414e1b9831
commit e20ed0a3ac
10 changed files with 222 additions and 41 deletions

View File

@@ -5,22 +5,22 @@
* declares some basic useful functions * declares some basic useful functions
*/ */
/* /*
PIP - Platform Independent Primitives PIP - Platform Independent Primitives
Base types and functions Base types and functions
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef PIBASE_H #ifndef PIBASE_H
@@ -176,9 +176,9 @@
# pragma warning(disable: 4986) # pragma warning(disable: 4986)
# pragma warning(disable: 4996) # pragma warning(disable: 4996)
# ifdef ARCH_BITS_32 # ifdef ARCH_BITS_32
typedef long ssize_t; typedef long ssize_t;
# else # else
typedef long long ssize_t; typedef long long ssize_t;
# endif # endif
#endif #endif
@@ -395,6 +395,11 @@ template<typename T> inline T piLetobe(const T & v) {T tv(v); piLetobe(&tv, size
// specialization // specialization
template<> inline ushort piLetobe(const ushort & v) {return (v << 8) | (v >> 8);} template<> inline ushort piLetobe(const ushort & v) {return (v << 8) | (v >> 8);}
template<> inline uint piLetobe(const uint & v) {return (v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | ((v << 24) & 0xFF000000);} template<> inline uint piLetobe(const uint & v) {return (v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | ((v << 24) & 0xFF000000);}
template<> inline float piLetobe(const float & f) {
uint v = *((uint *)&f);
v = (v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | ((v << 24) & 0xFF000000);
return *((float *)&v);
}
DEPRECATED inline ushort letobe_s(const ushort & v) {return (v << 8) | (v >> 8);} DEPRECATED inline ushort letobe_s(const ushort & v) {return (v << 8) | (v >> 8);}
DEPRECATED inline uint letobe_i(const uint & v) {return (v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | ((v << 24) & 0xFF000000);} DEPRECATED inline uint letobe_i(const uint & v) {return (v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF0000) | ((v << 24) & 0xFF000000);}
@@ -443,6 +448,7 @@ uint letobe_i(uint v) {return (v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF
#define piLetobei piLetobe<uint> #define piLetobei piLetobe<uint>
#define piLetobel piLetobe<ulong> #define piLetobel piLetobe<ulong>
#define piLetobell piLetobe<ullong> #define piLetobell piLetobe<ullong>
#define piLetobef piLetobe<float>
#endif // PIBASE_H #endif // PIBASE_H

View File

@@ -828,12 +828,6 @@ void PIConnection::stopAllSenders() {
} }
void PIConnection::destroy() {
stop();
removeAllDevices();
}
PIDiagnostics * PIConnection::diagnostic(const PIString & full_path_name) const { PIDiagnostics * PIConnection::diagnostic(const PIString & full_path_name) const {
PIIODevice * dev = deviceByFullPath(full_path_name); PIIODevice * dev = deviceByFullPath(full_path_name);
if (dev == 0) dev = device_names.value(full_path_name, 0); if (dev == 0) dev = device_names.value(full_path_name, 0);

View File

@@ -246,7 +246,8 @@ public:
//! Stop all read threads and senders //! Stop all read threads and senders
void stop() {stopAllThreadedReads(); stopAllSenders();} void stop() {stopAllThreadedReads(); stopAllSenders();}
void destroy(); //! Stop connection and remove all devices
void destroy() {stop(); removeAllDevices();}
//! Returns if there are no devices in this connection //! Returns if there are no devices in this connection
bool isEmpty() const {return device_modes.isEmpty();} bool isEmpty() const {return device_modes.isEmpty();}

View File

@@ -105,7 +105,9 @@ void PIDiagnostics::tick(void * , int ) {
float itr = disconn_ * (float(tcnt_recv) / history_rec.size()); float itr = disconn_ * (float(tcnt_recv) / history_rec.size());
float its = disconn_ * (float(tcnt_send) / history_send.size()); float its = disconn_ * (float(tcnt_send) / history_send.size());
float hz = interval() / 1000.f; float hz = interval() / 1000.f;
integral_freq = recv.cnt_ok / itr; if (tcnt_recv == 0) integral_freq = 0;
// piCoutObj << itr << tcnt_recv << history_rec.size();
else integral_freq = recv.cnt_ok / itr;
packets_recv_sec = ullong(float(recv.cnt_ok) / itr); packets_recv_sec = ullong(float(recv.cnt_ok) / itr);
packets_send_sec = ullong(float(send.cnt_ok) / its); packets_send_sec = ullong(float(send.cnt_ok) / its);
bytes_recv_sec = ullong(double(recv.bytes_ok) / itr); bytes_recv_sec = ullong(double(recv.bytes_ok) / itr);

95
src/io/piiobytearray.cpp Normal file
View File

@@ -0,0 +1,95 @@
/*
PIP - Platform Independent Primitives
PIIODevice wrapper around PIByteArray
Copyright (C) 2016 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 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 "piiobytearray.h"
/*! \class PIIOByteArray
* \brief PIIODevice wrapper around PIByteArray
*
* \section PIIOByteArray_sec0 Synopsis
* This class sllow you to use PIByteArray as PIIODevice and pass it to, e.g. PIConfig
*/
//REGISTER_DEVICE(PIIOByteArray);
PIIOByteArray::PIIOByteArray(PIByteArray *buffer, PIIODevice::DeviceMode mode) {
open(buffer, mode);
}
PIIOByteArray::PIIOByteArray(const PIByteArray &buffer) {
open(buffer);
}
bool PIIOByteArray::open(PIByteArray *buffer, PIIODevice::DeviceMode mode) {
data_ = buffer;
mode_ = mode;
return PIIODevice::open(mode);
}
bool PIIOByteArray::open(const PIByteArray &buffer) {
data_ = const_cast<PIByteArray*>(&buffer);
mode_ = PIIODevice::ReadOnly;
return PIIODevice::open(PIIODevice::ReadOnly);
}
int PIIOByteArray::read(void * read_to, int size) {
// piCout << "PIIOByteArray::read" << data_ << size << canRead();
if (!canRead() || !data_) return -1;
int ret = piMini(size, data_->size_s() - pos);
if (ret <= 0) return -1;
memcpy(read_to, data_->data(pos), ret);
// piCout << "readed" << ret;
pos += size;
if (pos > data_->size_s()) pos = data_->size_s();
return ret;
}
int PIIOByteArray::write(const void * data, int size) {
// piCout << "PIIOByteArray::write" << data << size << canWrite();
if (!canWrite() || !data) return -1;
//piCout << "write" << data;
if (pos > data_->size_s()) pos = data_->size_s();
PIByteArray rs = PIByteArray(data, size);
// piCoutObj << rs;
data_->insert(pos, rs);
pos += rs.size_s();
return rs.size_s();
}
int PIIOByteArray::writeByteArray(const PIByteArray &ba) {
if (!canWrite() || !data_) return -1;
if (pos > data_->size_s()) pos = data_->size_s();
data_->insert(pos, ba);
pos += ba.size_s();
return ba.size_s();
}
bool PIIOByteArray::openDevice() {
pos = 0;
return (data_ != 0);
}

82
src/io/piiobytearray.h Normal file
View File

@@ -0,0 +1,82 @@
/*! \file piiobytearray.h
* \brief PIIODevice wrapper around PIByteArray
*/
/*
PIP - Platform Independent Primitives
PIIODevice wrapper around PIString
Copyright (C) 2016 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 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 PIIOBYTEARRAY_H
#define PIIOBYTEARRAY_H
#include "piiodevice.h"
class PIP_EXPORT PIIOByteArray: public PIIODevice
{
PIIODEVICE(PIIOByteArray)
public:
//! Contructs %PIIOByteArray with \"buffer\" content and \"mode\" open mode
PIIOByteArray(PIByteArray * buffer = 0, PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
//! Contructs %PIIOByteArray with \"buffer\" content only for read
PIIOByteArray(const PIByteArray & buffer);
~PIIOByteArray() {closeDevice();}
//! Returns content
PIByteArray * byteArray() const {return data_;}
//! Clear content buffer
void clear() {if (data_) data_->clear(); pos = 0;}
//! Open \"buffer\" content with \"mode\" open mode
bool open(PIByteArray * buffer, PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);
//! Open \"buffer\" content only for read
bool open(const PIByteArray & buffer);
//! Returns if position is at the end of content
bool isEnd() const {if (!data_) return true; return pos >= data_->size_s();}
//! Move read/write position to \"position\"
void seek(llong position) {pos = position;}
//! Move read/write position to the begin of the string
void seekToBegin() {if (data_) pos = 0;}
//! Move read/write position to the end of the string
void seekToEnd() {if (data_) pos = data_->size_s();}
int read(void * read_to, int size);
int write(const void * data_, int size);
//! Insert data \"ba\" into content at current position
int writeByteArray(const PIByteArray & ba);
protected:
bool openDevice();
ssize_t pos;
PIByteArray * data_;
};
#endif // PIIOBYTEARRAY_H

View File

@@ -30,6 +30,7 @@
#include "pibinarylog.h" #include "pibinarylog.h"
#include "pifiletransfer.h" #include "pifiletransfer.h"
#include "piiostring.h" #include "piiostring.h"
#include "piiobytearray.h"
#include "pipeer.h" #include "pipeer.h"
#include "pipacketextractor.h" #include "pipacketextractor.h"
#include "piprotocol.h" #include "piprotocol.h"

View File

@@ -30,8 +30,8 @@
//REGISTER_DEVICE(PIIOString); //REGISTER_DEVICE(PIIOString);
PIIOString::PIIOString(PIString * string, PIIODevice::DeviceMode mode_) { PIIOString::PIIOString(PIString * string, PIIODevice::DeviceMode mode) {
open(string, mode_); open(string, mode);
} }
@@ -40,9 +40,9 @@ PIIOString::PIIOString(const PIString & string) {
} }
bool PIIOString::open(PIString * string, PIIODevice::DeviceMode mode_) { bool PIIOString::open(PIString * string, PIIODevice::DeviceMode mode) {
str = string; str = string;
return PIIODevice::open(mode_); return PIIODevice::open(mode);
} }

View File

@@ -62,7 +62,7 @@ public:
int bufferSize() const {return buffer_size;} int bufferSize() const {return buffer_size;}
//! Set buffer size to "new_size" bytes, should be at least greater than whole packet 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 setBufferSize(int new_size) {buffer_size = new_size; buffer.resize(buffer_size); memset(buffer.data(), 0, buffer.size());}
void setHeaderCheckSlot(PacketExtractorCheckFunc f) {ret_func_header = f;} void setHeaderCheckSlot(PacketExtractorCheckFunc f) {ret_func_header = f;}
void setFooterCheckSlot(PacketExtractorCheckFunc f) {ret_func_footer = f;} void setFooterCheckSlot(PacketExtractorCheckFunc f) {ret_func_footer = f;}
@@ -167,7 +167,7 @@ private:
bool openDevice() {if (dev == 0) return false; return dev->open();} bool openDevice() {if (dev == 0) return false; return dev->open();}
PIIODevice * dev; PIIODevice * dev;
PIByteArray buffer, sbuffer, tmpbuf, src_header, src_footer, trbuf; PIByteArray buffer, tmpbuf, src_header, src_footer, trbuf;
PacketExtractorCheckFunc ret_func_header, ret_func_footer; PacketExtractorCheckFunc ret_func_header, ret_func_footer;
SplitMode mode_; SplitMode mode_;
void * data; void * data;

View File

@@ -147,7 +147,6 @@ PIPeer::PIPeer(const PIString & n): PIIODevice() {
prev_ifaces = PIEthernet::interfaces(); prev_ifaces = PIEthernet::interfaces();
no_timer = false; no_timer = false;
// initNetwork(); // initNetwork();
sendSelfInfo();
sync_timer.addDelimiter(5); sync_timer.addDelimiter(5);
sync_timer.start(1000); sync_timer.start(1000);
} }
@@ -835,6 +834,7 @@ void PIPeer::reinit() {
PIMutexLocker pl(peers_mutex); PIMutexLocker pl(peers_mutex);
PIMutexLocker sl(send_mutex); PIMutexLocker sl(send_mutex);
initNetwork(); initNetwork();
sendSelfInfo();
// eth_send.close(); // eth_send.close();
// eth_lo.stopThreadedRead(); // eth_lo.stopThreadedRead();
// eth_lo.close(); // eth_lo.close();