From e20ed0a3ac30824d8f563a56064dd03584a5f745 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=8B=D1=87=D0=BA=D0=BE=D0=B2=20=D0=90=D0=BD=D0=B4?= =?UTF-8?q?=D1=80=D0=B5=D0=B9?= Date: Thu, 1 Sep 2016 15:15:51 +0000 Subject: [PATCH] git-svn-id: svn://db.shs.com.ru/pip@251 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5 --- src/core/pibase.h | 58 ++++++++++++----------- src/io/piconnection.cpp | 6 --- src/io/piconnection.h | 3 +- src/io/pidiagnostics.cpp | 4 +- src/io/piiobytearray.cpp | 95 ++++++++++++++++++++++++++++++++++++++ src/io/piiobytearray.h | 82 ++++++++++++++++++++++++++++++++ src/io/piiomodule.h | 1 + src/io/piiostring.cpp | 8 ++-- src/io/pipacketextractor.h | 4 +- src/io/pipeer.cpp | 2 +- 10 files changed, 222 insertions(+), 41 deletions(-) create mode 100644 src/io/piiobytearray.cpp create mode 100644 src/io/piiobytearray.h diff --git a/src/core/pibase.h b/src/core/pibase.h index 2082cd6b..9e413a1f 100644 --- a/src/core/pibase.h +++ b/src/core/pibase.h @@ -1,26 +1,26 @@ /*! \file pibase.h * \brief Base types and functions - * + * * This file implements first layer above the system and * declares some basic useful functions */ /* - PIP - Platform Independent Primitives - Base types and functions - Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru + PIP - Platform Independent Primitives + Base types and functions + Copyright (C) 2016 Ivan Pelipenko peri4ko@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 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. + 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 . + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ #ifndef PIBASE_H @@ -176,9 +176,9 @@ # pragma warning(disable: 4986) # pragma warning(disable: 4996) # ifdef ARCH_BITS_32 - typedef long ssize_t; + typedef long ssize_t; # else - typedef long long ssize_t; + typedef long long ssize_t; # endif #endif @@ -266,7 +266,7 @@ template inline void piSwapBinary(T & f, T & s) { * There are some macros: * - \c piRoundf for "float" * - \c piRoundd for "double" - * + * * Example: * \snippet piincludes.cpp round */ template inline int piRound(const T & v) {return int(v >= T(0.) ? v + T(0.5) : v - T(0.5));} @@ -276,7 +276,7 @@ template inline int piRound(const T & v) {return int(v >= T(0.) ? v * There are some macros: * - \c piFloorf for "float" * - \c piFloord for "double" - * + * * Example: * \snippet piincludes.cpp floor */ template inline int piFloor(const T & v) {return v < T(0) ? int(v) - 1 : int(v);} @@ -286,7 +286,7 @@ template inline int piFloor(const T & v) {return v < T(0) ? int(v) - * There are some macros: * - \c piCeilf for "float" * - \c piCeild for "double" - * + * * Example: * \snippet piincludes.cpp ceil */ template inline int piCeil(const T & v) {return v < T(0) ? int(v) : int(v) + 1;} @@ -300,7 +300,7 @@ template inline int piCeil(const T & v) {return v < T(0) ? int(v) : * - \c piAbsll for "llong" * - \c piAbsf for "float" * - \c piAbsd for "double" - * + * * Example: * \snippet piincludes.cpp abs */ template inline T piAbs(const T & v) {return (v >= T(0) ? v : -v);} @@ -313,7 +313,7 @@ template inline T piAbs(const T & v) {return (v >= T(0) ? v : -v);} * - \c piMinll for "llong" * - \c piMinf for "float" * - \c piMind for "double" - * + * * Example: * \snippet piincludes.cpp min2 */ template inline T piMin(const T & f, const T & s) {return ((f > s) ? s : f);} @@ -326,7 +326,7 @@ template inline T piMin(const T & f, const T & s) {return ((f > s) ? * - \c piMinll for "llong" * - \c piMinf for "float" * - \c piMind for "double" - * + * * Example: * \snippet piincludes.cpp min3 */ template inline T piMin(const T & f, const T & s, const T & t) {return ((f < s && f < t) ? f : ((s < t) ? s : t));} @@ -339,7 +339,7 @@ template inline T piMin(const T & f, const T & s, const T & t) {retu * - \c piMaxll for "llong" * - \c piMaxf for "float" * - \c piMaxd for "double" - * + * * Example: * \snippet piincludes.cpp max2 */ template inline T piMax(const T & f, const T & s) {return ((f < s) ? s : f);} @@ -352,7 +352,7 @@ template inline T piMax(const T & f, const T & s) {return ((f < s) ? * - \c piMaxll for "llong" * - \c piMaxf for "float" * - \c piMaxd for "double" - * + * * Example: * \snippet piincludes.cpp max3 */ template inline T piMax(const T & f, const T & s, const T & t) {return ((f > s && f > t) ? f : ((s > t) ? s : t));} @@ -366,7 +366,7 @@ template inline T piMax(const T & f, const T & s, const T & t) {retu * - \c piClampll for "llong" * - \c piClampf for "float" * - \c piClampd for "double" - * + * * Example: * \snippet piincludes.cpp clamp */ template inline T piClamp(const T & v, const T & min, const T & max) {return (v > max ? max : (v < min ? min : v));} @@ -387,7 +387,7 @@ template inline void piLetobe(T * v) {piLetobe(v, sizeof(T));} * - \c piLetobei for "uint" * - \c piLetobel for "ulong" * - \c piLetobell for "ullong" - * + * * Example: * \snippet piincludes.cpp letobe */ template inline T piLetobe(const T & v) {T tv(v); piLetobe(&tv, sizeof(T)); return tv;} @@ -395,6 +395,11 @@ template inline T piLetobe(const T & v) {T tv(v); piLetobe(&tv, size // specialization 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 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 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 #define piLetobel piLetobe #define piLetobell piLetobe +#define piLetobef piLetobe #endif // PIBASE_H diff --git a/src/io/piconnection.cpp b/src/io/piconnection.cpp index eb22186a..7b8ca17c 100755 --- a/src/io/piconnection.cpp +++ b/src/io/piconnection.cpp @@ -828,12 +828,6 @@ void PIConnection::stopAllSenders() { } -void PIConnection::destroy() { - stop(); - removeAllDevices(); -} - - PIDiagnostics * PIConnection::diagnostic(const PIString & full_path_name) const { PIIODevice * dev = deviceByFullPath(full_path_name); if (dev == 0) dev = device_names.value(full_path_name, 0); diff --git a/src/io/piconnection.h b/src/io/piconnection.h index c83baec0..294b98e1 100755 --- a/src/io/piconnection.h +++ b/src/io/piconnection.h @@ -246,7 +246,8 @@ public: //! Stop all read threads and senders 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 bool isEmpty() const {return device_modes.isEmpty();} diff --git a/src/io/pidiagnostics.cpp b/src/io/pidiagnostics.cpp index bdd3dac8..fad39985 100755 --- a/src/io/pidiagnostics.cpp +++ b/src/io/pidiagnostics.cpp @@ -105,7 +105,9 @@ void PIDiagnostics::tick(void * , int ) { float itr = disconn_ * (float(tcnt_recv) / history_rec.size()); float its = disconn_ * (float(tcnt_send) / history_send.size()); 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_send_sec = ullong(float(send.cnt_ok) / its); bytes_recv_sec = ullong(double(recv.bytes_ok) / itr); diff --git a/src/io/piiobytearray.cpp b/src/io/piiobytearray.cpp new file mode 100644 index 00000000..a8e047a4 --- /dev/null +++ b/src/io/piiobytearray.cpp @@ -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 . +*/ + +#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(&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); +} diff --git a/src/io/piiobytearray.h b/src/io/piiobytearray.h new file mode 100644 index 00000000..afc1d426 --- /dev/null +++ b/src/io/piiobytearray.h @@ -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 . +*/ + +#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 diff --git a/src/io/piiomodule.h b/src/io/piiomodule.h index 106deb98..b91d2dee 100644 --- a/src/io/piiomodule.h +++ b/src/io/piiomodule.h @@ -30,6 +30,7 @@ #include "pibinarylog.h" #include "pifiletransfer.h" #include "piiostring.h" +#include "piiobytearray.h" #include "pipeer.h" #include "pipacketextractor.h" #include "piprotocol.h" diff --git a/src/io/piiostring.cpp b/src/io/piiostring.cpp index 1c13b37b..fce16fdd 100644 --- a/src/io/piiostring.cpp +++ b/src/io/piiostring.cpp @@ -30,8 +30,8 @@ //REGISTER_DEVICE(PIIOString); -PIIOString::PIIOString(PIString * string, PIIODevice::DeviceMode mode_) { - open(string, mode_); +PIIOString::PIIOString(PIString * string, PIIODevice::DeviceMode 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; - return PIIODevice::open(mode_); + return PIIODevice::open(mode); } diff --git a/src/io/pipacketextractor.h b/src/io/pipacketextractor.h index 306161a8..bf8313a9 100755 --- a/src/io/pipacketextractor.h +++ b/src/io/pipacketextractor.h @@ -62,7 +62,7 @@ public: 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 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 setFooterCheckSlot(PacketExtractorCheckFunc f) {ret_func_footer = f;} @@ -167,7 +167,7 @@ private: bool openDevice() {if (dev == 0) return false; return dev->open();} 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; SplitMode mode_; void * data; diff --git a/src/io/pipeer.cpp b/src/io/pipeer.cpp index 49e9b313..13fefb0d 100755 --- a/src/io/pipeer.cpp +++ b/src/io/pipeer.cpp @@ -147,7 +147,6 @@ PIPeer::PIPeer(const PIString & n): PIIODevice() { prev_ifaces = PIEthernet::interfaces(); no_timer = false; // initNetwork(); - sendSelfInfo(); sync_timer.addDelimiter(5); sync_timer.start(1000); } @@ -835,6 +834,7 @@ void PIPeer::reinit() { PIMutexLocker pl(peers_mutex); PIMutexLocker sl(send_mutex); initNetwork(); + sendSelfInfo(); // eth_send.close(); // eth_lo.stopThreadedRead(); // eth_lo.close();