9 static const char rcsid[] __attribute__((used)) =
"$Id: Pipe.cpp 1653 2016-02-28 19:54:59Z sella $";
16 using namespace sella::net;
18 Pipe::Pipe(
bool nonBlock)
throw (SocketException) {
19 if (::pipe(fd) != 0) {
20 THROW2(SocketException,
"pipe()");
24 fcntl(fd[0], F_SETFL, O_NONBLOCK);
33 ssize_t Pipe::write(
const void *data,
size_t len)
throw (SocketException) {
38 if ((c = ::write(fd[1], data, len)) < 0) {
45 #if ((EAGAIN) != (EWOULDBLOCK))
49 if ((flg = fcntl(fd[1], F_GETFL)) != -1 && (flg & O_NONBLOCK)) {
50 THROW_CODE(SocketException, EWOULDBLOCK,
"Nonblocking socket would have blocked");
53 THROW_CODE(SocketException, EWOULDBLOCK,
"Send timed out");
56 THROW2(SocketException,
"write()");
60 }
while ((r -= c) > 0);
65 ssize_t Pipe::write(
const std::string &data)
throw (SocketException) {
66 return write((
const void*) data.c_str(), data.size());
69 ssize_t Pipe::write(
const std::vector<uint8_t> &data)
throw (SocketException) {
70 return write((
const void*) data.data(), data.size());
73 ssize_t Pipe::read(
void *data,
size_t len)
throw (SocketException) {
77 if ((c = ::read(fd[0], data, len)) < 0) {
82 #if ((EAGAIN) != (EWOULDBLOCK))
86 if ((flg = fcntl(fd[0], F_GETFL)) != -1 && (flg & O_NONBLOCK)) {
87 THROW_CODE(SocketException, EWOULDBLOCK,
"Nonblocking socket would have blocked");
90 THROW_CODE(SocketException, EWOULDBLOCK,
"Receive timed out");
93 THROW2(SocketException,
"read()");
99 ssize_t Pipe::read(std::string &data,
size_t len)
throw (SocketException) {
101 ssize_t size = read(buf, len);
103 std::string(buf, buf + len).swap(data);
108 ssize_t Pipe::read(std::vector<uint8_t> &data,
size_t len)
throw (SocketException) {
110 ssize_t size = read(buf, len);
112 std::vector<uint8_t>(buf, buf + len).swap(data);
117 int Pipe::getWriteFD(
void) {
121 int Pipe::getReadFD(
void) {
125 const Pipe::shared Pipe::shared_ptr(
bool nonBlock)
throw (SocketException) {
126 return std::make_shared<Pipe>(nonBlock);