From bdd18b614f5a97144097c815ba5cfe2077bccbfa Mon Sep 17 00:00:00 2001 From: peri4 Date: Tue, 17 Sep 2024 16:11:18 +0300 Subject: [PATCH] PIEthernet more accuracy construct PIThread windows fix --- libs/main/core/pibase.h | 19 ++++++++++++++++--- libs/main/io_devices/piethernet.cpp | 20 ++++++++++---------- libs/main/io_devices/piethernet.h | 6 +++--- libs/main/thread/pithread.cpp | 1 + 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/libs/main/core/pibase.h b/libs/main/core/pibase.h index 6ef208de..ec4491f8 100644 --- a/libs/main/core/pibase.h +++ b/libs/main/core/pibase.h @@ -677,15 +677,28 @@ public: //! \~\brief //! \~english Destructor that executes the function if it exists //! \~russian Деструктор, который выполняет функцию, если она существует - ~PIScopeExitCall() { - if (func) func(); - } + ~PIScopeExitCall() { call(); } //! \~\brief //! \~english Method for canceling the function //! \~russian Метод для отмены функции void cancel() { func = nullptr; } + //! \~\brief + //! \~english Method for call the function + //! \~russian Метод для вызова функции + void call() { + if (func) func(); + } + + //! \~\brief + //! \~english Method for call and canceling the function + //! \~russian Метод для вызова и отмены функции + void callAndCancel() { + call(); + cancel(); + } + private: NO_COPY_CLASS(PIScopeExitCall) diff --git a/libs/main/io_devices/piethernet.cpp b/libs/main/io_devices/piethernet.cpp index d91fe792..78757c26 100644 --- a/libs/main/io_devices/piethernet.cpp +++ b/libs/main/io_devices/piethernet.cpp @@ -119,7 +119,8 @@ PRIVATE_DEFINITION_END(PIEthernet) PIEthernet::PIEthernet(): PIIODevice("", ReadWrite) { construct(); - setType(UDP); + eth_type = UDP; + setProperty("type", (int)UDP); setParameters(PIEthernet::ReuseAddress | PIEthernet::MulticastLoop | PIEthernet::KeepConnection); } @@ -128,7 +129,8 @@ PIEthernet::PIEthernet(PIEthernet::Type type_, const PIString & ip_port, const P : PIIODevice(ip_port, ReadWrite) { construct(); addr_r.set(ip_port); - setType(type_); + eth_type = type_; + setProperty("type", (int)type_); setParameters(params_); if (type_ != UDP) init(); } @@ -140,9 +142,10 @@ PIEthernet::PIEthernet(int sock_, PIString ip_port): PIIODevice("", ReadWrite) { sock = sock_; opened_ = connected_ = true; is_server_client = true; + eth_type = TCP_Client; + setProperty("type", (int)TCP_Client); init(); setParameters(PIEthernet::ReuseAddress | PIEthernet::MulticastLoop); - setType(TCP_Client, false); setPath(ip_port); ethNonblocking(sock); PRIVATE->event.create(); @@ -162,8 +165,6 @@ PIEthernet::~PIEthernet() { void PIEthernet::construct() { // piCout << " PIEthernet" << uint(this); setOption(BlockingWrite); - connected_ = connecting_ = listen_threaded = server_bounded = false; - sock = sock_s = -1; setReadTimeout(10_s); setWriteTimeout(10_s); setTTL(64); @@ -179,9 +180,9 @@ void PIEthernet::construct() { } -bool PIEthernet::init() { - if (isOpened() || is_server_client) return true; - if (sock != -1) return true; +void PIEthernet::init() { + if (isOpened() || is_server_client) return; + if (sock != -1) return; // piCout << "init " << type(); PRIVATE->event.destroy(); if (sock_s == sock) sock_s = -1; @@ -204,13 +205,12 @@ bool PIEthernet::init() { sock_s = sock; if (sock == -1 || sock_s == -1) { piCoutObj << "Can`t create socket," << ethErrorString(); - return false; + return; } applyParameters(); applyTimeouts(); applyOptInt(IPPROTO_IP, IP_TTL, TTL()); // piCoutObj << "inited" << path(); - return true; } diff --git a/libs/main/io_devices/piethernet.h b/libs/main/io_devices/piethernet.h index 3015fb3d..7b16d0b1 100644 --- a/libs/main/io_devices/piethernet.h +++ b/libs/main/io_devices/piethernet.h @@ -472,7 +472,7 @@ protected: virtual void received(const void * data, int size) { ; } void construct(); - bool init(); + void init(); bool openDevice() override; bool closeDevice() override; void closeSocket(int & sd); @@ -481,8 +481,8 @@ protected: void applyOptInt(int level, int opt, int val); PRIVATE_DECLARATION(PIP_EXPORT) - int sock, sock_s; - std::atomic_bool connected_, connecting_, listen_threaded, server_bounded; + int sock = -1, sock_s = -1; + std::atomic_bool connected_ = {false}, connecting_ = {false}, listen_threaded = {false}, server_bounded = {false}; bool is_server_client = false; mutable PINetworkAddress addr_r, addr_s, addr_lr; Type eth_type; diff --git a/libs/main/thread/pithread.cpp b/libs/main/thread/pithread.cpp index c1810e68..afdef1f3 100644 --- a/libs/main/thread/pithread.cpp +++ b/libs/main/thread/pithread.cpp @@ -937,6 +937,7 @@ void PIThread::_endThread() { UNREGISTER_THREAD(this); PIINTROSPECTION_THREAD_STOP(this); #if defined(WINDOWS) + ec.callAndCancel(); # ifdef CC_GCC _endthreadex(0); # else