Bincimap 2.0.16
Easy Imapping
Loading...
Searching...
No Matches
iodevice.cc
Go to the documentation of this file.
1
7#include "iodevice.h"
8#include "convert.h" // BincStream
9#include "session.h" // getEnv/hasEnv
10
11#include <stdlib.h>
12#include <unistd.h>
13
14using namespace ::std;
15using namespace ::Binc;
16
17//------------------------------------------------------------------------
18IODevice::IODevice(int f) : flags(f | IsEnabled),
19 maxInputBufferSize(0),
20 maxOutputBufferSize(0),
21 timeout(0),
22 readCount(0), writeCount(0),
23 outputLevel(ErrorLevel),
24 outputLevelLimit(ErrorLevel),
25 error(Unknown), errorString("Unknown device error"),
26 dumpfd(0)
27{
28}
29
30//------------------------------------------------------------------------
32{
33}
34
35//------------------------------------------------------------------------
36IODevice &IODevice::operator <<(ostream &(*source)(ostream &))
37{
39 return *this;
40
41 static std::ostream &(*endl_funcptr)(ostream &) = endl;
42
43 if (source != endl_funcptr) return *this;
44
45 outputBuffer << "\r\n";
46
47 if (dumpfd) ::write(dumpfd, "\r\n", 2);
48
49 if (flags & FlushesOnEndl)
50 flush();
51 else if (flags & HasOutputLimit)
53 flush();
54
55 return *this;
56}
57
58//------------------------------------------------------------------------
59bool IODevice::canRead(void) const
60{
61 return false;
62}
63
64//------------------------------------------------------------------------
66{
67 if (!(flags & IsEnabled)) return;
68
71}
72
73//------------------------------------------------------------------------
75{
76 if (!(flags & IsEnabled)) return true;
77
78 WriteResult writeResult = WriteWait;
79 do {
80 unsigned int s = outputBuffer.getSize();
81 if (s == 0) break;
82 if (!waitForWrite()) return false;
83 writeResult = write();
84 if (writeResult == WriteError) return false;
86 } while (outputBuffer.getSize() > 0 && writeResult == WriteWait);
87
89
90 return true;
91}
92
93//------------------------------------------------------------------------
94void IODevice::setFlags(unsigned int f)
95{
96 flags |= f;
97}
98
99//------------------------------------------------------------------------
100void IODevice::clearFlags(unsigned int f)
101{
102 flags &= ~f;
103}
104
105//------------------------------------------------------------------------
107{
108 maxInputBufferSize = max;
109}
110
111//------------------------------------------------------------------------
113{
115}
116
117//------------------------------------------------------------------------
118void IODevice::setTimeout(unsigned int t)
119{
120 timeout = t;
121
122 if (t)
123 flags |= HasTimeout;
124 else
125 flags &= ~HasTimeout;
126}
127
128//------------------------------------------------------------------------
129unsigned int IODevice::getTimeout(void) const
130{
131 return timeout;
132}
133
134//------------------------------------------------------------------------
136{
137 outputLevel = level;
138}
139
140//------------------------------------------------------------------------
142{
143 return outputLevel;
144}
145
146//------------------------------------------------------------------------
148{
149 outputLevelLimit = level;
150}
151
152//------------------------------------------------------------------------
154{
155 return outputLevelLimit;
156}
157
158//------------------------------------------------------------------------
159bool IODevice::readStr(string *dest, unsigned int max)
160{
161 // If max is 0, fill the input buffer once only if it's empty.
162 if (!max && inputBuffer.getSize() == 0 && !fillInputBuffer()) return false;
163
164 // If max is != 0, wait until we have max.
165 while (max && inputBuffer.getSize() < max) {
166 if (!fillInputBuffer()) return false;
167 }
168
169 unsigned int bytesToRead = max ? max : inputBuffer.getSize();
170 *dest += inputBuffer.str().substr(0, bytesToRead);
171 if (dumpfd) {
172 ::write(dumpfd, inputBuffer.str().substr(0, bytesToRead).c_str(), bytesToRead);
173 }
174
175 inputBuffer.popString(bytesToRead);
176 readCount += bytesToRead;
177
178 return true;
179}
180
181//------------------------------------------------------------------------
182bool IODevice::readChar(char *dest)
183{
184 if (inputBuffer.getSize() == 0 && !fillInputBuffer()) return false;
185
186 char c = inputBuffer.popChar();
187 if (dest) *dest = c;
188 if (dumpfd) ::write(dumpfd, &c, 1);
189
190 ++readCount;
191
192 return true;
193}
194
195//------------------------------------------------------------------------
197{
199}
200
201//------------------------------------------------------------------------
202void IODevice::unreadStr(const string &s)
203{
205}
206
207//------------------------------------------------------------------------
209{
210 char dest = '\0';
211 do {
212 if (!readChar(&dest)) return false;
213 if (dumpfd) ::write(dumpfd, &dest, 1);
214 } while (c != dest);
215
216 return true;
217}
218
219//------------------------------------------------------------------------
220string IODevice::service(void) const
221{
222 return "nul";
223}
224
225//------------------------------------------------------------------------
227{
228 return false;
229}
230
231//------------------------------------------------------------------------
232bool IODevice::waitForRead(void) const
233{
234 return false;
235}
236
237//------------------------------------------------------------------------
239{
240 return WriteError;
241}
242
243//------------------------------------------------------------------------
245{
246 return false;
247}
248
249//------------------------------------------------------------------------
251{
252 return error;
253}
254
255//------------------------------------------------------------------------
257{
258 return errorString;
259}
260
261//------------------------------------------------------------------------
262unsigned int IODevice::getReadCount(void) const
263{
264 return readCount;
265}
266
267//------------------------------------------------------------------------
268unsigned int IODevice::getWriteCount(void) const
269{
270 return writeCount;
271}
272
273//------------------------------------------------------------------------
275{
276 BincStream ss;
277 ss << "/tmp/bincimap-dump-" << (int) time(0) << "-"
278 << Session::getInstance().getIP() << "-XXXXXX";
279 char *safename = strdup(ss.str().c_str());
280 dumpfd = mkstemp(safename);
281 if (dumpfd == -1) dumpfd = 0;
282 delete[] safename;
283}
void unpopStr(const std::string &s)
Definition: convert.cc:52
const std::string & str(void) const
Definition: convert.cc:58
std::string popString(unsigned int size)
Definition: convert.cc:25
char popChar(void)
Definition: convert.cc:35
unsigned int getSize(void) const
Definition: convert.cc:70
void unpopChar(char c)
Definition: convert.cc:46
void clear(void)
Definition: convert.cc:64
The IODevice class provides a framework for reading and writing to device.
Definition: iodevice.h:31
virtual bool waitForRead(void) const
Definition: iodevice.cc:232
void setMaxOutputBufferSize(unsigned int max)
Definition: iodevice.cc:112
bool flush(void)
Definition: iodevice.cc:74
virtual ~IODevice(void)
Definition: iodevice.cc:31
void setFlags(unsigned int f)
Definition: iodevice.cc:94
bool readStr(std::string *dest, unsigned int max=0)
Definition: iodevice.cc:159
virtual bool waitForWrite(void) const
Definition: iodevice.cc:226
void setTimeout(unsigned int t)
Definition: iodevice.cc:118
unsigned int timeout
Definition: iodevice.h:346
virtual WriteResult write(void)
Definition: iodevice.cc:238
bool readChar(char *dest=0)
Definition: iodevice.cc:182
void clearFlags(unsigned int f)
Definition: iodevice.cc:100
void enableProtocolDumping(void)
Definition: iodevice.cc:274
IODevice & operator<<(const T &source)
Definition: iodevice.h:361
LogLevel outputLevel
Definition: iodevice.h:351
virtual std::string service(void) const
Definition: iodevice.cc:220
virtual bool fillInputBuffer(void)
Definition: iodevice.cc:244
void setMaxInputBufferSize(unsigned int max)
Definition: iodevice.cc:106
LogLevel getOutputLevel(void) const
Definition: iodevice.cc:141
virtual bool canRead(void) const
Definition: iodevice.cc:59
bool skipTo(char c)
Definition: iodevice.cc:208
Error getLastError(void) const
Definition: iodevice.cc:250
unsigned int writeCount
Definition: iodevice.h:349
BincStream inputBuffer
Definition: iodevice.h:339
void unreadChar(char c)
Definition: iodevice.cc:196
LogLevel getOutputLevelLimit(void) const
Definition: iodevice.cc:153
unsigned int maxOutputBufferSize
Definition: iodevice.h:344
unsigned int maxInputBufferSize
Definition: iodevice.h:343
std::string errorString
Definition: iodevice.h:355
unsigned int getReadCount(void) const
Definition: iodevice.cc:262
unsigned int flags
Definition: iodevice.h:342
void setOutputLevel(LogLevel level)
Definition: iodevice.cc:135
unsigned int readCount
Definition: iodevice.h:348
void unreadStr(const std::string &s)
Definition: iodevice.cc:202
LogLevel outputLevelLimit
Definition: iodevice.h:352
void clear(void)
Definition: iodevice.cc:65
unsigned int getTimeout(void) const
Definition: iodevice.cc:129
unsigned int getWriteCount(void) const
Definition: iodevice.cc:268
std::string getLastErrorString(void) const
Definition: iodevice.cc:256
void setOutputLevelLimit(LogLevel level)
Definition: iodevice.cc:147
BincStream outputBuffer
Definition: iodevice.h:340
const std::string & getIP(void) const
Definition: session.cc:64
static Session & getInstance(void)
Definition: session.cc:33
Declaration of miscellaneous convertion functions.
Declaration of the IODevice class.
Definition: bincimapd.cc:9
int readChar(void)