1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
#ifndef __lib_base_message_h
#define __lib_base_message_h
#include <lib/base/ebase.h>
#include <lib/python/connections.h>
#include <lib/python/swig.h>
#include <unistd.h>
#include <lib/base/elock.h>
/**
* \brief A generic messagepump.
*
* You can send and receive messages with this class. Internally a fifo is used,
* so you can use them together with a \c eMainloop.
*/
#ifndef SWIG
class eMessagePump
{
int fd[2];
eLock content;
int ismt;
public:
eMessagePump(int mt=0);
virtual ~eMessagePump();
protected:
int send(const void *data, int len);
int recv(void *data, int len); // blockierend
int getInputFD() const;
int getOutputFD() const;
};
/**
* \brief A messagepump with fixed-length packets.
*
* Based on \ref eMessagePump, with this class you can send and receive fixed size messages.
* Automatically creates a eSocketNotifier and gives you a callback.
*/
template<class T>
class eFixedMessagePump: private eMessagePump, public Object
{
ePtr<eSocketNotifier> sn;
void do_recv(int)
{
T msg;
recv(&msg, sizeof(msg));
/*emit*/ recv_msg(msg);
}
public:
Signal1<void,const T&> recv_msg;
void send(const T &msg)
{
eMessagePump::send(&msg, sizeof(msg));
}
eFixedMessagePump(eMainloop *context, int mt): eMessagePump(mt)
{
sn=eSocketNotifier::create(context, getOutputFD(), eSocketNotifier::Read);
CONNECT(sn->activated, eFixedMessagePump<T>::do_recv);
sn->start();
}
void start() { if (sn) sn->start(); }
void stop() { if (sn) sn->stop(); }
};
#endif
class ePythonMessagePump: public eMessagePump, public Object
{
ePtr<eSocketNotifier> sn;
void do_recv(int)
{
int msg;
recv(&msg, sizeof(msg));
/*emit*/ recv_msg(msg);
}
public:
PSignal1<void,int> recv_msg;
void send(int msg)
{
eMessagePump::send(&msg, sizeof(msg));
}
ePythonMessagePump()
:eMessagePump(1)
{
sn=eSocketNotifier::create(eApp, getOutputFD(), eSocketNotifier::Read);
CONNECT(sn->activated, ePythonMessagePump::do_recv);
sn->start();
}
void start() { if (sn) sn->start(); }
void stop() { if (sn) sn->stop(); }
};
#endif
|