fixed getSize() to be non-blocking in CInputPacketStream.

This commit is contained in:
crs 2002-06-26 13:31:06 +00:00
parent 6cc3b50d3b
commit 78d28fd6e5
2 changed files with 40 additions and 12 deletions

View File

@ -32,7 +32,7 @@ CInputPacketStream::read(void* buffer, UInt32 n)
// wait for entire message to be read. return immediately if // wait for entire message to be read. return immediately if
// stream hungup. // stream hungup.
if (getSizeNoLock() == 0) { if (!waitForFullMessage()) {
return 0; return 0;
} }
@ -60,24 +60,50 @@ CInputPacketStream::getSize() const
UInt32 UInt32
CInputPacketStream::getSizeNoLock() const CInputPacketStream::getSizeNoLock() const
{ {
while (!hasFullMessage()) { while (!hasFullMessage() && getStream()->getSize() > 0) {
// read more data // read more data
char buffer[4096]; if (!getMoreMessage()) {
UInt32 n = getStream()->read(buffer, sizeof(buffer)); // stream hungup
return false;
// return if stream hungup
if (n == 0) {
m_buffer.hangup();
return 0;
} }
// append to our buffer
m_buffer.write(buffer, n);
} }
return m_size; return m_size;
} }
bool
CInputPacketStream::waitForFullMessage() const
{
while (!hasFullMessage()) {
// read more data
if (!getMoreMessage()) {
// stream hungup
return false;
}
}
return true;
}
bool
CInputPacketStream::getMoreMessage() const
{
// read more data
char buffer[4096];
UInt32 n = getStream()->read(buffer, sizeof(buffer));
// return if stream hungup
if (n == 0) {
m_buffer.hangup();
return false;
}
// append to our buffer
m_buffer.write(buffer, n);
return true;
}
bool bool
CInputPacketStream::hasFullMessage() const CInputPacketStream::hasFullMessage() const
{ {

View File

@ -21,6 +21,8 @@ public:
private: private:
UInt32 getSizeNoLock() const; UInt32 getSizeNoLock() const;
bool waitForFullMessage() const;
bool getMoreMessage() const;
bool hasFullMessage() const; bool hasFullMessage() const;
private: private: