From 9af6f707eb62fa02d9ef473ab4cc87ebfdc1c37b Mon Sep 17 00:00:00 2001 From: Andreas Volz Date: Thu, 12 Aug 2010 23:14:45 +0200 Subject: [PATCH] ok, now at least the structure is better but there's still a problem with the new mechanism as the size of the data isn't put into the pipe. I need to fix tis before someone could really use it... --- include/dbus-c++/pipe.h | 10 +++++---- src/Makefile.am | 4 ++-- src/eventloop-integration.cpp | 38 +++++++++++----------------------- src/pipe.cpp | 39 ++++++++++++++++++++++++++++------- 4 files changed, 52 insertions(+), 39 deletions(-) diff --git a/include/dbus-c++/pipe.h b/include/dbus-c++/pipe.h index c994b6e..393f39d 100644 --- a/include/dbus-c++/pipe.h +++ b/include/dbus-c++/pipe.h @@ -43,6 +43,8 @@ public: */ void write(const void *buffer, unsigned int nbytes); + ssize_t read(void *buffer, unsigned int nbytes); + /*! * Simply write one single byte into the pipe. This is a shortcut * if there's really no data to transport, but to activate the handler. @@ -51,12 +53,12 @@ public: private: void(*_handler)(const void *data, void *buffer, unsigned int nbyte); - int fd_write; - int fd_read; - const void *data; + int _fd_write; + int _fd_read; + const void *_data; // allow construction only in BusDispatcher - Pipe (); + Pipe (void(*handler)(const void *data, void *buffer, unsigned int nbyte), const void *data); ~Pipe () {}; friend class BusDispatcher; diff --git a/src/Makefile.am b/src/Makefile.am index 6c7eec4..538c031 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -36,14 +36,14 @@ HEADER_FILES = \ $(HEADER_DIR)/api.h \ $(HEADER_DIR)/eventloop.h \ $(HEADER_DIR)/eventloop-integration.h \ + $(HEADER_DIR)/pipe.h \ $(GLIB_H) $(ECORE_H) lib_includedir=$(includedir)/dbus-c++-1/dbus-c++/ lib_include_HEADERS = $(HEADER_FILES) lib_LTLIBRARIES = libdbus-c++-1.la -libdbus_c___1_la_SOURCES = $(HEADER_FILES) interface.cpp object.cpp introspection.cpp debug.cpp types.cpp connection.cpp connection_p.h property.cpp dispatcher.cpp dispatcher_p.h pendingcall.cpp pendingcall_p.h error.cpp internalerror.h message.cpp message_p.h server.cpp server_p.h eventloop.cpp eventloop-integration.cpp $(GLIB_CPP) $(ECORE_CPP) \ - pipe.cpp pipe.h +libdbus_c___1_la_SOURCES = $(HEADER_FILES) interface.cpp object.cpp introspection.cpp debug.cpp types.cpp connection.cpp connection_p.h property.cpp dispatcher.cpp dispatcher_p.h pendingcall.cpp pendingcall_p.h error.cpp internalerror.h message.cpp message_p.h server.cpp server_p.h eventloop.cpp eventloop-integration.cpp pipe.cpp $(GLIB_CPP) $(ECORE_CPP) libdbus_c___1_la_LIBADD = $(dbus_LIBS) $(glib_LIBS) $(pthread_LIBS) $(ecore_LIBS) libdbus_c___1_la_LDFLAGS = -no-undefined diff --git a/src/eventloop-integration.cpp b/src/eventloop-integration.cpp index e4a9ad3..689f2da 100644 --- a/src/eventloop-integration.cpp +++ b/src/eventloop-integration.cpp @@ -25,19 +25,20 @@ #include #endif -#include - +/* Project */ #include #include #include +/* DBus */ +#include + +/* STD */ +#include +#include #include #include -#include -#include -#include - using namespace DBus; using namespace std; @@ -97,16 +98,16 @@ void BusDispatcher::enter() { do_iteration(); - for (std::list ::const_iterator p_it = pipe_list.begin (); + for (std::list ::iterator p_it = pipe_list.begin (); p_it != pipe_list.end (); ++p_it) { - const Pipe* read_pipe = *p_it; + Pipe* read_pipe = *p_it; char buf; char buf_str[1024]; int i = 0; - while (read(read_pipe->fd_read, &buf, 1) > 0) + while (read_pipe->read((void*) &buf, 1) > 0) { buf_str[i] = buf; ++i; @@ -114,7 +115,7 @@ void BusDispatcher::enter() if (i > 0) { - read_pipe->_handler (read_pipe->data, buf_str, i); + read_pipe->_handler (read_pipe->_data, buf_str, i); } } } @@ -135,24 +136,9 @@ void BusDispatcher::leave() Pipe *BusDispatcher::add_pipe(void(*handler)(const void *data, void *buffer, unsigned int nbyte), const void *data) { - int fd[2]; - Pipe *new_pipe = new Pipe (); - new_pipe->_handler = handler; - new_pipe->data = data; + Pipe *new_pipe = new Pipe (handler, data); pipe_list.push_back (new_pipe); - if (pipe(fd) == 0) - { - new_pipe->fd_read = fd[0]; - new_pipe->fd_write = fd[1]; - fcntl(new_pipe->fd_read, F_SETFL, O_NONBLOCK); - fcntl(new_pipe->fd_write, F_SETFL, O_NONBLOCK); - } - else - { - throw Error("PipeError:errno", toString(errno).c_str()); - } - return new_pipe; } diff --git a/src/pipe.cpp b/src/pipe.cpp index 6045e0b..328670a 100644 --- a/src/pipe.cpp +++ b/src/pipe.cpp @@ -25,27 +25,52 @@ #include #endif +/* Project */ #include +#include +#include +/* STD */ #include +#include +#include +#include using namespace DBus; using namespace std; -Pipe::Pipe () : - _handler (NULL), - fd_write (0), - fd_read (0), - data (NULL) +Pipe::Pipe(void(*handler)(const void *data, void *buffer, unsigned int nbyte), const void *data) : + _handler(handler), + _fd_write (0), + _fd_read (0), + _data(data) { + int fd[2]; + + if(pipe(fd) == 0) + { + _fd_read = fd[0]; + _fd_write = fd[1]; + fcntl(_fd_read, F_SETFL, O_NONBLOCK); + fcntl(_fd_write, F_SETFL, O_NONBLOCK); + } + else + { + throw Error("PipeError:errno", toString(errno).c_str()); + } } void Pipe::write(const void *buffer, unsigned int nbytes) { - ::write(fd_write, buffer, nbytes); + ::write(_fd_write, buffer, nbytes); +} + +ssize_t Pipe::read(void *buffer, unsigned int nbytes) +{ + return ::read(_fd_read, buffer, nbytes); } void Pipe::signal() { - ::write(fd_write, '\0', 1); + ::write(_fd_write, '\0', 1); }