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

Revision 1172, 3.3 kB (checked in by ppalmers, 13 years ago)

lay down the foundations for easy ALSA/Pulse support

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 #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 int countMessages();
92     virtual bool canSend();
93     virtual bool canReceive();
94
95     virtual bool setNotificationHandler(Util::Functor *f);
96     virtual bool unsetNotificationHandler();
97     virtual bool enableNotification();
98     virtual bool disableNotification();
99
100     virtual unsigned int getMaxMessageLength()
101         {return m_attr.mq_msgsize;};
102
103     virtual void show();
104     virtual void setVerboseLevel(int l);
105
106 private:
107     bool doOpen(enum eDirection t, int, enum eBlocking);
108     static void notifyCallbackStatic(sigval_t t) {
109         PosixMessageQueue *obj;
110         obj = static_cast<PosixMessageQueue *>(t.sival_ptr);
111         obj->notifyCallback();
112     }
113     void notifyCallback();
114
115 protected:
116     DECLARE_DEBUG_MODULE;
117
118 private:
119     std::string         m_name;
120     enum eBlocking      m_blocking;
121     enum eDirection     m_direction;
122     bool                m_owner;
123     struct timespec     m_timeout;
124
125     mqd_t               m_handle;
126     struct mq_attr      m_attr;
127     char *              m_tmp_buffer;
128     Util::Functor *     m_notifyHandler;
129     Util::Mutex&        m_notifyHandlerLock;
130 };
131
132 } // end of namespace
133
134 #endif // __UTIL_POSIX_MESSAGE_QUEUE__
Note: See TracBrowser for help on using the browser.