diff --git a/sconex/Stream.cpp b/sconex/Stream.cpp index 8080560..64cca5a 100644 --- a/sconex/Stream.cpp +++ b/sconex/Stream.cpp @@ -21,6 +21,7 @@ Free Software Foundation, Inc., #include #include +#include namespace scx { ProviderScheme* Stream::s_providers = 0; @@ -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); } @@ -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) { @@ -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 // @@ -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 { diff --git a/sconex/Stream.h b/sconex/Stream.h index 595babc..2f2e039 100644 --- a/sconex/Stream.h +++ b/sconex/Stream.h @@ -30,6 +30,7 @@ namespace scx { class Module; class ScriptRef; +class Buffer; //============================================================================= class SCONEX_API Stream : public IOBase { @@ -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 { @@ -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. @@ -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, @@ -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 @@ -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 ModuleRefList; - ModuleRefList m_module_refs; - */ - // Upstream pointer Stream* m_chain;