1 |
/* |
---|
2 |
* Copyright (C) 2005-2008 by Pieter Palmers |
---|
3 |
* |
---|
4 |
* This file is part of FFADO |
---|
5 |
* FFADO = Free Firewire (pro-)audio drivers for linux |
---|
6 |
* |
---|
7 |
* FFADO is based upon FreeBoB |
---|
8 |
* |
---|
9 |
* This program is free software: you can redistribute it and/or modify |
---|
10 |
* it under the terms of the GNU General Public License as published by |
---|
11 |
* the Free Software Foundation, either version 2 of the License, or |
---|
12 |
* (at your option) version 3 of the License. |
---|
13 |
* |
---|
14 |
* This program is distributed in the hope that it will be useful, |
---|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 |
* GNU General Public License for more details. |
---|
18 |
* |
---|
19 |
* You should have received a copy of the GNU General Public License |
---|
20 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
21 |
* |
---|
22 |
*/ |
---|
23 |
|
---|
24 |
#ifndef __UTIL_POSIX_MESSAGE_QUEUE__ |
---|
25 |
#define __UTIL_POSIX_MESSAGE_QUEUE__ |
---|
26 |
|
---|
27 |
#include "debugmodule/debugmodule.h" |
---|
28 |
|
---|
29 |
#define _XOPEN_SOURCE 600 |
---|
30 |
#include <time.h> |
---|
31 |
#include <mqueue.h> |
---|
32 |
#include <string> |
---|
33 |
|
---|
34 |
namespace Util |
---|
35 |
{ |
---|
36 |
|
---|
37 |
class Functor; |
---|
38 |
class Mutex; |
---|
39 |
|
---|
40 |
/** |
---|
41 |
* @brief A POSIX Message Queue Wrapper. |
---|
42 |
*/ |
---|
43 |
|
---|
44 |
class PosixMessageQueue |
---|
45 |
{ |
---|
46 |
public: |
---|
47 |
class Message { |
---|
48 |
public: |
---|
49 |
Message() {}; |
---|
50 |
virtual ~Message() {}; |
---|
51 |
|
---|
52 |
virtual unsigned getPriority() = 0; |
---|
53 |
|
---|
54 |
virtual unsigned int getLength() = 0; |
---|
55 |
virtual bool serialize(char *buff) = 0; |
---|
56 |
virtual bool deserialize(const char *buff, unsigned int length, unsigned prio) = 0; |
---|
57 |
}; |
---|
58 |
|
---|
59 |
public: |
---|
60 |
enum eDirection { |
---|
61 |
eD_None, |
---|
62 |
eD_ReadOnly, |
---|
63 |
eD_WriteOnly, |
---|
64 |
eD_ReadWrite |
---|
65 |
}; |
---|
66 |
|
---|
67 |
enum eResult { |
---|
68 |
eR_OK, |
---|
69 |
eR_Again, |
---|
70 |
eR_Error, |
---|
71 |
eR_Timeout, |
---|
72 |
}; |
---|
73 |
|
---|
74 |
// blocking mode |
---|
75 |
enum eBlocking { |
---|
76 |
eB_Blocking, |
---|
77 |
eB_NonBlocking, |
---|
78 |
}; |
---|
79 |
|
---|
80 |
public: |
---|
81 |
PosixMessageQueue(std::string name); |
---|
82 |
virtual ~PosixMessageQueue(); |
---|
83 |
|
---|
84 |
virtual bool Open(enum eDirection, enum eBlocking blocking = eB_Blocking); |
---|
85 |
virtual bool Create(enum eDirection, enum eBlocking blocking = eB_Blocking); |
---|
86 |
virtual bool Close(); |
---|
87 |
|
---|
88 |
virtual enum eResult Send(Message &m); |
---|
89 |
virtual enum eResult Receive(Message &m); |
---|
90 |
|
---|
91 |
virtual enum eResult Clear(); |
---|
92 |
|
---|
93 |
virtual int countMessages(); |
---|
94 |
virtual bool canSend(); |
---|
95 |
virtual bool canReceive(); |
---|
96 |
|
---|
97 |
virtual bool setNotificationHandler(Util::Functor *f); |
---|
98 |
virtual bool unsetNotificationHandler(); |
---|
99 |
virtual bool enableNotification(); |
---|
100 |
virtual bool disableNotification(); |
---|
101 |
|
---|
102 |
virtual bool Wait(); |
---|
103 |
|
---|
104 |
virtual unsigned int getMaxMessageLength() |
---|
105 |
{return m_attr.mq_msgsize;}; |
---|
106 |
|
---|
107 |
virtual void show(); |
---|
108 |
virtual void setVerboseLevel(int l); |
---|
109 |
|
---|
110 |
private: |
---|
111 |
bool doOpen(enum eDirection t, int, enum eBlocking); |
---|
112 |
static void notifyCallbackStatic(sigval_t t) { |
---|
113 |
PosixMessageQueue *obj; |
---|
114 |
obj = static_cast<PosixMessageQueue *>(t.sival_ptr); |
---|
115 |
obj->notifyCallback(); |
---|
116 |
} |
---|
117 |
void notifyCallback(); |
---|
118 |
|
---|
119 |
protected: |
---|
120 |
DECLARE_DEBUG_MODULE; |
---|
121 |
|
---|
122 |
private: |
---|
123 |
std::string m_name; |
---|
124 |
enum eBlocking m_blocking; |
---|
125 |
enum eDirection m_direction; |
---|
126 |
bool m_owner; |
---|
127 |
struct timespec m_timeout; |
---|
128 |
|
---|
129 |
mqd_t m_handle; |
---|
130 |
struct mq_attr m_attr; |
---|
131 |
char * m_tmp_buffer; |
---|
132 |
Util::Functor * m_notifyHandler; |
---|
133 |
Util::Mutex& m_notifyHandlerLock; |
---|
134 |
}; |
---|
135 |
|
---|
136 |
} // end of namespace |
---|
137 |
|
---|
138 |
#endif // __UTIL_POSIX_MESSAGE_QUEUE__ |
---|