Skip to content

Commit

Permalink
sconex/Stream: Add Buffer read/write methods, plus general tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
sconemad committed Feb 28, 2016
1 parent 12cae28 commit 1352608
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 43 deletions.
53 changes: 35 additions & 18 deletions sconex/Stream.cpp
Expand Up @@ -21,6 +21,7 @@ Free Software Foundation, Inc.,

#include <sconex/Stream.h>
#include <sconex/Module.h>
#include <sconex/Buffer.h>
namespace scx {

ProviderScheme<Stream>* Stream::s_providers = 0;
Expand All @@ -46,16 +47,6 @@ Stream::Stream(const std::string& stream_name)
//=============================================================================
Stream::~Stream()
{
/*
// Release the module refs
for (ModuleRefList::iterator it = m_module_refs.begin();
it != m_module_refs.end();
++it) {
ScriptRef* r = (*it);
delete r;
}
*/

DEBUG_COUNT_DESTRUCTOR(Stream);
}

Expand All @@ -69,6 +60,22 @@ Condition Stream::read(void* buffer,int n,int& na)
return m_endpoint->endpoint_read(buffer,n,na);
}

//=============================================================================
Condition Stream::read(Buffer& buffer, int n)
{
Condition c = Error;
int na = 0;
int max = buffer.free();
if (n < 0 || n > max) n = max;
if (m_chain) {
c = m_chain->read(buffer.tail(),n,na);
} else if (m_endpoint) {
c = m_endpoint->endpoint_read(buffer.tail(),n,na);
}
buffer.push(na);
return c;
}

//=============================================================================
Condition Stream::write(const void* buffer,int n,int& na)
{
Expand All @@ -79,6 +86,24 @@ Condition Stream::write(const void* buffer,int n,int& na)
return m_endpoint->endpoint_write(buffer,n,na);
}

//=============================================================================
// Write adaptor for Buffer
//
Condition Stream::write(Buffer& buffer, int n)
{
Condition c = Error;
int na = 0;
int max = buffer.used();
if (n < 0 || n > max) n = max;
if (m_chain) {
c = m_chain->write(buffer.head(),n,na);
} else if (m_endpoint) {
c = m_endpoint->endpoint_write(buffer.head(),n,na);
}
buffer.pop(na);
return c;
}

//=============================================================================
// Write adaptor for zero terminated string
//
Expand Down Expand Up @@ -131,14 +156,6 @@ void Stream::set_chain(Stream* chain)
m_chain = chain;
}

/*
//=============================================================================
void Stream::add_module_ref(Module* module)
{
m_module_refs.push_back(new ScriptRef(module));
}
*/

//=============================================================================
const std::string& Stream::stream_name() const
{
Expand Down
48 changes: 23 additions & 25 deletions sconex/Stream.h
Expand Up @@ -30,6 +30,7 @@ namespace scx {

class Module;
class ScriptRef;
class Buffer;

//=============================================================================
class SCONEX_API Stream : public IOBase {
Expand All @@ -43,15 +44,23 @@ class SCONEX_API Stream : public IOBase {
Stream(const std::string& stream_name);
virtual ~Stream();

virtual Condition read(void* buffer,int n,int& na);
// Read n bytes from stream into buffer
virtual Condition read(void* buffer,int n,int& na);

virtual Condition write(const void* buffer,int n,int& na);
// Read into Buffer object
// n: Maximum number of bytest to read (-1 to fill buffer)
virtual Condition read(Buffer& buffer, int n=-1);

// write n bytes from buffer to stream
virtual Condition write(const void* buffer,int n,int& na);

int write(const char* string);
int write(const std::string& string);
// Write from Buffer object
// n: Maximum number of bytes to write (-1 for entire buffer)
virtual Condition write(Buffer& buffer, int n=-1);

// write string from buffer to stream
virtual int write(const char* string);
virtual int write(const std::string& string);

// Event types
enum Event {
Expand All @@ -60,7 +69,6 @@ class SCONEX_API Stream : public IOBase {
SendReadable, SendWriteable
};

virtual Condition event(Event e);
// Handle event notification
//
// Streams need to override this method in order to handle events.
Expand Down Expand Up @@ -100,27 +108,23 @@ class SCONEX_API Stream : public IOBase {
// Close - Initiate close sequence for this connection
// Error - Force the connection closed at earliest oppurtunity
//
virtual Condition event(Event e);

// Get/set chain pointer
void set_endpoint(Descriptor* endpoint);
void set_chain(Stream* chain);
// Get/set chain pointer

// void add_module_ref(Module* module);
// Make this stream reference a module
// This should prevent the module from being unloaded while this stream is
// active.

const std::string& stream_name() const;
// Get the name of the stream
const std::string& stream_name() const;

virtual std::string stream_status() const;
// Get current status of the stream (if any) for debugging or
// informational purposes. Derived classes can override this if
// they wish to supply status information which might be useful
// for debugging.
virtual std::string stream_status() const;

std::string event_status() const;
// Get a string indicating the current event status of this stream.
std::string event_status() const;

// Stream provider interface
static void register_stream(const std::string& type,
Expand All @@ -132,10 +136,9 @@ class SCONEX_API Stream : public IOBase {

friend class Descriptor;

bool event_enabled(Event e) const;
// Is the specified event enabled
bool event_enabled(Event e) const;

void enable_event(Event e, bool onoff);
// Enable/disable specified event
//
// Readable, Writeable
Expand All @@ -147,27 +150,22 @@ class SCONEX_API Stream : public IOBase {
// Enabling these will cause Readable/Writeable events to be generated
// by this stream and passed UP the chain from this stream.
//
void enable_event(Event e, bool onoff);

Stream* find_stream(const std::string& stream_name);
// Try and find named stream in the chain of preceeding streams
Stream* find_stream(const std::string& stream_name);

Descriptor& endpoint();
// Allow access to the endpoint
Descriptor& endpoint();

std::string m_stream_name;
// The name of the stream
std::string m_stream_name;

private:

// Event status
int m_events;

/*
// List of modules used by this stream
typedef std::list<ScriptRef*> ModuleRefList;
ModuleRefList m_module_refs;
*/

// Upstream pointer
Stream* m_chain;

Expand Down

0 comments on commit 1352608

Please sign in to comment.