BIG changes added

git-svn-id: svn://db.shs.com.ru/pip@128 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
2015-05-15 08:47:34 +00:00
parent 6283d78b03
commit 673a974c1e
3 changed files with 32 additions and 15 deletions

View File

@@ -87,7 +87,7 @@ PRIVATE_DEFINITION_END(PIEthernet)
PIEthernet::PIEthernet(): PIIODevice("", ReadWrite) { PIEthernet::PIEthernet(): PIIODevice("", ReadWrite) {
construct(); construct();
setType(UDP); setType(UDP);
setParameters(PIEthernet::ReuseAddress | PIEthernet::MulticastLoop); setParameters(PIEthernet::ReuseAddress | PIEthernet::MulticastLoop | PIEthernet::KeepConnection);
//if (type_ != UDP) init(); //if (type_ != UDP) init();
} }
@@ -106,16 +106,19 @@ PIEthernet::PIEthernet(int sock_, PIString ip_port): PIIODevice("", ReadWrite) {
parseAddress(ip_port, &ip_s, &port_s); parseAddress(ip_port, &ip_s, &port_s);
sock = sock_; sock = sock_;
init_ = opened_ = connected_ = true; init_ = opened_ = connected_ = true;
setParameters(PIEthernet::ReuseAddress | PIEthernet::MulticastLoop); setParameters(PIEthernet::ReuseAddress | PIEthernet::MulticastLoop | PIEthernet::KeepConnection);
setType(TCP_Client, false); setType(TCP_Client, false);
setPath(ip_port); setPath(ip_port);
//piCoutObj << "new tcp client" << sock_;
} }
PIEthernet::~PIEthernet() { PIEthernet::~PIEthernet() {
//piCoutObj << "~PIEthernet ...";
piMonitor.ethernets--; piMonitor.ethernets--;
stop(); stop();
closeDevice(); closeDevice();
//piCoutObj << "~PIEthernet done";
} }
@@ -161,11 +164,12 @@ bool PIEthernet::init() {
} }
if (params[PIEthernet::ReuseAddress]) ethSetsockoptBool(sock, SOL_SOCKET, SO_REUSEADDR); if (params[PIEthernet::ReuseAddress]) ethSetsockoptBool(sock, SOL_SOCKET, SO_REUSEADDR);
if (params[PIEthernet::Broadcast]) ethSetsockoptBool(sock, SOL_SOCKET, SO_BROADCAST); 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(); applyTimeouts();
applyOptInt(IPPROTO_IP, IP_TTL, TTL()); applyOptInt(IPPROTO_IP, IP_TTL, TTL());
//cout << "inited " << sock << ": bc = " << params << endl; //cout << "inited " << sock << ": bc = " << params << endl;
//fcntl(sock, F_SETFL, 0/*O_NONBLOCK*/); //fcntl(sock, F_SETFL, 0/*O_NONBLOCK*/);
//piCoutObj << "init" << sock;
return true; return true;
} }
@@ -230,11 +234,11 @@ bool PIEthernet::openDevice() {
#endif #endif
//piCout << "bind to" << (params[PIEthernet::Broadcast] ? "255.255.255.255" : ip_) << ":" << port_ << " ..."; //piCout << "bind to" << (params[PIEthernet::Broadcast] ? "255.255.255.255" : ip_) << ":" << port_ << " ...";
int tries = 0; 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(); init();
tries++; tries++;
} }
if (tries == 10) { if (tries == 2) {
piCoutObj << "Can`t bind to " << ip_ << ":" << port_ << ", " << ethErrorString(); piCoutObj << "Can`t bind to " << ip_ << ":" << port_ << ", " << ethErrorString();
return false; return false;
} }
@@ -261,9 +265,8 @@ bool PIEthernet::closeDevice() {
sock_s = -1; sock_s = -1;
closeSocket(sock); closeSocket(sock);
closeSocket(sock_s); closeSocket(sock_s);
piForeach (PIEthernet * i, clients_) while (!clients_.isEmpty())
delete i; delete clients_.back();
clients_.clear();
if (connected_) disconnected(false); if (connected_) disconnected(false);
connected_ = false; connected_ = false;
return true; return true;
@@ -437,11 +440,11 @@ bool PIEthernet::listen(bool threaded) {
#endif #endif
opened_ = false; opened_ = false;
int tries = 0; 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(); init();
tries++; tries++;
} }
if (tries == 10) { if (tries == 2) {
piCoutObj << "Can`t bind to " << ip_ << ":" << port_ << ", " << ethErrorString(); piCoutObj << "Can`t bind to " << ip_ << ":" << port_ << ", " << ethErrorString();
return false; return false;
} }
@@ -457,7 +460,7 @@ bool PIEthernet::listen(bool threaded) {
//#include <QDebug> //#include <QDebug>
int PIEthernet::read(void * read_to, int max_size) { int PIEthernet::read(void * read_to, int max_size) {
//cout << "read " << sock << endl; //piCout << "read" << sock;
if (sock == -1) init(); if (sock == -1) init();
if (sock == -1 || read_to == 0) return -1; if (sock == -1 || read_to == 0) return -1;
int rs = 0, s = 0, lerr = 0; int rs = 0, s = 0, lerr = 0;
@@ -478,6 +481,14 @@ int PIEthernet::read(void * read_to, int max_size) {
return rs; return rs;
case TCP_Client: case TCP_Client:
if (connecting_) { 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_)); memset(&PRIVATE->addr_, 0, sizeof(PRIVATE->addr_));
parseAddress(path(), &ip_, &port_); parseAddress(path(), &ip_, &port_);
PRIVATE->addr_.sin_port = htons(port_); PRIVATE->addr_.sin_port = htons(port_);
@@ -509,7 +520,7 @@ int PIEthernet::read(void * read_to, int max_size) {
//qDebug() << "readed" << rs; //qDebug() << "readed" << rs;
if (rs <= 0) { if (rs <= 0) {
lerr = ethErrorCore(); lerr = ethErrorCore();
//qDebug() << "readed error" << errorString().data(); //piCoutObj << "readed error" << lerr << errorString().data();
#ifdef WINDOWS #ifdef WINDOWS
if (lerr == WSAEWOULDBLOCK || /*lerr == NO_ERROR ||*/ lerr == WSAETIMEDOUT) { if (lerr == WSAEWOULDBLOCK || /*lerr == NO_ERROR ||*/ lerr == WSAETIMEDOUT) {
#else #else
@@ -524,6 +535,8 @@ int PIEthernet::read(void * read_to, int max_size) {
disconnected(rs < 0); disconnected(rs < 0);
} }
connected_ = false; connected_ = false;
if (parameters()[PIEthernet::KeepConnection])
connect();
//piCoutObj << "eth" << ip_ << "disconnected"; //piCoutObj << "eth" << ip_ << "disconnected";
} }
if (rs > 0) received(read_to, rs); 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) { void PIEthernet::ethClosesocket(int sock, bool shutdown) {
//piCout << "close socket" << sock << shutdown;
if (sock < 0) return; if (sock < 0) return;
if (shutdown) ::shutdown(sock, if (shutdown) ::shutdown(sock,
#ifdef WINDOWS #ifdef WINDOWS

View File

@@ -51,11 +51,12 @@ public:
Broadcast /** Broadcast send. Disabled by default */ = 0x2, Broadcast /** Broadcast send. Disabled by default */ = 0x2,
SeparateSockets /** If this parameter is set, %PIEthernet will initialize two different sockets, SeparateSockets /** If this parameter is set, %PIEthernet will initialize two different sockets,
for receive and send, instead of single one. Disabled by default */ = 0x4, 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" //! Contructs %PIEthernet with type "type", read address "ip_port" and parameters "params"
explicit PIEthernet(Type type, const PIString & ip_port = PIString(), const PIFlags<Parameters> params = 0); explicit PIEthernet(Type type, const PIString & ip_port = PIString(), const PIFlags<Parameters> params = PIEthernet::ReuseAddress | PIEthernet::MulticastLoop | PIEthernet::KeepConnection);
virtual ~PIEthernet(); virtual ~PIEthernet();

View File

@@ -125,6 +125,7 @@ bool PIThread::start(int timer_delay) {
# ifndef ANDROID # ifndef ANDROID
//pthread_attr_setschedparam(&attr, &sparam); //pthread_attr_setschedparam(&attr, &sparam);
# endif # endif
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
int ret = pthread_create(&thread, &attr, thread_function, this); int ret = pthread_create(&thread, &attr, thread_function, this);
pthread_attr_destroy(&attr); pthread_attr_destroy(&attr);
if (ret == 0) { if (ret == 0) {
@@ -154,6 +155,7 @@ bool PIThread::startOnce() {
# ifndef ANDROID # ifndef ANDROID
//pthread_attr_setschedparam(&attr, &sparam); //pthread_attr_setschedparam(&attr, &sparam);
# endif # endif
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
int ret = pthread_create(&thread, &attr, thread_function_once, this); int ret = pthread_create(&thread, &attr, thread_function_once, this);
pthread_attr_destroy(&attr); pthread_attr_destroy(&attr);
if (ret == 0) { if (ret == 0) {
@@ -184,7 +186,7 @@ void PIThread::terminate() {
//pthread_kill(thread, SIGKILL); //pthread_kill(thread, SIGKILL);
void * ret(0); void * ret(0);
pthread_cancel(thread); pthread_cancel(thread);
pthread_join(thread, &ret); //pthread_join(thread, &ret);
# endif # endif
#else #else
TerminateThread(thread, 0); TerminateThread(thread, 0);