From d13e68c2061ca4195383eaaf309c1436118b9560 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: Tue, 26 Jul 2022 17:18:08 +0300 Subject: [PATCH] threadedRead now const uchar * pipacketextractor Header mode now more flexible fix splitTime mode more refactoring add virtual override to functions remove piforeach replace 0 to nullptr iterate over pimap via iterators replace CONNECTU to CONNECT# with compile time check --- CMakeLists.txt | 2 +- libs/cloud/picloudclient.cpp | 2 +- libs/cloud/picloudserver.cpp | 4 +- libs/console/piscreen.cpp | 9 +- libs/io_utils/pibroadcast.cpp | 8 +- libs/io_utils/pistreampacker.cpp | 9 +- libs/main/cloud/picloudclient.h | 10 +- libs/main/cloud/picloudserver.h | 18 +- .../introspection/piintrospection_server.cpp | 2 +- libs/main/io_devices/pibinarylog.cpp | 7 +- libs/main/io_devices/pibinarylog.h | 24 +- libs/main/io_devices/pican.h | 18 +- libs/main/io_devices/piethernet.cpp | 2 +- libs/main/io_devices/piethernet.h | 24 +- libs/main/io_devices/pifile.h | 20 +- libs/main/io_devices/piiobytearray.h | 8 +- libs/main/io_devices/piiodevice.cpp | 10 +- libs/main/io_devices/piiodevice.h | 26 +- libs/main/io_devices/piiostring.h | 8 +- libs/main/io_devices/pipeer.cpp | 37 +- libs/main/io_devices/pipeer.h | 22 +- libs/main/io_devices/piserial.h | 26 +- libs/main/io_devices/pisharedmemory.h | 18 +- libs/main/io_devices/pispi.h | 18 +- libs/main/io_devices/pitransparentdevice.h | 10 +- libs/main/io_devices/piusb.h | 18 +- libs/main/io_utils/pibroadcast.h | 2 +- libs/main/io_utils/piconnection.cpp | 640 +++++++++--------- libs/main/io_utils/piconnection.h | 21 +- libs/main/io_utils/pipacketextractor.cpp | 128 ++-- libs/main/io_utils/pipacketextractor.h | 59 +- libs/main/io_utils/pistreampacker.h | 2 +- libs/main/thread/pithread.cpp | 4 +- main_picloud_test.cpp | 4 +- main_tcp_server.cpp | 10 +- utils/udp_file_transfer/main.cpp | 8 +- 36 files changed, 615 insertions(+), 623 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bebf884d..84dabcb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.0) cmake_policy(SET CMP0017 NEW) # need include() with .cmake project(pip) set(pip_MAJOR 2) -set(pip_MINOR 95) +set(pip_MINOR 96) set(pip_REVISION 0) set(pip_SUFFIX ) set(pip_COMPANY SHS) diff --git a/libs/cloud/picloudclient.cpp b/libs/cloud/picloudclient.cpp index fd2e5cd3..dc53176f 100644 --- a/libs/cloud/picloudclient.cpp +++ b/libs/cloud/picloudclient.cpp @@ -28,7 +28,7 @@ PICloudClient::PICloudClient(const PIString & path, PIIODevice::DeviceMode mode) is_deleted = false; // setReopenEnabled(false); CONNECTL(ð, connected, [this](){opened_ = true; tcp.sendStart();}); - CONNECTU(&streampacker, packetReceiveEvent, this, _readed); + CONNECT1(void, PIByteArray, &streampacker, packetReceiveEvent, this, _readed); CONNECTL(ð, disconnected, [this](bool){ if (is_deleted) return; bool need_disconn = is_connected; diff --git a/libs/cloud/picloudserver.cpp b/libs/cloud/picloudserver.cpp index 3f1dd9b7..19434b14 100644 --- a/libs/cloud/picloudserver.cpp +++ b/libs/cloud/picloudserver.cpp @@ -25,7 +25,7 @@ PICloudServer::PICloudServer(const PIString & path, PIIODevice::DeviceMode mode) tcp.setRole(PICloud::TCP::Server); tcp.setServerName(server_name); setName("cloud_server__" + server_name); - CONNECTU(&streampacker, packetReceiveEvent, this, _readed); + CONNECT1(void, PIByteArray, &streampacker, packetReceiveEvent, this, _readed); CONNECTL(ð, connected, [this](){opened_ = true; piCoutObj << "connected"; tcp.sendStart();}); CONNECTL(ð, disconnected, [this](bool){ piCoutObj << "disconnected"; @@ -189,7 +189,7 @@ void PICloudServer::_readed(PIByteArray & ba) { } else { //piCoutObj << "new Client" << id; Client * c = new Client(this, id); - CONNECTU(c, deleted, this, clientDeleted); + CONNECT1(void, PIObject *, c, deleted, this, clientDeleted); clients_mutex.lock(); clients_ << c; index_clients.insert(id, c); diff --git a/libs/console/piscreen.cpp b/libs/console/piscreen.cpp index fa8388a2..abbbf657 100644 --- a/libs/console/piscreen.cpp +++ b/libs/console/piscreen.cpp @@ -392,18 +392,17 @@ PIScreen::PIScreen(bool startNow, PIKbdListener::KBFunc slot): PIThread(), drawe needLockRun(true); mouse_ = false; ret_func = slot; - tile_focus = tile_dialog = 0; + tile_focus = tile_dialog = nullptr; root.screen = this; listener = new PIKbdListener(key_eventS, this, startNow); - CONNECTU(listener, mouseEvent, this, mouse_event); - CONNECTU(listener, wheelEvent, this, wheel_event); + CONNECT1(void, PIKbdListener::MouseEvent, listener, mouseEvent, this, mouse_event); + CONNECT1(void, PIKbdListener::WheelEvent, listener, wheelEvent, this, wheel_event); if (startNow) start(); } PIScreen::~PIScreen() { - if (isRunning()) - stop(); + if (isRunning()) stop(); PIThread::waitForFinish(10); listener->waitForFinish(10); delete listener; diff --git a/libs/io_utils/pibroadcast.cpp b/libs/io_utils/pibroadcast.cpp index 49a32f2e..7ffb74d2 100644 --- a/libs/io_utils/pibroadcast.cpp +++ b/libs/io_utils/pibroadcast.cpp @@ -151,7 +151,7 @@ void PIBroadcast::initAll(PIVector al) { //piCout << "mcast " << ce->readAddress() << ce->sendAddress(); if (ce->open()) { eth_mcast << ce; - CONNECTU(ce, threadedReadEvent, this, mcastRead); + CONNECT2(void, const uchar *, int, ce, threadedReadEvent, this, mcastRead); } else { delete ce; } @@ -173,7 +173,7 @@ void PIBroadcast::initAll(PIVector al) { //piCout << "bcast " << ce->readAddress() << ce->sendAddress(); if (ce->open()) { eth_mcast << ce; - CONNECTU(ce, threadedReadEvent, this, mcastRead); + CONNECT2(void, const uchar *, int, ce, threadedReadEvent, this, mcastRead); } else { delete ce; } @@ -189,7 +189,7 @@ void PIBroadcast::initAll(PIVector al) { eth_lo->setName("PIMulticast_loopback"); if (!_send_only) { eth_lo->setParameter(PIEthernet::ReuseAddress, false); - CONNECTU(eth_lo, threadedReadEvent, this, mcastRead); + CONNECT2(void, const uchar *, int, eth_lo, threadedReadEvent, this, mcastRead); for (int i = 0; i < lo_pcnt; ++i) { eth_lo->setReadAddress("127.0.0.1", lo_port + i); if (eth_lo->open()) { @@ -248,7 +248,7 @@ void PIBroadcast::reinit() { } -void PIBroadcast::mcastRead(uchar * data, int size) { +void PIBroadcast::mcastRead(const uchar * data, int size) { PIByteArray cd = decryptData(PIByteArray(data, size)); if (cd.isEmpty()) return; received(cd); diff --git a/libs/io_utils/pistreampacker.cpp b/libs/io_utils/pistreampacker.cpp index 5f95a953..75b31927 100644 --- a/libs/io_utils/pistreampacker.cpp +++ b/libs/io_utils/pistreampacker.cpp @@ -111,7 +111,7 @@ void PIStreamPacker::send(const PIByteArray & data) { } -void PIStreamPacker::received(uchar * readed, int size) { +void PIStreamPacker::received(const uchar * readed, int size) { received(PIByteArray(readed, size)); } @@ -195,8 +195,9 @@ void PIStreamPacker::received(const PIByteArray & data) { void PIStreamPacker::assignDevice(PIIODevice * dev) { if (!dev) return; - if (!dev->infoFlags()[PIIODevice::Reliable]) + if (!dev->infoFlags()[PIIODevice::Reliable]) { piCoutObj << "Warning! Not recommended to use with non-reliable" << dev; - CONNECTU(dev, threadedReadEvent, this, received); - CONNECTU(this, sendRequest, dev, write); + } + CONNECT2(void, const uchar *, int, dev, threadedReadEvent, this, received); + CONNECT1(void, PIByteArray, this, sendRequest, dev, write); } diff --git a/libs/main/cloud/picloudclient.h b/libs/main/cloud/picloudclient.h index cd0e713d..4d21cb1d 100644 --- a/libs/main/cloud/picloudclient.h +++ b/libs/main/cloud/picloudclient.h @@ -47,11 +47,11 @@ public: EVENT(disconnected); protected: - bool openDevice(); - bool closeDevice(); - int readDevice(void * read_to, int max_size); - int writeDevice(const void * data, int size); - DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Reliable;} + virtual bool openDevice() override; + virtual bool closeDevice() override; + virtual int readDevice(void * read_to, int max_size) override; + virtual int writeDevice(const void * data, int size) override; + virtual DeviceInfoFlags deviceInfoFlags() const override {return PIIODevice::Reliable;} private: EVENT_HANDLER1(void, _readed, PIByteArray &, data); diff --git a/libs/main/cloud/picloudserver.h b/libs/main/cloud/picloudserver.h index 2e8c6f9f..3936f0d3 100644 --- a/libs/main/cloud/picloudserver.h +++ b/libs/main/cloud/picloudserver.h @@ -45,11 +45,11 @@ public: Client(PICloudServer * srv = nullptr, uint id = 0); virtual ~Client(); protected: - bool openDevice(); - bool closeDevice(); - int readDevice(void * read_to, int max_size); - int writeDevice(const void * data, int size); - DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Reliable;} + virtual bool openDevice() override; + virtual bool closeDevice() override; + virtual int readDevice(void * read_to, int max_size) override; + virtual int writeDevice(const void * data, int size) override; + virtual DeviceInfoFlags deviceInfoFlags() const override {return PIIODevice::Reliable;} private: void pushBuffer(const PIByteArray & ba); @@ -68,10 +68,10 @@ public: EVENT1(newConnection, PICloudServer::Client * , client); protected: - bool openDevice(); - bool closeDevice(); - int readDevice(void * read_to, int max_size); - int writeDevice(const void * data, int max_size); + virtual bool openDevice() override; + virtual bool closeDevice() override; + virtual int readDevice(void * read_to, int max_size) override; + virtual int writeDevice(const void * data, int max_size) override; private: EVENT_HANDLER1(void, _readed, PIByteArray &, ba); diff --git a/libs/main/introspection/piintrospection_server.cpp b/libs/main/introspection/piintrospection_server.cpp index a8f7042e..6ad51013 100644 --- a/libs/main/introspection/piintrospection_server.cpp +++ b/libs/main/introspection/piintrospection_server.cpp @@ -56,7 +56,7 @@ void PIIntrospectionServer::start(const PIString & server_name) { sysmon = PISystemMonitor::Pool::instance()->getByPID(PIProcess::currentPID()); if (sysmon) { piCoutObj << "using existing sysmon"; - CONNECTU(sysmon, deleted, this, sysmonDeleted); + CONNECT1(void, PIObject *, sysmon, deleted, this, sysmonDeleted); } else { piCoutObj << "create own sysmon"; sysmon = new PISystemMonitor(); diff --git a/libs/main/io_devices/pibinarylog.cpp b/libs/main/io_devices/pibinarylog.cpp index 60a6d302..217abecf 100644 --- a/libs/main/io_devices/pibinarylog.cpp +++ b/libs/main/io_devices/pibinarylog.cpp @@ -170,7 +170,7 @@ bool PIBinaryLog::closeDevice() { } -bool PIBinaryLog::threadedRead(uchar *readed, int size) { +bool PIBinaryLog::threadedRead(const uchar *readed, int size) { // piCout << "binlog threaded read"; if (!canRead() || isEnd()) return PIIODevice::threadedRead(readed, size); is_thread_ok = false; @@ -429,6 +429,11 @@ int PIBinaryLog::readDevice(void *read_to, int max_size) { } +int PIBinaryLog::writeDevice(const void * data, int size) { + return writeBinLog(default_id, data, size); +} + + void PIBinaryLog::restart() { bool th = isRunning(); if (th) stopThreadedRead(); diff --git a/libs/main/io_devices/pibinarylog.h b/libs/main/io_devices/pibinarylog.h index 9b8e2c1c..a88b1345 100644 --- a/libs/main/io_devices/pibinarylog.h +++ b/libs/main/io_devices/pibinarylog.h @@ -301,18 +301,18 @@ public: static bool joinBinLogsSerial(const PIStringList & src, const PIString & dst); protected: - PIString constructFullPathDevice() const; - void configureFromFullPathDevice(const PIString & full_path); - PIPropertyStorage constructVariantDevice() const; - void configureFromVariantDevice(const PIPropertyStorage & d); - int readDevice(void *read_to, int max_size); - int writeDevice(const void * data, int size) {return writeBinLog(default_id, data, size);} - bool openDevice(); - bool closeDevice(); - void propertyChanged(const char * s); - bool threadedRead(uchar *readed, int size); - void threadedReadTerminated() {pausemutex.unlock();} - DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Reliable;} + virtual PIString constructFullPathDevice() const override; + virtual void configureFromFullPathDevice(const PIString & full_path) override; + virtual PIPropertyStorage constructVariantDevice() const override; + virtual void configureFromVariantDevice(const PIPropertyStorage & d) override; + virtual int readDevice(void *read_to, int max_size) override; + virtual int writeDevice(const void * data, int size) override; + virtual bool openDevice() override; + virtual bool closeDevice() override; + virtual void propertyChanged(const char * s) override; + virtual bool threadedRead(const uchar *readed, int size) override; + virtual void threadedReadTerminated() override {pausemutex.unlock();} + virtual DeviceInfoFlags deviceInfoFlags() const override {return PIIODevice::Reliable;} private: struct PIP_EXPORT BinLogRecord { diff --git a/libs/main/io_devices/pican.h b/libs/main/io_devices/pican.h index aa2c9473..65ae405d 100644 --- a/libs/main/io_devices/pican.h +++ b/libs/main/io_devices/pican.h @@ -41,15 +41,15 @@ public: int readedCANID() const; protected: - bool openDevice(); - bool closeDevice(); - int readDevice(void * read_to, int max_size); - int writeDevice(const void * data, int max_size); - PIString constructFullPathDevice() const; - void configureFromFullPathDevice(const PIString & full_path); - PIPropertyStorage constructVariantDevice() const; - void configureFromVariantDevice(const PIPropertyStorage & d); - DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Reliable;} + virtual bool openDevice() override; + virtual bool closeDevice() override; + virtual int readDevice(void * read_to, int max_size) override; + virtual int writeDevice(const void * data, int max_size) override; + virtual PIString constructFullPathDevice() const override; + virtual void configureFromFullPathDevice(const PIString & full_path) override; + virtual PIPropertyStorage constructVariantDevice() const override; + virtual void configureFromVariantDevice(const PIPropertyStorage & d) override; + virtual DeviceInfoFlags deviceInfoFlags() const override {return PIIODevice::Reliable;} private: int sock; diff --git a/libs/main/io_devices/piethernet.cpp b/libs/main/io_devices/piethernet.cpp index 9a848a8a..54fd8065 100644 --- a/libs/main/io_devices/piethernet.cpp +++ b/libs/main/io_devices/piethernet.cpp @@ -901,7 +901,7 @@ void PIEthernet::server_func(void * eth) { ip += ":" + PIString::fromNumber(htons(client_addr.sin_port)); PIEthernet * e = new PIEthernet(s, ip); ce->clients_mutex.lock(); - CONNECTU(e, deleted, ce, clientDeleted) + CONNECT1(void, PIObject *, e, deleted, ce, clientDeleted) ce->clients_ << e; ce->clients_mutex.unlock(); ce->newConnection(e); diff --git a/libs/main/io_devices/piethernet.h b/libs/main/io_devices/piethernet.h index fa74c14e..9cfe25f7 100644 --- a/libs/main/io_devices/piethernet.h +++ b/libs/main/io_devices/piethernet.h @@ -462,24 +462,24 @@ public: protected: explicit PIEthernet(int sock, PIString ip_port); - void propertyChanged(const char * name); + virtual void propertyChanged(const char * name) override; - PIString constructFullPathDevice() const; - void configureFromFullPathDevice(const PIString & full_path); - PIPropertyStorage constructVariantDevice() const; - void configureFromVariantDevice(const PIPropertyStorage & d); - bool configureDevice(const void * e_main, const void * e_parent = 0); - int readDevice(void * read_to, int max_size); - int writeDevice(const void * data, int max_size); - DeviceInfoFlags deviceInfoFlags() const; + virtual PIString constructFullPathDevice() const override; + virtual void configureFromFullPathDevice(const PIString & full_path) override; + virtual PIPropertyStorage constructVariantDevice() const override; + virtual void configureFromVariantDevice(const PIPropertyStorage & d) override; + virtual bool configureDevice(const void * e_main, const void * e_parent = 0) override; + virtual int readDevice(void * read_to, int max_size) override; + virtual int writeDevice(const void * data, int max_size) override; + virtual DeviceInfoFlags deviceInfoFlags() const override; //! Executes when any read function was successful. Default implementation does nothing virtual void received(const void * data, int size) {;} void construct(); - bool init(); - bool openDevice(); - bool closeDevice(); + virtual bool init() override; + virtual bool openDevice() override; + virtual bool closeDevice() override; void closeSocket(int & sd); void applyTimeouts(); void applyTimeout(int fd, int opt, double ms); diff --git a/libs/main/io_devices/pifile.h b/libs/main/io_devices/pifile.h index 82aef455..0ff74748 100644 --- a/libs/main/io_devices/pifile.h +++ b/libs/main/io_devices/pifile.h @@ -180,7 +180,7 @@ public: //! \~english Immediate write all buffered data to disk //! \~russian Немедленно записывает все буферизированные данные на диск - void flush(); + virtual void flush() override; //! \~english Move read/write position to "position" //! \~russian Перемещает позицию чтения/записи на "position" @@ -324,15 +324,15 @@ public: //! \} protected: - PIString constructFullPathDevice() const; - void configureFromFullPathDevice(const PIString & full_path); - PIPropertyStorage constructVariantDevice() const; - void configureFromVariantDevice(const PIPropertyStorage & d); - int readDevice(void * read_to, int max_size); - int writeDevice(const void * data, int max_size); - bool openDevice(); - bool closeDevice(); - DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Sequential | PIIODevice::Reliable;} + virtual PIString constructFullPathDevice() const override; + virtual void configureFromFullPathDevice(const PIString & full_path) override; + virtual PIPropertyStorage constructVariantDevice() const override; + virtual void configureFromVariantDevice(const PIPropertyStorage & d) override; + virtual int readDevice(void * read_to, int max_size) override; + virtual int writeDevice(const void * data, int max_size) override; + virtual bool openDevice() override; + virtual bool closeDevice() override; + virtual DeviceInfoFlags deviceInfoFlags() const override {return PIIODevice::Sequential | PIIODevice::Reliable;} private: PIString strType(const PIIODevice::DeviceMode type); diff --git a/libs/main/io_devices/piiobytearray.h b/libs/main/io_devices/piiobytearray.h index f20d5f37..38abb64d 100644 --- a/libs/main/io_devices/piiobytearray.h +++ b/libs/main/io_devices/piiobytearray.h @@ -85,10 +85,10 @@ public: int writeByteArray(const PIByteArray & ba); protected: - bool openDevice(); - int readDevice(void * read_to, int size); - int writeDevice(const void * data_, int size); - DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Sequential | PIIODevice::Reliable;} + virtual bool openDevice() override; + virtual int readDevice(void * read_to, int size) override; + virtual int writeDevice(const void * data_, int size) override; + virtual DeviceInfoFlags deviceInfoFlags() const override {return PIIODevice::Sequential | PIIODevice::Reliable;} ssize_t pos; PIByteArray * data_; diff --git a/libs/main/io_devices/piiodevice.cpp b/libs/main/io_devices/piiodevice.cpp index 6bc23365..08dde643 100644 --- a/libs/main/io_devices/piiodevice.cpp +++ b/libs/main/io_devices/piiodevice.cpp @@ -159,7 +159,7 @@ bool PIIODevice::setOption(PIIODevice::DeviceOption o, bool yes) { //! после каждого успешного потокового чтения. Метод должен быть //! в формате "bool func(void * data, uchar * readed, int size)" void PIIODevice::setThreadedReadSlot(ReadRetFunc func) { - ret_func_ = func; + func_read = func; } @@ -239,8 +239,8 @@ PIByteArray PIIODevice::read(int max_size) { void PIIODevice::_init() { opened_ = init_ = thread_started_ = false; raise_threaded_read_ = true; - ret_func_ = 0; - ret_data_ = 0; + func_read = nullptr; + ret_data_ = nullptr; tri = 0; setOptions(0); setReopenEnabled(true); @@ -596,9 +596,9 @@ PIMap & PIIODevice::fabrics() { } -bool PIIODevice::threadedRead(uchar *readed, int size) { +bool PIIODevice::threadedRead(const uchar *readed, int size) { // piCout << "iodevice threaded read"; - if (ret_func_ != 0) return ret_func_(ret_data_, readed, size); + if (func_read != 0) return func_read(readed, size, ret_data_); return true; } diff --git a/libs/main/io_devices/piiodevice.h b/libs/main/io_devices/piiodevice.h index 183e070c..604c2374 100644 --- a/libs/main/io_devices/piiodevice.h +++ b/libs/main/io_devices/piiodevice.h @@ -30,9 +30,9 @@ #include "pitimer.h" #include "piqueue.h" - -// function executed from threaded read, pass ThreadedReadData, readedData, sizeOfData -typedef bool (*ReadRetFunc)(void * , uchar * , int ); +/// TODO: написать документацию, тут ничего не понятно +// function executed from threaded read, pass readedData, sizeOfData, ThreadedReadData +typedef bool (*ReadRetFunc)(const uchar *, int, void *); #ifdef DOXYGEN @@ -64,9 +64,9 @@ typedef bool (*ReadRetFunc)(void * , uchar * , int ); # define PIIODEVICE(name, prefix) \ PIOBJECT_SUBCLASS(name, PIIODevice) \ - PIIODevice * copy() const {return new name();} \ + PIIODevice * copy() const override {return new name();} \ public: \ - virtual PIConstChars fullPathPrefix() const {return prefix;} \ + virtual PIConstChars fullPathPrefix() const override {return prefix;} \ static PIConstChars fullPathPrefixS() {return prefix;} \ private: @@ -239,7 +239,7 @@ public: //! \~english Start threaded read and assign threaded read callback to "func" //! \~russian Запускает потоковое чтение и устанавливает callback потокового чтения в "func" - void startThreadedRead(ReadRetFunc func) {ret_func_ = func; startThreadedRead();} + void startThreadedRead(ReadRetFunc func) {func_read = func; startThreadedRead();} //! \~english Stop threaded read. Hard stop terminate thread, otherwise wait fo 10 seconds //! \~russian Останавливает потоковое чтение. Жесткая остановка убивает поток, иначе ожидает 10 секунд @@ -369,7 +369,7 @@ public: EVENT(opened); EVENT(closed); - EVENT2(threadedReadEvent, uchar * , readed, int, size); + EVENT2(threadedReadEvent, const uchar * , readed, int, size); EVENT2(threadedWriteEvent, ullong, id, int, written_size); //! \handlers @@ -419,7 +419,7 @@ public: //! \~english Raise if succesfull close //! \~russian Вызывается при успешном закрытии - //! \fn void threadedReadEvent(uchar * readed, int size) + //! \fn void threadedReadEvent(const uchar * readed, int size) //! \~english Raise if read thread succesfull read some data //! \~russian Вызывается при успешном потоковом чтении данных @@ -475,7 +475,7 @@ protected: //! \~english Function executed when thread read some data, default implementation execute external callback "ret_func_" //! \~russian Метод вызывается после каждого успешного потокового чтения, по умолчанию вызывает callback "ret_func_" - virtual bool threadedRead(uchar * readed, int size); + virtual bool threadedRead(const uchar * readed, int size); //! \~english Reimplement to construct full unambiguous string, describes this device. //! Default implementation returns \a path() @@ -528,7 +528,7 @@ protected: DeviceMode mode_; DeviceOptions options_; - ReadRetFunc ret_func_; + ReadRetFunc func_read; bool opened_; void * ret_data_; @@ -539,9 +539,9 @@ private: virtual PIIODevice * copy() const {return 0;} PIString fullPathOptions() const; void _init(); - void begin(); - void run(); - void end() {terminate();} + void begin() override; + void run() override; + void end() override {terminate();} static void cacheFullPath(const PIString & full_path, const PIIODevice * d); static PIMap & fabrics(); diff --git a/libs/main/io_devices/piiostring.h b/libs/main/io_devices/piiostring.h index e7f5f694..098c6f2a 100644 --- a/libs/main/io_devices/piiostring.h +++ b/libs/main/io_devices/piiostring.h @@ -89,10 +89,10 @@ public: int writeString(const PIString & string); protected: - bool openDevice(); - int readDevice(void * read_to, int max_size); - int writeDevice(const void * data, int max_size); - DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Sequential | PIIODevice::Reliable;} + virtual bool openDevice() override; + virtual int readDevice(void * read_to, int max_size) override; + virtual int writeDevice(const void * data, int max_size) override; + virtual DeviceInfoFlags deviceInfoFlags() const override {return PIIODevice::Sequential | PIIODevice::Reliable;} ssize_t pos; PIString * str; diff --git a/libs/main/io_devices/pipeer.cpp b/libs/main/io_devices/pipeer.cpp index 5aa241bf..e120c6e1 100644 --- a/libs/main/io_devices/pipeer.cpp +++ b/libs/main/io_devices/pipeer.cpp @@ -61,11 +61,11 @@ PIPeer::PeerData::PeerData(const PIString & n): PIObject(n) { dt_out.setPacketSize(_PIPEER_MSG_SIZE); dt_in.setCRCEnabled(false); dt_out.setCRCEnabled(false); - CONNECTU(&dt_in, sendRequest, this, dtSendRequestIn); - CONNECTU(&dt_out, sendRequest, this, dtSendRequestOut); - CONNECTU(&dt_in, receiveFinished, this, dtReceiveFinishedIn); - CONNECTU(&dt_out, receiveFinished, this, dtReceiveFinishedOut); - CONNECTU(&t, started, this, dtThread); + CONNECT1(void, PIByteArray &, &dt_in, sendRequest, this, dtSendRequestIn); + CONNECT1(void, PIByteArray &, &dt_out, sendRequest, this, dtSendRequestOut); + CONNECT1(void, bool, &dt_in, receiveFinished, this, dtReceiveFinishedIn); + CONNECT1(void, bool, &dt_out, receiveFinished, this, dtReceiveFinishedOut); + CONNECT0(void, &t, started, this, dtThread); } @@ -172,7 +172,7 @@ PIPeer::PIPeer(const PIString & n): PIIODevice(), inited__(false), eth_tcp_srv(P self_info.dist = 0; self_info.time = PISystemTime::current(); randomize(); - CONNECTU(&sync_timer, tickEvent, this, timerEvent); + CONNECT2(void, void *, int, &sync_timer, tickEvent, this, timerEvent); prev_ifaces = PIEthernet::interfaces(); no_timer = false; sync_timer.addDelimiter(5); @@ -247,7 +247,7 @@ void PIPeer::initEths(PIStringList al) { eths_traffic << ce; cint = prev_ifaces.getByAddress(a); self_info.addresses << PeerInfo::PeerAddress(ce->path(), cint == 0 ? "255.255.255.0" : cint->netmask); - CONNECTU(ce, threadedReadEvent, this, dataRead); + CONNECT2(void, const uchar *, int, ce, threadedReadEvent, this, dataRead); ce->startThreadedRead(); // piCoutObj << "dc binded to" << ce->path(); // piCoutObj << "add eth" << a; @@ -282,7 +282,7 @@ void PIPeer::initMBcasts(PIStringList al) { ce->joinMulticastGroup(_PIPEER_MULTICAST_IP); if (ce->open()) { eths_mcast << ce; - CONNECTU(ce, threadedReadEvent, this, mbcastRead); + CONNECT2(void, const uchar *, int, ce, threadedReadEvent, this, mbcastRead); ce->startThreadedRead(); // piCout << "mcast bind to" << a << ce->sendIP(); } else { @@ -302,7 +302,7 @@ void PIPeer::initMBcasts(PIStringList al) { ce->setReadAddress(a, _PIPEER_BROADCAST_PORT); if (ce->open()) { eths_bcast << ce; - CONNECTU(ce, threadedReadEvent, this, mbcastRead); + CONNECT2(void, const uchar *, int, ce, threadedReadEvent, this, mbcastRead); ce->startThreadedRead(); // piCout << "mc BC try" << a << nm << ce->sendIP(); // piCout << "bcast bind to" << a << nm; @@ -319,7 +319,7 @@ void PIPeer::initMBcasts(PIStringList al) { eth_lo.setReadAddress("127.0.0.1", p); if (eth_lo.open()) { eth_lo.setSendIP("127.0.0.1"); - CONNECTU(ð_lo, threadedReadEvent, this, mbcastRead); + CONNECT2(void, const uchar *, int, ð_lo, threadedReadEvent, this, mbcastRead); eth_lo.startThreadedRead(); // piCout << "lo binded to" << eth_lo.readAddress() << eth_lo.sendAddress(); //piCout << "add eth" << ta; @@ -330,13 +330,13 @@ void PIPeer::initMBcasts(PIStringList al) { eth_tcp_srv.init(); eth_tcp_srv.listen("0.0.0.0", _PIPEER_TCP_PORT, true); eth_tcp_srv.setDebug(false); - CONNECTU(ð_tcp_srv, newConnection, this, newTcpClient); + CONNECT1(void, PIEthernet *, ð_tcp_srv, newConnection, this, newTcpClient); eth_tcp_srv.startThreadedRead(); eth_tcp_cli.setName("__S__PIPeer_eth_TCP_Client"); eth_tcp_cli.init(); eth_tcp_cli.setDebug(false); tcpClientReconnect(); - CONNECTU(ð_tcp_cli, threadedReadEvent, this, mbcastRead); + CONNECT2(void, const uchar *, int, ð_tcp_cli, threadedReadEvent, this, mbcastRead); CONNECTU(ð_tcp_cli, disconnected, this, tcpClientReconnect); eth_tcp_cli.startThreadedRead(); if (eths_mcast.isEmpty() && eths_bcast.isEmpty() && !eth_lo.isOpened()) piCoutObj << "Warning! Can`t find suitable network interface for multicast receive, check for exists at least one interface with multicasting enabled!"; @@ -444,7 +444,7 @@ void PIPeer::dtReceived(const PIString & from, const PIByteArray & data) { } -bool PIPeer::dataRead(uchar * readed, int size) { +bool PIPeer::dataRead(const uchar * readed, int size) { if (destroyed) { //piCout << "[PIPeer] SegFault"; return true; @@ -561,7 +561,7 @@ bool PIPeer::dataRead(uchar * readed, int size) { } -bool PIPeer::mbcastRead(uchar * data, int size) { +bool PIPeer::mbcastRead(const uchar * data, int size) { if (destroyed) { //piCout << "[PIPeer] SegFault"; return true; @@ -765,8 +765,8 @@ void PIPeer::addPeer(const PIPeer::PeerInfo & pd) { peers << pd; PeerInfo & p(peers.back()); p.init(); - CONNECTU(p._data, sendRequest, this, sendInternal) - CONNECTU(p._data, received, this, dtReceived) + CONNECT2(void, const PIString &, const PIByteArray &, p._data, sendRequest, this, sendInternal) + CONNECT2(void, const PIString &, const PIByteArray &, p._data, received, this, dtReceived) } @@ -961,7 +961,7 @@ int PIPeer::writeDevice(const void *data, int size) { void PIPeer::newTcpClient(PIEthernet *client) { client->setName("__S__PIPeer_eth_TCP_ServerClient" + client->path()); piCoutObj << "client" << client->path(); - CONNECTU(client, threadedReadEvent, this, mbcastRead); + CONNECT2(void, const uchar *, int, client, threadedReadEvent, this, mbcastRead); client->startThreadedRead(); } @@ -1009,8 +1009,9 @@ void PIPeer::initNetwork() { self_info.addresses.clear(); PIVector al = PIEthernet::allAddresses(); PIStringList sl; - piForeachC (PIEthernet::Address & a, al) + for (const PIEthernet::Address & a : al) { sl << a.ipString(); + } initEths(sl); // piCoutObj << sl << self_info.addresses.size(); sl.removeAll("127.0.0.1"); diff --git a/libs/main/io_devices/pipeer.h b/libs/main/io_devices/pipeer.h index d54d5564..fbd88558 100644 --- a/libs/main/io_devices/pipeer.h +++ b/libs/main/io_devices/pipeer.h @@ -132,8 +132,8 @@ protected: virtual void peerConnected(const PIString & name) {;} virtual void peerDisconnected(const PIString & name) {;} - EVENT_HANDLER2(bool, dataRead, uchar *, readed, int, size); - EVENT_HANDLER2(bool, mbcastRead, uchar *, readed, int, size); + EVENT_HANDLER2(bool, dataRead, const uchar *, readed, int, size); + EVENT_HANDLER2(bool, mbcastRead, const uchar *, readed, int, size); private: EVENT_HANDLER2(void, timerEvent, void * , data, int, delim); @@ -164,15 +164,15 @@ private: void addToRemoved(const PeerInfo & pi) {removed[pi.name] = PIPair(pi.cnt, pi.time);} bool isRemoved(const PeerInfo & pi) const {return (removed.value(pi.name) == PIPair(pi.cnt, pi.time));} - bool openDevice(); - bool closeDevice(); - PIString constructFullPathDevice() const; - void configureFromFullPathDevice(const PIString &full_path); - PIPropertyStorage constructVariantDevice() const; - void configureFromVariantDevice(const PIPropertyStorage & d); - int readDevice(void * read_to, int max_size); - int writeDevice(const void * data, int size); - DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Reliable;} + virtual bool openDevice() override; + virtual bool closeDevice() override; + virtual PIString constructFullPathDevice() const override; + virtual void configureFromFullPathDevice(const PIString &full_path) override; + virtual PIPropertyStorage constructVariantDevice() const override; + virtual void configureFromVariantDevice(const PIPropertyStorage & d) override; + virtual int readDevice(void * read_to, int max_size) override; + virtual int writeDevice(const void * data, int size) override; + virtual DeviceInfoFlags deviceInfoFlags() const override {return PIIODevice::Reliable;} PeerInfo * quickestPeer(const PIString & to); bool sendToNeighbour(PeerInfo * peer, const PIByteArray & ba); diff --git a/libs/main/io_devices/piserial.h b/libs/main/io_devices/piserial.h index f12df86b..96c12dac 100644 --- a/libs/main/io_devices/piserial.h +++ b/libs/main/io_devices/piserial.h @@ -216,7 +216,7 @@ public: //! \~english Discard all buffered input and output data //! \~russian Откидывает все буферизированные данные для передачи и приема - void flush(); + virtual void flush() override; int read(void * read_to, int max_size) {return readDevice(read_to, max_size);} @@ -282,19 +282,19 @@ public: //! \} protected: - PIString constructFullPathDevice() const; - void configureFromFullPathDevice(const PIString & full_path); - PIPropertyStorage constructVariantDevice() const; - void configureFromVariantDevice(const PIPropertyStorage & d); - bool configureDevice(const void * e_main, const void * e_parent = 0); - void optionsChanged(); - void threadedReadBufferSizeChanged(); + virtual PIString constructFullPathDevice() const override; + virtual void configureFromFullPathDevice(const PIString & full_path) override; + virtual PIPropertyStorage constructVariantDevice() const override; + virtual void configureFromVariantDevice(const PIPropertyStorage & d) override; + virtual bool configureDevice(const void * e_main, const void * e_parent = 0) override; + virtual void optionsChanged() override; + virtual void threadedReadBufferSizeChanged() override; //! \~english Basic read function //! \~russian Базовое чтение - int readDevice(void * read_to, int max_size); - int writeDevice(const void * data, int max_size); - DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Sequential;} + virtual int readDevice(void * read_to, int max_size) override; + virtual int writeDevice(const void * data, int max_size) override; + virtual DeviceInfoFlags deviceInfoFlags() const override {return PIIODevice::Sequential;} //! Executes when any read function was successful. Default implementation does nothing virtual void received(const void * data, int size) {;} @@ -306,8 +306,8 @@ protected: bool setBit(int bit, bool on, const PIString & bname); bool isBit(int bit, const PIString & bname) const; - bool openDevice(); - bool closeDevice(); + virtual bool openDevice() override; + virtual bool closeDevice() override; PRIVATE_DECLARATION(PIP_EXPORT) int fd, vtime; diff --git a/libs/main/io_devices/pisharedmemory.h b/libs/main/io_devices/pisharedmemory.h index d7697725..3fe9db8b 100644 --- a/libs/main/io_devices/pisharedmemory.h +++ b/libs/main/io_devices/pisharedmemory.h @@ -91,15 +91,15 @@ public: protected: - bool openDevice(); - bool closeDevice(); - PIString constructFullPathDevice() const; - void configureFromFullPathDevice(const PIString & full_path); - PIPropertyStorage constructVariantDevice() const; - void configureFromVariantDevice(const PIPropertyStorage & d); - int readDevice(void * read_to, int max_size) {return read(read_to, max_size, 0);} - int writeDevice(const void * data, int max_size) {return write(data, max_size, 0);} - DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Reliable;} + virtual bool openDevice() override; + virtual bool closeDevice() override; + virtual PIString constructFullPathDevice() const override; + virtual void configureFromFullPathDevice(const PIString & full_path) override; + virtual PIPropertyStorage constructVariantDevice() const override; + virtual void configureFromVariantDevice(const PIPropertyStorage & d) override; + virtual int readDevice(void * read_to, int max_size) override {return read(read_to, max_size, 0);} + virtual int writeDevice(const void * data, int max_size) override {return write(data, max_size, 0);} + virtual DeviceInfoFlags deviceInfoFlags() const override {return PIIODevice::Reliable;} private: void initPrivate(); diff --git a/libs/main/io_devices/pispi.h b/libs/main/io_devices/pispi.h index 88eac21d..ad50e8b2 100644 --- a/libs/main/io_devices/pispi.h +++ b/libs/main/io_devices/pispi.h @@ -62,16 +62,16 @@ public: protected: - bool openDevice(); - bool closeDevice(); - int readDevice(void * read_to, int max_size); - int writeDevice(const void * data, int max_size); + virtual bool openDevice() override; + virtual bool closeDevice() override; + virtual int readDevice(void * read_to, int max_size) override; + virtual int writeDevice(const void * data, int max_size) override; - PIString constructFullPathDevice() const; - void configureFromFullPathDevice(const PIString & full_path); - PIPropertyStorage constructVariantDevice() const; - void configureFromVariantDevice(const PIPropertyStorage & d); - DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Sequential;} + virtual PIString constructFullPathDevice() const override; + virtual void configureFromFullPathDevice(const PIString & full_path) override; + virtual PIPropertyStorage constructVariantDevice() const override; + virtual void configureFromVariantDevice(const PIPropertyStorage & d) override; + virtual DeviceInfoFlags deviceInfoFlags() const override {return PIIODevice::Sequential;} private: uint spi_speed; diff --git a/libs/main/io_devices/pitransparentdevice.h b/libs/main/io_devices/pitransparentdevice.h index 462ed565..b3080996 100644 --- a/libs/main/io_devices/pitransparentdevice.h +++ b/libs/main/io_devices/pitransparentdevice.h @@ -45,11 +45,11 @@ public: virtual ~PITransparentDevice(); protected: - bool openDevice(); - bool closeDevice(); - int readDevice(void * read_to, int max_size); - int writeDevice(const void * data, int max_size); - DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Reliable;} + virtual bool openDevice() override; + virtual bool closeDevice() override; + virtual int readDevice(void * read_to, int max_size) override; + virtual int writeDevice(const void * data, int max_size) override; + virtual DeviceInfoFlags deviceInfoFlags() const override {return PIIODevice::Reliable;} PIMutex que_mutex; PIQueue que; diff --git a/libs/main/io_devices/piusb.h b/libs/main/io_devices/piusb.h index c4e976d9..3e7650c1 100644 --- a/libs/main/io_devices/piusb.h +++ b/libs/main/io_devices/piusb.h @@ -157,17 +157,17 @@ public: int controlWrite(const void * data, int max_size); - void flush(); + virtual void flush() override; protected: - bool configureDevice(const void * e_main, const void * e_parent = 0); - PIString constructFullPathDevice() const; - void configureFromFullPathDevice(const PIString & full_path); - int readDevice(void * read_to, int max_size); - int writeDevice(const void * data, int max_size); - bool openDevice(); - bool closeDevice(); - DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Reliable;} + virtual bool configureDevice(const void * e_main, const void * e_parent = 0) override; + virtual PIString constructFullPathDevice() const override; + virtual void configureFromFullPathDevice(const PIString & full_path) override; + virtual int readDevice(void * read_to, int max_size) override; + virtual int writeDevice(const void * data, int max_size) override; + virtual bool openDevice() override; + virtual bool closeDevice() override; + virtual DeviceInfoFlags deviceInfoFlags() const {return PIIODevice::Reliable;} PIVector eps; ushort vid_, pid_; diff --git a/libs/main/io_utils/pibroadcast.h b/libs/main/io_utils/pibroadcast.h index c5cb9e7e..32777b8a 100644 --- a/libs/main/io_utils/pibroadcast.h +++ b/libs/main/io_utils/pibroadcast.h @@ -133,7 +133,7 @@ protected: virtual void addressesChanged() {} private: - EVENT_HANDLER2(void, mcastRead, uchar * , data, int, size); + EVENT_HANDLER2(void, mcastRead, const uchar * , data, int, size); void destroyAll(); void initAll(PIVector al); void run(); diff --git a/libs/main/io_utils/piconnection.cpp b/libs/main/io_utils/piconnection.cpp index 14dc1dcd..d307e39a 100644 --- a/libs/main/io_utils/piconnection.cpp +++ b/libs/main/io_utils/piconnection.cpp @@ -128,12 +128,13 @@ bool PIConnection::configure(PIConfig & conf, const PIString & name_) { PIStringList dev_list(ce.getValue("device").toString()); PIStringList name_list(ce.getValue("device").name()); PIStringList flt_list(ce.getValue("filter").toString()); - piForeachC (PIConfig::Entry * e, db) { + for (const PIConfig::Entry * e : db) { dev_list << e->value(); name_list << e->name(); } - piForeachC (PIConfig::Entry * e, fb) + for (const PIConfig::Entry * e : fb) { flt_list << e->name(); + } PISet chk_set = (PISet(name_list) & PISet(flt_list)); //piCout << name_list << flt_list << chk_set; chk_set.remove(""); @@ -157,9 +158,8 @@ bool PIConnection::configure(PIConfig & conf, const PIString & name_) { dev->setName(name_ + ".device." + dev_list[i]); PIConfig::Entry de = ce.getValue("device." + n); dev->setThreadedReadBufferSize(de.getValue("bufferSize", dev->threadedReadBufferSize()).toInt()); - PIDiagnostics * diag = diags_.value(dev, 0); - if (diag != 0) - diag->setDisconnectTimeout(de.getValue("disconnectTimeout", diag->disconnectTimeout()).toFloat()); + PIDiagnostics * diag = diags_.value(dev, nullptr); + if (diag) diag->setDisconnectTimeout(de.getValue("disconnectTimeout", diag->disconnectTimeout()).toFloat()); } int added(0), padded(-1), tries(0); bool pdebug = debug(); @@ -169,7 +169,7 @@ bool PIConnection::configure(PIConfig & conf, const PIString & name_) { padded = added; added = 0; ++tries; - piForeachC (PIConfig::Entry * e, fb) { + for (const PIConfig::Entry * e : fb) { PIPacketExtractor::SplitMode sm = PIPacketExtractor::None; PIString sms(e->getValue("splitMode").value()); int smi = sms.toInt(); @@ -198,15 +198,15 @@ bool PIConnection::configure(PIConfig & conf, const PIString & name_) { PIStringList devs(e->value()); PIConfig::Branch db(e->getValue("device").children()); devs << e->getValue("device", "").value(); - piForeachC (PIConfig::Entry * e2, db) + for (const PIConfig::Entry * e2 : db) { devs << e2->value(); + } devs.removeStrings(""); if (devs.isEmpty()) continue; PIString dname = dev_aliases.value(devs.front(), devs.front()); PIPacketExtractor * pe = addFilter(e->name(), dname, sm); - if (pe == 0) { - if (!filter_fails.contains(dname)) - filter_fails << dname; + if (!pe) { + if (!filter_fails.contains(dname)) filter_fails << dname; continue; } else { filter_fails.removeAll(dname); @@ -214,7 +214,7 @@ bool PIConnection::configure(PIConfig & conf, const PIString & name_) { ++added; for (int i = 1; i < devs.size_s(); ++i) { dname = dev_aliases.value(devs[i], devs[i]); - if (addFilter(e->name(), dname, sm) != 0) { + if (addFilter(e->name(), dname, sm)) { filter_fails.removeAll(dname); ++added; } else { @@ -222,9 +222,8 @@ bool PIConnection::configure(PIConfig & conf, const PIString & name_) { filter_fails << dname; } } - PIDiagnostics * diag = diags_.value(pe, 0); - if (diag != 0) - diag->setDisconnectTimeout(e->getValue("disconnectTimeout", diag->disconnectTimeout()).toFloat()); + PIDiagnostics * diag = diags_.value(pe, nullptr); + if (diag) diag->setDisconnectTimeout(e->getValue("disconnectTimeout", diag->disconnectTimeout()).toFloat()); pe->setBufferSize(e->getValue("bufferSize", pe->bufferSize()).toInt()); pe->setPayloadSize(e->getValue("payloadSize", pe->payloadSize()).toInt()); pe->setPacketSize(e->getValue("packetSize", pe->packetSize()).toInt()); @@ -234,23 +233,26 @@ bool PIConnection::configure(PIConfig & conf, const PIString & name_) { } } setDebug(pdebug); - piForeachC (PIString & f, filter_fails) + for (const PIString & f : filter_fails) { piCoutObj << "\"addFilter\" error: no such device \"" << f << "\"!"; - piForeachC (PIConfig::Entry * e, cb) { + } + for (const PIConfig::Entry * e : cb) { PIString f(e->getValue("from").value()), t(e->getValue("to").value()); addChannel(dev_aliases.value(f, f), dev_aliases.value(t, t)); } - piForeachC (PIConfig::Entry * e, sb) { + for (const PIConfig::Entry * e : sb) { PIStringList devs(e->value()); PIConfig::Branch db(e->getValue("device").children()); devs << e->getValue("device", "").value(); - piForeachC (PIConfig::Entry * e2, db) + for (const PIConfig::Entry * e2 : db) { devs << e2->value(); + } devs.removeStrings(""); if (devs.isEmpty()) continue; float freq = e->getValue("frequency").toFloat(); - piForeachC (PIString & d, devs) + for (const PIString & d : devs) { addSender(e->name(), dev_aliases.value(d, d), freq); + } PIByteArray fd(PIByteArray::fromUserInput(e->getValue("fixedData").toString())); setSenderFixedData(e->name(), fd); } @@ -264,32 +266,31 @@ PIString PIConnection::makeConfig() const { ts << "[" << name() << "]\n"; PIVector devs(boundedDevices()); int dn(-1); - piForeachC (PIIODevice * d, devs) { + for (const PIIODevice * d : devs) { PIStringList dnl(deviceNames(d)); if (dnl.isEmpty()) dnl << PIString::fromNumber(++dn); - piForeachC (PIString & dname, dnl) { + for (const PIString & dname : dnl) { ts << "device." << dname << " = " << d->constructFullPath() << " #s\n"; ts << "device." << dname << ".bufferSize = " << d->threadedReadBufferSize() << " #n\n"; - PIDiagnostics * diag = diags_.value(const_cast(d), 0); - if (diag != 0) - ts << "device." << dname << ".disconnectTimeout = " << diag->disconnectTimeout() << " #f\n"; + PIDiagnostics * diag = diags_.value(const_cast(d), nullptr); + if (diag) ts << "device." << dname << ".disconnectTimeout = " << diag->disconnectTimeout() << " #f\n"; } } - piForeachC (PEPair & f, extractors) { - if (f.second == 0) continue; - if (f.second->extractor == 0) continue; - PIString prefix = "filter." + f.first; - for (int i = 0; i < f.second->devices.size_s(); ++i) { - PIString dname = device_names.key(f.second->devices[i]); - if (dname.isEmpty()) dname = devPath(f.second->devices[i]); + auto ite = extractors.makeIterator(); + while (ite.next()) { + if (!ite.value()) continue; + if (!ite.value()->extractor) continue; + PIString prefix = "filter." + ite.key(); + for (int i = 0; i < ite.value()->devices.size_s(); ++i) { + PIString dname = device_names.key(ite.value()->devices[i]); + if (dname.isEmpty()) dname = devPath(ite.value()->devices[i]); ts << prefix << ".device." << i << " = " << dname << " #s\n"; } - PIDiagnostics * diag = diags_.value(f.second->extractor, 0); - ts << prefix << ".bufferSize = " << f.second->extractor->bufferSize() << " #n\n"; - if (diag != 0) - ts << prefix << ".disconnectTimeout = " << diag->disconnectTimeout() << " #f\n"; + PIDiagnostics * diag = diags_.value(ite.value()->extractor, nullptr); + ts << prefix << ".bufferSize = " << ite.value()->extractor->bufferSize() << " #n\n"; + if (diag) ts << prefix << ".disconnectTimeout = " << diag->disconnectTimeout() << " #f\n"; ts << prefix << ".splitMode = "; - switch (f.second->extractor->splitMode()) { + switch (ite.value()->extractor->splitMode()) { case PIPacketExtractor::None: ts << "none"; break; case PIPacketExtractor::Header: ts << "header"; break; case PIPacketExtractor::Footer: ts << "footer"; break; @@ -298,37 +299,39 @@ PIString PIConnection::makeConfig() const { case PIPacketExtractor::Timeout: ts << "timeout"; break; } ts << " #s\n"; - ts << prefix << ".payloadSize = " << f.second->extractor->payloadSize() << " #n\n"; - ts << prefix << ".packetSize = " << f.second->extractor->packetSize() << " #n\n"; - ts << prefix << ".timeout = " << f.second->extractor->timeout() << " #f\n"; - ts << prefix << ".header = " << f.second->extractor->header().toString() << " #s\n"; - ts << prefix << ".footer = " << f.second->extractor->footer().toString() << " #s\n"; + ts << prefix << ".payloadSize = " << ite.value()->extractor->payloadSize() << " #n\n"; + ts << prefix << ".packetSize = " << ite.value()->extractor->packetSize() << " #n\n"; + ts << prefix << ".timeout = " << ite.value()->extractor->timeout() << " #f\n"; + ts << prefix << ".header = " << ite.value()->extractor->header().toString() << " #s\n"; + ts << prefix << ".footer = " << ite.value()->extractor->footer().toString() << " #s\n"; } dn = 0; - piForeachC (CPair & c, channels_) { - piForeachC (PIIODevice * d, c.second) { + auto itc = channels_.makeIterator(); + while (itc.next()) { + for (const PIIODevice * d : itc.value()) { PIString prefix = "channel." + PIString::fromNumber(dn); ++dn; - PIString dname = device_names.key(c.first); - if (dname.isEmpty()) dname = devPath(c.first); + PIString dname = device_names.key(itc.key()); + if (dname.isEmpty()) dname = devPath(itc.key()); ts << prefix << ".from = " << dname << " #s\n"; dname = device_names.key(const_cast(d)); if (dname.isEmpty()) dname = devPath(d); ts << prefix << ".to = " << dname << " #s\n"; } } - piForeachC (SPair & s, senders) { - if (s.second == 0) continue; - PIString prefix = "sender." + s.second->name(); - for (int i = 0; i < s.second->devices.size_s(); ++i) { - PIString dname = device_names.key(s.second->devices[i]); - if (dname.isEmpty()) dname = devPath(s.second->devices[i]); + auto its = senders.makeIterator(); + while (its.next()) { + if (!its.value()) continue; + PIString prefix = "sender." + its.value()->name(); + for (int i = 0; i < its.value()->devices.size_s(); ++i) { + PIString dname = device_names.key(its.value()->devices[i]); + if (dname.isEmpty()) dname = devPath(its.value()->devices[i]); ts << prefix << ".device." << i << " = " << dname << " #s\n"; } - double int_ = s.second->int_; + double int_ = its.value()->int_; if (int_ > 0.) ts << prefix << ".frequency = " << (1000. / int_) << " #f\n"; - if (!s.second->sdata.isEmpty()) - ts << prefix << ".fixedData = " << s.second->sdata.toString() << " #s\n"; + if (!its.value()->sdata.isEmpty()) + ts << prefix << ".fixedData = " << its.value()->sdata.toString() << " #s\n"; } ts << "[]\n"; return ret; @@ -342,11 +345,11 @@ PIIODevice * PIConnection::addDevice(const PIString & full_path, PIIODevice::Dev dev->setName(name() + ".device." + fp); device_modes[dev] = mode; __device_pool__->lock(); - if (diags_.value(dev, 0) == 0) { + if (!diags_.value(dev, nullptr)) { PIDiagnostics * d = new PIDiagnostics(false); d->setInterval(100.); diags_[dev] = d; - CONNECTU(d, qualityChanged, this, diagQualityChanged); + CONNECT2(void, PIDiagnostics::Quality, PIDiagnostics::Quality, d, qualityChanged, this, diagQualityChanged); __device_pool__->init(); } __device_pool__->unlock(); @@ -363,9 +366,10 @@ void PIConnection::setDeviceName(PIIODevice * dev, const PIString & name) { PIStringList PIConnection::deviceNames(const PIIODevice * dev) const { PIStringList ret; - piForeachC (DNPair & s, device_names) - if (s.second == dev) - ret << s.first; + auto it = device_names.makeIterator(); + while(it.next()) { + if (it.value() == dev) ret << it.key(); + } return ret; } @@ -373,29 +377,31 @@ PIStringList PIConnection::deviceNames(const PIIODevice * dev) const { bool PIConnection::removeDevice(const PIString & full_path) { PIString fp(PIIODevice::normalizeFullPath(full_path)); PIIODevice * dev = __device_pool__->device(fp); - if (dev == 0) return false; + if (!dev) return false; PIStringList dntd(deviceNames(dev)); - piForeachC (PIString & n, dntd) + for (const PIString & n : dntd) { device_names.removeOne(n); + } for (auto s = senders.constBegin(); s != senders.constEnd(); s++) { - if (s.value() == 0) continue; + if (!s.value()) continue; s.value()->lock(); s.value()->devices.removeAll(dev); s.value()->unlock(); } device_modes.remove(dev); for (auto i = extractors.constBegin(); i != extractors.constEnd(); i++) { - if (i.value() == 0) continue; + if (!i.value()) continue; i.value()->devices.removeAll(dev); } bounded_extractors.remove(dev); channels_.remove(dev); auto it = channels_.makeIterator(); - while (it.next()) + while (it.next()) { it.valueRef().removeAll(dev); + } __device_pool__->lock(); - if (diags_.value(dev, 0) != 0) - delete diags_.value(dev); + PIDiagnostics * dg = diags_.value(dev, nullptr); + if (dg) delete dg; diags_.remove(dev); __device_pool__->unlock(); return __device_pool__->removeDevice(this, fp); @@ -406,19 +412,20 @@ void PIConnection::removeAllDevices() { device_names.clear(); PIVector bdevs(__device_pool__->boundedDevices(this)); __device_pool__->lock(); - piForeach (PIIODevice * d, bdevs) { + for (PIIODevice * d : bdevs) { for (auto s = senders.constBegin(); s != senders.constEnd(); s++) { - if (s.value() == 0) continue; + if (!s.value()) continue; s.value()->lock(); s.value()->devices.removeAll(d); s.value()->unlock(); } channels_.remove(d); auto it = channels_.makeIterator(); - while (it.next()) + while (it.next()) { it.valueRef().removeAll(d); - if (diags_.value(d, 0) != 0) - delete diags_.value(d); + } + PIDiagnostics * dg = diags_.value(d, nullptr); + if (dg) delete dg; diags_.remove(d); } __device_pool__->unboundConnection(this); @@ -426,7 +433,7 @@ void PIConnection::removeAllDevices() { device_modes.clear(); bounded_extractors.clear(); for (auto i = extractors.constBegin(); i != extractors.constEnd(); i++) { - if (i.value() == 0) continue; + if (!i.value()) continue; i.value()->devices.clear(); } } @@ -434,16 +441,16 @@ void PIConnection::removeAllDevices() { PIIODevice * PIConnection::deviceByFullPath(const PIString & full_path) const { PIString fp(PIIODevice::normalizeFullPath(full_path)); - DevicePool::DeviceData * dd = __device_pool__->devices.value(fp); - if (dd == 0) return 0; - if (dd->dev == 0) return 0; - if (!dd->listeners.contains(const_cast(this))) return 0; + DevicePool::DeviceData * dd = __device_pool__->devices.value(fp, nullptr); + if (!dd) return nullptr; + if (!dd->dev) return nullptr; + if (!dd->listeners.contains(const_cast(this))) return nullptr; return dd->dev; } PIIODevice * PIConnection::deviceByName(const PIString & name) const { - return device_names.value(name, 0); + return device_names.value(name, nullptr); } @@ -454,36 +461,32 @@ PIVector PIConnection::boundedDevices() const { PIPacketExtractor * PIConnection::addFilter(const PIString & name_, const PIString & full_path, PIPacketExtractor::SplitMode mode) { PIString fname_ = name_.trimmed(); - Extractor * e = extractors.value(fname_); - if (full_path.isEmpty()) return (e == 0 ? 0 : e->extractor); + Extractor * e = extractors.value(fname_, nullptr); + if (full_path.isEmpty()) return (e ? e->extractor : nullptr); PIIODevice * dev = devByString(full_path); - PIPacketExtractor * pe(0); - if (extractors.value(full_path) != 0) pe = extractors.value(full_path)->extractor; - if (pe != 0) dev = pe; - if (dev == 0) { + PIPacketExtractor * pe = nullptr; + if (extractors.value(full_path, nullptr)) pe = extractors.value(full_path, nullptr)->extractor; + if (pe) dev = pe; + if (!dev) { piCoutObj << "\"addFilter\" error: no such device or filter \"" << full_path << "\"!"; - return 0; + return nullptr; } - if (e == 0) { + if (!e) { e = new Extractor(); extractors[fname_] = e; } - if (e->extractor == 0) { - e->extractor = new PIPacketExtractor(0, mode); + if (!e->extractor) { + e->extractor = new PIPacketExtractor(nullptr, mode); e->extractor->setName(fname_); - e->extractor->setThreadedReadData(new PIPair(this, fname_)); - e->extractor->setHeaderCheckSlot(filterValidateHeaderS); - e->extractor->setFooterCheckSlot(filterValidateFooterS); - e->extractor->setPayloadCheckSlot(filterValidatePayloadS); __device_pool__->lock(); - if (diags_.value(e->extractor, 0) == 0) { + if (!diags_.value(e->extractor, nullptr)) { PIDiagnostics * d = new PIDiagnostics(false); d->setInterval(100.); diags_[e->extractor] = d; - CONNECTU(d, qualityChanged, this, diagQualityChanged); + CONNECT2(void, PIDiagnostics::Quality, PIDiagnostics::Quality, d, qualityChanged, this, diagQualityChanged); } __device_pool__->unlock(); - CONNECT2(void, uchar * , int, e->extractor, packetReceived, this, packetExtractorReceived) + CONNECT2(void, const uchar * , int, e->extractor, packetReceived, this, packetExtractorReceived) } if (!e->devices.contains(dev)) { bounded_extractors[dev] << e->extractor; @@ -495,32 +498,33 @@ PIPacketExtractor * PIConnection::addFilter(const PIString & name_, const PIStri PIPacketExtractor * PIConnection::addFilter(PIPacketExtractor * filter, const PIString & full_path) { - Extractor * e = 0; - if (full_path.isEmpty()) return (e == 0 ? 0 : e->extractor); + Extractor * e = nullptr; + if (full_path.isEmpty()) return nullptr; PIIODevice * dev = devByString(full_path); - PIPacketExtractor * pe(0); - if (extractors.value(full_path) != 0) pe = extractors.value(full_path)->extractor; - if (pe != 0) dev = pe; - if (dev == 0) { + PIPacketExtractor * pe = nullptr; + e = extractors.value(full_path, nullptr); + if (e) pe = e->extractor; + if (pe) { + dev = pe; + } else { piCoutObj << "\"addFilter\" error: no such device or filter \"" << full_path << "\"!"; - return 0; + return nullptr; } - if (e == 0) { + if (!e) { e = new Extractor(); extractors[filter->name()] = e; } - if (e->extractor == 0) { + if (!e->extractor) { e->extractor = filter; - e->extractor->setThreadedReadData(new PIPair(this, filter->name())); __device_pool__->lock(); if (diags_.value(e->extractor, 0) == 0) { PIDiagnostics * d = new PIDiagnostics(false); d->setInterval(100.); diags_[e->extractor] = d; - CONNECTU(d, qualityChanged, this, diagQualityChanged); + CONNECT2(void, PIDiagnostics::Quality, PIDiagnostics::Quality, d, qualityChanged, this, diagQualityChanged); } __device_pool__->unlock(); - CONNECT2(void, uchar * , int, e->extractor, packetReceived, this, packetExtractorReceived) + CONNECT2(void, const uchar * , int, e->extractor, packetReceived, this, packetExtractorReceived) } if (!e->devices.contains(dev)) { bounded_extractors[dev] << e->extractor; @@ -536,9 +540,9 @@ bool PIConnection::removeFilter(const PIString & name_, const PIString & full_pa bool PIConnection::removeFilter(const PIString & name_, const PIIODevice * dev) { - if (dev == 0) return false; - Extractor * p = extractors.value(name_.trimmed()); - if (p == 0) return false; + if (!dev) return false; + Extractor * p = extractors.value(name_.trimmed(), nullptr); + if (!p) return false; bool ret = false; for (int i = 0; i < p->devices.size_s(); ++i) { if (p->devices[i] == dev) { @@ -557,8 +561,8 @@ bool PIConnection::removeFilter(const PIString & name_, const PIIODevice * dev) bool PIConnection::removeFilter(const PIString & name_) { - Extractor * p = extractors.value(name_.trimmed()); - if (p == 0) return false; + Extractor * p = extractors.value(name_.trimmed(), nullptr); + if (!p) return false; unboundExtractor(p->extractor); delete p; return true; @@ -568,13 +572,15 @@ bool PIConnection::removeFilter(const PIString & name_) { void PIConnection::removeAllFilters() { __device_pool__->lock(); for (auto i = extractors.constBegin(); i != extractors.constEnd(); i++) { - if (i.value() == 0) continue; + if (!i.value()) continue; channels_.remove(i.value()->extractor); auto it = channels_.makeIterator(); - while (it.next()) + while (it.next()) { it.valueRef().removeAll(i.value()->extractor); - if (diags_.value(i.value()->extractor, 0) != 0) + } + if (diags_.value(i.value()->extractor)) { delete diags_.value(i.value()->extractor); + } diags_.remove(i.value()->extractor); delete i.value(); } @@ -587,8 +593,9 @@ void PIConnection::removeAllFilters() { PIVector PIConnection::filters() const { PIVector ret; for (auto i = extractors.constBegin(); i != extractors.constEnd(); i++) { - if (i.value() != 0) - if (i.value()->extractor != 0) ret << i.value()->extractor; + if (i.value()) { + if (i.value()->extractor) ret << i.value()->extractor; + } } return ret; } @@ -597,8 +604,8 @@ PIVector PIConnection::filters() const { PIStringList PIConnection::filterNames() const { PIStringList ret; for (auto i = extractors.constBegin(); i != extractors.constEnd(); i++) { - if (i.value() != 0) - if (i.value()->extractor != 0) ret << i.key(); + if (i.value()) + if (i.value()->extractor) ret << i.key(); } return ret; } @@ -607,18 +614,18 @@ PIStringList PIConnection::filterNames() const { PIPacketExtractor * PIConnection::filter(const PIString & name_) const { PIString fname_ = name_.trimmed(); for (auto i = extractors.constBegin(); i != extractors.constEnd(); i++) { - if (i.value() != 0) - if (i.value()->extractor != 0 && i.key() == fname_) - return i.value()->extractor; + if (i.value()) { + if ((i.value()->extractor) && (i.key() == fname_)) return i.value()->extractor; + } } - return 0; + return nullptr; } PIVector PIConnection::filterBoundedDevices(const PIString & name_) const { PIVector ret; Extractor * p = extractors.value(name_.trimmed()); - if (p == 0) return ret; + if (!p) return ret; return p->devices; } @@ -626,15 +633,19 @@ PIVector PIConnection::filterBoundedDevices(const PIString & name bool PIConnection::addChannel(const PIString & name0, const PIString & name1) { //piCout << "addChannel" << name0 << name1; if (name0.isEmpty() || name1.isEmpty()) return false; - PIIODevice * dev0 = devByString(name0), * dev1 = devByString(name1); - PIPacketExtractor * pe0(0), * pe1(0); - if (extractors.value(name0) != 0) pe0 = extractors.value(name0)->extractor; - if (extractors.value(name1) != 0) pe1 = extractors.value(name1)->extractor; - if (pe0 != 0) dev0 = pe0; - if (pe1 != 0) dev1 = pe1; - if (dev0 == 0 || dev1 == 0) { - if (dev0 == 0) piCoutObj << "\"addChannel\" error: no such device \"" << name0 << "\"!"; - if (dev1 == 0) piCoutObj << "\"addChannel\" error: no such device \"" << name1 << "\"!"; + PIIODevice * dev0 = devByString(name0); + PIIODevice * dev1 = devByString(name1); + Extractor * p0 = extractors.value(name0, nullptr); + Extractor * p1 = extractors.value(name1, nullptr); + PIPacketExtractor * pe0 = nullptr; + PIPacketExtractor * pe1 = nullptr; + if (p0) pe0 = p0->extractor; + if (p1) pe1 = p1->extractor; + if (pe0) dev0 = pe0; + if (pe1) dev1 = pe1; + if (!dev0 || !dev1) { + if (!dev0) piCoutObj << "\"addChannel\" error: no such device \"" << name0 << "\"!"; + if (!dev1) piCoutObj << "\"addChannel\" error: no such device \"" << name1 << "\"!"; return false; } if (!channels_[dev0].contains(dev1)) @@ -644,13 +655,17 @@ bool PIConnection::addChannel(const PIString & name0, const PIString & name1) { bool PIConnection::removeChannel(const PIString & name0, const PIString & name1) { - PIIODevice * dev0 = devByString(name0), * dev1 = devByString(name1); - PIPacketExtractor * pe0(0), * pe1(0); - if (extractors.value(name0) != 0) pe0 = extractors.value(name0)->extractor; - if (extractors.value(name1) != 0) pe1 = extractors.value(name1)->extractor; - if (pe0 != 0) dev0 = pe0; - if (pe1 != 0) dev1 = pe1; - if (dev0 == 0 || dev1 == 0) return false; + PIIODevice * dev0 = devByString(name0); + PIIODevice * dev1 = devByString(name1); + Extractor * p0 = extractors.value(name0, nullptr); + Extractor * p1 = extractors.value(name1, nullptr); + PIPacketExtractor * pe0 = nullptr; + PIPacketExtractor * pe1 = nullptr; + if (p0) pe0 = p0->extractor; + if (p1) pe1 = p1->extractor; + if (pe0) dev0 = pe0; + if (pe1) dev1 = pe1; + if (!dev0 || !dev1) return false; channels_[dev0].removeAll(dev1); return true; } @@ -658,14 +673,16 @@ bool PIConnection::removeChannel(const PIString & name0, const PIString & name1) bool PIConnection::removeChannel(const PIString & name0) { PIIODevice * dev0 = devByString(name0); - PIPacketExtractor * pe0(0); - if (extractors.value(name0) != 0) pe0 = extractors.value(name0)->extractor; - if (pe0 != 0) dev0 = pe0; - if (dev0 == 0) return false; + Extractor * p0 = extractors.value(name0, nullptr); + PIPacketExtractor * pe0 = nullptr; + if (p0) pe0 = p0->extractor; + if (pe0) dev0 = pe0; + if (!dev0) return false; channels_.remove(dev0); auto it = channels_.makeIterator(); - while (it.next()) + while (it.next()) { it.valueRef().removeAll(dev0); + } return true; } @@ -676,21 +693,21 @@ void PIConnection::removeAllChannels() { PIString PIConnection::devPath(const PIIODevice * d) const { - if (d == 0) return PIString(); + if (!d) return PIString(); if (strcmp(d->className(), "PIPacketExtractor") == 0) return d->name(); return d->constructFullPath(); } PIString PIConnection::devFPath(const PIIODevice * d) const { - if (d == 0) return PIString(); + if (!d) return PIString(); if (d->isPropertyExists("__fullPath__")) return d->property("__fullPath__").toString(); return d->name(); } PIIODevice * PIConnection::devByString(const PIString & s) const { - if (s.isEmpty()) return 0; + if (s.isEmpty()) return nullptr; PIIODevice * ret = deviceByName(s); if (!ret) ret = deviceByFullPath(s); return ret; @@ -699,10 +716,12 @@ PIIODevice * PIConnection::devByString(const PIString & s) const { PIVector > PIConnection::channels() const { PIVector > ret; - piForeachC (CPair & i, channels_) { - PIString fp0(devFPath(i.first)); - piForeachC (PIIODevice * d, i.second) + auto it = channels_.makeIterator(); + while (it.next()) { + PIString fp0(devFPath(it.key())); + for (const PIIODevice * d : it.value()) { ret << PIPair(fp0, devFPath(d)); + } } return ret; } @@ -711,15 +730,15 @@ PIVector > PIConnection::channels() const { void PIConnection::addSender(const PIString & name_, const PIString & full_path_name, float frequency, bool start_) { PIString fname_ = name_.trimmed(); if (full_path_name.isEmpty() || frequency <= 0.) return; - Sender * s = senders.value(fname_); - if (s == 0) { + Sender * s = senders.value(fname_, nullptr); + if (!s) { s = new Sender(this); s->setName(fname_); s->int_ = 1000. / frequency; senders[fname_] = s; } PIIODevice * dev = devByString(full_path_name); - if (dev == 0) { + if (!dev) { piCoutObj << "\"addSender\" error: no such device \"" << full_path_name << "\"!"; return; } @@ -728,28 +747,26 @@ void PIConnection::addSender(const PIString & name_, const PIString & full_path_ if (!__device_pool__->fake) s->start(s->int_); } s->lock(); - if (!s->devices.contains(dev)) - s->devices << dev; + if (!s->devices.contains(dev)) s->devices << dev; s->unlock(); } bool PIConnection::removeSender(const PIString & name, const PIString & full_path_name) { - Sender * s = senders.value(name, 0); + Sender * s = senders.value(name, nullptr); PIIODevice * d = devByString(full_path_name); - if (s == 0 || d == 0) return false; + if (!s || !d) return false; s->lock(); bool ret = s->devices.contains(d); - if (ret) - s->devices.removeAll(d); + if (ret) s->devices.removeAll(d); s->unlock(); return ret; } bool PIConnection::removeSender(const PIString & name) { - Sender * s = senders.value(name, 0); - if (s == 0) return false; + Sender * s = senders.value(name, nullptr); + if (!s) return false; delete s; senders.remove(name); return true; @@ -757,8 +774,8 @@ bool PIConnection::removeSender(const PIString & name) { bool PIConnection::setSenderFixedData(const PIString & name, const PIByteArray & data) { - Sender * s = senders.value(name, 0); - if (s == 0) return false; + Sender * s = senders.value(name, nullptr); + if (!s) return false; s->lock(); s->sdata = data; s->unlock(); @@ -767,8 +784,8 @@ bool PIConnection::setSenderFixedData(const PIString & name, const PIByteArray & bool PIConnection::clearSenderFixedData(const PIString & name) { - Sender * s = senders.value(name, 0); - if (s == 0) return false; + Sender * s = senders.value(name, nullptr); + if (!s) return false; s->lock(); s->sdata.clear(); s->unlock(); @@ -777,15 +794,15 @@ bool PIConnection::clearSenderFixedData(const PIString & name) { PIByteArray PIConnection::senderFixedData(const PIString & name) const { - Sender * s = senders.value(name, 0); - if (s == 0) return PIByteArray(); + Sender * s = senders.value(name, nullptr); + if (!s) return PIByteArray(); return s->sdata; } float PIConnection::senderFrequency(const PIString & name) const { - Sender * s = senders.value(name, 0); - if (s == 0) return -1.f; + Sender * s = senders.value(name, nullptr); + if (!s) return -1.f; double i = s->interval(); if (i == 0.) return 0.f; return 1000. / s->interval(); @@ -794,8 +811,7 @@ float PIConnection::senderFrequency(const PIString & name) const { void PIConnection::removeAllSenders() { for (auto s = senders.constBegin(); s != senders.constEnd(); s++) { - if (s.value() != 0) - delete s.value(); + if (s.value()) delete s.value(); } senders.clear(); } @@ -803,8 +819,8 @@ void PIConnection::removeAllSenders() { void PIConnection::startThreadedRead(const PIString & full_path_name) { DevicePool::DeviceData * dd = __device_pool__->deviceData(devByString(full_path_name)); - if (dd == 0) return; - if (dd->dev == 0) return; + if (!dd) return; + if (!dd->dev) return; if (dd->started || dd->dev->mode() == PIIODevice::WriteOnly) return; if (!__device_pool__->fake) dd->rthread->start(); dd->started = true; @@ -812,32 +828,33 @@ void PIConnection::startThreadedRead(const PIString & full_path_name) { void PIConnection::startAllThreadedReads() { - for (auto d = __device_pool__->devices.constBegin(); d != __device_pool__->devices.constEnd(); d++) + for (auto d = __device_pool__->devices.constBegin(); d != __device_pool__->devices.constEnd(); d++) { startThreadedRead(d.key()); + } } void PIConnection::startSender(const PIString & name) { - Sender * s = senders.value(name, 0); - if (s == 0) return; - if (!s->isRunning() && !__device_pool__->fake) - s->start(s->int_); + Sender * s = senders.value(name, nullptr); + if (!s) return; + if (!s->isRunning() && !__device_pool__->fake) s->start(s->int_); } void PIConnection::startAllSenders() { for (auto s = senders.constBegin(); s != senders.constEnd(); s++) { - if (s.value() == 0) continue; - if (!s.value()->isRunning() && !__device_pool__->fake) + if (!s.value()) continue; + if (!s.value()->isRunning() && !__device_pool__->fake) { s.value()->start(s.value()->int_); + } } } void PIConnection::stopThreadedRead(const PIString & full_path_name) { DevicePool::DeviceData * dd = __device_pool__->deviceData(devByString(full_path_name)); - if (dd == 0) return; - if (dd->dev == 0) return; + if (!dd) return; + if (!dd->dev) return; if (!dd->started || dd->dev->mode() == PIIODevice::WriteOnly) return; dd->rthread->stop(); dd->started = false; @@ -845,34 +862,35 @@ void PIConnection::stopThreadedRead(const PIString & full_path_name) { void PIConnection::stopAllThreadedReads() { - for (auto d = __device_pool__->devices.constBegin(); d != __device_pool__->devices.constEnd(); d++) + for (auto d = __device_pool__->devices.constBegin(); d != __device_pool__->devices.constEnd(); d++) { stopThreadedRead(d.key()); + } } void PIConnection::stopSender(const PIString & name) { - Sender * s = senders.value(name, 0); - if (s == 0) return; + Sender * s = senders.value(name, nullptr); + if (!s) return; if (s->isRunning()) s->stop(); } void PIConnection::stopAllSenders() { for (auto s = senders.constBegin(); s != senders.constEnd(); s++) { - if (s.value() == 0) continue; - if (s.value()->isRunning()) - s.value()->stop(); + if (!s.value()) continue; + if (s.value()->isRunning()) s.value()->stop(); } } PIDiagnostics * PIConnection::diagnostic(const PIString & full_path_name) const { PIIODevice * dev = devByString(full_path_name); - PIPacketExtractor * pe(0); - if (extractors.value(full_path_name) != 0) pe = extractors.value(full_path_name)->extractor; - if (pe != 0) dev = pe; - if (dev == 0) return 0; - return diags_.value(dev, 0); + Extractor * e = extractors.value(full_path_name, nullptr); + PIPacketExtractor * pe = nullptr; + if (e) pe = e->extractor; + if (pe) dev = pe; + if (!dev) return 0; + return diags_.value(dev, nullptr); } @@ -899,7 +917,7 @@ int PIConnection::writeByName(const PIString & name_, const PIByteArray & data) int PIConnection::write(PIIODevice * dev, const PIByteArray & data) { - if (dev == 0) { + if (!dev) { piCoutObj << "Null Device!"; return -1; } @@ -909,8 +927,8 @@ int PIConnection::write(PIIODevice * dev, const PIByteArray & data) { return -1; } int ret = dev->write(data); - PIDiagnostics * diag = diags_.value(dev); - if (diag != 0 && ret > 0) diag->sended(ret); + PIDiagnostics * diag = diags_.value(dev, nullptr); + if (diag && ret > 0) diag->sended(ret); return ret; } @@ -951,37 +969,35 @@ PIConnection::DevicePool::~DevicePool() { void PIConnection::DevicePool::init() { - if (!isRunning()) - start(100); + if (!isRunning()) start(100); } PIIODevice * PIConnection::DevicePool::addDevice(PIConnection * parent, const PIString & fp, PIIODevice::DeviceMode mode, bool start) { DeviceData * dd = devices[fp]; - int pmode(0); + int pmode = 0; bool need_start = false; - if (dd == 0) { + if (!dd) { dd = new DeviceData(); devices[fp] = dd; } - if (dd->dev == 0) { + if (!dd->dev) { //piCout << "new device" << fp; dd->dev = PIIODevice::createFromFullPath(fp); - if (dd->dev == 0) { + if (!dd->dev) { piCoutObj << "Error: can`t create device \"" << fp << "\"!"; //:" << errorString(); - return 0; + return nullptr; } dd->dev->setProperty("__fullPath__", fp); - } else + } else { pmode = dd->dev->mode(); - if (!dd->listeners.contains(parent)) - dd->listeners << parent; - if (pmode == mode || pmode == PIIODevice::ReadWrite) - return dd->dev; + } + if (!dd->listeners.contains(parent)) dd->listeners << parent; + if (pmode == mode || pmode == PIIODevice::ReadWrite) return dd->dev; if ((mode & PIIODevice::ReadOnly) > 0) { - if (dd->rthread != 0) { + if (dd->rthread) { delete dd->rthread; - dd->rthread = 0; + dd->rthread = nullptr; dd->started = false; } dd->rthread = new PIThread(dd, __DevicePool_threadReadDP); @@ -989,13 +1005,13 @@ PIIODevice * PIConnection::DevicePool::addDevice(PIConnection * parent, const PI need_start = true; pmode |= PIIODevice::ReadOnly; } - if ((mode & PIIODevice::WriteOnly) > 0) - pmode |= PIIODevice::WriteOnly; + if ((mode & PIIODevice::WriteOnly) > 0) pmode |= PIIODevice::WriteOnly; if (!fake) { dd->dev->close(); dd->dev->open((PIIODevice::DeviceMode)pmode); - } else + } else { dd->dev->setMode((PIIODevice::DeviceMode)pmode); + } if (need_start && start) { if (!fake) dd->rthread->start(); dd->started = true; @@ -1005,11 +1021,9 @@ PIIODevice * PIConnection::DevicePool::addDevice(PIConnection * parent, const PI bool PIConnection::DevicePool::removeDevice(PIConnection * parent, const PIString & fp) { - DeviceData * dd = devices.value(fp); - if (dd == 0) - return false; - if (dd->dev == 0) - return false; + DeviceData * dd = devices.value(fp, nullptr); + if (!dd) return false; + if (!dd->dev) return false; bool ok = dd->listeners.contains(parent); dd->listeners.removeAll(parent); if (dd->listeners.isEmpty()) { @@ -1023,18 +1037,16 @@ bool PIConnection::DevicePool::removeDevice(PIConnection * parent, const PIStrin void PIConnection::DevicePool::unboundConnection(PIConnection * parent) { PIStringList rem; for (auto i = devices.constBegin(); i != devices.constEnd(); i++) { - if (i.value() == 0) { + if (!i.value()) { rem << i.key(); continue; } i.value()->listeners.removeAll(parent); - if (i.value()->listeners.isEmpty()) - rem << i.key(); + if (i.value()->listeners.isEmpty()) rem << i.key(); } - piForeachC (PIString & i, rem) { - DeviceData * dd = devices.value(i); - if (dd == 0) - continue; + for (const PIString & i : rem) { + DeviceData * dd = devices.value(i, nullptr); + if (!dd) continue; delete dd; devices.remove(i); } @@ -1042,45 +1054,49 @@ void PIConnection::DevicePool::unboundConnection(PIConnection * parent) { PIIODevice * PIConnection::DevicePool::device(const PIString & fp) const { - DeviceData * dd = devices.value(fp); - if (dd == 0) return 0; + DeviceData * dd = devices.value(fp, nullptr); + if (!dd) return nullptr; return dd->dev; } PIConnection::DevicePool::DeviceData * PIConnection::DevicePool::deviceData(PIIODevice * d) const { - if (!d) return 0; - piForeachC (DDPair & i, devices) { - if (i.second->dev == d) - return i.second; + if (!d) return nullptr; + auto it = devices.makeIterator(); + while (it.next()) { + if (!it.value()) continue; + if (it.value()->dev == d) return it.value(); } - return 0; + return nullptr; } PIVector PIConnection::DevicePool::boundedConnections() const { PIVector ret; - piForeachC (DDPair & i, devices) { - if (i.second == 0) - continue; - ret << i.second->listeners; + auto it = devices.makeIterator(); + while (it.next()) { + if (!it.value()) continue; + ret << it.value()->listeners; } - for (int i = 0; i < ret.size_s(); ++i) - for (int j = i + 1; j < ret.size_s(); ++j) + for (int i = 0; i < ret.size_s(); ++i) { + for (int j = i + 1; j < ret.size_s(); ++j) { if (ret[i] == ret[j]) { ret.remove(j); --j; } + } + } return ret; } PIVector< PIIODevice * > PIConnection::DevicePool::boundedDevices() const { PIVector ret; - piForeachC (DDPair & i, devices) { - if (i.second == 0) continue; - if (i.second->dev == 0) continue; - ret << i.second->dev; + auto it = devices.makeIterator(); + while (it.next()) { + if (!it.value()) continue; + if (!it.value()->dev) continue; + ret << it.value()->dev; } return ret; } @@ -1088,11 +1104,13 @@ PIVector< PIIODevice * > PIConnection::DevicePool::boundedDevices() const { PIVector PIConnection::DevicePool::boundedDevices(const PIConnection * parent) const { PIVector ret; - piForeachC (DDPair & i, devices) { - if (i.second == 0) continue; - if (i.second->dev == 0) continue; - if (i.second->listeners.contains(const_cast(parent))) - ret << i.second->dev; + auto it = devices.makeIterator(); + while (it.next()) { + if (!it.value()) continue; + if (!it.value()->dev) continue; + if (it.value()->listeners.contains(const_cast(parent))) { + ret << it.value()->dev; + } } return ret; } @@ -1102,21 +1120,21 @@ PIConnection::DevicePool::DeviceData::~DeviceData() { if (rthread) { rthread->terminate(); delete rthread; - rthread = 0; + rthread = nullptr; } if (dev) { dev->close(); delete dev; - dev = 0; + dev = nullptr; } } void PIConnection::DevicePool::run() { PIVector conns(PIConnection::allConnections()); - piForeach (PIConnection * c, conns) { + for (PIConnection * c : conns) { for (auto d = c->diags_.constBegin(); d != c->diags_.constEnd(); d++) { - if (d.value() == 0) continue; + if (!d.value()) continue; d.value()->tick(0, 1); } } @@ -1125,13 +1143,23 @@ void PIConnection::DevicePool::run() { void __DevicePool_threadReadDP(void * ddp) { PIConnection::DevicePool::DeviceData * dd((PIConnection::DevicePool::DeviceData * )ddp); - if (dd->dev == 0) {piMSleep(100); return;} PIIODevice * dev = dd->dev; - if (dev->isClosed()) - if (!dev->open()) {piMSleep(dev->reopenTimeout()); return;} + if (!dev) { + piMSleep(100); + return; + } + if (dev->isClosed()) { + if (!dev->open()) { + piMSleep(dev->reopenTimeout()); + return; + } + } PIByteArray ba; ba = dev->read(dev->threadedReadBufferSize()); - if (ba.isEmpty()) {piMSleep(10); return;} + if (ba.isEmpty()) { + piMSleep(10); + return; + } dev->threadedRead(ba.data(), ba.size_s()); dev->threadedReadEvent(ba.data(), ba.size_s()); //piCout << "Readed from" << dd->dev->path() << Hex << ba; @@ -1141,26 +1169,9 @@ void __DevicePool_threadReadDP(void * ddp) { void PIConnection::DevicePool::deviceReaded(PIConnection::DevicePool::DeviceData * dd, const PIByteArray & data) { PIString from = dd->dev->property("__fullPath__").toString(); - piForeach (PIConnection * ld, dd->listeners) + for (PIConnection * ld : dd->listeners) { ld->rawReceived(dd->dev, from, data); -} - - -bool PIConnection::filterValidateHeaderS(void * c, uchar * src, uchar * rec, int size) { - PIPair * p((PIPair * )c); - return p->first->filterValidateHeader(p->second, src, rec, size); -} - - -bool PIConnection::filterValidateFooterS(void * c, uchar * src, uchar * rec, int size) { - PIPair * p((PIPair * )c); - return p->first->filterValidateFooter(p->second, src, rec, size); -} - - -bool PIConnection::filterValidatePayloadS(void * c, uchar * rec, int size) { - PIPair * p((PIPair * )c); - return p->first->filterValidatePayload(p->second, rec, size); + } } @@ -1169,39 +1180,20 @@ void PIConnection::rawReceived(PIIODevice * dev, const PIString & from, const PI dataReceivedEvent(from, data); PIVector be(bounded_extractors.value(dev)); //piCout << be; - piForeach (PIPacketExtractor * i, be) - i->threadedRead(const_cast(data.data()), data.size_s()); - PIVector chd(channels_.value(dev)); - piForeach (PIIODevice * d, chd) { - int ret = d->write(data); - PIDiagnostics * diag = diags_.value(d); - if (diag != 0 && ret > 0) diag->sended(ret); + for (PIPacketExtractor * i : be) { + i->threadedRead(data.data(), data.size_s()); } - PIDiagnostics * diag = diags_.value(dev); - if (diag != 0) diag->received(data.size_s()); + PIVector chd(channels_.value(dev)); + for (PIIODevice * d : chd) { + int ret = d->write(data); + PIDiagnostics * diag = diags_.value(d, nullptr); + if (diag && ret > 0) diag->sended(ret); + } + PIDiagnostics * diag = diags_.value(dev, nullptr); + if (diag) diag->received(data.size_s()); } -bool PIConnection::filterValidateHeader(const PIString & filter_name, uchar * src, uchar * rec, int size) { - for (int i = 0; i < size; ++i) - if (src[i] != rec[i]) - return false; - return true; -} - - -bool PIConnection::filterValidateFooter(const PIString & filter_name, uchar * src, uchar * rec, int size) { - for (int i = 0; i < size; ++i) - if (src[i] != rec[i]) - return false; - return true; -} - - -bool PIConnection::filterValidatePayload(const PIString & filter_name, uchar * rec, int size) { - return true; -} - PIByteArray PIConnection::senderData(const PIString & sender_name) { return PIByteArray(); @@ -1209,11 +1201,9 @@ PIByteArray PIConnection::senderData(const PIString & sender_name) { PIConnection::Extractor::~Extractor() { - if (extractor != 0) { - if (extractor->threadedReadData() != 0) - delete (PIPair * )(extractor->threadedReadData()); + if (extractor) { delete extractor; - extractor = 0; + extractor = nullptr; } } @@ -1227,16 +1217,16 @@ PIConnection::Sender::Sender(PIConnection * parent_): parent(parent_), int_(0.f) void PIConnection::Sender::tick(void * , int) { - if (parent == 0) return; + if (!parent) return; PIByteArray data; if (!sdata.isEmpty()) data = sdata; else data = parent->senderData(name()); if (data.isEmpty()) return; //piCoutObj << "write"<write(data); - PIDiagnostics * diag = parent->diags_.value(d); - if (diag != 0 && ret > 0) diag->sended(ret); + PIDiagnostics * diag = parent->diags_.value(d, nullptr); + if (diag && ret > 0) diag->sended(ret); } } @@ -1244,45 +1234,45 @@ void PIConnection::Sender::tick(void * , int) { void PIConnection::unboundExtractor(PIPacketExtractor * pe) { - if (pe == 0) return; + if (!pe) return; channels_.remove(pe); auto it = channels_.makeIterator(); - while (it.next()) + while (it.next()) { it.valueRef().removeAll(pe); + } bounded_extractors.remove(pe); PIVector k = bounded_extractors.keys(); - piForeach (PIIODevice * i, k) { + for (PIIODevice * i : k) { PIVector & be(bounded_extractors[i]); be.removeAll(pe); - if (be.isEmpty()) - bounded_extractors.remove(i); + if (be.isEmpty()) bounded_extractors.remove(i); } __device_pool__->lock(); - if (diags_.value(pe, 0) != 0) - delete diags_.value(pe); + if (diags_.value(pe, nullptr)) delete diags_.value(pe); diags_.remove(pe); extractors.remove(pe->name()); __device_pool__->unlock(); } -void PIConnection::packetExtractorReceived(uchar * data, int size) { - PIString from(emitter() == 0 ? "" : emitter()->name()); +void PIConnection::packetExtractorReceived(const uchar * data, int size) { + PIString from(emitter()? emitter()->name() : PIString()); PIIODevice * cd = (PIIODevice * )emitter(); // piCout << "packetExtractorReceived" << from << cd; - if (cd != 0) { + if (cd) { PIVector be(bounded_extractors.value(cd)); //piCout << be << (void*)data << size; - piForeach (PIPacketExtractor * i, be) + for (PIPacketExtractor * i : be) { i->threadedRead(data, size); + } PIVector chd(channels_.value(cd)); - piForeach (PIIODevice * d, chd) { + for (PIIODevice * d : chd) { int ret = d->write(data, size); PIDiagnostics * diag = diags_.value(d); - if (diag != 0) diag->sended(ret); + if (diag) diag->sended(ret); } PIDiagnostics * diag = diags_.value(cd); - if (diag != 0) diag->received(size); + if (diag) diag->received(size); } packetReceived(from, PIByteArray(data, size)); packetReceivedEvent(from, PIByteArray(data, size)); diff --git a/libs/main/io_utils/piconnection.h b/libs/main/io_utils/piconnection.h index bd6e97a4..f176be21 100644 --- a/libs/main/io_utils/piconnection.h +++ b/libs/main/io_utils/piconnection.h @@ -312,7 +312,6 @@ public: void deviceReaded(DeviceData * dd, const PIByteArray & data); - typedef PIMap::value_type DDPair; PIMap devices; bool fake; }; @@ -343,26 +342,14 @@ protected: //! Executes on packet received from filter with name "from" virtual void packetReceived(const PIString & from, const PIByteArray & data) {} - //! Validate header "rec" with source header "src" and size "size", executes from filter "filter_name" - virtual bool filterValidateHeader(const PIString & filter_name, uchar * src, uchar * rec, int size); - - //! Validate footer "rec" with source footer "src" and size "size", executes from filter "filter_name" - virtual bool filterValidateFooter(const PIString & filter_name, uchar * src, uchar * rec, int size); - - //! Validate payload "rec" with size "size", executes from filter "filter_name" - virtual bool filterValidatePayload(const PIString & filter_name, uchar * rec, int size); - //! You should returns data for sender "sender_name" virtual PIByteArray senderData(const PIString & sender_name); private: - static bool filterValidateHeaderS(void * c, uchar * src, uchar * rec, int size); - static bool filterValidateFooterS(void * c, uchar * src, uchar * rec, int size); - static bool filterValidatePayloadS(void * c, uchar * rec, int size); bool configure(PIConfig & conf, const PIString & name_); void rawReceived(PIIODevice * dev, const PIString & from, const PIByteArray & data); void unboundExtractor(PIPacketExtractor * pe); - EVENT_HANDLER2(void, packetExtractorReceived, uchar * , data, int, size); + EVENT_HANDLER2(void, packetExtractorReceived, const uchar * , data, int, size); EVENT_HANDLER2(void, diagQualityChanged, PIDiagnostics::Quality, new_quality, PIDiagnostics::Quality, old_quality); PIString devPath(const PIIODevice * d) const; @@ -388,12 +375,6 @@ private: void tick(void * , int); }; - typedef PIMap::value_type PEPair; - typedef PIMap::value_type SPair; - typedef PIMap::value_type DNPair; - typedef PIMap >::value_type BEPair; - typedef PIMap >::value_type CPair; - typedef PIMap::value_type DPair; PIMap extractors; PIMap senders; PIMap device_names; diff --git a/libs/main/io_utils/pipacketextractor.cpp b/libs/main/io_utils/pipacketextractor.cpp index 08908e84..ac43501a 100644 --- a/libs/main/io_utils/pipacketextractor.cpp +++ b/libs/main/io_utils/pipacketextractor.cpp @@ -91,7 +91,9 @@ PIPacketExtractor::PIPacketExtractor(PIIODevice * device_, PIPacketExtractor::Sp void PIPacketExtractor::construct() { - ret_func_header = ret_func_footer = 0; + func_header = nullptr; + func_footer = nullptr; + func_payload = nullptr; setPayloadSize(0); setTimeout(100); #ifdef MICRO_PIP @@ -113,13 +115,25 @@ void PIPacketExtractor::propertyChanged(const char *) { dataSize = property("payloadSize").toInt(); src_header = property("header").toByteArray(); src_footer = property("footer").toByteArray(); + time_ = property("timeout").toDouble(); packetSize_hf = src_header.size_s() + src_footer.size_s() + payloadSize(); } +int PIPacketExtractor::readDevice(void * read_to, int max_size) { + if (dev) return dev->read(read_to, max_size); + return -1; +} + + +int PIPacketExtractor::writeDevice(const void * data, int max_size) { + if (dev) return dev->write(data, max_size); + return -1; +} + + void PIPacketExtractor::setDevice(PIIODevice * device_) { dev = device_; - if (dev == 0) return; } @@ -152,23 +166,51 @@ void PIPacketExtractor::setFooter(const PIByteArray & data) { } -bool PIPacketExtractor::threadedRead(uchar * readed, int size_) { +int PIPacketExtractor::validateHeader(const uchar * src, const uchar * rec, int size) { + if (func_header) return func_header(src, rec, size); + for (int i = 0; i < size; ++i) { + if (src[i] != rec[i]) return -1; + } + return dataSize; +} + + +bool PIPacketExtractor::validateFooter(const uchar * src, const uchar * rec, int size) { + if (func_footer) return func_footer(src, rec, size); + for (int i = 0; i < size; ++i) { + if (src[i] != rec[i]) return false; + } + return true; +} + + +bool PIPacketExtractor::validatePayload(const uchar * rec, int size) { + if (func_payload) return func_payload(rec, size); + return true; +} + + +bool PIPacketExtractor::threadedRead(const uchar * readed, int size_) { //piCoutObj << "readed" << size_; int ss; switch (mode_) { case PIPacketExtractor::None: - if (validatePayload(readed, size_)) + if (validatePayload(readed, size_)) { packetReceived(readed, size_); - break; + } + break; case PIPacketExtractor::Header: tmpbuf.append(readed, size_); - ss = src_header.size_s() + dataSize; + ss = src_header.size_s(); while (tmpbuf.size_s() >= ss) { - while (!validateHeader(src_header.data(), tmpbuf.data(), src_header.size_s())) { + int ns = validateHeader(src_header.data(), tmpbuf.data(), src_header.size_s()); + while (ns < 0) { tmpbuf.pop_front(); ++missed; - if (tmpbuf.size_s() < ss) return true; + if (tmpbuf.size() < src_header.size()) return true; + ns = validateHeader(src_header.data(), tmpbuf.data(), src_header.size_s()); } + ss = src_header.size_s() + ns; while (!validatePayload(tmpbuf.data(src_header.size_s()), dataSize)) { tmpbuf.pop_front(); ++missed; @@ -179,56 +221,6 @@ bool PIPacketExtractor::threadedRead(uchar * readed, int size_) { } break; case PIPacketExtractor::Footer: - /*memcpy(buffer.data(allReaded), readed, size_); - allReaded += size_; - footer_ = (mode_ == PIPacketExtractor::Footer); - while (allReaded >= packetSize_hf + addSize && allReaded > 0) { - if (!src_header.isEmpty()) { - if (allReaded + curInd >= buffer_size) { - memcpy(sbuffer.data(), buffer.data(), buffer_size); - memcpy(buffer.data(), sbuffer.data(buffer_size - packetSize_hf), allReaded); - allReaded = packetSize_hf; - addSize = curInd = 0; - } - bool brk = false; - while (!validateHeader((uchar * )(footer_ ? src_footer.data() : src_header.data()), buffer.data(curInd + (footer_ ? dataSize : 0)), footer_ ? src_footer.size_s() : src_header.size_s())) { - ++curInd; ++missed; - if (packetSize_hf > 0) missed_packets = missed / packetSize_hf; - if (curInd > addSize) { - addSize += packetSize_hf; - brk = true; - break; - } - } - if (brk) continue; - //memcpy(mheader.data(), buffer.data(curInd + (footer_ ? dataSize : 0)), src_header.size_s()); - if (!src_header.isEmpty()) memcpy(src_header.data(), buffer.data(curInd), src_header.size_s()); - if (!validatePayload(buffer.data(curInd + src_header.size_s()), dataSize)) { - ++curInd; ++missed; - if (packetSize_hf > 0) missed_packets = missed / packetSize_hf; - continue; - } - packetReceived(buffer.data(curInd), packetSize_hf); - memcpy(sbuffer.data(), buffer.data(), allReaded); - memcpy(buffer.data(), sbuffer.data(packetSize_hf + curInd), allReaded); - allReaded -= packetSize_hf + curInd; - curInd = addSize = 0; - } else { - if (dataSize == 0) { - if (validatePayload(buffer.data(), size_)) - packetReceived(buffer.data(), size_); - memcpy(sbuffer.data(), buffer.data(), allReaded); - memcpy(buffer.data(), sbuffer.data(size_), allReaded); - allReaded -= size_; - } else { - if (validatePayload(buffer.data(), dataSize)) - packetReceived(buffer.data(), dataSize); - memcpy(sbuffer.data(), buffer.data(), allReaded); - memcpy(buffer.data(), sbuffer.data(packetSize_hf), allReaded); - allReaded -= packetSize_hf; - } - } - }*/ tmpbuf.append(readed, size_); ss = src_footer.size_s() + dataSize; while (tmpbuf.size_s() >= ss) { @@ -252,7 +244,7 @@ bool PIPacketExtractor::threadedRead(uchar * readed, int size_) { while (tmpbuf.size_s() >= ss) { if (!header_found) { if (tmpbuf.size_s() < ss) return true; - while (!validateHeader(src_header.data(), tmpbuf.data(), src_header.size_s())) { + while (validateHeader(src_header.data(), tmpbuf.data(), src_header.size_s()) < 0) { tmpbuf.pop_front(); ++missed; if (tmpbuf.size_s() < ss) return true; @@ -310,3 +302,21 @@ bool PIPacketExtractor::threadedRead(uchar * readed, int size_) { PIString PIPacketExtractor::constructFullPathDevice() const { return ""; } + + +bool PIPacketExtractor::openDevice() { + if (dev) return dev->open(); + return false; +} + + +bool PIPacketExtractor::closeDevice() { + if (dev) return dev->close(); + return false; +} + + +PIIODevice::DeviceInfoFlags PIPacketExtractor::deviceInfoFlags() const { + if (dev) return dev->infoFlags(); + return 0; +} diff --git a/libs/main/io_utils/pipacketextractor.h b/libs/main/io_utils/pipacketextractor.h index 1200da09..6c5cfa34 100644 --- a/libs/main/io_utils/pipacketextractor.h +++ b/libs/main/io_utils/pipacketextractor.h @@ -28,8 +28,11 @@ #include "piiodevice.h" +/// TODO: написать документацию, тут ничего не понятно // Pass data, recHeaderPtr, received_data, recHeaderSize. Return true if packet is correct nor return false. -typedef bool (*PacketExtractorCheckFunc)(void * , uchar * , uchar * , int ); +typedef int (*PacketExtractorHeaderFunc)(const uchar *, const uchar *, int); +typedef bool (*PacketExtractorPayloadFunc)(const uchar *, int ); +typedef bool (*PacketExtractorFooterFunc)(const uchar *, const uchar *, int); class PIP_EXPORT PIPacketExtractor: public PIIODevice { @@ -48,7 +51,7 @@ public: }; //! Contructs extractor with child device "device_" and extract algorithm "mode" - explicit PIPacketExtractor(PIIODevice * device_ = 0, SplitMode mode = None); + explicit PIPacketExtractor(PIIODevice * device_ = nullptr, SplitMode mode = None); virtual ~PIPacketExtractor() {stop();} @@ -66,13 +69,13 @@ public: //! Set buffer size to "new_size" bytes, should be at least greater than whole packet size void setBufferSize(int new_size); - void setHeaderCheckSlot(PacketExtractorCheckFunc f) {ret_func_header = f;} - void setFooterCheckSlot(PacketExtractorCheckFunc f) {ret_func_footer = f;} - void setPayloadCheckSlot(ReadRetFunc f) {ret_func_ = f;} + void setHeaderCheckSlot(PacketExtractorHeaderFunc f) {func_header = f;} + void setPayloadCheckSlot(PacketExtractorPayloadFunc f) {func_payload = f;} + void setFooterCheckSlot(PacketExtractorFooterFunc f) {func_footer = f;} //! Set extract algorithm - void setSplitMode(SplitMode mode) {setProperty("splitMode", int(mode)); mode_ = mode;} + void setSplitMode(SplitMode mode) {setProperty("splitMode", int(mode));} //! Set payload size, used for PIPacketExtractor::Header and PIPacketExtractor::Footer algorithms void setPayloadSize(int size); @@ -84,17 +87,17 @@ public: void setFooter(const PIByteArray & data); //! Set packet size, used for PIPacketExtractor::Size algorithm - void setPacketSize(int size) {setProperty("packetSize", size); packetSize_ = size;} + void setPacketSize(int size) {setProperty("packetSize", size);} //! Set timeout in milliseconds, used for PIPacketExtractor::Timeout algorithm - void setTimeout(double msecs) {setProperty("timeout", msecs); time_ = msecs;} + void setTimeout(double msecs) {setProperty("timeout", msecs);} //! Returns current extract algorithm - SplitMode splitMode() const {return (SplitMode)(property("splitMode").toInt());} + SplitMode splitMode() const {return mode_;} //! Returns current payload size, used for PIPacketExtractor::Header and PIPacketExtractor::Footer algorithms - int payloadSize() const {return property("payloadSize").toInt();} + int payloadSize() const {return dataSize;} //! Returns current header data, used for PIPacketExtractor::Header and PIPacketExtractor::HeaderAndFooter algorithms PIByteArray header() const {return src_header;} @@ -103,10 +106,10 @@ public: PIByteArray footer() const {return src_footer;} //! Returns current packet size, used for PIPacketExtractor::Size algorithm - int packetSize() const {return property("packetSize").toInt();} + int packetSize() const {return packetSize_;} //! Returns current timeout in milliseconds, used for PIPacketExtractor::Timeout algorithm - double timeout() const {return property("timeout").toDouble();} + double timeout() const {return time_;} //! Returns missed by validating functions bytes count @@ -122,17 +125,17 @@ public: const ullong * missedPackets_ptr() const {return &missed_packets;} //! Add data to extractor, raise \a packetReceived() if packet is ready - void appendData(const void * d, int s) {threadedRead(const_cast((const uchar *)d), s);} + void appendData(const uchar * d, int s) {threadedRead(d, s);} //! Add data to extractor, raise \a packetReceived() if packet is ready - void appendData(const PIByteArray & data) {appendData(data.data(), data.size_s());} + void appendData(const PIByteArray & data) {threadedRead(data.data(), data.size_s());} - EVENT2(packetReceived, uchar * , data, int, size); + EVENT2(packetReceived, const uchar * , data, int, size); //! \events //! \{ - //! \fn void packetReceived(uchar * data, int size) + //! \fn void packetReceived(const uchar * data, int size) //! \brief Raise on successfull \a packetValidate() function //! \} @@ -144,35 +147,37 @@ protected: * \param rec Received header * \param size Header size * \details Default implementation returns by-byte "src" with "rec" compare result */ - virtual bool validateHeader(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;} + virtual int validateHeader(const uchar * src, const uchar * rec, int size); /** \brief Function to validate footer * \param src Your footer content * \param rec Received footer * \param size Footer size * \details Default implementation returns by-byte "src" with "rec" compare result */ - virtual bool validateFooter(uchar * src, uchar * rec, int size) {if (ret_func_footer != 0) return ret_func_footer(ret_data_, src, rec, size); for (int i = 0; i < size; ++i) if (src[i] != rec[i]) return false; return true;} + virtual bool validateFooter(const uchar * src, const uchar * rec, int size); /** \brief Function to validate payload * \param rec Received payload * \param size payload size * \details Default implementation returns \b true */ - virtual bool validatePayload(uchar * rec, int size) {if (ret_func_ != 0) return ret_func_(ret_data_, rec, size); return true;} + virtual bool validatePayload(const uchar * rec, int size); private: void construct(); void propertyChanged(const char *); - int readDevice(void * read_to, int max_size) {if (dev == 0) return -1; return dev->read(read_to, max_size);} - int writeDevice(const void * data, int max_size) {if (dev == 0) return -1; return dev->write(data, max_size);} - bool threadedRead(uchar * readed, int size); - PIString constructFullPathDevice() const; - bool openDevice() {if (dev == 0) return false; return dev->open();} - bool closeDevice() {if (dev == 0) return false; return dev->close();} - DeviceInfoFlags deviceInfoFlags() const {if (dev) return dev->infoFlags(); return 0;} + virtual int readDevice(void * read_to, int max_size) override; + virtual int writeDevice(const void * data, int max_size) override; + virtual bool threadedRead(const uchar * readed, int size) override; + virtual PIString constructFullPathDevice() const override; + virtual bool openDevice() override; + virtual bool closeDevice() override; + virtual DeviceInfoFlags deviceInfoFlags() const override; PIIODevice * dev; PIByteArray buffer, tmpbuf, src_header, src_footer, trbuf; - PacketExtractorCheckFunc ret_func_header, ret_func_footer; + PacketExtractorHeaderFunc func_header; + PacketExtractorPayloadFunc func_payload; + PacketExtractorFooterFunc func_footer; SplitMode mode_; int buffer_size, dataSize, packetSize_hf, footerInd, packetSize_; double time_; diff --git a/libs/main/io_utils/pistreampacker.h b/libs/main/io_utils/pistreampacker.h index a6a71582..2963e500 100644 --- a/libs/main/io_utils/pistreampacker.h +++ b/libs/main/io_utils/pistreampacker.h @@ -80,7 +80,7 @@ public: //! and \a packetReceived() virtual method void received(const PIByteArray & data); - EVENT_HANDLER2(void, received, uchar * , readed, int, size); + EVENT_HANDLER2(void, received, const uchar * , readed, int, size); //! Connect \"dev\" \a PIIODevice::threadedReadEvent() event to \a received() handler //! and \a sendRequest() event to \"dev\" \a PIIODevice::write() handler diff --git a/libs/main/thread/pithread.cpp b/libs/main/thread/pithread.cpp index 47fabe6c..1f4e3d2b 100644 --- a/libs/main/thread/pithread.cpp +++ b/libs/main/thread/pithread.cpp @@ -962,7 +962,7 @@ void PIThread::runOnce(PIObject * object, const char * handler, const PIString & } #ifndef MICRO_PIP __PIThreadCollection::instance()->startedAuto(t); - CONNECTU(t, stopped, __PIThreadCollection::instance(), stoppedAuto); + CONNECT0(void, t, stopped, __PIThreadCollection::instance(), stoppedAuto); #endif t->startOnce(); } @@ -996,7 +996,7 @@ void PIThread::runOnce(std::function func, const PIString & name) { t->setSlot(func); #ifndef MICRO_PIP __PIThreadCollection::instance()->startedAuto(t); - CONNECTU(t, stopped, __PIThreadCollection::instance(), stoppedAuto); + CONNECT0(void, t, stopped, __PIThreadCollection::instance(), stoppedAuto); #endif t->startOnce(); } diff --git a/main_picloud_test.cpp b/main_picloud_test.cpp index 296577fc..f885ffa8 100644 --- a/main_picloud_test.cpp +++ b/main_picloud_test.cpp @@ -28,7 +28,7 @@ int main(int argc, char * argv[]) { } } })); - CONNECTL(&c, threadedReadEvent, ([&](uchar * readed, int size){ + CONNECTL(&c, threadedReadEvent, ([&](const uchar * readed, int size){ PIByteArray ba(readed, size); if (size < 1024) { PIString str = PIString(ba); @@ -41,7 +41,7 @@ int main(int argc, char * argv[]) { CONNECTL(&s, newConnection, ([&](PICloudServer::Client * cl){ piCout << "[Server] new client:" << cl; clients << cl; - CONNECTL(cl, threadedReadEvent, ([&c, &s, cl, &rnd](uchar * readed, int size){ + CONNECTL(cl, threadedReadEvent, ([&c, &s, cl, &rnd](const uchar * readed, int size){ PIByteArray ba(readed, size); PIString str = PIString(ba); piCout << "[Server] data from" << cl << ":" << str; diff --git a/main_tcp_server.cpp b/main_tcp_server.cpp index 58cac913..fc828b61 100644 --- a/main_tcp_server.cpp +++ b/main_tcp_server.cpp @@ -6,12 +6,12 @@ public: Client(PIEthernet * eth_) { eth = eth_; eth->startThreadedRead(); - CONNECTU(eth, threadedReadEvent, this, readed); - CONNECTU(eth, disconnected, this, disconnected); + CONNECT2(void, const uchar *, int, eth, threadedReadEvent, this, readed); + CONNECT1(void, bool, eth, disconnected, this, disconnected); piCoutObj << uint(eth) << "client connected"; } ~Client() {} - EVENT_HANDLER2(void, readed, uchar * , data, int, size) { + EVENT_HANDLER2(void, readed, const uchar *, data, int, size) { PIByteArray ba(data, size); piCoutObj << uint(eth) << "readed" << size << "bytes" << Hex << ba; eth->write(ba); @@ -37,7 +37,7 @@ public: Server(int port) { eth = new PIEthernet(PIEthernet::TCP_Server); eth->setParameter(PIEthernet::ReuseAddress); - CONNECTU(eth, newConnection, this, newConnection); + CONNECT1(void, PIEthernet *, eth, newConnection, this, newConnection); PIString path = "0.0.0.0:" + PIString::fromNumber(port); eth->listen(path, true); piCoutObj << uint(eth) << "server started" << path; @@ -50,7 +50,7 @@ public: EVENT_HANDLER1(void, newConnection, PIEthernet * , cl) { piCoutObj << uint(eth) << "add client"; Client * client = new Client(cl); - CONNECTU(client, disconnect, this, disconnect); + CONNECT1(void, Client *, client, disconnect, this, disconnect); clients.push_back(client); } EVENT_HANDLER1(void, disconnect, Client *, client) { diff --git a/utils/udp_file_transfer/main.cpp b/utils/udp_file_transfer/main.cpp index 67d46949..f06eecec 100644 --- a/utils/udp_file_transfer/main.cpp +++ b/utils/udp_file_transfer/main.cpp @@ -40,17 +40,17 @@ public: if (test_) { testt.setCRCEnabled(false); testt.setName("TEST"); - CONNECTU(&testt, sendRequest, this, ftsend); + CONNECT1(void, PIByteArray &, &testt, sendRequest, this, ftsend); } else { ft.setPacketSize(65000); ft.setName("PIFT"); - CONNECTU(&ft, sendRequest, this, ftsend); + CONNECT1(void, PIByteArray &, &ft, sendRequest, this, ftsend); CONNECTU(&ft, sendFilesStarted, this, ftevent); CONNECTU(&ft, receiveFilesStarted, this, ftevent); CONNECTU(&ft, sendFilesFinished, this, ftevent); CONNECTU(&ft, receiveFilesFinished, this, ftevent); } - CONNECTU(ð, threadedReadEvent, this, received); + CONNECT2(void, const uchar *, int, ð, threadedReadEvent, this, received); start(50); eth.setParameter(PIEthernet::SeparateSockets); eth.startThreadedRead(); @@ -136,7 +136,7 @@ private: eth.send(data); } - EVENT_HANDLER2(void, received, uchar * , readed, int, size) { + EVENT_HANDLER2(void, received, const uchar * , readed, int, size) { PIByteArray ba(readed, size); if(test_) { testt.received(ba);