00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "JackFifo.h"
00021 #include "JackTools.h"
00022 #include "JackError.h"
00023 #include "JackConstants.h"
00024 #include <sys/types.h>
00025 #include <sys/stat.h>
00026 #include <unistd.h>
00027 #include <fcntl.h>
00028 #include <stdio.h>
00029
00030 namespace Jack
00031 {
00032
00033 void JackFifo::BuildName(const char* client_name, const char* server_name, char* res)
00034 {
00035 char ext_client_name[JACK_CLIENT_NAME_SIZE + 1];
00036 JackTools::RewriteName(client_name, ext_client_name);
00037 sprintf(res, "%s/jack_fifo.%d_%s_%s", jack_client_dir, JackTools::GetUID(), server_name, ext_client_name);
00038 }
00039
00040 bool JackFifo::Signal()
00041 {
00042 bool res;
00043 char c = 0;
00044
00045 if (fFifo < 0) {
00046 jack_error("JackFifo::Signal name = %s already desallocated!!", fName);
00047 return false;
00048 }
00049
00050 if (fFlush)
00051 return true;
00052
00053 if ((res = (write(fFifo, &c, sizeof(c)) != sizeof(c)))) {
00054 jack_error("JackFifo::Signal name = %s err = %s", fName, strerror(errno));
00055 }
00056 return !res;
00057 }
00058
00059 bool JackFifo::SignalAll()
00060 {
00061 bool res;
00062 char c = 0;
00063
00064 if (fFifo < 0) {
00065 jack_error("JackFifo::SignalAll name = %s already desallocated!!", fName);
00066 return false;
00067 }
00068
00069 if (fFlush)
00070 return true;
00071
00072 if ((res = (write(fFifo, &c, sizeof(c)) != sizeof(c)))) {
00073 jack_error("JackFifo::SignalAll name = %s err = %s", fName, strerror(errno));
00074 }
00075 return !res;
00076 }
00077
00078 bool JackFifo::Wait()
00079 {
00080 bool res;
00081 char c;
00082
00083 if (fFifo < 0) {
00084 jack_error("JackFifo::Wait name = %s already desallocated!!", fName);
00085 return false;
00086 }
00087
00088 if ((res = (read(fFifo, &c, sizeof(c)) != sizeof(c)))) {
00089 jack_error("JackFifo::Wait name = %s err = %s", fName, strerror(errno));
00090 }
00091 return !res;
00092 }
00093
00094 #ifdef __APPLE__
00095 #warning JackFifo::TimedWait not available : synchronous mode may not work correctly if FIFO are used
00096 bool JackFifo::TimedWait(long usec)
00097 {
00098 return Wait();
00099 }
00100 #else
00101
00102 bool JackFifo::TimedWait(long usec)
00103 {
00104 assert(fFifo >= 0);
00105
00106 if ((poll(&fPoll, 1, usec / 1000) < 0) && (errno != EINTR)) {
00107 jack_error("JackFifo::TimedWait name = %s err = %s", fName, strerror(errno));
00108 return false;
00109 }
00110
00111 if (fPoll.revents & POLLIN) {
00112 return Wait();
00113 } else {
00114
00115 jack_log("JackFifo::TimedWait name = %s usec = %ld err = %s", fName, usec, strerror(errno));
00116 return true;
00117 }
00118 }
00119 #endif
00120
00121
00122 bool JackFifo::Allocate(const char* name, const char* server_name, int value)
00123 {
00124 struct stat statbuf;
00125 BuildName(name, server_name, fName);
00126 jack_log("JackFifo::Allocate name = %s", fName);
00127
00128 if (stat(fName, &statbuf) < 0) {
00129 if (errno == ENOENT || errno == EPERM) {
00130 if (mkfifo(fName, 0666) < 0) {
00131 jack_error("Cannot create inter-client FIFO name = %s err = %s", name, strerror(errno));
00132 return false;
00133 }
00134 } else {
00135 jack_error("Cannot check on FIFO %s", name);
00136 return false;
00137 }
00138 } else {
00139 if (!S_ISFIFO(statbuf.st_mode)) {
00140 jack_error("FIFO name = %s already exists, but is not a FIFO", name);
00141 return false;
00142 }
00143 }
00144
00145 if ((fFifo = open(fName, O_RDWR | O_CREAT, 0666)) < 0) {
00146 jack_error("Cannot open FIFO name = %s err = %s", name, strerror(errno));
00147 return false;
00148 } else {
00149 fPoll.fd = fFifo;
00150 fPoll.events = POLLERR | POLLIN | POLLHUP | POLLNVAL;
00151 return true;
00152 }
00153 }
00154
00155
00156 bool JackFifo::ConnectAux(const char* name, const char* server_name, int access)
00157 {
00158 BuildName(name, server_name, fName);
00159 jack_log("JackFifo::ConnectAux name = %s", fName);
00160
00161
00162 if (fFifo >= 0) {
00163 jack_log("Already connected name = %s", name);
00164 return true;
00165 }
00166
00167 if ((fFifo = open(fName, access)) < 0) {
00168 jack_error("Connect: can't connect named fifo name = %s err = %s", fName, strerror(errno));
00169 return false;
00170 } else {
00171 fPoll.fd = fFifo;
00172 fPoll.events = POLLERR | POLLIN | POLLHUP | POLLNVAL;
00173 return true;
00174 }
00175 }
00176
00177 bool JackFifo::Connect(const char* name, const char* server_name)
00178 {
00179 return ConnectAux(name, server_name, O_RDWR);
00180 }
00181
00182 bool JackFifo::ConnectOutput(const char* name, const char* server_name)
00183 {
00184 return ConnectAux(name, server_name, O_WRONLY | O_NONBLOCK);
00185 }
00186
00187 bool JackFifo::ConnectInput(const char* name, const char* server_name)
00188 {
00189 return ConnectAux(name, server_name, O_RDONLY);
00190 }
00191
00192 bool JackFifo::Disconnect()
00193 {
00194 if (fFifo >= 0) {
00195 jack_log("JackFifo::Disconnect %s", fName);
00196 if (close(fFifo) != 0) {
00197 jack_error("Disconnect: can't disconnect named fifo name = %s err = %s", fName, strerror(errno));
00198 return false;
00199 } else {
00200 fFifo = -1;
00201 return true;
00202 }
00203 } else {
00204 return true;
00205 }
00206 }
00207
00208
00209 void JackFifo::Destroy()
00210 {
00211 if (fFifo > 0) {
00212 jack_log("JackFifo::Destroy name = %s", fName);
00213 unlink(fName);
00214 if (close(fFifo) != 0) {
00215 jack_error("Destroy: can't destroy fifo name = %s err = %s", fName, strerror(errno));
00216 }
00217 fFifo = -1;
00218 } else {
00219 jack_error("JackFifo::Destroy fifo < 0");
00220 }
00221 }
00222
00223 }
00224