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!!
This commit is contained in:
Andreas Volz 2010-08-17 22:43:24 +02:00
parent 9af6f707eb
commit dc833f4a89
8 changed files with 54 additions and 31 deletions

View file

@ -36,6 +36,9 @@ EchoClient *g_client = NULL;
DBus::Pipe *thread_pipe_list[THREADS]; DBus::Pipe *thread_pipe_list[THREADS];
DBus::BusDispatcher dispatcher;
DBus::DefaultTimeout *timeout;
void *greeter_thread(void *arg) void *greeter_thread(void *arg)
{ {
char idstr[16]; char idstr[16];
@ -50,8 +53,6 @@ void *greeter_thread(void *arg)
return NULL; return NULL;
} }
DBus::BusDispatcher dispatcher;
void niam(int sig) void niam(int sig)
{ {
spin = false; spin = false;
@ -62,30 +63,30 @@ void niam(int sig)
void handler1 (const void *data, void *buffer, unsigned int nbyte) void handler1 (const void *data, void *buffer, unsigned int nbyte)
{ {
char *str = (char*) buffer; char *str = (char*) buffer;
cout << "buffer1: " << str << endl; cout << "buffer1: " << str << ", size: " << nbyte << endl;
for (int i = 0; i < 30 && spin; ++i) 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) void handler2 (const void *data, void *buffer, unsigned int nbyte)
{ {
char *str = (char*) buffer; char *str = (char*) buffer;
cout << "buffer2: " << str << endl; cout << "buffer2: " << str << ", size: " << nbyte <<endl;
for (int i = 0; i < 30 && spin; ++i) 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) void handler3 (const void *data, void *buffer, unsigned int nbyte)
{ {
char *str = (char*) buffer; char *str = (char*) buffer;
cout << "buffer3: " << str << endl; cout << "buffer3: " << str << ", size: " << nbyte <<endl;
for (int i = 0; i < 30 && spin; ++i) for (int i = 0; i < 30 && spin; ++i)
{ {
cout << g_client->Hello (str) << endl; cout << "call3: " << g_client->Hello (str) << endl;
} }
} }
@ -98,6 +99,9 @@ int main()
DBus::default_dispatcher = &dispatcher; DBus::default_dispatcher = &dispatcher;
// increase DBus-C++ frequency
new DBus::DefaultTimeout(100, false, &dispatcher);
DBus::Connection conn = DBus::Connection::SessionBus(); DBus::Connection conn = DBus::Connection::SessionBus();
EchoClient client (conn, ECHO_SERVER_PATH, ECHO_SERVER_NAME); EchoClient client (conn, ECHO_SERVER_PATH, ECHO_SERVER_NAME);

View file

@ -44,7 +44,7 @@ public:
/*! /*!
* \brief Gets the timeout interval. * \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. * starting after it elapses once.
* *
* The interval may change during the life of the timeout; if so, the timeout * The interval may change during the life of the timeout; if so, the timeout

View file

@ -54,9 +54,9 @@ private:
private: private:
GSource *_source;
GMainContext *_ctx; GMainContext *_ctx;
int _priority; int _priority;
GSource *_source;
friend class BusDispatcher; friend class BusDispatcher;
}; };
@ -79,9 +79,9 @@ private:
private: private:
GSource *_source;
GMainContext *_ctx; GMainContext *_ctx;
int _priority; int _priority;
GSource *_source;
friend class BusDispatcher; friend class BusDispatcher;
}; };

View file

@ -43,7 +43,7 @@ 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); 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

View file

@ -28,6 +28,8 @@
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
#include <cassert>
#include "api.h" #include "api.h"
#include "debug.h" #include "debug.h"
@ -232,15 +234,29 @@ public:
R operator()(P param) const 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 R call(P param) const
{ {
/*if (_cb.get())*/ return _cb->call(param); if (!empty())
{
return _cb->call(param);
} }
bool empty() // 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() const
{ {
return _cb.get() == 0; return _cb.get() == 0;
} }

View file

@ -103,20 +103,14 @@ void BusDispatcher::enter()
++p_it) ++p_it)
{ {
Pipe* read_pipe = *p_it; Pipe* read_pipe = *p_it;
char buf; char buffer[1024]; // TODO: should be max pipe size
char buf_str[1024]; unsigned int nbytes = 0;
int i = 0;
while (read_pipe->read((void*) &buf, 1) > 0) while (read_pipe->read(buffer, nbytes) > 0)
{ {
buf_str[i] = buf; read_pipe->_handler (read_pipe->_data, buffer, nbytes);
++i;
} }
if (i > 0)
{
read_pipe->_handler (read_pipe->_data, buf_str, i);
}
} }
} }

View file

@ -34,6 +34,7 @@
#include <dbus/dbus.h> #include <dbus/dbus.h>
using namespace DBus; using namespace DBus;
using namespace std;
static double millis(timeval tv) static double millis(timeval tv)
{ {

View file

@ -35,6 +35,7 @@
#include <sys/poll.h> #include <sys/poll.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <cassert>
using namespace DBus; using namespace DBus;
using namespace std; 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_read = fd[0];
_fd_write = fd[1]; _fd_write = fd[1];
fcntl(_fd_read, F_SETFL, O_NONBLOCK); fcntl(_fd_read, F_SETFL, O_NONBLOCK);
fcntl(_fd_write, F_SETFL, O_NONBLOCK);
} }
else 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) 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); ::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); return ::read(_fd_read, buffer, nbytes);
} }