From 8ba3d5dc8e12131db5bf9b4e4b9e236655389c6e 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: Sun, 20 Jan 2019 15:24:07 +0000 Subject: [PATCH] git-svn-id: svn://db.shs.com.ru/pip@680 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5 --- src_main/io_devices/piethernet.cpp | 33 ++++++++++++++++++++++++++---- src_main/io_devices/piethernet.h | 12 ++++++----- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src_main/io_devices/piethernet.cpp b/src_main/io_devices/piethernet.cpp index ba75e87c..44ed68b6 100755 --- a/src_main/io_devices/piethernet.cpp +++ b/src_main/io_devices/piethernet.cpp @@ -543,9 +543,30 @@ bool PIEthernet::leaveMulticastGroup(const PIString & group) { } -bool PIEthernet::connect() { - connecting_ = true; - return true; +bool PIEthernet::connect(bool threaded) { + if (threaded) { + connecting_ = true; + return true; + } + if (sock == -1) init(); + if (sock == -1) return false; + memset(&PRIVATE->addr_, 0, sizeof(PRIVATE->addr_)); + addr_r.set(path()); + PRIVATE->addr_.sin_port = htons(addr_r.port()); + PRIVATE->addr_.sin_addr.s_addr = addr_r.ip(); + PRIVATE->addr_.sin_family = AF_INET; +#ifdef QNX + PRIVATE->addr_.sin_len = sizeof(PRIVATE->addr_); +#endif + connected_ = (::connect(sock, (sockaddr * )&PRIVATE->addr_, sizeof(PRIVATE->addr_)) == 0); + if (!connected_) + piCoutObj << "Can`t connect to" << addr_r << "," << ethErrorString(); + opened_ = connected_; + if (connected_) { + connecting_ = false; + connected(); + } + return connected_; /*if (sock == -1) return false; memset(addr_, 0, sizeof(*addr_)); parseAddress(path_, &ip_, &port_); @@ -674,11 +695,13 @@ int PIEthernet::readDevice(void * read_to, int max_size) { //piCoutObj << "readed error" << lerr << errorString().data() << parameters()[DisonnectOnTimeout]; #ifdef WINDOWS if ((lerr == WSAEWOULDBLOCK || lerr == WSAETIMEDOUT) && !parameters()[DisonnectOnTimeout]) { +#elif ANDROID + if ((lerr == EWOULDBLOCK || lerr == EAGAIN || lerr == EINTR) && !parameters()[DisonnectOnTimeout]) { #else if ((lerr == EWOULDBLOCK || lerr == EAGAIN) && !parameters()[DisonnectOnTimeout]) { #endif //piCoutObj << errorString(); - piMSleep(10); + //piMSleep(10); return -1; } if (connected_) { @@ -828,6 +851,8 @@ void PIEthernet::server_func(void * eth) { int lerr = ethErrorCore(); #ifdef WINDOWS if (lerr == WSAETIMEDOUT) { +#elif ANDROID + if ((lerr == EAGAIN || lerr == EINTR)) { #else if (lerr == EAGAIN) { #endif diff --git a/src_main/io_devices/piethernet.h b/src_main/io_devices/piethernet.h index 2db57f0e..3fbfe9b2 100755 --- a/src_main/io_devices/piethernet.h +++ b/src_main/io_devices/piethernet.h @@ -252,17 +252,19 @@ public: const PIStringList & multicastGroups() const {return mcast_groups;} - //! Connect to TCP server with address \a readAddress(). Use only for TCP_Client - bool connect(); + //! If \"threaded\" queue connect to TCP server with address \a readAddress() in + //! any \a read() or \a write() call. Otherwise connect immediate. + //! Use only for TCP_Client + bool connect(bool threaded = true); //! Connect to TCP server with address "ip":"port". Use only for TCP_Client - bool connect(const PIString & ip, int port) {setPath(ip + PIStringAscii(":") + PIString::fromNumber(port)); return connect();} + bool connect(const PIString & ip, int port, bool threaded = true) {setPath(ip + PIStringAscii(":") + PIString::fromNumber(port)); return connect(threaded);} //! Connect to TCP server with address "ip_port". Use only for TCP_Client - bool connect(const PIString & ip_port) {setPath(ip_port); return connect();} + bool connect(const PIString & ip_port, bool threaded = true) {setPath(ip_port); return connect(threaded);} //! Connect to TCP server with address "addr". Use only for TCP_Client - bool connect(const Address & addr) {setPath(addr.toString()); return connect();} + bool connect(const Address & addr, bool threaded = true) {setPath(addr.toString()); return connect(threaded);} //! Returns if %PIEthernet connected to TCP server. Use only for TCP_Client bool isConnected() const {return connected_;}