JackFifo.cpp 5.93 KB
Newer Older
sletz's avatar
sletz committed
1
/*
sletz's avatar
sletz committed
2
Copyright (C) 2004-2008 Grame
sletz's avatar
sletz committed
3
4

This program is free software; you can redistribute it and/or modify
sletz's avatar
sletz committed
5
6
7
8
9
10
11
12
13
14
15
16
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
sletz's avatar
sletz committed
17
18
19
20

*/

#include "JackFifo.h"
21
#include "JackTools.h"
sletz's avatar
sletz committed
22
#include "JackError.h"
sletz's avatar
sletz committed
23
#include "JackConstants.h"
sletz's avatar
sletz committed
24
25
26
27
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
sletz's avatar
sletz committed
28
#include <stdio.h>
sletz's avatar
sletz committed
29
30
31
32

namespace Jack
{

33
void JackFifo::BuildName(const char* client_name, const char* server_name, char* res)
sletz's avatar
sletz committed
34
{
35
36
37
    char ext_client_name[JACK_CLIENT_NAME_SIZE + 1];
    JackTools::RewriteName(client_name, ext_client_name);
    sprintf(res, "%s/jack_fifo.%d_%s_%s", jack_client_dir, JackTools::GetUID(), server_name, ext_client_name);
sletz's avatar
sletz committed
38
39
40
41
42
43
}

bool JackFifo::Signal()
{
    bool res;
    char c = 0;
sletz's avatar
sletz committed
44
45
46
47
48
49

    if (fFifo < 0) {
        jack_error("JackFifo::Signal name = %s already desallocated!!", fName);
        return false;
    }

sletz's avatar
sletz committed
50
51
52
53
54
55
56
57
58
59
60
61
62
    if (fFlush)
        return true;

    if ((res = (write(fFifo, &c, sizeof(c)) != sizeof(c)))) {
        jack_error("JackFifo::Signal name = %s err = %s", fName, strerror(errno));
    }
    return !res;
}

bool JackFifo::SignalAll()
{
    bool res;
    char c = 0;
sletz's avatar
sletz committed
63
64
65
66
67

    if (fFifo < 0) {
        jack_error("JackFifo::SignalAll name = %s already desallocated!!", fName);
        return false;
    }
sletz's avatar
sletz committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81

    if (fFlush)
        return true;

    if ((res = (write(fFifo, &c, sizeof(c)) != sizeof(c)))) {
        jack_error("JackFifo::SignalAll name = %s err = %s", fName, strerror(errno));
    }
    return !res;
}

bool JackFifo::Wait()
{
    bool res;
    char c;
sletz's avatar
sletz committed
82

83
    if (fFifo < 0) {
sletz's avatar
sletz committed
84
85
86
        jack_error("JackFifo::Wait name = %s already desallocated!!", fName);
        return false;
    }
sletz's avatar
sletz committed
87
88
89
90
91
92
93
94
95
96
97
98
99

    if ((res = (read(fFifo, &c, sizeof(c)) != sizeof(c)))) {
        jack_error("JackFifo::Wait name = %s err = %s", fName, strerror(errno));
    }
    return !res;
}

#ifdef __APPLE__
#warning JackFifo::TimedWait not available : synchronous mode may not work correctly if FIFO are used
bool JackFifo::TimedWait(long usec)
{
    return Wait();
}
sletz's avatar
sletz committed
100
#else
sletz's avatar
sletz committed
101
102
103
// Does not work on OSX ??
bool JackFifo::TimedWait(long usec)
{
104
105
106
107
    int res;
    
    if (fFifo < 0) {
        jack_error("JackFifo::TimedWait name = %s already desallocated!!", fName);
sletz's avatar
sletz committed
108
109
        return false;
    }
110
111
112
113
   
    do {
        res = poll(&fPoll, 1, usec / 1000);
    } while (res < 0 && errno == EINTR);
sletz's avatar
sletz committed
114
115
116
117

    if (fPoll.revents & POLLIN) {
        return Wait();
    } else {
118
        // Wait failure but we still continue...
119
        jack_log("JackFifo::TimedWait name = %s usec = %ld err = %s", fName, usec, strerror(errno));
120
        return true;
sletz's avatar
sletz committed
121
122
123
124
125
    }
}
#endif

// Server side
126
bool JackFifo::Allocate(const char* name, const char* server_name, int value)
sletz's avatar
sletz committed
127
128
{
    struct stat statbuf;
129
    BuildName(name, server_name, fName);
sletz's avatar
sletz committed
130
    jack_log("JackFifo::Allocate name = %s", fName);
sletz's avatar
sletz committed
131

sletz's avatar
sletz committed
132
133
    if (stat(fName, &statbuf) < 0) {
        if (errno == ENOENT || errno == EPERM) {
sletz's avatar
sletz committed
134
            if (mkfifo(fName, 0666) < 0) {
sletz's avatar
sletz committed
135
                jack_error("Cannot create inter-client FIFO name = %s err = %s", name, strerror(errno));
sletz's avatar
sletz committed
136
137
138
                return false;
            }
        } else {
sletz's avatar
sletz committed
139
            jack_error("Cannot check on FIFO %s", name);
sletz's avatar
sletz committed
140
141
142
143
            return false;
        }
    } else {
        if (!S_ISFIFO(statbuf.st_mode)) {
sletz's avatar
sletz committed
144
            jack_error("FIFO name = %s already exists, but is not a FIFO", name);
sletz's avatar
sletz committed
145
146
147
148
149
            return false;
        }
    }

    if ((fFifo = open(fName, O_RDWR | O_CREAT, 0666)) < 0) {
sletz's avatar
sletz committed
150
        jack_error("Cannot open FIFO name = %s err = %s", name, strerror(errno));
sletz's avatar
sletz committed
151
152
153
154
155
156
157
158
159
        return false;
    } else {
        fPoll.fd = fFifo;
        fPoll.events = POLLERR | POLLIN | POLLHUP | POLLNVAL;
        return true;
    }
}

// Client side
160
bool JackFifo::ConnectAux(const char* name, const char* server_name, int access)
sletz's avatar
sletz committed
161
{
162
    BuildName(name, server_name, fName);
sletz's avatar
sletz committed
163
    jack_log("JackFifo::ConnectAux name = %s", fName);
sletz's avatar
sletz committed
164
165
166

    // Temporary...
    if (fFifo >= 0) {
sletz's avatar
sletz committed
167
        jack_log("Already connected name = %s", name);
sletz's avatar
sletz committed
168
169
170
171
172
173
174
175
176
177
178
179
180
        return true;
    }

    if ((fFifo = open(fName, access)) < 0) {
        jack_error("Connect: can't connect named fifo name = %s err = %s", fName, strerror(errno));
        return false;
    } else {
        fPoll.fd = fFifo;
        fPoll.events = POLLERR | POLLIN | POLLHUP | POLLNVAL;
        return true;
    }
}

181
bool JackFifo::Connect(const char* name, const char* server_name)
sletz's avatar
sletz committed
182
{
183
    return ConnectAux(name, server_name, O_RDWR);
sletz's avatar
sletz committed
184
185
}

186
bool JackFifo::ConnectOutput(const char* name, const char* server_name)
sletz's avatar
sletz committed
187
{
188
    return ConnectAux(name, server_name, O_WRONLY | O_NONBLOCK);
sletz's avatar
sletz committed
189
190
}

191
bool JackFifo::ConnectInput(const char* name, const char* server_name)
sletz's avatar
sletz committed
192
{
193
    return ConnectAux(name, server_name, O_RDONLY);
sletz's avatar
sletz committed
194
195
196
197
198
}

bool JackFifo::Disconnect()
{
    if (fFifo >= 0) {
sletz's avatar
sletz committed
199
        jack_log("JackFifo::Disconnect %s", fName);
sletz's avatar
sletz committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
        if (close(fFifo) != 0) {
            jack_error("Disconnect: can't disconnect named fifo name = %s err = %s", fName, strerror(errno));
            return false;
        } else {
            fFifo = -1;
            return true;
        }
    } else {
        return true;
    }
}

// Server side : destroy the fifo
void JackFifo::Destroy()
{
    if (fFifo > 0) {
sletz's avatar
sletz committed
216
        jack_log("JackFifo::Destroy name = %s", fName);
sletz's avatar
sletz committed
217
218
219
220
221
222
223
224
225
226
227
228
        unlink(fName);
        if (close(fFifo) != 0) {
            jack_error("Destroy: can't destroy fifo name = %s err = %s", fName, strerror(errno));
        }
        fFifo = -1;
    } else {
        jack_error("JackFifo::Destroy fifo < 0");
    }
}

} // end of namespace