diff options
author | Andreas Volz <andreas@er00923n.(none)> | 2010-08-17 22:43:24 +0200 |
---|---|---|
committer | Andreas Volz <andreas@er00923n.(none)> | 2010-08-17 22:43:24 +0200 |
commit | dc833f4a89bcb661274e453a5bb92a2d7655faba (patch) | |
tree | 9beeae3ddd19c63860f190925975d61a65db45cb | |
parent | 9af6f707eb62fa02d9ef473ab4cc87ebfdc1c37b (diff) | |
download | dbus-c++-dc833f4a89bcb661274e453a5bb92a2d7655faba.tar.gz |
this fixed the correct handling of the default main loop
but setting another DefaultTimeout isn't nice. I've to look later again into the problem. Maybe I did it wrong. Currently there's a compiler warning that could be ignores as long as Slot is created with void parameter, but I consider this as bug in the long time and I've to fix it!!
-rw-r--r-- | examples/echo/echo-client.cpp | 22 | ||||
-rw-r--r-- | include/dbus-c++/dispatcher.h | 2 | ||||
-rw-r--r-- | include/dbus-c++/glib-integration.h | 4 | ||||
-rw-r--r-- | include/dbus-c++/pipe.h | 2 | ||||
-rw-r--r-- | include/dbus-c++/util.h | 22 | ||||
-rw-r--r-- | src/eventloop-integration.cpp | 20 | ||||
-rw-r--r-- | src/eventloop.cpp | 1 | ||||
-rw-r--r-- | src/pipe.cpp | 14 |
8 files changed, 55 insertions, 32 deletions
diff --git a/examples/echo/echo-client.cpp b/examples/echo/echo-client.cpp index f6d59a8..f01f23a 100644 --- a/examples/echo/echo-client.cpp +++ b/examples/echo/echo-client.cpp @@ -36,6 +36,9 @@ EchoClient *g_client = NULL; DBus::Pipe *thread_pipe_list[THREADS]; +DBus::BusDispatcher dispatcher; +DBus::DefaultTimeout *timeout; + void *greeter_thread(void *arg) { char idstr[16]; @@ -50,8 +53,6 @@ void *greeter_thread(void *arg) return NULL; } -DBus::BusDispatcher dispatcher; - void niam(int sig) { spin = false; @@ -62,30 +63,30 @@ void niam(int sig) void handler1 (const void *data, void *buffer, unsigned int nbyte) { char *str = (char*) buffer; - cout << "buffer1: " << str << endl; + cout << "buffer1: " << str << ", size: " << nbyte << endl; for (int i = 0; i < 30 && spin; ++i) { - cout << g_client->Hello (str) << endl; + cout << "call1: " << g_client->Hello (str) << endl; } } void handler2 (const void *data, void *buffer, unsigned int nbyte) { char *str = (char*) buffer; - cout << "buffer2: " << str << endl; + cout << "buffer2: " << str << ", size: " << nbyte <<endl; for (int i = 0; i < 30 && spin; ++i) { - cout << g_client->Hello (str) << endl; + cout << "call2: " << g_client->Hello (str) << endl; } } void handler3 (const void *data, void *buffer, unsigned int nbyte) { char *str = (char*) buffer; - cout << "buffer3: " << str << endl; + cout << "buffer3: " << str << ", size: " << nbyte <<endl; for (int i = 0; i < 30 && spin; ++i) { - cout << g_client->Hello (str) << endl; + cout << "call3: " << g_client->Hello (str) << endl; } } @@ -96,7 +97,10 @@ int main() DBus::_init_threading(); - DBus::default_dispatcher = &dispatcher; + DBus::default_dispatcher = &dispatcher; + + // increase DBus-C++ frequency + new DBus::DefaultTimeout(100, false, &dispatcher); DBus::Connection conn = DBus::Connection::SessionBus(); diff --git a/include/dbus-c++/dispatcher.h b/include/dbus-c++/dispatcher.h index 10179ff..728f9d8 100644 --- a/include/dbus-c++/dispatcher.h +++ b/include/dbus-c++/dispatcher.h @@ -44,7 +44,7 @@ public: /*! * \brief Gets the timeout interval. * - * The dbus_timeout_handle() should be called each time this interval elapses, + * The handle() should be called each time this interval elapses, * starting after it elapses once. * * The interval may change during the life of the timeout; if so, the timeout diff --git a/include/dbus-c++/glib-integration.h b/include/dbus-c++/glib-integration.h index 76eae5e..0f68852 100644 --- a/include/dbus-c++/glib-integration.h +++ b/include/dbus-c++/glib-integration.h @@ -54,9 +54,9 @@ private: private: - GSource *_source; GMainContext *_ctx; int _priority; + GSource *_source; friend class BusDispatcher; }; @@ -79,9 +79,9 @@ private: private: - GSource *_source; GMainContext *_ctx; int _priority; + GSource *_source; friend class BusDispatcher; }; diff --git a/include/dbus-c++/pipe.h b/include/dbus-c++/pipe.h index 393f39d..752d48d 100644 --- a/include/dbus-c++/pipe.h +++ b/include/dbus-c++/pipe.h @@ -43,7 +43,7 @@ public: */ void write(const void *buffer, unsigned int nbytes); - ssize_t read(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 diff --git a/include/dbus-c++/util.h b/include/dbus-c++/util.h index b46732f..8b3806d 100644 --- a/include/dbus-c++/util.h +++ b/include/dbus-c++/util.h @@ -28,6 +28,8 @@ #include <sstream> #include <iostream> #include <iomanip> +#include <cassert> + #include "api.h" #include "debug.h" @@ -232,15 +234,29 @@ public: R operator()(P param) const { - /*if (_cb.get())*/ return _cb->call(param); + if (!empty()) + { + return _cb->call(param); + } + + // TODO: think about return type in this case + // this assert should help me to find the use case where it's needed... + //assert (false); } R call(P param) const { - /*if (_cb.get())*/ return _cb->call(param); + if (!empty()) + { + return _cb->call(param); + } + + // TODO: think about return type in this case + // this assert should help me to find the use case where it's needed... + //assert (false); } - bool empty() + bool empty() const { return _cb.get() == 0; } diff --git a/src/eventloop-integration.cpp b/src/eventloop-integration.cpp index 689f2da..0c86ffb 100644 --- a/src/eventloop-integration.cpp +++ b/src/eventloop-integration.cpp @@ -103,20 +103,14 @@ void BusDispatcher::enter() ++p_it) { Pipe* read_pipe = *p_it; - char buf; - char buf_str[1024]; - int i = 0; + char buffer[1024]; // TODO: should be max pipe size + unsigned int nbytes = 0; - while (read_pipe->read((void*) &buf, 1) > 0) - { - buf_str[i] = buf; - ++i; - } - - if (i > 0) - { - read_pipe->_handler (read_pipe->_data, buf_str, i); - } + while (read_pipe->read(buffer, nbytes) > 0) + { + read_pipe->_handler (read_pipe->_data, buffer, nbytes); + } + } } diff --git a/src/eventloop.cpp b/src/eventloop.cpp index 76b94f8..0268162 100644 --- a/src/eventloop.cpp +++ b/src/eventloop.cpp @@ -34,6 +34,7 @@ #include <dbus/dbus.h> using namespace DBus; +using namespace std; static double millis(timeval tv) { diff --git a/src/pipe.cpp b/src/pipe.cpp index 328670a..96f1b30 100644 --- a/src/pipe.cpp +++ b/src/pipe.cpp @@ -35,6 +35,7 @@ #include <sys/poll.h> #include <fcntl.h> #include <errno.h> +#include <cassert> using namespace DBus; using namespace std; @@ -52,7 +53,6 @@ Pipe::Pipe(void(*handler)(const void *data, void *buffer, unsigned int nbyte), c _fd_read = fd[0]; _fd_write = fd[1]; fcntl(_fd_read, F_SETFL, O_NONBLOCK); - fcntl(_fd_write, F_SETFL, O_NONBLOCK); } else { @@ -62,11 +62,19 @@ Pipe::Pipe(void(*handler)(const void *data, void *buffer, unsigned int nbyte), c void Pipe::write(const void *buffer, unsigned int nbytes) { + // first write the size into the pipe... + ::write(_fd_write, static_cast <const void*> (&nbytes), sizeof(nbytes)); + + // ...then write the real data ::write(_fd_write, buffer, nbytes); } -ssize_t Pipe::read(void *buffer, unsigned int nbytes) -{ +ssize_t Pipe::read(void *buffer, unsigned int &nbytes) +{ + // first read the size from the pipe... + ::read(_fd_read, &nbytes, sizeof (nbytes)); + + //ssize_t size = 0; return ::read(_fd_read, buffer, nbytes); } |