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

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

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