Blame src/pipe.cpp

Packit 4a0280
/*
Packit 4a0280
 *
Packit 4a0280
 *  D-Bus++ - C++ bindings for D-Bus
Packit 4a0280
 *
Packit 4a0280
 *  Copyright (C) 2005-2007  Paolo Durante <shackan@gmail.com>
Packit 4a0280
 *
Packit 4a0280
 *
Packit 4a0280
 *  This library is free software; you can redistribute it and/or
Packit 4a0280
 *  modify it under the terms of the GNU Lesser General Public
Packit 4a0280
 *  License as published by the Free Software Foundation; either
Packit 4a0280
 *  version 2.1 of the License, or (at your option) any later version.
Packit 4a0280
 *
Packit 4a0280
 *  This library is distributed in the hope that it will be useful,
Packit 4a0280
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a0280
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 4a0280
 *  Lesser General Public License for more details.
Packit 4a0280
 *
Packit 4a0280
 *  You should have received a copy of the GNU Lesser General Public
Packit 4a0280
 *  License along with this library; if not, write to the Free Software
Packit 4a0280
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Packit 4a0280
 *
Packit 4a0280
 */
Packit 4a0280
Packit 4a0280
#ifdef HAVE_CONFIG_H
Packit 4a0280
#include <config.h>
Packit 4a0280
#endif
Packit 4a0280
Packit 4a0280
/* Project */
Packit 4a0280
#include <dbus-c++/pipe.h>
Packit 4a0280
#include <dbus-c++/util.h>
Packit 4a0280
#include <dbus-c++/error.h>
Packit 4a0280
Packit 4a0280
/* STD */
Packit 4a0280
#include <unistd.h>
Packit 4a0280
#include <sys/poll.h>
Packit 4a0280
#include <fcntl.h>
Packit 4a0280
#include <errno.h>
Packit 4a0280
#include <cassert>
Packit 4a0280
Packit 4a0280
using namespace DBus;
Packit 4a0280
using namespace std;
Packit 4a0280
Packit 4a0280
Pipe::Pipe(void(*handler)(const void *data, void *buffer, unsigned int nbyte), const void *data) :
Packit 4a0280
  _handler(handler),
Packit 4a0280
  _fd_write(0),
Packit 4a0280
  _fd_read(0),
Packit 4a0280
  _data(data)
Packit 4a0280
{
Packit 4a0280
  int fd[2];
Packit 4a0280
Packit 4a0280
  if (pipe(fd) == 0)
Packit 4a0280
  {
Packit 4a0280
    _fd_read = fd[0];
Packit 4a0280
    _fd_write = fd[1];
Packit 4a0280
    fcntl(_fd_read, F_SETFL, O_NONBLOCK);
Packit 4a0280
  }
Packit 4a0280
  else
Packit 4a0280
  {
Packit 4a0280
    throw Error("PipeError:errno", toString(errno).c_str());
Packit 4a0280
  }
Packit 4a0280
}
Packit 4a0280
Packit 4a0280
void Pipe::write(const void *buffer, unsigned int nbytes)
Packit 4a0280
{
Packit 4a0280
  // TODO: ignoring return of read/write generates warning; maybe relevant for eventloop work...
Packit 4a0280
  // first write the size into the pipe...
Packit 4a0280
  ::write(_fd_write, static_cast <const void *>(&nbytes), sizeof(nbytes));
Packit 4a0280
Packit 4a0280
  // ...then write the real data
Packit 4a0280
  ::write(_fd_write, buffer, nbytes);
Packit 4a0280
}
Packit 4a0280
Packit 4a0280
ssize_t Pipe::read(void *buffer, unsigned int &nbytes)
Packit 4a0280
{
Packit 4a0280
  // TODO: ignoring return of read/write generates warning; maybe relevant for eventloop work...
Packit 4a0280
  // first read the size from the pipe...
Packit 4a0280
  ::read(_fd_read, &nbytes, sizeof(nbytes));
Packit 4a0280
Packit 4a0280
  //ssize_t size = 0;
Packit 4a0280
  return ::read(_fd_read, buffer, nbytes);
Packit 4a0280
}
Packit 4a0280
Packit 4a0280
void Pipe::signal()
Packit 4a0280
{
Packit 4a0280
  // TODO: ignoring return of read/write generates warning; maybe relevant for eventloop work...
Packit Service 230bf1
  ::write(_fd_write, "", 1);
Packit 4a0280
}