Commit 60884957 authored by Pavel Vainerman's avatar Pavel Vainerman

(TCP): добавил функцию setKeepAliveParams() позволяющую задать

"тонкие" настройки по отслеживанию "живости соединения", а также мелкие правки.
parent ffbaa93f
...@@ -80,7 +80,7 @@ int main( int argc, char* argv[], char* envp[] ) ...@@ -80,7 +80,7 @@ int main( int argc, char* argv[], char* envp[] )
if( r > 0 ) if( r > 0 )
{ {
buf[r] = '\0'; buf[r] = '\0';
(*(log.get())) << buf; log->any() << buf;
} }
} }
} }
......
...@@ -91,7 +91,7 @@ int main( int argc, char* argv[], char* envp[] ) ...@@ -91,7 +91,7 @@ int main( int argc, char* argv[], char* envp[] )
if( r > 0 ) if( r > 0 )
{ {
buf[r] = '\0'; buf[r] = '\0';
(*(zlog.get())) << buf; zlog->any() << buf;
} }
} }
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
Name: libuniset2 Name: libuniset2
Version: 2.1 Version: 2.1
Release: alt13 Release: alt14
Summary: UniSet - library for building distributed industrial control systems Summary: UniSet - library for building distributed industrial control systems
...@@ -456,6 +456,9 @@ mv -f %buildroot%python_sitelibdir_noarch/* %buildroot%python_sitelibdir/%oname ...@@ -456,6 +456,9 @@ mv -f %buildroot%python_sitelibdir_noarch/* %buildroot%python_sitelibdir/%oname
# .. # ..
%changelog %changelog
* Sun Aug 09 2015 Pavel Vainerman <pv@altlinux.ru> 2.1-alt14
- (UTCPStream): add new function "setKeepAliveParams"
* Sat Aug 08 2015 Pavel Vainerman <pv@altlinux.ru> 2.1-alt13 * Sat Aug 08 2015 Pavel Vainerman <pv@altlinux.ru> 2.1-alt13
- (ModbusMultiMaster): minor fixes (add "force" for <GateList>) - (ModbusMultiMaster): minor fixes (add "force" for <GateList>)
......
...@@ -368,7 +368,6 @@ void MBTCPMultiMaster::check_thread() ...@@ -368,7 +368,6 @@ void MBTCPMultiMaster::check_thread()
bool r = it->check(); bool r = it->check();
mbinfo << myname << "(check): " << it->myname << " " << ( r ? "OK" : "FAIL" ) << endl; mbinfo << myname << "(check): " << it->myname << " " << ( r ? "OK" : "FAIL" ) << endl;
cerr << "respond_init=" << it->respond_init << endl;
try try
{ {
......
...@@ -15,7 +15,11 @@ class UTCPStream: ...@@ -15,7 +15,11 @@ class UTCPStream:
void create( const std::string& hname, int port, bool throwflag = false, timeout_t timer = 0 ); void create( const std::string& hname, int port, bool throwflag = false, timeout_t timer = 0 );
// set keepalive params
void setKeepAliveParams( timeout_t timeout_sec = 5, int conn_keepcnt = 1, int keepintvl=2 );
protected: protected:
private: private:
}; };
......
...@@ -105,7 +105,7 @@ mbErrCode ModbusTCPMaster::query( ModbusAddr addr, ModbusMessage& msg, ...@@ -105,7 +105,7 @@ mbErrCode ModbusTCPMaster::query( ModbusAddr addr, ModbusMessage& msg,
if( dlog->is_info() ) if( dlog->is_info() )
{ {
dlog->info() << iaddr << "(ModbusTCPMaster::query): send tcp header(" << sizeof(mh) << "): "; dlog->info() << iaddr << "(ModbusTCPMaster::query): send tcp header(" << sizeof(mh) << "): ";
mbPrintMessage( *(dlog.get()), (ModbusByte*)(&mh), sizeof(mh)); mbPrintMessage( dlog->info(), (ModbusByte*)(&mh), sizeof(mh));
dlog->info() << endl; dlog->info() << endl;
} }
...@@ -177,7 +177,7 @@ mbErrCode ModbusTCPMaster::query( ModbusAddr addr, ModbusMessage& msg, ...@@ -177,7 +177,7 @@ mbErrCode ModbusTCPMaster::query( ModbusAddr addr, ModbusMessage& msg,
if( dlog->is_info() ) if( dlog->is_info() )
{ {
dlog->info() << "(ModbusTCPMaster::query): recv tcp header(" << ret << "): "; dlog->info() << "(ModbusTCPMaster::query): recv tcp header(" << ret << "): ";
mbPrintMessage( *(dlog.get()), (ModbusByte*)(&rmh), sizeof(rmh)); mbPrintMessage( dlog->info(), (ModbusByte*)(&rmh), sizeof(rmh));
dlog->info(false) << endl; dlog->info(false) << endl;
} }
...@@ -306,6 +306,7 @@ bool ModbusTCPMaster::checkConnection( const std::string& ip, int port, int time ...@@ -306,6 +306,7 @@ bool ModbusTCPMaster::checkConnection( const std::string& ip, int port, int time
// Проверяем просто попыткой создать соединение.. // Проверяем просто попыткой создать соединение..
UTCPStream t; UTCPStream t;
t.create(ip, port, true, timeout_msec); t.create(ip, port, true, timeout_msec);
t.setKeepAliveParams( (timeout_msec > 1000 ? timeout_msec/1000 : 1),1,1);
t.disconnect(); t.disconnect();
return true; return true;
} }
...@@ -332,6 +333,7 @@ void ModbusTCPMaster::reconnect() ...@@ -332,6 +333,7 @@ void ModbusTCPMaster::reconnect()
tcp = make_shared<UTCPStream>(); tcp = make_shared<UTCPStream>();
tcp->create(iaddr, port, true, 500); tcp->create(iaddr, port, true, 500);
tcp->setTimeout(replyTimeOut_ms); tcp->setTimeout(replyTimeOut_ms);
tcp->setKeepAliveParams((replyTimeOut_ms > 1000 ? replyTimeOut_ms/1000 : 1));
} }
catch( const std::exception& e ) catch( const std::exception& e )
{ {
...@@ -385,6 +387,7 @@ void ModbusTCPMaster::connect( ost::InetAddress addr, int _port ) ...@@ -385,6 +387,7 @@ void ModbusTCPMaster::connect( ost::InetAddress addr, int _port )
tcp = make_shared<UTCPStream>(); tcp = make_shared<UTCPStream>();
tcp->create(iaddr, port, true, 500); tcp->create(iaddr, port, true, 500);
tcp->setTimeout(replyTimeOut_ms); tcp->setTimeout(replyTimeOut_ms);
tcp->setKeepAliveParams((replyTimeOut_ms > 1000 ? replyTimeOut_ms/1000 : 1));
} }
catch( const std::exception& e ) catch( const std::exception& e )
{ {
......
...@@ -288,8 +288,8 @@ mbErrCode ModbusTCPServer::tcp_processing( ost::TCPStream& tcp, ModbusTCP::MBAPH ...@@ -288,8 +288,8 @@ mbErrCode ModbusTCPServer::tcp_processing( ost::TCPStream& tcp, ModbusTCP::MBAPH
if( dlog->is_info() ) if( dlog->is_info() )
{ {
dlog->info() << "(ModbusTCPServer::tcp_processing): recv tcp header(" << len << "): "; dlog->info() << "(ModbusTCPServer::tcp_processing): recv tcp header(" << len << "): ";
mbPrintMessage( *(dlog.get()), (ModbusByte*)(&mhead), sizeof(mhead)); mbPrintMessage( dlog->info(), (ModbusByte*)(&mhead), sizeof(mhead));
(*(dlog.get()))(Debug::INFO) << endl; dlog->info() << endl;
} }
// check header // check header
...@@ -331,8 +331,8 @@ mbErrCode ModbusTCPServer::pre_send_request( ModbusMessage& request ) ...@@ -331,8 +331,8 @@ mbErrCode ModbusTCPServer::pre_send_request( ModbusMessage& request )
if( dlog->is_info() ) if( dlog->is_info() )
{ {
dlog->info() << "(ModbusTCPServer::pre_send_request): send tcp header: "; dlog->info() << "(ModbusTCPServer::pre_send_request): send tcp header: ";
mbPrintMessage( *(dlog.get()), (ModbusByte*)(&curQueryHeader), sizeof(curQueryHeader)); mbPrintMessage( dlog->info(), (ModbusByte*)(&curQueryHeader), sizeof(curQueryHeader));
(*(dlog.get()))(Debug::INFO) << endl; dlog->info() << endl;
} }
tcp << curQueryHeader; tcp << curQueryHeader;
......
...@@ -212,8 +212,8 @@ mbErrCode ModbusTCPSession::tcp_processing( ost::TCPStream& tcp, ModbusTCP::MBAP ...@@ -212,8 +212,8 @@ mbErrCode ModbusTCPSession::tcp_processing( ost::TCPStream& tcp, ModbusTCP::MBAP
if( dlog->is_info() ) if( dlog->is_info() )
{ {
dlog->info() << peername << "(tcp_processing): recv tcp header(" << len << "): "; dlog->info() << peername << "(tcp_processing): recv tcp header(" << len << "): ";
mbPrintMessage( *(dlog.get()), (ModbusByte*)(&mhead), sizeof(mhead)); mbPrintMessage( dlog->info(), (ModbusByte*)(&mhead), sizeof(mhead));
(*(dlog.get()))(Debug::INFO) << endl; dlog->info() << endl;
} }
// check header // check header
...@@ -255,8 +255,8 @@ mbErrCode ModbusTCPSession::pre_send_request( ModbusMessage& request ) ...@@ -255,8 +255,8 @@ mbErrCode ModbusTCPSession::pre_send_request( ModbusMessage& request )
if( dlog->is_info() ) if( dlog->is_info() )
{ {
dlog->info() << peername << "(pre_send_request): send tcp header: "; dlog->info() << peername << "(pre_send_request): send tcp header: ";
mbPrintMessage( *(dlog.get()), (ModbusByte*)(&curQueryHeader), sizeof(curQueryHeader)); mbPrintMessage( dlog->info(), (ModbusByte*)(&curQueryHeader), sizeof(curQueryHeader));
(*(dlog.get()))(Debug::INFO) << endl; dlog->info() << endl;
} }
*tcp() << curQueryHeader; *tcp() << curQueryHeader;
......
...@@ -60,6 +60,7 @@ void TCPCheck::check_thread() ...@@ -60,6 +60,7 @@ void TCPCheck::check_thread()
ost::Thread::setException(ost::Thread::throwException); ost::Thread::setException(ost::Thread::throwException);
UTCPStream t; UTCPStream t;
t.create(ip, port, true, tout_msec); t.create(ip, port, true, tout_msec);
t.setKeepAliveParams( (tout_msec > 1000 ? tout_msec/1000 : 1) );
setResult(true); setResult(true);
t.disconnect(); t.disconnect();
} }
......
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
#include "UTCPStream.h" #include "UTCPStream.h"
#include "PassiveTimer.h" #include "PassiveTimer.h"
#include "UniSetTypes.h" #include "UniSetTypes.h"
// glibc..
#include <netinet/tcp.h>
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
using namespace std; using namespace std;
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
...@@ -20,6 +23,16 @@ UTCPStream::UTCPStream(): ...@@ -20,6 +23,16 @@ UTCPStream::UTCPStream():
{ {
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
void UTCPStream::setKeepAliveParams(timeout_t timeout_sec, int keepcnt, int keepintvl )
{
SOCKET fd = TCPStream::so;
int enable = 1;
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,(void*)&enable,sizeof(enable));
setsockopt(fd, SOL_TCP, TCP_KEEPCNT, (void *) &keepcnt, sizeof(keepcnt));
setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, (void*) &keepintvl, sizeof (keepintvl));
setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, (void*) &timeout_sec, sizeof (timeout_sec));
}
// -------------------------------------------------------------------------
void UTCPStream::create( const std::string& hname, int port, bool throwflag, timeout_t t ) void UTCPStream::create( const std::string& hname, int port, bool throwflag, timeout_t t )
{ {
family = ost::Socket::IPV4; family = ost::Socket::IPV4;
...@@ -30,6 +43,6 @@ void UTCPStream::create( const std::string& hname, int port, bool throwflag, tim ...@@ -30,6 +43,6 @@ void UTCPStream::create( const std::string& hname, int port, bool throwflag, tim
connect(h, port, mss); connect(h, port, mss);
setKeepAlive(true); setKeepAlive(true);
setLinger(true); setLinger(true);
//setCompletion(false); setKeepAliveParams();
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment