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