Commit 8f722848 authored by Pavel Vainerman's avatar Pavel Vainerman

ModbusTCPCore: сделал чтение не по одному байту, а сразу "много"

parent 003f7f6a
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
using namespace std; using namespace std;
using namespace ModbusRTU; using namespace ModbusRTU;
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
#define USE_BUFFER_FOR_READ 1
// -------------------------------------------------------------------------
size_t ModbusTCPCore::readNextData(UTCPStream* tcp, size_t ModbusTCPCore::readNextData(UTCPStream* tcp,
std::queue<unsigned char>& qrecv, size_t max, timeout_t t ) std::queue<unsigned char>& qrecv, size_t max, timeout_t t )
{ {
...@@ -27,10 +29,35 @@ size_t ModbusTCPCore::readNextData(UTCPStream* tcp, ...@@ -27,10 +29,35 @@ size_t ModbusTCPCore::readNextData(UTCPStream* tcp,
size_t i = 0; size_t i = 0;
#ifdef USE_BUFFER_FOR_READ
char* buf = new char[max];
if( buf == 0 )
return 0;
try
{
ssize_t l = tcp->readData(buf,max, 0, t);
if( l > 0 )
{
for( ssize_t k=0; k<l; k++ )
qrecv.push(buf[k]);
i = l;
}
}
catch( ost::SockException& e )
{
}
delete [] buf;
#else
try try
{ {
for( ; i < max; i++ ) for( ; i < max; i++ )
{ {
// (не оптимально): читаем один символ за раз..
unsigned char c; unsigned char c;
ssize_t l = tcp->readData(&c, sizeof(c), 0, t); ssize_t l = tcp->readData(&c, sizeof(c), 0, t);
...@@ -43,6 +70,8 @@ size_t ModbusTCPCore::readNextData(UTCPStream* tcp, ...@@ -43,6 +70,8 @@ size_t ModbusTCPCore::readNextData(UTCPStream* tcp,
catch( ost::SockException& e ) catch( ost::SockException& e )
{ {
} }
#endif
return i; return i;
} }
...@@ -78,43 +107,50 @@ size_t ModbusTCPCore::getNextData(UTCPStream* tcp, ...@@ -78,43 +107,50 @@ size_t ModbusTCPCore::getNextData(UTCPStream* tcp,
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
size_t ModbusTCPCore::readDataFD( int fd, std::queue<unsigned char>& qrecv, size_t max , size_t attempts ) size_t ModbusTCPCore::readDataFD( int fd, std::queue<unsigned char>& qrecv, size_t max , size_t attempts )
{ {
size_t i = 0;
#if 1 #ifdef USE_BUFFER_FOR_READ
char* buf = new char[max];
if( buf == 0 )
return 0;
ssize_t l = 0;
size_t cnt = 0;
for( size_t a = 0; a < attempts; a++ ) for( size_t a = 0; a < attempts; a++ )
{ {
for( ; i < max; i++ ) l = ::read(fd, buf, max);
if( l > 0 )
{ {
// читаем один символ за раз.. for( int k = 0; k < l; k++ )
unsigned char c; qrecv.push(buf[k]);
ssize_t l = ::read(fd, &c, sizeof(c));
if( l <= 0 ) cnt += l;
if( cnt >= max )
break; break;
qrecv.push(c);
} }
} }
delete [] buf;
#else #else
char* buf = new char[max];
ssize_t l = 0;
size_t i = 0;
for( size_t a = 0; a < attempts; a++ ) for( size_t a = 0; a < attempts; a++ )
{ {
l = ::read(fd, buf, sizeof(buf)); for( ; i < max; i++ )
{
// (не оптимально): читаем один символ за раз..
unsigned char c;
ssize_t l = ::read(fd, &c, sizeof(c));
if( l > 0 ) if( l <= 0 )
break; break;
}
if( l > 0 ) qrecv.push(c);
{ }
for( int i = 0; i < l; i++ )
qrecv.push(buf[i]);
} }
delete [] buf;
#endif #endif
return ( qrecv.size() >= max ? max : qrecv.size() ); return ( qrecv.size() >= max ? max : qrecv.size() );
......
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