Change API of raw_istream to be more similar to raw_ostream.

This commit is contained in:
Peter Johnson
2016-09-02 20:53:45 -07:00
parent 94c2b65798
commit c2ae897b02
6 changed files with 43 additions and 17 deletions

View File

@@ -52,7 +52,7 @@ class WireDecoder {
bool Read(const char** buf, std::size_t len) {
if (len > m_allocated) Realloc(len);
*buf = m_buf;
bool rv = m_is.read(m_buf, len);
m_is.read(m_buf, len);
#if 0
nt::Logger& logger = nt::Logger::GetInstance();
if (logger.min_level() <= NT_LOG_DEBUG4 && logger.HasLogger()) {
@@ -67,7 +67,7 @@ class WireDecoder {
logger.Log(NT_LOG_DEBUG4, __FILE__, __LINE__, oss.str().c_str());
}
#endif
return rv;
return !m_is.has_error();
}
/* Reads a single byte. */

View File

@@ -16,21 +16,37 @@ class raw_istream {
public:
raw_istream() = default;
virtual ~raw_istream() = default;
virtual bool read(void* data, std::size_t len) = 0;
raw_istream& read(void* data, std::size_t len) {
read_impl(data, len);
return *this;
}
virtual void close() = 0;
bool has_error() const { return m_error; }
void clear_error() { m_error = false; }
raw_istream(const raw_istream&) = delete;
raw_istream& operator=(const raw_istream&) = delete;
protected:
void error_detected() { m_error = true; }
private:
virtual void read_impl(void* data, std::size_t len) = 0;
bool m_error = false;
};
class raw_mem_istream : public raw_istream {
public:
raw_mem_istream(const char* mem, std::size_t len) : m_cur(mem), m_left(len) {}
virtual ~raw_mem_istream() = default;
virtual bool read(void* data, std::size_t len);
virtual void close() {}
void close() override;
private:
void read_impl(void* data, std::size_t len) override;
const char* m_cur;
std::size_t m_left;
};

View File

@@ -9,19 +9,21 @@
#define WPIUTIL_SUPPORT_RAW_SOCKET_ISTREAM_H_
#include "support/raw_istream.h"
#include "tcpsockets/NetworkStream.h"
namespace wpi {
class NetworkStream;
class raw_socket_istream : public raw_istream {
public:
raw_socket_istream(NetworkStream& stream, int timeout = 0)
: m_stream(stream), m_timeout(timeout) {}
virtual ~raw_socket_istream() = default;
virtual bool read(void* data, std::size_t len);
virtual void close();
void close() override;
private:
void read_impl(void* data, std::size_t len) override;
NetworkStream& m_stream;
int m_timeout;
};

View File

@@ -103,7 +103,8 @@ bool ReadUleb128(raw_istream& is, unsigned long* ret) {
while (1) {
unsigned char byte;
if (!is.read((char*)&byte, 1)) return false;
is.read((char*)&byte, 1);
if (is.has_error()) return false;
result |= (byte & 0x7f) << shift;
shift += 7;

View File

@@ -11,10 +11,14 @@
using namespace wpi;
bool raw_mem_istream::read(void* data, std::size_t len) {
if (len > m_left) return false;
void raw_mem_istream::close() {}
void raw_mem_istream::read_impl(void* data, std::size_t len) {
if (len > m_left) {
error_detected();
return;
}
std::memcpy(data, m_cur, len);
m_cur += len;
m_left -= len;
return true;
}

View File

@@ -6,10 +6,11 @@
/*----------------------------------------------------------------------------*/
#include "support/raw_socket_istream.h"
#include "tcpsockets/NetworkStream.h"
using namespace wpi;
bool raw_socket_istream::read(void* data, std::size_t len) {
void raw_socket_istream::read_impl(void* data, std::size_t len) {
char* cdata = static_cast<char*>(data);
std::size_t pos = 0;
@@ -17,10 +18,12 @@ bool raw_socket_istream::read(void* data, std::size_t len) {
NetworkStream::Error err;
std::size_t count =
m_stream.receive(&cdata[pos], len - pos, &err, m_timeout);
if (count == 0) return false;
if (count == 0) {
error_detected();
return;
}
pos += count;
}
return true;
}
void raw_socket_istream::close() { m_stream.close(); }