| 1 | /* | 
|---|
| 2 | * synergy -- mouse and keyboard sharing utility | 
|---|
| 3 | * Copyright (C) 2004 Chris Schoeneman | 
|---|
| 4 | * | 
|---|
| 5 | * This package is free software; you can redistribute it and/or | 
|---|
| 6 | * modify it under the terms of the GNU General Public License | 
|---|
| 7 | * found in the file COPYING that should have accompanied this file. | 
|---|
| 8 | * | 
|---|
| 9 | * This package is distributed in the hope that it will be useful, | 
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 12 | * GNU General Public License for more details. | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | #ifndef ISTREAM_H | 
|---|
| 16 | #define ISTREAM_H | 
|---|
| 17 |  | 
|---|
| 18 | #include "IInterface.h" | 
|---|
| 19 | #include "CEvent.h" | 
|---|
| 20 |  | 
|---|
| 21 | //! Bidirectional stream interface | 
|---|
| 22 | /*! | 
|---|
| 23 | Defines the interface for all streams. | 
|---|
| 24 | */ | 
|---|
| 25 | class IStream : public IInterface { | 
|---|
| 26 | public: | 
|---|
| 27 | //! @name manipulators | 
|---|
| 28 | //@{ | 
|---|
| 29 |  | 
|---|
| 30 | //! Close the stream | 
|---|
| 31 | /*! | 
|---|
| 32 | Closes the stream.  Pending input data and buffered output data | 
|---|
| 33 | are discarded.  Use \c flush() before \c close() to send buffered | 
|---|
| 34 | output data.  Attempts to \c read() after a close return 0, | 
|---|
| 35 | attempts to \c write() generate output error events, and attempts | 
|---|
| 36 | to \c flush() return immediately. | 
|---|
| 37 | */ | 
|---|
| 38 | virtual void            close() = 0; | 
|---|
| 39 |  | 
|---|
| 40 | //! Read from stream | 
|---|
| 41 | /*! | 
|---|
| 42 | Read up to \p n bytes into \p buffer, returning the number read | 
|---|
| 43 | (zero if no data is available or input is shutdown).  \p buffer | 
|---|
| 44 | may be NULL in which case the data is discarded. | 
|---|
| 45 | */ | 
|---|
| 46 | virtual UInt32          read(void* buffer, UInt32 n) = 0; | 
|---|
| 47 |  | 
|---|
| 48 | //! Write to stream | 
|---|
| 49 | /*! | 
|---|
| 50 | Write \c n bytes from \c buffer to the stream.  If this can't | 
|---|
| 51 | complete immediately it will block.  Data may be buffered in | 
|---|
| 52 | order to return more quickly.  A output error event is generated | 
|---|
| 53 | when writing fails. | 
|---|
| 54 | */ | 
|---|
| 55 | virtual void            write(const void* buffer, UInt32 n) = 0; | 
|---|
| 56 |  | 
|---|
| 57 | //! Flush the stream | 
|---|
| 58 | /*! | 
|---|
| 59 | Waits until all buffered data has been written to the stream. | 
|---|
| 60 | */ | 
|---|
| 61 | virtual void            flush() = 0; | 
|---|
| 62 |  | 
|---|
| 63 | //! Shutdown input | 
|---|
| 64 | /*! | 
|---|
| 65 | Shutdown the input side of the stream.  Any pending input data is | 
|---|
| 66 | discarded and further reads immediately return 0. | 
|---|
| 67 | */ | 
|---|
| 68 | virtual void            shutdownInput() = 0; | 
|---|
| 69 |  | 
|---|
| 70 | //! Shutdown output | 
|---|
| 71 | /*! | 
|---|
| 72 | Shutdown the output side of the stream.  Any buffered output data | 
|---|
| 73 | is discarded and further writes generate output error events.  Use | 
|---|
| 74 | \c flush() before \c shutdownOutput() to send buffered output data. | 
|---|
| 75 | */ | 
|---|
| 76 | virtual void            shutdownOutput() = 0; | 
|---|
| 77 |  | 
|---|
| 78 | //@} | 
|---|
| 79 | //! @name accessors | 
|---|
| 80 | //@{ | 
|---|
| 81 |  | 
|---|
| 82 | //! Get event target | 
|---|
| 83 | /*! | 
|---|
| 84 | Returns the event target for events generated by this stream.  It | 
|---|
| 85 | should be the source stream in a chain of stream filters. | 
|---|
| 86 | */ | 
|---|
| 87 | virtual void*           getEventTarget() const = 0; | 
|---|
| 88 |  | 
|---|
| 89 | //! Test if \c read() will succeed | 
|---|
| 90 | /*! | 
|---|
| 91 | Returns true iff an immediate \c read() will return data.  This | 
|---|
| 92 | may or may not be the same as \c getSize() > 0, depending on the | 
|---|
| 93 | stream type. | 
|---|
| 94 | */ | 
|---|
| 95 | virtual bool            isReady() const = 0; | 
|---|
| 96 |  | 
|---|
| 97 | //! Get bytes available to read | 
|---|
| 98 | /*! | 
|---|
| 99 | Returns a conservative estimate of the available bytes to read | 
|---|
| 100 | (i.e. a number not greater than the actual number of bytes). | 
|---|
| 101 | Some streams may not be able to determine this and will always | 
|---|
| 102 | return zero. | 
|---|
| 103 | */ | 
|---|
| 104 | virtual UInt32          getSize() const = 0; | 
|---|
| 105 |  | 
|---|
| 106 | //! Get input ready event type | 
|---|
| 107 | /*! | 
|---|
| 108 | Returns the input ready event type.  A stream sends this event | 
|---|
| 109 | when \c read() will return with data. | 
|---|
| 110 | */ | 
|---|
| 111 | static CEvent::Type     getInputReadyEvent(); | 
|---|
| 112 |  | 
|---|
| 113 | //! Get output flushed event type | 
|---|
| 114 | /*! | 
|---|
| 115 | Returns the output flushed event type.  A stream sends this event | 
|---|
| 116 | when the output buffer has been flushed.  If there have been no | 
|---|
| 117 | writes since the event was posted, calling \c shutdownOutput() or | 
|---|
| 118 | \c close() will not discard any data and \c flush() will return | 
|---|
| 119 | immediately. | 
|---|
| 120 | */ | 
|---|
| 121 | static CEvent::Type     getOutputFlushedEvent(); | 
|---|
| 122 |  | 
|---|
| 123 | //! Get output error event type | 
|---|
| 124 | /*! | 
|---|
| 125 | Returns the output error event type.  A stream sends this event | 
|---|
| 126 | when a write has failed. | 
|---|
| 127 | */ | 
|---|
| 128 | static CEvent::Type     getOutputErrorEvent(); | 
|---|
| 129 |  | 
|---|
| 130 | //! Get input shutdown event type | 
|---|
| 131 | /*! | 
|---|
| 132 | Returns the input shutdown event type.  This is sent when the | 
|---|
| 133 | input side of the stream has shutdown.  When the input has | 
|---|
| 134 | shutdown, no more data will ever be available to read. | 
|---|
| 135 | */ | 
|---|
| 136 | static CEvent::Type     getInputShutdownEvent(); | 
|---|
| 137 |  | 
|---|
| 138 | //! Get output shutdown event type | 
|---|
| 139 | /*! | 
|---|
| 140 | Returns the output shutdown event type.  This is sent when the | 
|---|
| 141 | output side of the stream has shutdown.  When the output has | 
|---|
| 142 | shutdown, no more data can ever be written to the stream.  Any | 
|---|
| 143 | attempt to do so will generate a output error event. | 
|---|
| 144 | */ | 
|---|
| 145 | static CEvent::Type     getOutputShutdownEvent(); | 
|---|
| 146 |  | 
|---|
| 147 | //@} | 
|---|
| 148 |  | 
|---|
| 149 | private: | 
|---|
| 150 | static CEvent::Type     s_inputReadyEvent; | 
|---|
| 151 | static CEvent::Type     s_outputFlushedEvent; | 
|---|
| 152 | static CEvent::Type     s_outputErrorEvent; | 
|---|
| 153 | static CEvent::Type     s_inputShutdownEvent; | 
|---|
| 154 | static CEvent::Type     s_outputShutdownEvent; | 
|---|
| 155 | }; | 
|---|
| 156 |  | 
|---|
| 157 | #endif | 
|---|