Commit 04b387b3 authored by Pavel Vainerman's avatar Pavel Vainerman

(UNetReceiver): перевёл все receiver-ы на использование CommonEventLoop

parent fc665a71
...@@ -698,11 +698,21 @@ bool UNetExchange::activateObject() ...@@ -698,11 +698,21 @@ bool UNetExchange::activateObject()
return true; return true;
} }
// ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------
void UNetExchange::sigterm( int signo ) bool UNetExchange::deactivateObject()
{ {
unetinfo << myname << ": ********* SIGTERM(" << signo << ") ********" << endl; if( activated )
activated = false; {
unetinfo << myname << "(deactivateObject): disactivate.." << endl;
activated = false;
termReceivers();
termSenders();
}
return UniSetObject::deactivateObject();
}
// ------------------------------------------------------------------------------------------
void UNetExchange::termReceivers()
{
for( const auto& it : recvlist ) for( const auto& it : recvlist )
{ {
try try
...@@ -719,7 +729,10 @@ void UNetExchange::sigterm( int signo ) ...@@ -719,7 +729,10 @@ void UNetExchange::sigterm( int signo )
} }
catch(...) {} catch(...) {}
} }
}
// ------------------------------------------------------------------------------------------
void UNetExchange::termSenders()
{
try try
{ {
if( sender ) if( sender )
...@@ -733,6 +746,15 @@ void UNetExchange::sigterm( int signo ) ...@@ -733,6 +746,15 @@ void UNetExchange::sigterm( int signo )
sender2->stop(); sender2->stop();
} }
catch(...) {} catch(...) {}
}
// ------------------------------------------------------------------------------------------
void UNetExchange::sigterm( int signo )
{
unetinfo << myname << ": ********* SIGTERM(" << signo << ") ********" << endl;
activated = false;
termReceivers();
termSenders();
UniSetObject::sigterm(signo); UniSetObject::sigterm(signo);
} }
......
...@@ -157,10 +157,13 @@ class UNetExchange: ...@@ -157,10 +157,13 @@ class UNetExchange:
void waitSMReady(); void waitSMReady();
void receiverEvent( const std::shared_ptr<UNetReceiver>& r, UNetReceiver::Event ev ); void receiverEvent( const std::shared_ptr<UNetReceiver>& r, UNetReceiver::Event ev );
virtual bool activateObject(); virtual bool activateObject() override;
virtual bool deactivateObject() override;
// действия при завершении работы // действия при завершении работы
virtual void sigterm( int signo ); virtual void sigterm( int signo );
void termSenders();
void termReceivers();
void initIterators(); void initIterators();
void startReceivers(); void startReceivers();
...@@ -170,7 +173,7 @@ class UNetExchange: ...@@ -170,7 +173,7 @@ class UNetExchange:
tmStep tmStep
}; };
private: private:
UNetExchange(); UNetExchange();
timeout_t initPause; timeout_t initPause;
UniSetTypes::uniset_rwmutex mutex_start; UniSetTypes::uniset_rwmutex mutex_start;
......
...@@ -25,6 +25,8 @@ using namespace std; ...@@ -25,6 +25,8 @@ using namespace std;
using namespace UniSetTypes; using namespace UniSetTypes;
using namespace UniSetExtensions; using namespace UniSetExtensions;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
CommonEventLoop UNetReceiver::loop;
// -----------------------------------------------------------------------------
/* /*
bool UNetReceiver::PacketCompare::operator()(const UniSetUDP::UDPMessage& lhs, bool UNetReceiver::PacketCompare::operator()(const UniSetUDP::UDPMessage& lhs,
const UniSetUDP::UDPMessage& rhs) const const UniSetUDP::UDPMessage& rhs) const
...@@ -101,8 +103,6 @@ UNetReceiver::UNetReceiver( const std::string& s_host, const ost::tpport_t port, ...@@ -101,8 +103,6 @@ UNetReceiver::UNetReceiver( const std::string& s_host, const ost::tpport_t port,
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
UNetReceiver::~UNetReceiver() UNetReceiver::~UNetReceiver()
{ {
evReceive.stop();
evUpdate.stop();
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void UNetReceiver::setReceiveTimeout( timeout_t msec ) void UNetReceiver::setReceiveTimeout( timeout_t msec )
...@@ -182,25 +182,32 @@ void UNetReceiver::start() ...@@ -182,25 +182,32 @@ void UNetReceiver::start()
if( !activated ) if( !activated )
{ {
activated = true; activated = true;
evrun(true); loop.evrun(this,true);
} }
else else
forceUpdate(); forceUpdate();
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void UNetReceiver::evprepare() void UNetReceiver::evprepare( const ev::loop_ref& eloop )
{ {
evReceive.set(loop); evReceive.set(eloop);
evReceive.start(udp->getSocket(),ev::READ); evReceive.start(udp->getSocket(),ev::READ);
evUpdate.set(loop); evUpdate.set(eloop);
evUpdate.start( updateTime ); evUpdate.start( updateTime );
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void UNetReceiver::evfinish() void UNetReceiver::evfinish( const ev::loop_ref& eloop )
{ {
evReceive.stop(); activated = false;
evUpdate.stop(); if( evReceive.is_active() )
evReceive.stop();
if( evUpdate.is_active() )
evUpdate.stop();
//udp->disconnect();
udp = nullptr;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void UNetReceiver::forceUpdate() void UNetReceiver::forceUpdate()
...@@ -419,10 +426,14 @@ void UNetReceiver::readEvent( ev::io& watcher ) ...@@ -419,10 +426,14 @@ void UNetReceiver::readEvent( ev::io& watcher )
// только если "режим подготовки закончился, то можем генерировать "события" // только если "режим подготовки закончился, то можем генерировать "события"
if( ptPrepare.checkTime() && trTimeout.change(tout) ) if( ptPrepare.checkTime() && trTimeout.change(tout) )
{ {
if( tout ) auto w = shared_from_this();
slEvent(shared_from_this(), evTimeout); if( w )
else {
slEvent(shared_from_this(), evOK); if( tout )
slEvent(w, evTimeout);
else
slEvent(w, evOK);
}
} }
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
...@@ -480,7 +491,7 @@ void UNetReceiver::stop() ...@@ -480,7 +491,7 @@ void UNetReceiver::stop()
{ {
unetinfo << myname << ": stop.." << endl; unetinfo << myname << ": stop.." << endl;
activated = false; activated = false;
evstop(); loop.evstop(this);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
bool UNetReceiver::receive() bool UNetReceiver::receive()
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
#include "SMInterface.h" #include "SMInterface.h"
#include "SharedMemory.h" #include "SharedMemory.h"
#include "UDPPacket.h" #include "UDPPacket.h"
#include "EventLoopServer.h" #include "CommonEventLoop.h"
#include "UDPCore.h" #include "UDPCore.h"
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
/* Основная идея: сделать проверку очерёдности пакетов, но при этом использовать UDP. /* Основная идея: сделать проверку очерёдности пакетов, но при этом использовать UDP.
...@@ -77,8 +77,8 @@ ...@@ -77,8 +77,8 @@
*/ */
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
class UNetReceiver: class UNetReceiver:
public std::enable_shared_from_this<UNetReceiver>, protected EvWatcher,
public EventLoopServer public std::enable_shared_from_this<UNetReceiver>
{ {
public: public:
UNetReceiver( const std::string& host, const ost::tpport_t port, const std::shared_ptr<SMInterface>& smi ); UNetReceiver( const std::string& host, const ost::tpport_t port, const std::shared_ptr<SMInterface>& smi );
...@@ -161,8 +161,9 @@ class UNetReceiver: ...@@ -161,8 +161,9 @@ class UNetReceiver:
void callback( ev::io& watcher, int revents ); void callback( ev::io& watcher, int revents );
void readEvent( ev::io& watcher ); void readEvent( ev::io& watcher );
void updateEvent( ev::periodic& watcher, int revents ); void updateEvent( ev::periodic& watcher, int revents );
virtual void evfinish() override; virtual void evprepare( const ev::loop_ref& eloop ) override;
virtual void evprepare() override; virtual void evfinish(const ev::loop_ref& eloop ) override;
virtual std::string wname(){ return myname; }
void initIterators(); void initIterators();
...@@ -192,6 +193,10 @@ class UNetReceiver: ...@@ -192,6 +193,10 @@ class UNetReceiver:
std::string myname; std::string myname;
ev::io evReceive; ev::io evReceive;
ev::periodic evUpdate; ev::periodic evUpdate;
// делаем loop общим.. одним на всех!
static CommonEventLoop loop;
double updateTime = { 0.0 }; double updateTime = { 0.0 };
UniSetTypes::uniset_rwmutex pollMutex; UniSetTypes::uniset_rwmutex pollMutex;
......
...@@ -207,11 +207,17 @@ void UNetSender::send() ...@@ -207,11 +207,17 @@ void UNetSender::send()
if( it.first > 1 && (ncycle % it.first) != 0 ) if( it.first > 1 && (ncycle % it.first) != 0 )
continue; continue;
if( !activated )
break;
auto& pk = it.second; auto& pk = it.second;
int size = pk.size(); int size = pk.size();
for(int i = 0; i < size; ++i) for(int i = 0; i < size; ++i)
{ {
if( !activated )
break;
real_send(pk[i]); real_send(pk[i]);
msleep(packsendpause); msleep(packsendpause);
} }
...@@ -236,6 +242,9 @@ void UNetSender::send() ...@@ -236,6 +242,9 @@ void UNetSender::send()
unetwarn << myname << "(send): catch ..." << std::endl; unetwarn << myname << "(send): catch ..." << std::endl;
} }
if( !activated )
break;
msleep(sendpause); msleep(sendpause);
} }
...@@ -279,6 +288,8 @@ void UNetSender::stop() ...@@ -279,6 +288,8 @@ void UNetSender::stop()
{ {
activated = false; activated = false;
// s_thr->stop(); // s_thr->stop();
if( s_thr )
s_thr->join();
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void UNetSender::start() void UNetSender::start()
......
...@@ -3,6 +3,6 @@ ...@@ -3,6 +3,6 @@
uniset2-start.sh -f ./uniset2-unetexchange --unet-name UNetExchange --unet-run-logserver \ uniset2-start.sh -f ./uniset2-unetexchange --unet-name UNetExchange --unet-run-logserver \
--confile test.xml --smemory-id SharedMemory \ --confile test.xml --smemory-id SharedMemory \
--unet-filter-field rs --unet-filter-value 2 --unet-maxdifferense 40 --unet-sendpause 1000 \ --unet-filter-field rs --unet-filter-value 2 --unet-maxdifferense 40 --unet-sendpause 1000 \
--dlog-add-levels info,crit,warn --unet-log-add-levels info,crit,warn $* --dlog-add-levels info,crit,warn --unet-log-add-levels info,crit,warn,any $*
#--unet-nodes-confnode specnet #--unet-nodes-confnode specnet
...@@ -50,7 +50,7 @@ class CommonEventLoop ...@@ -50,7 +50,7 @@ class CommonEventLoop
bool evrun( EvWatcher* w, bool thread = true ); bool evrun( EvWatcher* w, bool thread = true );
/*! \return TRUE - если это был последний EvWatcher и loop остановлен */ /*! \return TRUE - если это был последний EvWatcher и loop остановлен */
bool evstop( EvWatcher* s ); bool evstop( EvWatcher* w );
inline const ev::loop_ref evloop(){ return loop; } inline const ev::loop_ref evloop(){ return loop; }
...@@ -76,7 +76,8 @@ class CommonEventLoop ...@@ -76,7 +76,8 @@ class CommonEventLoop
std::mutex wlist_mutex; std::mutex wlist_mutex;
std::list<EvWatcher*> wlist; std::list<EvWatcher*> wlist;
// готовящийся Watcher..(он может быть только один, единицу времени) // готовящийся Watcher..он может быть только один в единицу времени
// это гарантирует prep_mutex
EvWatcher* wprep = { nullptr }; EvWatcher* wprep = { nullptr };
ev::async evprep; ev::async evprep;
std::condition_variable prep_event; std::condition_variable prep_event;
......
...@@ -207,7 +207,6 @@ void LogServer::ioAccept( ev::io& watcher, int revents ) ...@@ -207,7 +207,6 @@ void LogServer::ioAccept( ev::io& watcher, int revents )
void LogServer::sessionFinished( LogSession* s ) void LogServer::sessionFinished( LogSession* s )
{ {
uniset_rwmutex_wrlock l(mutSList); uniset_rwmutex_wrlock l(mutSList);
for( SessionList::iterator i = slist.begin(); i != slist.end(); ++i ) for( SessionList::iterator i = slist.begin(); i != slist.end(); ++i )
{ {
if( i->get() == s ) if( i->get() == s )
......
...@@ -462,22 +462,6 @@ void UniSetActivator::stop() ...@@ -462,22 +462,6 @@ void UniSetActivator::stop()
deactivate(); deactivate();
try
{
deactivateObject();
}
catch( const omniORB::fatalException& fe )
{
ucrit << myname << "(stop): : поймали omniORB::fatalException:" << endl;
ucrit << myname << "(stop): file: " << fe.file() << endl;
ucrit << myname << "(stop): line: " << fe.line() << endl;
ucrit << myname << "(stop): mesg: " << fe.errmsg() << endl;
}
catch( const std::exception& ex )
{
ucrit << myname << "(stop): " << ex.what() << endl;
}
ulogsys << myname << "(stop): deactivate ok. " << endl; ulogsys << myname << "(stop): deactivate ok. " << endl;
ulogsys << myname << "(stop): discard request..." << endl; ulogsys << myname << "(stop): discard request..." << endl;
......
...@@ -27,7 +27,7 @@ CommonEventLoop::~CommonEventLoop() ...@@ -27,7 +27,7 @@ CommonEventLoop::~CommonEventLoop()
} }
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
bool CommonEventLoop::evrun( EvWatcher* w, bool thread ) bool CommonEventLoop::evrun(EvWatcher* w, bool thread )
{ {
if( !w ) if( !w )
return false; return false;
...@@ -80,25 +80,21 @@ bool CommonEventLoop::evIsActive() ...@@ -80,25 +80,21 @@ bool CommonEventLoop::evIsActive()
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
bool CommonEventLoop::evstop( EvWatcher* w ) bool CommonEventLoop::evstop( EvWatcher* w )
{ {
if( !w )
return false;
std::unique_lock<std::mutex> l(wlist_mutex); std::unique_lock<std::mutex> l(wlist_mutex);
for( auto i = wlist.begin(); i!=wlist.end(); i++ ) try
{ {
if( (*i) == w ) w->evfinish(loop); // для этого Watcher это уже finish..
{ }
try catch( std::exception& ex )
{ {
w->evfinish(loop); // для этого Watcher это уже finish.. cerr << "(CommonEventLoop::evfinish): evfinish err: " << ex.what() << endl;
}
catch( std::exception& ex )
{
cerr << "(CommonEventLoop::evfinish): evfinish err: " << ex.what() << endl;
}
wlist.erase(i);
break;
}
} }
wlist.remove(w);
if( !wlist.empty() ) if( !wlist.empty() )
return false; return false;
......
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