Commit 30945503 authored by Pavel Vainerman's avatar Pavel Vainerman

(LogServer): попытка решить проблемму с SEGFAULT,

немного переписал работу с shared_ptr
parent 6fd4537e
...@@ -76,7 +76,6 @@ int main( int argc, char** argv ) ...@@ -76,7 +76,6 @@ int main( int argc, char** argv )
int cmdonly = 0; int cmdonly = 0;
timeout_t tout = 0; timeout_t tout = 0;
timeout_t rdelay = 5000; timeout_t rdelay = 5000;
bool cmdlist = false;
try try
{ {
...@@ -140,7 +139,6 @@ int main( int argc, char** argv ) ...@@ -140,7 +139,6 @@ int main( int argc, char** argv )
filter = string(arg2); filter = string(arg2);
vcmd.push_back( LogReader::Command(LogServerTypes::cmdList, 0, filter) ); vcmd.push_back( LogReader::Command(LogServerTypes::cmdList, 0, filter) );
cmdlist = true;
} }
break; break;
...@@ -239,15 +237,6 @@ int main( int argc, char** argv ) ...@@ -239,15 +237,6 @@ int main( int argc, char** argv )
lr.setinTimeout(tout); lr.setinTimeout(tout);
lr.setReconnectDelay(rdelay); lr.setReconnectDelay(rdelay);
/*
if( cmdlist && vcmd.size() == 1 )
{
cmdonly = 1;
lr.setReadCount(1);
lr.sendCommand(addr, port, vcmd, cmdonly, verb);
return 0;
}
*/
if( !vcmd.empty() ) if( !vcmd.empty() )
lr.sendCommand(addr, port, vcmd, cmdonly, verb); lr.sendCommand(addr, port, vcmd, cmdonly, verb);
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
Name: libuniset2 Name: libuniset2
Version: 2.1 Version: 2.1
Release: alt23 Release: alt24
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
* Thu Sep 10 2015 Pavel Vainerman <pv@altlinux.ru> 2.1-alt24
- (LogServer): refactoring (more use shared_ptr)
* Mon Sep 07 2015 Pavel Vainerman <pv@altlinux.ru> 2.1-alt23 * Mon Sep 07 2015 Pavel Vainerman <pv@altlinux.ru> 2.1-alt23
- (ModbusPersistentSlave): fixed bug in end connection processing - (ModbusPersistentSlave): fixed bug in end connection processing
- (uniset-log): fixed bug in end connection processing - (uniset-log): fixed bug in end connection processing
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
--mbtcp-force-out 1 \ --mbtcp-force-out 1 \
--mbtcp-log-add-levels any \ --mbtcp-log-add-levels any \
--mbtcp-persistent-connection 1 \ --mbtcp-persistent-connection 1 \
--mbtcp-run-logserver \
$* $*
#--mbtcp-exchange-mode-id MB1_Mode_AS \ #--mbtcp-exchange-mode-id MB1_Mode_AS \
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#define LogReader_H_ #define LogReader_H_
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
#include <string> #include <string>
#include <memory>
#include <queue> #include <queue>
#include <vector> #include <vector>
#include <cc++/socket.h> #include <cc++/socket.h>
...@@ -73,7 +74,7 @@ class LogReader ...@@ -73,7 +74,7 @@ class LogReader
timeout_t reconDelay; timeout_t reconDelay;
private: private:
UTCPStream* tcp; std::shared_ptr<UTCPStream> tcp;
std::string iaddr; std::string iaddr;
ost::tpport_t port; ost::tpport_t port;
bool cmdonly; bool cmdonly;
......
...@@ -113,7 +113,7 @@ class LogServer ...@@ -113,7 +113,7 @@ class LogServer
DebugStream mylog; DebugStream mylog;
std::shared_ptr< ThreadCreator<LogServer> > thr; std::shared_ptr< ThreadCreator<LogServer> > thr;
ost::TCPSocket* tcp; std::shared_ptr<ost::TCPSocket> tcp;
std::shared_ptr<DebugStream> elog; std::shared_ptr<DebugStream> elog;
std::shared_ptr<NullLogSession> nullsess; std::shared_ptr<NullLogSession> nullsess;
}; };
......
...@@ -70,6 +70,8 @@ class LogSession: ...@@ -70,6 +70,8 @@ class LogSession:
std::shared_ptr<LogAgregator> alog; std::shared_ptr<LogAgregator> alog;
sigc::connection conn; sigc::connection conn;
std::shared_ptr<LogSession> myptr;
// PassiveTimer ptSessionTimeout; // PassiveTimer ptSessionTimeout;
FinalSlot slFin; FinalSlot slFin;
std::atomic_bool cancelled; std::atomic_bool cancelled;
......
...@@ -13,7 +13,6 @@ LogReader::LogReader(): ...@@ -13,7 +13,6 @@ LogReader::LogReader():
inTimeout(10000), inTimeout(10000),
outTimeout(6000), outTimeout(6000),
reconDelay(5000), reconDelay(5000),
tcp(0),
iaddr(""), iaddr(""),
cmdonly(false), cmdonly(false),
readcount(0) readcount(0)
...@@ -50,9 +49,8 @@ void LogReader::connect( ost::InetAddress addr, ost::tpport_t _port, timeout_t m ...@@ -50,9 +49,8 @@ void LogReader::connect( ost::InetAddress addr, ost::tpport_t _port, timeout_t m
{ {
if( tcp ) if( tcp )
{ {
(*tcp) << endl; (*tcp.get()) << endl;
disconnect(); disconnect();
delete tcp;
tcp = 0; tcp = 0;
} }
...@@ -71,7 +69,7 @@ void LogReader::connect( ost::InetAddress addr, ost::tpport_t _port, timeout_t m ...@@ -71,7 +69,7 @@ void LogReader::connect( ost::InetAddress addr, ost::tpport_t _port, timeout_t m
try try
{ {
tcp = new UTCPStream(); tcp = make_shared<UTCPStream>();
tcp->create(iaddr, port, true, 500); tcp->create(iaddr, port, true, 500);
tcp->setTimeout(msec); tcp->setTimeout(msec);
tcp->setKeepAlive(true); tcp->setKeepAlive(true);
...@@ -85,7 +83,6 @@ void LogReader::connect( ost::InetAddress addr, ost::tpport_t _port, timeout_t m ...@@ -85,7 +83,6 @@ void LogReader::connect( ost::InetAddress addr, ost::tpport_t _port, timeout_t m
rlog.crit() << s.str() << std::endl; rlog.crit() << s.str() << std::endl;
} }
delete tcp;
tcp = 0; tcp = 0;
} }
catch( ... ) catch( ... )
...@@ -110,7 +107,6 @@ void LogReader::disconnect() ...@@ -110,7 +107,6 @@ void LogReader::disconnect()
rlog.info() << iaddr << "(LogReader): disconnect." << endl; rlog.info() << iaddr << "(LogReader): disconnect." << endl;
tcp->disconnect(); tcp->disconnect();
delete tcp;
tcp = 0; tcp = 0;
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
......
...@@ -29,10 +29,11 @@ LogServer::~LogServer() ...@@ -29,10 +29,11 @@ LogServer::~LogServer()
tcp->reject(); tcp->reject();
if( thr ) if( thr )
{
thr->stop(); thr->stop();
if( thr->isRunning() )
delete tcp; thr->join();
tcp = 0; }
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
LogServer::LogServer( std::shared_ptr<LogAgregator> log ): LogServer::LogServer( std::shared_ptr<LogAgregator> log ):
...@@ -74,17 +75,7 @@ void LogServer::run( const std::string& addr, ost::tpport_t port, bool thread ) ...@@ -74,17 +75,7 @@ void LogServer::run( const std::string& addr, ost::tpport_t port, bool thread )
try try
{ {
ost::InetAddress iaddr(addr.c_str()); ost::InetAddress iaddr(addr.c_str());
tcp = new ost::TCPSocket(iaddr, port); tcp = make_shared<ost::TCPSocket>(iaddr, port);
#if 0
if( !nullsess )
{
ostringstream err;
err << "(LOG SERVER): Exceeded the limit on the number of sessions = " << sessMaxCount << endl;
nullsess = make_shared<NullLogSession>(err.str());
}
#endif
} }
catch( ost::Socket* socket ) catch( ost::Socket* socket )
{ {
...@@ -138,26 +129,28 @@ void LogServer::work() ...@@ -138,26 +129,28 @@ void LogServer::work()
ostringstream err; ostringstream err;
err << "(LOG SERVER): Exceeded the limit on the number of sessions = " << sessMaxCount << endl; err << "(LOG SERVER): Exceeded the limit on the number of sessions = " << sessMaxCount << endl;
nullsess = make_shared<NullLogSession>(err.str()); nullsess = make_shared<NullLogSession>(err.str());
nullsess->detach(); // start(); //nullsess->detach();
nullsess->start();
} }
else else
nullsess->setMessage(err.str()); nullsess->setMessage(err.str());
nullsess->add(*tcp); nullsess->add(*(tcp.get())); // опасно передавать "сырой указатель", теряем контроль
continue; continue;
} }
} }
if( cancelled ) break; if( cancelled ) break;
auto s = make_shared<LogSession>(*tcp, elog, sessTimeout, cmdTimeout, outTimeout); auto s = make_shared<LogSession>( *(tcp.get()), elog, sessTimeout, cmdTimeout, outTimeout);
s->setSessionLogLevel(sessLogLevel); s->setSessionLogLevel(sessLogLevel);
{ {
uniset_rwmutex_wrlock l(mutSList); uniset_rwmutex_wrlock l(mutSList);
slist.push_back(s); slist.push_back(s);
} }
s->connectFinalSession( sigc::mem_fun(this, &LogServer::sessionFinished) ); s->connectFinalSession( sigc::mem_fun(this, &LogServer::sessionFinished) );
s->detach(); //s->detach();
s->start();
} }
} }
catch( ost::Socket* socket ) catch( ost::Socket* socket )
...@@ -175,6 +168,10 @@ void LogServer::work() ...@@ -175,6 +168,10 @@ void LogServer::work()
else else
cerr << "client socket failed" << endl; cerr << "client socket failed" << endl;
} }
catch( const std::exception& ex )
{
cerr << "catch exception: " << ex.what() << endl;
}
} }
{ {
...@@ -185,6 +182,12 @@ void LogServer::work() ...@@ -185,6 +182,12 @@ void LogServer::work()
if( nullsess ) if( nullsess )
nullsess->cancel(); nullsess->cancel();
} }
for( const auto& i : slist )
{
if( i->isRunning() )
i->ost::Thread::join();
}
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
void LogServer::sessionFinished( std::shared_ptr<LogSession> s ) void LogServer::sessionFinished( std::shared_ptr<LogSession> s )
......
...@@ -18,9 +18,9 @@ using namespace UniSetTypes; ...@@ -18,9 +18,9 @@ using namespace UniSetTypes;
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
LogSession::~LogSession() LogSession::~LogSession()
{ {
cancelled = true;
if( isRunning() ) if( isRunning() )
{ {
cancelled = true;
ost::Thread::join(); ost::Thread::join();
disconnect(); disconnect();
} }
...@@ -73,6 +73,10 @@ void LogSession::run() ...@@ -73,6 +73,10 @@ void LogSession::run()
if( cancelled ) if( cancelled )
return; return;
// удерживаем указатель на себя, пока работает поток..
myptr = shared_from_this();
cancelled = false;
{ {
ost::tpport_t p; ost::tpport_t p;
ost::InetAddress iaddr = getIPV4Peer(&p); ost::InetAddress iaddr = getIPV4Peer(&p);
...@@ -95,7 +99,7 @@ void LogSession::run() ...@@ -95,7 +99,7 @@ void LogSession::run()
setKeepAlive(true); setKeepAlive(true);
// Команды могут посылаться только в начале сессии.. // Команды могут посылаться только в начале сессии..
while( isPending(Socket::pendingInput, cmdTimeout) ) while( !cancelled && isPending(Socket::pendingInput, cmdTimeout) )
{ {
LogServerTypes::lsMessage msg; LogServerTypes::lsMessage msg;
...@@ -242,8 +246,6 @@ void LogSession::run() ...@@ -242,8 +246,6 @@ void LogSession::run()
#endif #endif
cancelled = false;
while( !cancelled && isConnected() ) // !ptSessionTimeout.checkTime() while( !cancelled && isConnected() ) // !ptSessionTimeout.checkTime()
{ {
// проверка только ради проверки "целостности" соединения // проверка только ради проверки "целостности" соединения
...@@ -307,18 +309,22 @@ void LogSession::run() ...@@ -307,18 +309,22 @@ void LogSession::run()
disconnect(); disconnect();
cancelled = true;
if( slog.debugging(Debug::INFO) ) if( slog.debugging(Debug::INFO) )
slog[Debug::INFO] << peername << "(run): thread stopping..." << endl; slog[Debug::INFO] << peername << "(run): thread stopping..." << endl;
myptr = 0;
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
void LogSession::final() void LogSession::final()
{ {
tcp()->sync(); tcp()->sync();
cancelled = true;
try try
{ {
auto s = shared_from_this(); auto s = shared_from_this();
if( s ) if( s )
slFin(s); slFin(s);
} }
...@@ -344,8 +350,8 @@ NullLogSession::~NullLogSession() ...@@ -344,8 +350,8 @@ NullLogSession::~NullLogSession()
{ {
cancelled = true; cancelled = true;
if( isRunning() ) // if( isRunning() )
exit(); // terminate(); // exit(); // terminate();
} }
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
void NullLogSession::add( ost::TCPSocket& sock ) void NullLogSession::add( ost::TCPSocket& sock )
......
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