Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * D-Bus++ - C++ bindings for D-Bus |
| 4 | * |
| 5 | * Copyright (C) 2005-2007 Paolo Durante <shackan@gmail.com> |
| 6 | * |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; either |
| 11 | * version 2.1 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #ifdef HAVE_CONFIG_H |
| 25 | #include <config.h> |
| 26 | #endif |
| 27 | |
| 28 | /* Project */ |
| 29 | #include <dbus-c++/pipe.h> |
| 30 | #include <dbus-c++/util.h> |
| 31 | #include <dbus-c++/error.h> |
| 32 | |
| 33 | /* STD */ |
| 34 | #include <unistd.h> |
| 35 | #include <sys/poll.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <errno.h> |
| 38 | #include <cassert> |
| 39 | |
| 40 | using namespace DBus; |
| 41 | using namespace std; |
| 42 | |
| 43 | Pipe::Pipe(void(*handler)(const void *data, void *buffer, unsigned int nbyte), const void *data) : |
| 44 | _handler(handler), |
| 45 | _fd_write(0), |
| 46 | _fd_read(0), |
| 47 | _data(data) |
| 48 | { |
| 49 | int fd[2]; |
| 50 | |
| 51 | if (pipe(fd) == 0) |
| 52 | { |
| 53 | _fd_read = fd[0]; |
| 54 | _fd_write = fd[1]; |
| 55 | fcntl(_fd_read, F_SETFL, O_NONBLOCK); |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | throw Error("PipeError:errno", toString(errno).c_str()); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void Pipe::write(const void *buffer, unsigned int nbytes) |
| 64 | { |
| 65 | // TODO: ignoring return of read/write generates warning; maybe relevant for eventloop work... |
| 66 | // first write the size into the pipe... |
| 67 | ::write(_fd_write, static_cast <const void *>(&nbytes), sizeof(nbytes)); |
| 68 | |
| 69 | // ...then write the real data |
| 70 | ::write(_fd_write, buffer, nbytes); |
| 71 | } |
| 72 | |
| 73 | ssize_t Pipe::read(void *buffer, unsigned int &nbytes) |
| 74 | { |
| 75 | // TODO: ignoring return of read/write generates warning; maybe relevant for eventloop work... |
| 76 | // first read the size from the pipe... |
| 77 | ::read(_fd_read, &nbytes, sizeof(nbytes)); |
| 78 | |
| 79 | //ssize_t size = 0; |
| 80 | return ::read(_fd_read, buffer, nbytes); |
| 81 | } |
| 82 | |
| 83 | void Pipe::signal() |
| 84 | { |
| 85 | // TODO: ignoring return of read/write generates warning; maybe relevant for eventloop work... |
| 86 | ::write(_fd_write, '\0', 1); |
| 87 | } |