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::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();

View file

@ -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

View file

@ -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;
};

View file

@ -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

View file

@ -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;
}

View file

@ -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;
}
while (read_pipe->read(buffer, nbytes) > 0)
{
read_pipe->_handler (read_pipe->_data, buffer, nbytes);
}
if (i > 0)
{
read_pipe->_handler (read_pipe->_data, buf_str, i);
}
}
}

View file

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

View file

@ -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);
}