2 Copyright (C) 2004-2008 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "JackTools.h"
22 #include "JackError.h"
23 #include "JackPlatformPlug.h"
24 #include <sys/types.h>
33 void JackFifo::BuildName(const char* client_name
, const char* server_name
, char* res
)
35 char ext_client_name
[JACK_CLIENT_NAME_SIZE
+ 1];
36 JackTools::RewriteName(client_name
, ext_client_name
);
37 sprintf(res
, "%s/jack_fifo.%d_%s_%s", jack_client_dir
, JackTools::GetUID(), server_name
, ext_client_name
);
40 bool JackFifo::Signal()
46 jack_error("JackFifo::Signal name = %s already desallocated!!", fName
);
53 if ((res
= (write(fFifo
, &c
, sizeof(c
)) != sizeof(c
)))) {
54 jack_error("JackFifo::Signal name = %s err = %s", fName
, strerror(errno
));
59 bool JackFifo::SignalAll()
65 jack_error("JackFifo::SignalAll name = %s already desallocated!!", fName
);
72 if ((res
= (write(fFifo
, &c
, sizeof(c
)) != sizeof(c
)))) {
73 jack_error("JackFifo::SignalAll name = %s err = %s", fName
, strerror(errno
));
84 jack_error("JackFifo::Wait name = %s already desallocated!!", fName
);
88 if ((res
= (read(fFifo
, &c
, sizeof(c
)) != sizeof(c
)))) {
89 jack_error("JackFifo::Wait name = %s err = %s", fName
, strerror(errno
));
95 #warning JackFifo::TimedWait not available : synchronous mode may not work correctly if FIFO are used
96 bool JackFifo::TimedWait(long usec
)
101 // Does not work on OSX ??
102 bool JackFifo::TimedWait(long usec
)
107 jack_error("JackFifo::TimedWait name = %s already desallocated!!", fName
);
112 res
= poll(&fPoll
, 1, usec
/ 1000);
113 } while (res
< 0 && errno
== EINTR
);
115 if (fPoll
.revents
& POLLIN
) {
118 // Wait failure but we still continue...
119 jack_log("JackFifo::TimedWait name = %s usec = %ld err = %s", fName
, usec
, strerror(errno
));
126 bool JackFifo::Allocate(const char* name
, const char* server_name
, int value
)
129 BuildName(name
, server_name
, fName
);
130 jack_log("JackFifo::Allocate name = %s", fName
);
132 if (stat(fName
, &statbuf
) < 0) {
133 if (errno
== ENOENT
|| errno
== EPERM
) {
134 if (mkfifo(fName
, 0666) < 0) {
135 jack_error("Cannot create inter-client FIFO name = %s err = %s", name
, strerror(errno
));
139 jack_error("Cannot check on FIFO %s", name
);
143 if (!S_ISFIFO(statbuf
.st_mode
)) {
144 jack_error("FIFO name = %s already exists, but is not a FIFO", name
);
149 if ((fFifo
= open(fName
, O_RDWR
| O_CREAT
, 0666)) < 0) {
150 jack_error("Cannot open FIFO name = %s err = %s", name
, strerror(errno
));
154 fPoll
.events
= POLLERR
| POLLIN
| POLLHUP
| POLLNVAL
;
160 bool JackFifo::ConnectAux(const char* name
, const char* server_name
, int access
)
162 BuildName(name
, server_name
, fName
);
163 jack_log("JackFifo::ConnectAux name = %s", fName
);
167 jack_log("Already connected name = %s", name
);
171 if ((fFifo
= open(fName
, access
)) < 0) {
172 jack_error("Connect: can't connect named fifo name = %s err = %s", fName
, strerror(errno
));
176 fPoll
.events
= POLLERR
| POLLIN
| POLLHUP
| POLLNVAL
;
181 bool JackFifo::Connect(const char* name
, const char* server_name
)
183 return ConnectAux(name
, server_name
, O_RDWR
);
186 bool JackFifo::ConnectOutput(const char* name
, const char* server_name
)
188 return ConnectAux(name
, server_name
, O_WRONLY
| O_NONBLOCK
);
191 bool JackFifo::ConnectInput(const char* name
, const char* server_name
)
193 return ConnectAux(name
, server_name
, O_RDONLY
);
196 bool JackFifo::Disconnect()
199 jack_log("JackFifo::Disconnect %s", fName
);
200 if (close(fFifo
) != 0) {
201 jack_error("Disconnect: can't disconnect named fifo name = %s err = %s", fName
, strerror(errno
));
212 // Server side : destroy the fifo
213 void JackFifo::Destroy()
216 jack_log("JackFifo::Destroy name = %s", fName
);
218 if (close(fFifo
) != 0) {
219 jack_error("Destroy: can't destroy fifo name = %s err = %s", fName
, strerror(errno
));
223 jack_error("JackFifo::Destroy fifo < 0");
227 } // end of namespace