Bincimap 2.0.16
Easy Imapping
Loading...
Searching...
No Matches
stdiodevice.cc
Go to the documentation of this file.
1
7#include "stdiodevice.h"
8#include <string>
9
10#include <sys/types.h>
11#include <sys/ioctl.h>
12#include "hasfilio.h"
13#ifdef HASFILIO
14#include <sys/filio.h>
15#endif
16#include <sys/select.h>
17#include <sys/time.h>
18#include <unistd.h>
19#include <errno.h>
20
21using namespace ::std;
22using namespace ::Binc;
23
24//------------------------------------------------------------------------
25StdIODevice::StdIODevice(int f) : IODevice(f)
26{
27}
28
29//------------------------------------------------------------------------
31{
32}
33
34//------------------------------------------------------------------------
35string StdIODevice::service(void) const
36{
37 return "client";
38}
39
40//------------------------------------------------------------------------
41bool StdIODevice::canRead(void) const
42{
43 size_t bytes;
44 return ioctl(fileno(stdin), FIONREAD, (char *) &bytes) > 0;
45}
46
47//------------------------------------------------------------------------
49{
50 fd_set writeMask;
51 FD_ZERO(&writeMask);
52 FD_SET(fileno(stdout), &writeMask);
53
54 struct timeval tv;
55 tv.tv_sec = timeout;
56 tv.tv_usec = 0;
57
58 int result = select(fileno(stdout) + 1, 0, &writeMask, 0, timeout ? &tv : 0);
59 if (result == 0) error = Timeout;
60 return result > 0;
61}
62
63//------------------------------------------------------------------------
65{
66 fd_set readMask;
67 FD_ZERO(&readMask);
68 FD_SET(fileno(stdin), &readMask);
69
70 struct timeval tv;
71 tv.tv_sec = timeout;
72 tv.tv_usec = 0;
73
74 int result = select(fileno(stdin) + 1, &readMask, 0, 0, timeout ? &tv : 0);
75 if (result == 0) error = Timeout;
76 return result > 0;
77}
78
79//------------------------------------------------------------------------
81{
82 for (;;) {
83 ssize_t wrote = ::write(fileno(stdout), outputBuffer.str().c_str(),
85
86 if (wrote == -1) {
87 if (errno == EINTR)
88 continue;
89 else
90 return WriteError;
91 }
92
94
95 if (wrote == (ssize_t) outputBuffer.getSize())
96 return WriteDone;
97 return WriteWait;
98 }
99}
100
101//------------------------------------------------------------------------
103{
104 if (!waitForRead())
105 return false;
106
107 char buf[4096];
108 ssize_t red = read(fileno(stdin), buf, sizeof(buf) - 1);
109 if (red <= 0)
110 return false;
111
112 buf[red] = '\0';
113 inputBuffer << buf;
114 return true;
115}
const std::string & str(void) const
Definition: convert.cc:58
std::string popString(unsigned int size)
Definition: convert.cc:25
unsigned int getSize(void) const
Definition: convert.cc:70
The IODevice class provides a framework for reading and writing to device.
Definition: iodevice.h:31
unsigned int timeout
Definition: iodevice.h:346
BincStream inputBuffer
Definition: iodevice.h:339
BincStream outputBuffer
Definition: iodevice.h:340
bool waitForRead(void) const
Definition: stdiodevice.cc:64
bool waitForWrite(void) const
Definition: stdiodevice.cc:48
WriteResult write(void)
Definition: stdiodevice.cc:80
std::string service(void) const
Definition: stdiodevice.cc:35
bool fillInputBuffer(void)
Definition: stdiodevice.cc:102
bool canRead(void) const
Definition: stdiodevice.cc:41
Definition: bincimapd.cc:9
Declaration of the StdIODevice class.