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
, int size
)
35 char ext_client_name
[SYNC_MAX_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 deallocated!!", 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 deallocated!!", 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 deallocated!!", 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 deallocated!!", 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
, sizeof(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", fName
, 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
));
155 fPoll
.events
= POLLERR
| POLLIN
| POLLHUP
| POLLNVAL
;
164 bool JackFifo::ConnectAux(const char* name
, const char* server_name
, int access
)
166 BuildName(name
, server_name
, fName
, sizeof(fName
));
167 jack_log("JackFifo::ConnectAux name = %s", fName
);
171 jack_log("Already connected name = %s", name
);
175 if ((fFifo
= open(fName
, access
)) < 0) {
176 jack_error("Connect: can't connect named fifo name = %s err = %s", fName
, strerror(errno
));
180 fPoll
.events
= POLLERR
| POLLIN
| POLLHUP
| POLLNVAL
;
185 bool JackFifo::Connect(const char* name
, const char* server_name
)
187 return ConnectAux(name
, server_name
, O_RDWR
);
190 bool JackFifo::ConnectOutput(const char* name
, const char* server_name
)
192 return ConnectAux(name
, server_name
, O_WRONLY
| O_NONBLOCK
);
195 bool JackFifo::ConnectInput(const char* name
, const char* server_name
)
197 return ConnectAux(name
, server_name
, O_RDONLY
);
200 bool JackFifo::Disconnect()
203 jack_log("JackFifo::Disconnect %s", fName
);
204 if (close(fFifo
) != 0) {
205 jack_error("Disconnect: can't disconnect named fifo name = %s err = %s", fName
, strerror(errno
));
216 // Server side : destroy the fifo
217 void JackFifo::Destroy()
220 jack_log("JackFifo::Destroy name = %s", fName
);
222 if (close(fFifo
) != 0) {
223 jack_error("Destroy: can't destroy fifo name = %s err = %s", fName
, strerror(errno
));
227 jack_error("JackFifo::Destroy fifo < 0");
231 } // end of namespace