From 673a974c1ee408d0b3f932a5d33b4bbdcb3eb963 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B5=D0=BB=D0=B8=D0=BF=D0=B5=D0=BD=D0=BA=D0=BE=20?= =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD?= Date: Fri, 15 May 2015 08:47:34 +0000 Subject: [PATCH] BIG changes added git-svn-id: svn://db.shs.com.ru/pip@128 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5 --- src/io/piethernet.cpp | 38 ++++++++++++++++++++++++++------------ src/io/piethernet.h | 5 +++-- src/thread/pithread.cpp | 4 +++- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/io/piethernet.cpp b/src/io/piethernet.cpp index ae2801d8..30aa91a1 100755 --- a/src/io/piethernet.cpp +++ b/src/io/piethernet.cpp @@ -87,7 +87,7 @@ PRIVATE_DEFINITION_END(PIEthernet) PIEthernet::PIEthernet(): PIIODevice("", ReadWrite) { construct(); setType(UDP); - setParameters(PIEthernet::ReuseAddress | PIEthernet::MulticastLoop); + setParameters(PIEthernet::ReuseAddress | PIEthernet::MulticastLoop | PIEthernet::KeepConnection); //if (type_ != UDP) init(); } @@ -106,16 +106,19 @@ PIEthernet::PIEthernet(int sock_, PIString ip_port): PIIODevice("", ReadWrite) { parseAddress(ip_port, &ip_s, &port_s); sock = sock_; init_ = opened_ = connected_ = true; - setParameters(PIEthernet::ReuseAddress | PIEthernet::MulticastLoop); + setParameters(PIEthernet::ReuseAddress | PIEthernet::MulticastLoop | PIEthernet::KeepConnection); setType(TCP_Client, false); setPath(ip_port); + //piCoutObj << "new tcp client" << sock_; } PIEthernet::~PIEthernet() { + //piCoutObj << "~PIEthernet ..."; piMonitor.ethernets--; stop(); closeDevice(); + //piCoutObj << "~PIEthernet done"; } @@ -161,11 +164,12 @@ bool PIEthernet::init() { } if (params[PIEthernet::ReuseAddress]) ethSetsockoptBool(sock, SOL_SOCKET, SO_REUSEADDR); if (params[PIEthernet::Broadcast]) ethSetsockoptBool(sock, SOL_SOCKET, SO_BROADCAST); - if (type() == PIEthernet::TCP_Client) ethSetsockoptBool(sock, SOL_SOCKET, SO_KEEPALIVE); + //if (type() == PIEthernet::TCP_Client) ethSetsockoptBool(sock, SOL_SOCKET, SO_KEEPALIVE); applyTimeouts(); applyOptInt(IPPROTO_IP, IP_TTL, TTL()); //cout << "inited " << sock << ": bc = " << params << endl; //fcntl(sock, F_SETFL, 0/*O_NONBLOCK*/); + //piCoutObj << "init" << sock; return true; } @@ -230,11 +234,11 @@ bool PIEthernet::openDevice() { #endif //piCout << "bind to" << (params[PIEthernet::Broadcast] ? "255.255.255.255" : ip_) << ":" << port_ << " ..."; int tries = 0; - while ((bind(sock, (sockaddr * )&PRIVATE->addr_, sizeof(PRIVATE->addr_)) == -1) && (tries < 10)) { + while ((bind(sock, (sockaddr * )&PRIVATE->addr_, sizeof(PRIVATE->addr_)) == -1) && (tries < 2)) { init(); tries++; } - if (tries == 10) { + if (tries == 2) { piCoutObj << "Can`t bind to " << ip_ << ":" << port_ << ", " << ethErrorString(); return false; } @@ -261,9 +265,8 @@ bool PIEthernet::closeDevice() { sock_s = -1; closeSocket(sock); closeSocket(sock_s); - piForeach (PIEthernet * i, clients_) - delete i; - clients_.clear(); + while (!clients_.isEmpty()) + delete clients_.back(); if (connected_) disconnected(false); connected_ = false; return true; @@ -437,11 +440,11 @@ bool PIEthernet::listen(bool threaded) { #endif opened_ = false; int tries = 0; - while ((bind(sock, (sockaddr * )&PRIVATE->addr_, sizeof(PRIVATE->addr_)) == -1) && (tries < 10)) { + while ((bind(sock, (sockaddr * )&PRIVATE->addr_, sizeof(PRIVATE->addr_)) == -1) && (tries < 2)) { init(); tries++; } - if (tries == 10) { + if (tries == 2) { piCoutObj << "Can`t bind to " << ip_ << ":" << port_ << ", " << ethErrorString(); return false; } @@ -457,7 +460,7 @@ bool PIEthernet::listen(bool threaded) { //#include int PIEthernet::read(void * read_to, int max_size) { - //cout << "read " << sock << endl; + //piCout << "read" << sock; if (sock == -1) init(); if (sock == -1 || read_to == 0) return -1; int rs = 0, s = 0, lerr = 0; @@ -478,6 +481,14 @@ int PIEthernet::read(void * read_to, int max_size) { return rs; case TCP_Client: if (connecting_) { +#ifdef ANDROID + /*if (sock_s == sock) + sock_s = -1; + closeSocket(sock); + closeSocket(sock_s); + init(); + qDebug() << "init() in read thread";*/ +#endif memset(&PRIVATE->addr_, 0, sizeof(PRIVATE->addr_)); parseAddress(path(), &ip_, &port_); PRIVATE->addr_.sin_port = htons(port_); @@ -509,7 +520,7 @@ int PIEthernet::read(void * read_to, int max_size) { //qDebug() << "readed" << rs; if (rs <= 0) { lerr = ethErrorCore(); - //qDebug() << "readed error" << errorString().data(); + //piCoutObj << "readed error" << lerr << errorString().data(); #ifdef WINDOWS if (lerr == WSAEWOULDBLOCK || /*lerr == NO_ERROR ||*/ lerr == WSAETIMEDOUT) { #else @@ -524,6 +535,8 @@ int PIEthernet::read(void * read_to, int max_size) { disconnected(rs < 0); } connected_ = false; + if (parameters()[PIEthernet::KeepConnection]) + connect(); //piCoutObj << "eth" << ip_ << "disconnected"; } if (rs > 0) received(read_to, rs); @@ -1036,6 +1049,7 @@ int PIEthernet::ethSendto(int sock, const void * buf, int size, int flags, socka void PIEthernet::ethClosesocket(int sock, bool shutdown) { + //piCout << "close socket" << sock << shutdown; if (sock < 0) return; if (shutdown) ::shutdown(sock, #ifdef WINDOWS diff --git a/src/io/piethernet.h b/src/io/piethernet.h index 45884272..ed2afa8d 100755 --- a/src/io/piethernet.h +++ b/src/io/piethernet.h @@ -51,11 +51,12 @@ public: Broadcast /** Broadcast send. Disabled by default */ = 0x2, SeparateSockets /** If this parameter is set, %PIEthernet will initialize two different sockets, for receive and send, instead of single one. Disabled by default */ = 0x4, - MulticastLoop /** Enable receiving multicast packets from same host. Enabled by default */ = 0x8 + MulticastLoop /** Enable receiving multicast packets from same host. Enabled by default */ = 0x8, + KeepConnection /** Automatic reconnect TCP connection on disconnect. Enabled by default */ = 0x10 }; //! Contructs %PIEthernet with type "type", read address "ip_port" and parameters "params" - explicit PIEthernet(Type type, const PIString & ip_port = PIString(), const PIFlags params = 0); + explicit PIEthernet(Type type, const PIString & ip_port = PIString(), const PIFlags params = PIEthernet::ReuseAddress | PIEthernet::MulticastLoop | PIEthernet::KeepConnection); virtual ~PIEthernet(); diff --git a/src/thread/pithread.cpp b/src/thread/pithread.cpp index ecd87e75..0ae84c6d 100755 --- a/src/thread/pithread.cpp +++ b/src/thread/pithread.cpp @@ -125,6 +125,7 @@ bool PIThread::start(int timer_delay) { # ifndef ANDROID //pthread_attr_setschedparam(&attr, &sparam); # endif + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); int ret = pthread_create(&thread, &attr, thread_function, this); pthread_attr_destroy(&attr); if (ret == 0) { @@ -154,6 +155,7 @@ bool PIThread::startOnce() { # ifndef ANDROID //pthread_attr_setschedparam(&attr, &sparam); # endif + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); int ret = pthread_create(&thread, &attr, thread_function_once, this); pthread_attr_destroy(&attr); if (ret == 0) { @@ -184,7 +186,7 @@ void PIThread::terminate() { //pthread_kill(thread, SIGKILL); void * ret(0); pthread_cancel(thread); - pthread_join(thread, &ret); + //pthread_join(thread, &ret); # endif #else TerminateThread(thread, 0);