mirror of https://github.com/Icinga/icinga2.git
parent
f097e48889
commit
9e936cbea4
|
@ -34,11 +34,13 @@ using namespace icinga;
|
|||
*/
|
||||
StreamReadStatus NetString::ReadStringFromStream(const Stream::Ptr& stream, String *str, StreamReadContext& context)
|
||||
{
|
||||
if (stream->IsEof())
|
||||
if (context.Eof)
|
||||
return StatusEof;
|
||||
|
||||
if (context.MustRead && context.FillFromStream(stream) == 0)
|
||||
if (context.MustRead && !context.FillFromStream(stream)) {
|
||||
context.Eof = true;
|
||||
return StatusEof;
|
||||
}
|
||||
|
||||
size_t header_length = 0;
|
||||
|
||||
|
|
|
@ -63,11 +63,13 @@ void Stream::WaitForData(void)
|
|||
|
||||
StreamReadStatus Stream::ReadLine(String *line, StreamReadContext& context)
|
||||
{
|
||||
if (IsEof())
|
||||
if (context.Eof)
|
||||
return StatusEof;
|
||||
|
||||
if (context.MustRead) {
|
||||
if (!context.FillFromStream(this)) {
|
||||
context.Eof = true;
|
||||
|
||||
*line = String(context.Buffer, &(context.Buffer[context.Size]));
|
||||
boost::algorithm::trim_right(*line);
|
||||
|
||||
|
@ -106,6 +108,8 @@ bool StreamReadContext::FillFromStream(const Stream::Ptr& stream)
|
|||
if (Wait && stream->SupportsWaiting())
|
||||
stream->WaitForData();
|
||||
|
||||
size_t count = 0;
|
||||
|
||||
do {
|
||||
Buffer = (char *)realloc(Buffer, Size + 4096);
|
||||
|
||||
|
@ -114,13 +118,14 @@ bool StreamReadContext::FillFromStream(const Stream::Ptr& stream)
|
|||
|
||||
size_t rc = stream->Read(Buffer + Size, 4096, true);
|
||||
|
||||
if (rc == 0 && stream->IsEof())
|
||||
return false;
|
||||
|
||||
Size += rc;
|
||||
count += rc;
|
||||
} while (stream->IsDataAvailable());
|
||||
|
||||
return true;
|
||||
if (count == 0 && stream->IsEof())
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
void StreamReadContext::DropData(size_t count)
|
||||
|
|
|
@ -39,7 +39,7 @@ enum ConnectionRole
|
|||
struct StreamReadContext
|
||||
{
|
||||
StreamReadContext(bool wait = true)
|
||||
: Buffer(NULL), Size(0), MustRead(true), Wait(wait)
|
||||
: Buffer(NULL), Size(0), MustRead(true), Eof(false), Wait(wait)
|
||||
{ }
|
||||
|
||||
~StreamReadContext(void)
|
||||
|
@ -53,6 +53,7 @@ struct StreamReadContext
|
|||
char *Buffer;
|
||||
size_t Size;
|
||||
bool MustRead;
|
||||
bool Eof;
|
||||
bool Wait;
|
||||
};
|
||||
|
||||
|
|
|
@ -43,12 +43,12 @@ BOOST_AUTO_TEST_CASE(io)
|
|||
BOOST_CHECK(fifo->GetAvailableBytes() == 5);
|
||||
|
||||
char buffer1[2];
|
||||
fifo->Read(buffer1, 2);
|
||||
fifo->Read(buffer1, 2, true);
|
||||
BOOST_CHECK(memcmp(buffer1, "he", 2) == 0);
|
||||
BOOST_CHECK(fifo->GetAvailableBytes() == 3);
|
||||
|
||||
char buffer2[5];
|
||||
size_t rc = fifo->Read(buffer2, 5);
|
||||
size_t rc = fifo->Read(buffer2, 5, true);
|
||||
BOOST_CHECK(rc == 3);
|
||||
BOOST_CHECK(memcmp(buffer2, "llo", 3) == 0);
|
||||
BOOST_CHECK(fifo->GetAvailableBytes() == 0);
|
||||
|
|
|
@ -33,7 +33,7 @@ BOOST_AUTO_TEST_CASE(netstring)
|
|||
|
||||
String s;
|
||||
StreamReadContext src;
|
||||
BOOST_CHECK(NetString::ReadStringFromStream(fifo, &s, src));
|
||||
BOOST_CHECK(NetString::ReadStringFromStream(fifo, &s, src) == StatusNewItem);
|
||||
BOOST_CHECK(s == "hello");
|
||||
|
||||
fifo->Close();
|
||||
|
|
|
@ -37,18 +37,20 @@ BOOST_AUTO_TEST_CASE(readline_stdio)
|
|||
StreamReadContext rlc;
|
||||
|
||||
String line;
|
||||
BOOST_CHECK(stdstream->ReadLine(&line, rlc));
|
||||
BOOST_CHECK(stdstream->ReadLine(&line, rlc) == StatusNewItem);
|
||||
BOOST_CHECK(line == "Hello");
|
||||
|
||||
BOOST_CHECK(stdstream->ReadLine(&line, rlc));
|
||||
BOOST_CHECK(stdstream->ReadLine(&line, rlc) == StatusNewItem);
|
||||
BOOST_CHECK(line == "World");
|
||||
|
||||
BOOST_CHECK(stdstream->ReadLine(&line, rlc));
|
||||
BOOST_CHECK(stdstream->ReadLine(&line, rlc) == StatusNewItem);
|
||||
BOOST_CHECK(line == "");
|
||||
|
||||
BOOST_CHECK(stdstream->ReadLine(&line, rlc));
|
||||
BOOST_CHECK(stdstream->ReadLine(&line, rlc) == StatusNewItem);
|
||||
BOOST_CHECK(line == "");
|
||||
|
||||
BOOST_CHECK(stdstream->ReadLine(&line, rlc) == StatusEof);
|
||||
|
||||
stdstream->Close();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue