PIEthernet more accuracy construct

PIThread windows fix
This commit is contained in:
2024-09-17 16:11:18 +03:00
parent e186e0adff
commit bdd18b614f
4 changed files with 30 additions and 16 deletions

View File

@@ -677,15 +677,28 @@ public:
//! \~\brief //! \~\brief
//! \~english Destructor that executes the function if it exists //! \~english Destructor that executes the function if it exists
//! \~russian Деструктор, который выполняет функцию, если она существует //! \~russian Деструктор, который выполняет функцию, если она существует
~PIScopeExitCall() { ~PIScopeExitCall() { call(); }
if (func) func();
}
//! \~\brief //! \~\brief
//! \~english Method for canceling the function //! \~english Method for canceling the function
//! \~russian Метод для отмены функции //! \~russian Метод для отмены функции
void cancel() { func = nullptr; } 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: private:
NO_COPY_CLASS(PIScopeExitCall) NO_COPY_CLASS(PIScopeExitCall)

View File

@@ -119,7 +119,8 @@ PRIVATE_DEFINITION_END(PIEthernet)
PIEthernet::PIEthernet(): PIIODevice("", ReadWrite) { PIEthernet::PIEthernet(): PIIODevice("", ReadWrite) {
construct(); construct();
setType(UDP); eth_type = UDP;
setProperty("type", (int)UDP);
setParameters(PIEthernet::ReuseAddress | PIEthernet::MulticastLoop | PIEthernet::KeepConnection); 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) { : PIIODevice(ip_port, ReadWrite) {
construct(); construct();
addr_r.set(ip_port); addr_r.set(ip_port);
setType(type_); eth_type = type_;
setProperty("type", (int)type_);
setParameters(params_); setParameters(params_);
if (type_ != UDP) init(); if (type_ != UDP) init();
} }
@@ -140,9 +142,10 @@ PIEthernet::PIEthernet(int sock_, PIString ip_port): PIIODevice("", ReadWrite) {
sock = sock_; sock = sock_;
opened_ = connected_ = true; opened_ = connected_ = true;
is_server_client = true; is_server_client = true;
eth_type = TCP_Client;
setProperty("type", (int)TCP_Client);
init(); init();
setParameters(PIEthernet::ReuseAddress | PIEthernet::MulticastLoop); setParameters(PIEthernet::ReuseAddress | PIEthernet::MulticastLoop);
setType(TCP_Client, false);
setPath(ip_port); setPath(ip_port);
ethNonblocking(sock); ethNonblocking(sock);
PRIVATE->event.create(); PRIVATE->event.create();
@@ -162,8 +165,6 @@ PIEthernet::~PIEthernet() {
void PIEthernet::construct() { void PIEthernet::construct() {
// piCout << " PIEthernet" << uint(this); // piCout << " PIEthernet" << uint(this);
setOption(BlockingWrite); setOption(BlockingWrite);
connected_ = connecting_ = listen_threaded = server_bounded = false;
sock = sock_s = -1;
setReadTimeout(10_s); setReadTimeout(10_s);
setWriteTimeout(10_s); setWriteTimeout(10_s);
setTTL(64); setTTL(64);
@@ -179,9 +180,9 @@ void PIEthernet::construct() {
} }
bool PIEthernet::init() { void PIEthernet::init() {
if (isOpened() || is_server_client) return true; if (isOpened() || is_server_client) return;
if (sock != -1) return true; if (sock != -1) return;
// piCout << "init " << type(); // piCout << "init " << type();
PRIVATE->event.destroy(); PRIVATE->event.destroy();
if (sock_s == sock) sock_s = -1; if (sock_s == sock) sock_s = -1;
@@ -204,13 +205,12 @@ bool PIEthernet::init() {
sock_s = sock; sock_s = sock;
if (sock == -1 || sock_s == -1) { if (sock == -1 || sock_s == -1) {
piCoutObj << "Can`t create socket," << ethErrorString(); piCoutObj << "Can`t create socket," << ethErrorString();
return false; return;
} }
applyParameters(); applyParameters();
applyTimeouts(); applyTimeouts();
applyOptInt(IPPROTO_IP, IP_TTL, TTL()); applyOptInt(IPPROTO_IP, IP_TTL, TTL());
// piCoutObj << "inited" << path(); // piCoutObj << "inited" << path();
return true;
} }

View File

@@ -472,7 +472,7 @@ protected:
virtual void received(const void * data, int size) { ; } virtual void received(const void * data, int size) { ; }
void construct(); void construct();
bool init(); void init();
bool openDevice() override; bool openDevice() override;
bool closeDevice() override; bool closeDevice() override;
void closeSocket(int & sd); void closeSocket(int & sd);
@@ -481,8 +481,8 @@ protected:
void applyOptInt(int level, int opt, int val); void applyOptInt(int level, int opt, int val);
PRIVATE_DECLARATION(PIP_EXPORT) PRIVATE_DECLARATION(PIP_EXPORT)
int sock, sock_s; int sock = -1, sock_s = -1;
std::atomic_bool connected_, connecting_, listen_threaded, server_bounded; std::atomic_bool connected_ = {false}, connecting_ = {false}, listen_threaded = {false}, server_bounded = {false};
bool is_server_client = false; bool is_server_client = false;
mutable PINetworkAddress addr_r, addr_s, addr_lr; mutable PINetworkAddress addr_r, addr_s, addr_lr;
Type eth_type; Type eth_type;

View File

@@ -937,6 +937,7 @@ void PIThread::_endThread() {
UNREGISTER_THREAD(this); UNREGISTER_THREAD(this);
PIINTROSPECTION_THREAD_STOP(this); PIINTROSPECTION_THREAD_STOP(this);
#if defined(WINDOWS) #if defined(WINDOWS)
ec.callAndCancel();
# ifdef CC_GCC # ifdef CC_GCC
_endthreadex(0); _endthreadex(0);
# else # else