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

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

fix bugs in IPC comms. Add preliminary FFADO-IPC ALSA plugin

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