summaryrefslogtreecommitdiffstats
path: root/source/core/slang-stream.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2021-11-15 20:45:21 -0500
committerGitHub <noreply@github.com>2021-11-15 20:45:21 -0500
commit914a3808ebefb0f7f0a728469a2ce6b56dfc316c (patch)
tree9e452c21cf2a1d89d180818c65c6e7909f3328cf /source/core/slang-stream.cpp
parentae9df74fce7e3583effc822b3003542cf753823d (diff)
Http protocol support (#2012)
* #include an absolute path didn't work - because paths were taken to always be relative. * Use 'Process' to communicate with an command line tool. * Remove slang-win-stream * Tidy up windows ProcessUtil. * First version of BufferedReadStream. * Windows working IPC for steams. * Test proxy count option. * Split Process/ProcessUtil. Process is platform dependant. ProcessUtil are functions that are platform independent. * First implementation of Unix Process interface. * Unix process compiles on cygwin. * Fix typo in unix process. * Separate unix pipe stream error of invalid access, from pipe availability. * Fix in standard line extraction. * Make fd non blocking. * Fix issues with Windows Process streams. * Added UnixPipe. * Some fixes around UnixPipeStream. * Make a unix stream closed explicit. * Hack to debug linux process/stream. * Revert to old linux pipe handling. * Pass executable path for unit tests. Split out CommandLine into own source. * Small improvements in process/command line. * Check process behavior with crash. * Make stderr and stdout unbuffered for crash testing. * Only turn disable buffering in crash test. * Disable crash test on CI. * Fix crash on clang/linux. * Enable crash test. Remove _appendBuffer as can use StreamUtil functionality. * Added inital processing for http headers. * Small improvements to HttpHeader. * First pass HTTPPacketConnection working on windows. * Enable other Process communication tests. * Update comments.
Diffstat (limited to 'source/core/slang-stream.cpp')
-rw-r--r--source/core/slang-stream.cpp81
1 files changed, 53 insertions, 28 deletions
diff --git a/source/core/slang-stream.cpp b/source/core/slang-stream.cpp
index 042b1d898..7d4fab09e 100644
--- a/source/core/slang-stream.cpp
+++ b/source/core/slang-stream.cpp
@@ -3,6 +3,7 @@
#include <share.h>
#endif
#include "slang-io.h"
+#include "slang-process.h"
namespace Slang
{
@@ -374,13 +375,13 @@ SlangResult OwnedMemoryStream::write(const void * buffer, size_t length)
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!! BufferedReadStream !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-void BufferedReadStream::_advanceStartIndex(Index byteCount)
+void BufferedReadStream::consume(Index byteCount)
{
SLANG_ASSERT(Index(getCount()) >= byteCount && byteCount >= 0);
m_startIndex += byteCount;
if (getCount() == 0)
{
- m_startIndex = 0;
+ _resetBuffer();
}
}
@@ -402,14 +403,13 @@ SlangResult BufferedReadStream::seek(SeekOrigin origin, Int64 offset)
if (origin == SeekOrigin::End || origin == SeekOrigin::Start || offset < 0 || offset >= Int64(getCount()))
{
// Empty the buffer
- m_startIndex = 0;
- m_buffer.setCount(0);
+ _resetBuffer();
// Seek on underlying stream
return m_stream->seek(origin, offset);
}
// We can just seek on the buffered data
- _advanceStartIndex(Index(offset));
+ consume(Index(offset));
return SLANG_OK;
}
@@ -439,7 +439,7 @@ SlangResult BufferedReadStream::read(void* inBuffer, size_t length, size_t& outR
::memcpy(buffer, getBuffer(), readCount);
- _advanceStartIndex(Index(readCount));
+ consume(Index(readCount));
buffer += readCount;
length -= readCount;
@@ -510,35 +510,37 @@ SlangResult BufferedReadStream::update()
return SLANG_OK;
}
+ // Repeat until we have enough space
+ for (;;)
{
// How much buffer space do we have. We need at least m_defaultReadSize
const size_t remainingCount = size_t(m_buffer.getCapacity() - m_buffer.getCount());
- // Repeat until we have enough space
- while (remainingCount < m_defaultReadSize)
+ if (remainingCount >= m_defaultReadSize)
{
- // If there is anything in the buffer shift it all down
- if (m_startIndex > 0)
- {
- Byte* buffer = m_buffer.getBuffer();
- const Index count = getCount();
- if (count > 0)
- {
- ::memmove(buffer, buffer + m_startIndex, count);
- }
-
- m_buffer.setCount(count);
- m_startIndex = 0;
- }
- else
+ break;
+ }
+
+ // If there is anything in the buffer shift it all down
+ if (m_startIndex > 0)
+ {
+ Byte* buffer = m_buffer.getBuffer();
+ const Index count = getCount();
+ if (count > 0)
{
- // Make sure we have the space
- const Index prevCount = m_buffer.getCount();
- m_buffer.setCount(prevCount + m_defaultReadSize);
- m_buffer.setCount(prevCount);
+ ::memmove(buffer, buffer + m_startIndex, count);
}
+
+ m_buffer.setCount(count);
+ m_startIndex = 0;
+ }
+ else
+ {
+ // Make sure we have the space
+ const Index prevCount = m_buffer.getCount();
+ m_buffer.setCount(prevCount + m_defaultReadSize);
+ m_buffer.setCount(prevCount);
}
- SLANG_ASSERT(size_t(m_buffer.getCapacity() - m_buffer.getCount()) >= m_defaultReadSize);
}
{
@@ -547,7 +549,7 @@ SlangResult BufferedReadStream::update()
size_t readBytes = 0;
- const SlangResult res = m_stream->read(m_buffer.getBuffer() + prevCount, m_defaultReadSize, readBytes);
+ const SlangResult res = m_stream->read(m_buffer.getBuffer() + prevCount, m_defaultReadSize, readBytes);
m_buffer.setCount(prevCount + Index(readBytes));
@@ -555,6 +557,29 @@ SlangResult BufferedReadStream::update()
}
}
+SlangResult BufferedReadStream::readUntilContains(size_t size)
+{
+ while (true)
+ {
+ if (size_t(getCount()) >= size)
+ {
+ return SLANG_OK;
+ }
+
+ const size_t preCount = size_t(getCount());
+
+ // Update buffer
+ SLANG_RETURN_ON_FAIL(update());
+
+ // If nothing was read yield
+ if (preCount == getCount())
+ {
+ Process::sleepCurrentThread(0);
+ }
+ }
+}
+
+
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!! StreamUtil !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/* static */SlangResult StreamUtil::readAll(Stream* stream, size_t readSize, List<Byte>& ioBytes)