]> granicus.if.org Git - icinga2/blob - lib/base/stream.hpp
Update copyright headers for 2016
[icinga2] / lib / base / stream.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #ifndef STREAM_H
21 #define STREAM_H
22
23 #include "base/i2-base.hpp"
24 #include "base/object.hpp"
25 #include <boost/signals2.hpp>
26
27 namespace icinga
28 {
29
30 class String;
31 class Stream;
32
33 enum ConnectionRole
34 {
35         RoleClient,
36         RoleServer
37 };
38
39 struct I2_BASE_API StreamReadContext
40 {
41         StreamReadContext(void)
42                 : Buffer(NULL), Size(0), MustRead(true), Eof(false)
43         { }
44
45         ~StreamReadContext(void)
46         {
47                 free(Buffer);
48         }
49
50         bool FillFromStream(const intrusive_ptr<Stream>& stream, bool may_wait);
51         void DropData(size_t count);
52
53         char *Buffer;
54         size_t Size;
55         bool MustRead;
56         bool Eof;
57 };
58
59 enum StreamReadStatus
60 {
61         StatusNewItem,
62         StatusNeedData,
63         StatusEof
64 };
65
66 /**
67  * A stream.
68  *
69  * @ingroup base
70  */
71 class I2_BASE_API Stream : public Object
72 {
73 public:
74         DECLARE_PTR_TYPEDEFS(Stream);
75
76         /**
77          * Reads data from the stream without removing it from the stream buffer.
78          *
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.
84          */
85         virtual size_t Peek(void *buffer, size_t count, bool allow_partial = false);
86
87         /**
88          * Reads data from the stream.
89          *
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.
95          */
96         virtual size_t Read(void *buffer, size_t count, bool allow_partial = false) = 0;
97
98         /**
99          * Writes data to the stream.
100          *
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
104          */
105         virtual void Write(const void *buffer, size_t count) = 0;
106
107         /**
108          * Causes the stream to be closed (via Close()) once all pending data has been
109          * written.
110          */
111         virtual void Shutdown(void);
112
113         /**
114          * Closes the stream and releases resources.
115          */
116         virtual void Close(void);
117
118         /**
119          * Checks whether we've reached the end-of-file condition.
120          *
121          * @returns true if EOF.
122          */
123         virtual bool IsEof(void) const = 0;
124
125         /**
126          * Waits until data can be read from the stream.
127          */
128         bool WaitForData(int timeout = -1);
129
130         virtual bool SupportsWaiting(void) const;
131
132         virtual bool IsDataAvailable(void) const;
133
134         void RegisterDataHandler(const boost::function<void(const Stream::Ptr&)>& handler);
135
136         StreamReadStatus ReadLine(String *line, StreamReadContext& context, bool may_wait = false);
137
138 protected:
139         void SignalDataAvailable(void);
140
141 private:
142         boost::signals2::signal<void(const Stream::Ptr&)> OnDataAvailable;
143
144         boost::mutex m_Mutex;
145         boost::condition_variable m_CV;
146 };
147
148 }
149
150 #endif /* STREAM_H */