Dangerous SNMP
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
bytestream.hpp
1 #pragma once
2 
3 #include <cstdint>
4 #include <condition_variable>
5 #include <list>
6 #include <memory>
7 #include <mutex>
8 #include <string>
9 #include <vector>
10 
11 namespace dangerous { namespace snmp {
12 
30 class ByteStream {
31 public:
36  enum Access {
41  };
42 
43 public:
47  ByteStream();
48 
52  ~ByteStream();
53 
59  void copyFrom( const std::string& bytes, Access access );
60 
66  void copyFrom( const std::vector<char>& bytes, Access access );
67 
74  void copyFrom( const ByteStream& byteStream, Access access );
75 
84  void linkFrom( char* bytes, unsigned int bytesSize, Access access );
85 
91  void copyTo( std::vector<char>& buffer ) const;
92 
98  void copyTo( std::string& buffer ) const;
99 
107  int compareWith( const char* buffer, unsigned int bufferLength ) const;
108 
113  inline const bool isErrored() const { return _isErrored; }
114 
120  inline const bool lastReadTimedOut() const { return _lastReadTimedOut; }
121 
128  inline const bool isLive() const { return _isLive; }
129 
134  void setLive( bool isLive );
135 
141  inline std::chrono::milliseconds readTimeout() const { return _readTimeout; }
142 
148  inline void readTimeout( const std::chrono::milliseconds& timeout ) { _readTimeout = timeout; }
149 
154  unsigned int remainingReadLength() const;
155 
160  unsigned int remainingWriteLength() const;
161 
168  void reset();
169 
170  /*
171  * Peek functions.
172  */
173 
180  bool peekByte( char& byte );
181 
182  /*
183  * Read functions.
184  */
185 
191  bool readByte( char& byte );
192 
200  bool readBytes( char* buffer, unsigned int bufferSize, unsigned int& bytesRead );
201 
202  /*
203  * Write functions.
204  * All of these functions will advance the write head.
205  */
206 
212  bool writeByte( char byte );
213 
221  bool writeBytes( const char* buffer, unsigned int bufferSize, unsigned int& bytesWritten );
222 
223  /*
224  * Packet functions.
225  */
226 
230  void endPacket();
231 
236  bool hasNextPacket();
237 
242  bool nextPacket();
243 
244  /*
245  * Debugging functions.
246  */
247 
254  std::string debugString();
255 
256 protected:
260 
264 
269  bool _isLive;
271  mutable std::mutex liveLock;
273  mutable std::condition_variable liveConditionVariable;
274 
277  bool ownBytes;
279  char* bytes;
281  unsigned int bytesSize;
283  unsigned int readIndex;
285  unsigned int writeIndex;
286 
291  std::list<unsigned int> boundaries;
292 
294  std::chrono::milliseconds _readTimeout;
295 
303  void freeBytes();
304 
309  unsigned int remainingReadLength_nolock() const;
310 
315  unsigned int remainingWriteLength_nolock() const;
316 
323  bool waitForReading( unsigned int increment );
324 
331  bool grow( unsigned int increment );
332 
333 };
334 
335 } }
336 
bool writeByte(char byte)
This writes a byte to the ByteStream.
ByteStream()
Default constructor; this creates a new, empty ByteStream.
A ByteStream is an object that is basically a big wrapper around a character buffer.
Definition: bytestream.hpp:30
void readTimeout(const std::chrono::milliseconds &timeout)
This sets the read timeout for the stream.
Definition: bytestream.hpp:148
bool peekByte(char &byte)
This reads a byte from the ByteStream.
bool _isErrored
This is whether or not this ByteStream is in an errored state.
Definition: bytestream.hpp:259
const bool isLive() const
This returns whether or not the stream is connected to "live" data, such as a socket.
Definition: bytestream.hpp:128
bool readByte(char &byte)
This reads a byte from the ByteStream.
int compareWith(const char *buffer, unsigned int bufferLength) const
This compares the ByteStream to the given character buffer and returns the equivalent of "memcmp"...
void copyFrom(const std::string &bytes, Access access)
This copies the bytes from the given string into the ByteStream's character buffer.
std::list< unsigned int > boundaries
This is the list of the boundaries (if applicable) for this stream.
Definition: bytestream.hpp:291
std::chrono::milliseconds _readTimeout
This is the timeout for reads.
Definition: bytestream.hpp:294
bool writeBytes(const char *buffer, unsigned int bufferSize, unsigned int &bytesWritten)
This writes the bytes given to the ByteStream.
Only read operations may be performed.
Definition: bytestream.hpp:38
Both read and write operations may be performed.
Definition: bytestream.hpp:40
Access
This defines the kind of access that a ByteStream will have.
Definition: bytestream.hpp:36
std::condition_variable liveConditionVariable
TODO: DOCUMENT THIS VARIABLE.
Definition: bytestream.hpp:273
std::chrono::milliseconds readTimeout() const
This returns the current read timeout for the stream.
Definition: bytestream.hpp:141
std::mutex liveLock
This is the mutex for handling streaming operations.
Definition: bytestream.hpp:271
void setLive(bool isLive)
This sets the "live" state of the stream.
unsigned int bytesSize
This is the current size of "bytes".
Definition: bytestream.hpp:281
unsigned int remainingReadLength() const
This returns the number of bytes remaining in the stream.
unsigned int remainingWriteLength() const
This returns the number of bytes remaining in the stream.
unsigned int readIndex
This is the current position for reading.
Definition: bytestream.hpp:283
bool nextPacket()
This advances the ByteStream to the next packet, if there is one.
void endPacket()
This ends the current packet and starts a new one.
const bool isErrored() const
This returns whether or not the stream is in an errored state.
Definition: bytestream.hpp:113
unsigned int remainingWriteLength_nolock() const
This returns the number of bytes remaining in the stream.
std::string debugString()
This returns a string that can be printed in order to see debugging information about the ByteStream...
unsigned int writeIndex
This is the current position for writing.
Definition: bytestream.hpp:285
void reset()
This resets the internal character buffer (if owned by the ByteStream).
bool ownBytes
This is whether or not this ByteStream owns the memory pointed to by "bytes".
Definition: bytestream.hpp:277
const bool lastReadTimedOut() const
If the last "read*" function failed, then this will return whether or not the cause was a timeout...
Definition: bytestream.hpp:120
bool readBytes(char *buffer, unsigned int bufferSize, unsigned int &bytesRead)
This reads an arbitrary amount of bytes into the specified character buffer.
void linkFrom(char *bytes, unsigned int bytesSize, Access access)
This links the bytes given to the ByteStream's character buffer.
bool hasNextPacket()
This returns whether or not there is a next packet.
unsigned int remainingReadLength_nolock() const
This returns the number of bytes remaining in the stream.
bool _lastReadTimedOut
If this is a live stream, then this will be set to true if the last "read*" function failed due to a ...
Definition: bytestream.hpp:263
void freeBytes()
This frees the memory associated with the internal character buffer.
void copyTo(std::vector< char > &buffer) const
This copies the portion of the ByteStream that's currently available for reading to the specified tar...
char * bytes
This is the buffer allocated for this ByteStream.
Definition: bytestream.hpp:279
bool grow(unsigned int increment)
This attempts to increase the size of the buffer by the specified amount.
bool waitForReading(unsigned int increment)
This attempts to wait for the given number of bytes to become available for reading.
bool _isLive
This is whether or not this ByteStream is hooked up (in some way) to live data.
Definition: bytestream.hpp:269