root/trunk/libffado/src/libutil/PosixMessageQueue.h

Revision 1764, 3.4 kB (checked in by ppalmers, 14 years ago)

fix 32bit format string warnings

Line 
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 #ifdef _XOPEN_SOURCE
28 #undef _XOPEN_SOURCE
29 #endif
30
31 #define _XOPEN_SOURCE 600
32
33 #include "debugmodule/debugmodule.h"
34
35 #include <time.h>
36 #include <mqueue.h>
37 #include <string>
38
39 namespace Util
40 {
41
42 class Functor;
43 class Mutex;
44
45 /**
46  * @brief A POSIX Message Queue Wrapper.
47  */
48
49 class PosixMessageQueue
50 {
51 public:
52     class Message {
53         public:
54             Message() {};
55             virtual ~Message() {};
56
57             virtual unsigned getPriority() = 0;
58
59             virtual unsigned int getLength() = 0;
60             virtual bool serialize(char *buff) = 0;
61             virtual bool deserialize(const char *buff, unsigned int length, unsigned prio) = 0;
62     };
63
64 public:
65     enum eDirection {
66         eD_None,
67         eD_ReadOnly,
68         eD_WriteOnly,
69         eD_ReadWrite
70     };
71
72     enum eResult {
73         eR_OK,
74         eR_Again,
75         eR_Error,
76         eR_Timeout,
77     };
78
79     // blocking mode
80     enum eBlocking {
81         eB_Blocking,
82         eB_NonBlocking,
83     };
84
85 public:
86     PosixMessageQueue(std::string name);
87     virtual ~PosixMessageQueue();
88
89     virtual bool Open(enum eDirection, enum eBlocking blocking = eB_Blocking);
90     virtual bool Create(enum eDirection, enum eBlocking blocking = eB_Blocking);
91     virtual bool Close();
92
93     virtual enum eResult Send(Message &m);
94     virtual enum eResult Receive(Message &m);
95
96     virtual enum eResult Clear();
97
98     virtual int countMessages();
99     virtual bool canSend();
100     virtual bool canReceive();
101
102     virtual bool setNotificationHandler(Util::Functor *f);
103     virtual bool unsetNotificationHandler();
104     virtual bool enableNotification();
105     virtual bool disableNotification();
106
107     virtual bool Wait();
108
109     virtual unsigned int getMaxMessageLength()
110         {return m_attr.mq_msgsize;};
111
112     virtual void show();
113     virtual void setVerboseLevel(int l);
114
115 private:
116     bool doOpen(enum eDirection t, int, enum eBlocking);
117     static void notifyCallbackStatic(sigval_t t) {
118         PosixMessageQueue *obj;
119         obj = static_cast<PosixMessageQueue *>(t.sival_ptr);
120         obj->notifyCallback();
121     }
122     void notifyCallback();
123
124 protected:
125     DECLARE_DEBUG_MODULE;
126
127 private:
128     std::string         m_name;
129     enum eBlocking      m_blocking;
130     enum eDirection     m_direction;
131     bool                m_owner;
132     struct timespec     m_timeout;
133
134     mqd_t               m_handle;
135     struct mq_attr      m_attr;
136     char *              m_tmp_buffer;
137     Util::Functor *     m_notifyHandler;
138     Util::Mutex&        m_notifyHandlerLock;
139 };
140
141 } // end of namespace
142
143 #endif // __UTIL_POSIX_MESSAGE_QUEUE__
Note: See TracBrowser for help on using the browser.