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...
This commit is contained in:
Andreas Volz 2010-08-12 23:14:45 +02:00
parent f0dcaa239f
commit 9af6f707eb
4 changed files with 52 additions and 39 deletions

View file

@ -43,6 +43,8 @@ public:
*/ */
void write(const void *buffer, unsigned int nbytes); 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 * 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. * if there's really no data to transport, but to activate the handler.
@ -51,12 +53,12 @@ public:
private: private:
void(*_handler)(const void *data, void *buffer, unsigned int nbyte); void(*_handler)(const void *data, void *buffer, unsigned int nbyte);
int fd_write; int _fd_write;
int fd_read; int _fd_read;
const void *data; const void *_data;
// allow construction only in BusDispatcher // allow construction only in BusDispatcher
Pipe (); Pipe (void(*handler)(const void *data, void *buffer, unsigned int nbyte), const void *data);
~Pipe () {}; ~Pipe () {};
friend class BusDispatcher; friend class BusDispatcher;

View file

@ -36,14 +36,14 @@ HEADER_FILES = \
$(HEADER_DIR)/api.h \ $(HEADER_DIR)/api.h \
$(HEADER_DIR)/eventloop.h \ $(HEADER_DIR)/eventloop.h \
$(HEADER_DIR)/eventloop-integration.h \ $(HEADER_DIR)/eventloop-integration.h \
$(HEADER_DIR)/pipe.h \
$(GLIB_H) $(ECORE_H) $(GLIB_H) $(ECORE_H)
lib_includedir=$(includedir)/dbus-c++-1/dbus-c++/ lib_includedir=$(includedir)/dbus-c++-1/dbus-c++/
lib_include_HEADERS = $(HEADER_FILES) lib_include_HEADERS = $(HEADER_FILES)
lib_LTLIBRARIES = libdbus-c++-1.la 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) \ 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)
pipe.cpp pipe.h
libdbus_c___1_la_LIBADD = $(dbus_LIBS) $(glib_LIBS) $(pthread_LIBS) $(ecore_LIBS) libdbus_c___1_la_LIBADD = $(dbus_LIBS) $(glib_LIBS) $(pthread_LIBS) $(ecore_LIBS)
libdbus_c___1_la_LDFLAGS = -no-undefined libdbus_c___1_la_LDFLAGS = -no-undefined

View file

@ -25,19 +25,20 @@
#include <config.h> #include <config.h>
#endif #endif
#include <string.h> /* Project */
#include <dbus-c++/eventloop-integration.h> #include <dbus-c++/eventloop-integration.h>
#include <dbus-c++/debug.h> #include <dbus-c++/debug.h>
#include <dbus-c++/pipe.h> #include <dbus-c++/pipe.h>
/* DBus */
#include <dbus/dbus.h>
/* STD */
#include <string.h>
#include <cassert>
#include <sys/poll.h> #include <sys/poll.h>
#include <fcntl.h> #include <fcntl.h>
#include <dbus/dbus.h>
#include <errno.h>
#include <cassert>
using namespace DBus; using namespace DBus;
using namespace std; using namespace std;
@ -97,16 +98,16 @@ void BusDispatcher::enter()
{ {
do_iteration(); do_iteration();
for (std::list <Pipe*>::const_iterator p_it = pipe_list.begin (); for (std::list <Pipe*>::iterator p_it = pipe_list.begin ();
p_it != pipe_list.end (); p_it != pipe_list.end ();
++p_it) ++p_it)
{ {
const Pipe* read_pipe = *p_it; Pipe* read_pipe = *p_it;
char buf; char buf;
char buf_str[1024]; char buf_str[1024];
int i = 0; int i = 0;
while (read(read_pipe->fd_read, &buf, 1) > 0) while (read_pipe->read((void*) &buf, 1) > 0)
{ {
buf_str[i] = buf; buf_str[i] = buf;
++i; ++i;
@ -114,7 +115,7 @@ void BusDispatcher::enter()
if (i > 0) 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) 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 (handler, data);
Pipe *new_pipe = new Pipe ();
new_pipe->_handler = handler;
new_pipe->data = data;
pipe_list.push_back (new_pipe); 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; return new_pipe;
} }

View file

@ -25,27 +25,52 @@
#include <config.h> #include <config.h>
#endif #endif
/* Project */
#include <dbus-c++/pipe.h> #include <dbus-c++/pipe.h>
#include <dbus-c++/util.h>
#include <dbus-c++/error.h>
/* STD */
#include <unistd.h> #include <unistd.h>
#include <sys/poll.h>
#include <fcntl.h>
#include <errno.h>
using namespace DBus; using namespace DBus;
using namespace std; using namespace std;
Pipe::Pipe () : Pipe::Pipe(void(*handler)(const void *data, void *buffer, unsigned int nbyte), const void *data) :
_handler (NULL), _handler(handler),
fd_write (0), _fd_write (0),
fd_read (0), _fd_read (0),
data (NULL) _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) 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() void Pipe::signal()
{ {
::write(fd_write, '\0', 1); ::write(_fd_write, '\0', 1);
} }