1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
6 #include "base/i2-base.hpp"
7 #include "base/object.hpp"
8 #include <boost/signals2.hpp>
9 #include <boost/thread/mutex.hpp>
10 #include <boost/thread/condition_variable.hpp>
24 struct StreamReadContext
31 bool FillFromStream(const intrusive_ptr<Stream>& stream, bool may_wait);
32 void DropData(size_t count);
34 char *Buffer{nullptr};
52 class Stream : public Object
55 DECLARE_PTR_TYPEDEFS(Stream);
58 * Reads data from the stream without removing it from the stream buffer.
60 * @param buffer The buffer where data should be stored. May be nullptr if you're
61 * not actually interested in the data.
62 * @param count The number of bytes to read from the queue.
63 * @param allow_partial Whether to allow partial reads.
64 * @returns The number of bytes actually read.
66 virtual size_t Peek(void *buffer, size_t count, bool allow_partial = false);
69 * Reads data from the stream.
71 * @param buffer The buffer where data should be stored. May be nullptr if you're
72 * not actually interested in the data.
73 * @param count The number of bytes to read from the queue.
74 * @param allow_partial Whether to allow partial reads.
75 * @returns The number of bytes actually read.
77 virtual size_t Read(void *buffer, size_t count, bool allow_partial = false) = 0;
80 * Writes data to the stream.
82 * @param buffer The data that is to be written.
83 * @param count The number of bytes to write.
84 * @returns The number of bytes written
86 virtual void Write(const void *buffer, size_t count) = 0;
89 * Causes the stream to be closed (via Close()) once all pending data has been
92 virtual void Shutdown();
95 * Closes the stream and releases resources.
100 * Checks whether we've reached the end-of-file condition.
102 * @returns true if EOF.
104 virtual bool IsEof() const = 0;
107 * Waits until data can be read from the stream.
108 * Optionally with a timeout.
111 bool WaitForData(int timeout);
113 virtual bool SupportsWaiting() const;
115 virtual bool IsDataAvailable() const;
117 void RegisterDataHandler(const std::function<void(const Stream::Ptr&)>& handler);
119 StreamReadStatus ReadLine(String *line, StreamReadContext& context, bool may_wait = false);
122 void SignalDataAvailable();
125 boost::signals2::signal<void(const Stream::Ptr&)> OnDataAvailable;
127 boost::mutex m_Mutex;
128 boost::condition_variable m_CV;
133 #endif /* STREAM_H */