Commit 4940bae0 authored by Pavel Vainerman's avatar Pavel Vainerman

(ModbusTCPSession): попытка убрать sleep-ы в обработке запросов

parent f1f55d9f
...@@ -612,7 +612,9 @@ void MBSlave::execute_tcp() ...@@ -612,7 +612,9 @@ void MBSlave::execute_tcp()
// для обновления пороговых датчиков // для обновления пороговых датчиков
tcpserver->signal_post_receive().connect( sigc::mem_fun(this, &MBSlave::postReceiveEvent) ); tcpserver->signal_post_receive().connect( sigc::mem_fun(this, &MBSlave::postReceiveEvent) );
mbinfo << myname << "(execute_tcp): run tcpserver.." << endl; mbinfo << myname << "(execute_tcp): run tcpserver ("
<< tcpserver->getInetAddress() << ":" << tcpserver->getInetPort()
<<")" << endl;
tcpCancelled = false; tcpCancelled = false;
......
...@@ -104,6 +104,8 @@ class ModbusTCPServer: ...@@ -104,6 +104,8 @@ class ModbusTCPServer:
return tmTime; return tmTime;
} }
void iowait( timeout_t msec ); // ожидание (при этом время отдаётся eventloop-у)
protected: protected:
// функция receive пока не поддерживается... // функция receive пока не поддерживается...
...@@ -157,6 +159,8 @@ class ModbusTCPServer: ...@@ -157,6 +159,8 @@ class ModbusTCPServer:
timeout_t tmTime_msec = { TIMEOUT_INF }; // время по умолчанию для таймера (TimerSignal) timeout_t tmTime_msec = { TIMEOUT_INF }; // время по умолчанию для таймера (TimerSignal)
double tmTime = { 0.0 }; double tmTime = { 0.0 };
PassiveTimer ptWait;
private: private:
// транслирование сигналов от Sessions.. // транслирование сигналов от Sessions..
void postReceiveEvent( ModbusRTU::mbErrCode res ); void postReceiveEvent( ModbusRTU::mbErrCode res );
......
...@@ -57,6 +57,8 @@ class ModbusTCPSession: ...@@ -57,6 +57,8 @@ class ModbusTCPSession:
virtual bool isActive() override; virtual bool isActive() override;
void iowait( timeout_t msec );
protected: protected:
virtual ModbusRTU::mbErrCode realReceive( const std::unordered_set<ModbusRTU::ModbusAddr>& vmbaddr, timeout_t msecTimeout ) override; virtual ModbusRTU::mbErrCode realReceive( const std::unordered_set<ModbusRTU::ModbusAddr>& vmbaddr, timeout_t msecTimeout ) override;
...@@ -139,6 +141,8 @@ class ModbusTCPSession: ...@@ -139,6 +141,8 @@ class ModbusTCPSession:
FinalSlot slFin; FinalSlot slFin;
std::atomic_bool cancelled = { false }; std::atomic_bool cancelled = { false };
PassiveTimer pt;
PassiveTimer ptWait;
}; };
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
#endif // ModbusTCPSession_H_ #endif // ModbusTCPSession_H_
......
...@@ -196,6 +196,13 @@ void ModbusTCPServer::setTimer( timeout_t msec ) ...@@ -196,6 +196,13 @@ void ModbusTCPServer::setTimer( timeout_t msec )
} }
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
void ModbusTCPServer::iowait( timeout_t msec )
{
ptWait.setTiming(msec);
while( !ptWait.checkTime() )
io.loop.iteration();
}
// -------------------------------------------------------------------------
void ModbusTCPServer::ioAccept(ev::io& watcher, int revents) void ModbusTCPServer::ioAccept(ev::io& watcher, int revents)
{ {
if (EV_ERROR & revents) if (EV_ERROR & revents)
......
...@@ -89,6 +89,13 @@ void ModbusTCPSession::setSessionTimeout( double t ) ...@@ -89,6 +89,13 @@ void ModbusTCPSession::setSessionTimeout( double t )
ioTimeout.start(t); ioTimeout.start(t);
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
void ModbusTCPSession::iowait( timeout_t msec )
{
ptWait.setTiming(msec);
while( !ptWait.checkTime() )
io.loop.iteration();
}
// -------------------------------------------------------------------------
void ModbusTCPSession::run( ev::loop_ref& loop ) void ModbusTCPSession::run( ev::loop_ref& loop )
{ {
if( dlog->is_info() ) if( dlog->is_info() )
...@@ -225,7 +232,7 @@ ModbusRTU::mbErrCode ModbusTCPSession::realReceive( const std::unordered_set<Mod ...@@ -225,7 +232,7 @@ ModbusRTU::mbErrCode ModbusTCPSession::realReceive( const std::unordered_set<Mod
printProcessingTime(); printProcessingTime();
} }
else if( aftersend_msec > 0 ) else if( aftersend_msec > 0 )
msleep(aftersend_msec); iowait(aftersend_msec);
return res; return res;
} }
...@@ -321,14 +328,14 @@ mbErrCode ModbusTCPSession::tcp_processing( ModbusTCP::MBAPHeader& mhead ) ...@@ -321,14 +328,14 @@ mbErrCode ModbusTCPSession::tcp_processing( ModbusTCP::MBAPHeader& mhead )
return erInvalidFormat; return erInvalidFormat;
} }
len = ModbusTCPCore::readDataFD( sock->getSocket(), qrecv, mhead.len ); pt.setTiming(10);
do
if( len == 0 )
{ {
// делаем ещё одну попытку чтения через некоторое время
msleep(5);
len = ModbusTCPCore::readDataFD( sock->getSocket(), qrecv, mhead.len ); len = ModbusTCPCore::readDataFD( sock->getSocket(), qrecv, mhead.len );
if( len == 0 )
io.loop.iteration();
} }
while( len == 0 && !pt.checkTime() );
if( len < mhead.len ) if( len < mhead.len )
{ {
......
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