1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
23 #include "base/i2-base.hpp"
24 #include "base/object.hpp"
25 #include <boost/signals2.hpp>
39 struct I2_BASE_API StreamReadContext
41 StreamReadContext(void)
42 : Buffer(NULL), Size(0), MustRead(true), Eof(false)
45 ~StreamReadContext(void)
50 bool FillFromStream(const intrusive_ptr<Stream>& stream, bool may_wait);
51 void DropData(size_t count);
71 class I2_BASE_API Stream : public Object
74 DECLARE_PTR_TYPEDEFS(Stream);
77 * Reads data from the stream without removing it from the stream buffer.
79 * @param buffer The buffer where data should be stored. May be NULL if you're
80 * not actually interested in the data.
81 * @param count The number of bytes to read from the queue.
82 * @param allow_partial Whether to allow partial reads.
83 * @returns The number of bytes actually read.
85 virtual size_t Peek(void *buffer, size_t count, bool allow_partial = false);
88 * Reads data from the stream.
90 * @param buffer The buffer where data should be stored. May be NULL if you're
91 * not actually interested in the data.
92 * @param count The number of bytes to read from the queue.
93 * @param allow_partial Whether to allow partial reads.
94 * @returns The number of bytes actually read.
96 virtual size_t Read(void *buffer, size_t count, bool allow_partial = false) = 0;
99 * Writes data to the stream.
101 * @param buffer The data that is to be written.
102 * @param count The number of bytes to write.
103 * @returns The number of bytes written
105 virtual void Write(const void *buffer, size_t count) = 0;
108 * Causes the stream to be closed (via Close()) once all pending data has been
111 virtual void Shutdown(void);
114 * Closes the stream and releases resources.
116 virtual void Close(void);
119 * Checks whether we've reached the end-of-file condition.
121 * @returns true if EOF.
123 virtual bool IsEof(void) const = 0;
126 * Waits until data can be read from the stream.
128 bool WaitForData(int timeout = -1);
130 virtual bool SupportsWaiting(void) const;
132 virtual bool IsDataAvailable(void) const;
134 void RegisterDataHandler(const boost::function<void(const Stream::Ptr&)>& handler);
136 StreamReadStatus ReadLine(String *line, StreamReadContext& context, bool may_wait = false);
139 void SignalDataAvailable(void);
142 boost::signals2::signal<void(const Stream::Ptr&)> OnDataAvailable;
144 boost::mutex m_Mutex;
145 boost::condition_variable m_CV;
150 #endif /* STREAM_H */