Jack2  1.9.8
JackWinNamedPipeClientChannel.cpp
1 /*
2  Copyright (C) 2004-2008 Grame
3 
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.
8 
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.
13 
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.
17 
18  */
19 
20 
21 #include "JackWinNamedPipeClientChannel.h"
22 #include "JackRequest.h"
23 #include "JackClient.h"
24 #include "JackGlobals.h"
25 #include "JackError.h"
26 
27 namespace Jack
28 {
29 
30 JackWinNamedPipeClientChannel::JackWinNamedPipeClientChannel():fThread(this)
31 {
32  fClient = NULL;
33 }
34 
35 JackWinNamedPipeClientChannel::~JackWinNamedPipeClientChannel()
36 {}
37 
38 int JackWinNamedPipeClientChannel::ServerCheck(const char* server_name)
39 {
40  jack_log("JackWinNamedPipeClientChannel::ServerCheck = %s", server_name);
41 
42  // Connect to server
43  if (fRequestPipe.Connect(jack_server_dir, server_name, 0) < 0) {
44  jack_error("Cannot connect to server pipe");
45  return -1;
46  } else {
47  return 0;
48  }
49 }
50 
51 int JackWinNamedPipeClientChannel::Open(const char* server_name, const char* name, int uuid, char* name_res, JackClient* obj, jack_options_t options, jack_status_t* status)
52 {
53  int result = 0;
54  jack_log("JackWinNamedPipeClientChannel::Open name = %s", name);
55 
56  /*
57  16/08/07: was called before doing "fRequestPipe.Connect" .... still necessary?
58  if (fNotificationListenPipe.Bind(jack_client_dir, name, 0) < 0) {
59  jack_error("Cannot bind pipe");
60  goto error;
61  }
62  */
63 
64  if (fRequestPipe.Connect(jack_server_dir, server_name, 0) < 0) {
65  jack_error("Cannot connect to server pipe");
66  goto error;
67  }
68 
69  // Check name in server
70  ClientCheck(name, uuid, name_res, JACK_PROTOCOL_VERSION, (int)options, (int*)status, &result, true);
71  if (result < 0) {
72  int status1 = *status;
73  if (status1 & JackVersionError) {
74  jack_error("JACK protocol mismatch %d", JACK_PROTOCOL_VERSION);
75  } else {
76  jack_error("Client name = %s conflits with another running client", name);
77  }
78  }
79 
80  if (fNotificationListenPipe.Bind(jack_client_dir, name_res, 0) < 0) {
81  jack_error("Cannot bind pipe");
82  goto error;
83  }
84 
85  fClient = obj;
86  return 0;
87 
88 error:
89  fRequestPipe.Close();
90  fNotificationListenPipe.Close();
91  return -1;
92 }
93 
94 void JackWinNamedPipeClientChannel::Close()
95 {
96  fRequestPipe.Close();
97  fNotificationListenPipe.Close();
98  // Here the thread will correctly stop when the pipe are closed
99  fThread.Stop();
100 }
101 
102 int JackWinNamedPipeClientChannel::Start()
103 {
104  jack_log("JackWinNamedPipeClientChannel::Start");
105  /*
106  To be sure notification thread is started before ClientOpen is called.
107  */
108  if (fThread.StartSync() != 0) {
109  jack_error("Cannot start Jack client listener");
110  return -1;
111  } else {
112  return 0;
113  }
114 }
115 
116 void JackWinNamedPipeClientChannel::Stop()
117 {
118  jack_log("JackWinNamedPipeClientChannel::Stop");
119  fThread.Kill(); // Unsafe on WIN32... TODO : solve WIN32 thread Kill issue
120 }
121 
122 void JackWinNamedPipeClientChannel::ServerSyncCall(JackRequest* req, JackResult* res, int* result)
123 {
124  if (req->Write(&fRequestPipe) < 0) {
125  jack_error("Could not write request type = %ld", req->fType);
126  *result = -1;
127  return ;
128  }
129 
130  if (res->Read(&fRequestPipe) < 0) {
131  jack_error("Could not read result type = %ld", req->fType);
132  *result = -1;
133  return ;
134  }
135 
136  *result = res->fResult;
137 }
138 
139 void JackWinNamedPipeClientChannel::ServerAsyncCall(JackRequest* req, JackResult* res, int* result)
140 {
141  if (req->Write(&fRequestPipe) < 0) {
142  jack_error("Could not write request type = %ld", req->fType);
143  *result = -1;
144  } else {
145  *result = 0;
146  }
147 }
148 
149 void JackWinNamedPipeClientChannel::ClientCheck(const char* name, int uuid, char* name_res, int protocol, int options, int* status, int* result, int open)
150 {
151  JackClientCheckRequest req(name, protocol, options, uuid, open);
152  JackClientCheckResult res;
153  ServerSyncCall(&req, &res, result);
154  *status = res.fStatus;
155  strcpy(name_res, res.fName);
156 }
157 
158 void JackWinNamedPipeClientChannel::ClientOpen(const char* name, int pid, int uuid, int* shared_engine, int* shared_client, int* shared_graph, int* result)
159 {
160  JackClientOpenRequest req(name, pid, uuid);
161  JackClientOpenResult res;
162  ServerSyncCall(&req, &res, result);
163  *shared_engine = res.fSharedEngine;
164  *shared_client = res.fSharedClient;
165  *shared_graph = res.fSharedGraph;
166 }
167 
168 void JackWinNamedPipeClientChannel::ClientClose(int refnum, int* result)
169 {
170  JackClientCloseRequest req(refnum);
171  JackResult res;
172  ServerSyncCall(&req, &res, result);
173 }
174 
175 void JackWinNamedPipeClientChannel::ClientActivate(int refnum, int is_real_time, int* result)
176 {
177  JackActivateRequest req(refnum, is_real_time);
178  JackResult res;
179  ServerSyncCall(&req, &res, result);
180 }
181 
182 void JackWinNamedPipeClientChannel::ClientDeactivate(int refnum, int* result)
183 {
184  JackDeactivateRequest req(refnum);
185  JackResult res;
186  ServerSyncCall(&req, &res, result);
187 }
188 
189 void JackWinNamedPipeClientChannel::PortRegister(int refnum, const char* name, const char* type, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index, int* result)
190 {
191  JackPortRegisterRequest req(refnum, name, type, flags, buffer_size);
192  JackPortRegisterResult res;
193  ServerSyncCall(&req, &res, result);
194  *port_index = res.fPortIndex;
195 }
196 
197 void JackWinNamedPipeClientChannel::PortUnRegister(int refnum, jack_port_id_t port_index, int* result)
198 {
199  JackPortUnRegisterRequest req(refnum, port_index);
200  JackResult res;
201  ServerSyncCall(&req, &res, result);
202 }
203 
204 void JackWinNamedPipeClientChannel::PortConnect(int refnum, const char* src, const char* dst, int* result)
205 {
206  JackPortConnectNameRequest req(refnum, src, dst);
207  JackResult res;
208  ServerSyncCall(&req, &res, result);
209 }
210 
211 void JackWinNamedPipeClientChannel::PortDisconnect(int refnum, const char* src, const char* dst, int* result)
212 {
213  JackPortDisconnectNameRequest req(refnum, src, dst);
214  JackResult res;
215  ServerSyncCall(&req, &res, result);
216 }
217 
218 void JackWinNamedPipeClientChannel::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
219 {
220  JackPortConnectRequest req(refnum, src, dst);
221  JackResult res;
222  ServerSyncCall(&req, &res, result);
223 }
224 
225 void JackWinNamedPipeClientChannel::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
226 {
227  JackPortDisconnectRequest req(refnum, src, dst);
228  JackResult res;
229  ServerSyncCall(&req, &res, result);
230 }
231 
232 void JackWinNamedPipeClientChannel::PortRename(int refnum, jack_port_id_t port, const char* name, int* result)
233 {
234  JackPortRenameRequest req(refnum, port, name);
235  JackResult res;
236  ServerSyncCall(&req, &res, result);
237 }
238 
239 void JackWinNamedPipeClientChannel::SetBufferSize(jack_nframes_t buffer_size, int* result)
240 {
241  JackSetBufferSizeRequest req(buffer_size);
242  JackResult res;
243  ServerSyncCall(&req, &res, result);
244 }
245 
246 void JackWinNamedPipeClientChannel::SetFreewheel(int onoff, int* result)
247 {
248  JackSetFreeWheelRequest req(onoff);
249  JackResult res;
250  ServerSyncCall(&req, &res, result);
251 }
252 
253 void JackWinNamedPipeClientChannel::ComputeTotalLatencies(int* result)
254 {
255  JackComputeTotalLatenciesRequest req;
256  JackResult res;
257  ServerSyncCall(&req, &res, result);
258 }
259 
260 void JackWinNamedPipeClientChannel::SessionNotify(int refnum, const char* target, jack_session_event_type_t type, const char* path, jack_session_command_t** result)
261 {
262  JackSessionNotifyRequest req(refnum, path, type, target);
263  JackSessionNotifyResult res;
264  int intresult;
265  ServerSyncCall(&req, &res, &intresult);
266  *result = res.GetCommands();
267 }
268 
269 void JackWinNamedPipeClientChannel::SessionReply(int refnum, int* result)
270 {
271  JackSessionReplyRequest req(refnum);
272  JackResult res;
273  ServerSyncCall(&req, &res, result);
274 }
275 
276 void JackWinNamedPipeClientChannel::GetUUIDForClientName(int refnum, const char* client_name, char* uuid_res, int* result)
277 {
278  JackGetUUIDRequest req(client_name);
279  JackUUIDResult res;
280  ServerSyncCall(&req, &res, result);
281  strncpy(uuid_res, res.fUUID, JACK_UUID_SIZE);
282 }
283 
284 void JackWinNamedPipeClientChannel::GetClientNameForUUID(int refnum, const char* uuid, char* name_res, int* result)
285 {
286  JackGetClientNameRequest req(uuid);
287  JackClientNameResult res;
288  ServerSyncCall(&req, &res, result);
289  strncpy(name_res, res.fName, JACK_CLIENT_NAME_SIZE);
290 }
291 
292 void JackWinNamedPipeClientChannel::ClientHasSessionCallback(const char* client_name, int* result)
293 {
294  JackClientHasSessionCallbackRequest req(client_name);
295  JackResult res;
296  ServerSyncCall(&req, &res, result);
297 }
298 
299 void JackWinNamedPipeClientChannel::ReserveClientName(int refnum, const char* client_name, const char* uuid, int* result)
300 {
301  JackReserveNameRequest req(refnum, client_name, uuid);
302  JackResult res;
303  ServerSyncCall(&req, &res, result);
304 }
305 
306 void JackWinNamedPipeClientChannel::ReleaseTimebase(int refnum, int* result)
307 {
308  JackReleaseTimebaseRequest req(refnum);
309  JackResult res;
310  ServerSyncCall(&req, &res, result);
311 }
312 
313 void JackWinNamedPipeClientChannel::SetTimebaseCallback(int refnum, int conditional, int* result)
314 {
315  JackSetTimebaseCallbackRequest req(refnum, conditional);
316  JackResult res;
317  ServerSyncCall(&req, &res, result);
318 }
319 
320 void JackWinNamedPipeClientChannel::GetInternalClientName(int refnum, int int_ref, char* name_res, int* result)
321 {
322  JackGetInternalClientNameRequest req(refnum, int_ref);
323  JackGetInternalClientNameResult res;
324  ServerSyncCall(&req, &res, result);
325  strcpy(name_res, res.fName);
326 }
327 
328 void JackWinNamedPipeClientChannel::InternalClientHandle(int refnum, const char* client_name, int* status, int* int_ref, int* result)
329 {
330  JackInternalClientHandleRequest req(refnum, client_name);
331  JackInternalClientHandleResult res;
332  ServerSyncCall(&req, &res, result);
333  *int_ref = res.fIntRefNum;
334  *status = res.fStatus;
335 }
336 
337 void JackWinNamedPipeClientChannel::InternalClientLoad(int refnum, const char* client_name, const char* so_name, const char* objet_data, int options, int* status, int* int_ref, int uuid, int* result)
338 {
339  JackInternalClientLoadRequest req(refnum, client_name, so_name, objet_data, options, uuid);
340  JackInternalClientLoadResult res;
341  ServerSyncCall(&req, &res, result);
342  *int_ref = res.fIntRefNum;
343  *status = res.fStatus;
344 }
345 
346 void JackWinNamedPipeClientChannel::InternalClientUnload(int refnum, int int_ref, int* status, int* result)
347 {
348  JackInternalClientUnloadRequest req(refnum, int_ref);
349  JackInternalClientUnloadResult res;
350  ServerSyncCall(&req, &res, result);
351  *status = res.fStatus;
352 }
353 
355 {
356  jack_log("JackWinNamedPipeClientChannel::Init");
357 
358  if (!fNotificationListenPipe.Accept()) {
359  jack_error("JackWinNamedPipeClientChannel: cannot establish notification pipe");
360  return false;
361  } else {
362  return true;
363  }
364 }
365 
366 bool JackWinNamedPipeClientChannel::Execute()
367 {
369  JackResult res;
370 
371  if (event.Read(&fNotificationListenPipe) < 0) {
372  jack_error("JackWinNamedPipeClientChannel read fail");
373  goto error;
374  }
375 
376  res.fResult = fClient->ClientNotify(event.fRefNum, event.fName, event.fNotify, event.fSync, event.fMessage, event.fValue1, event.fValue2);
377 
378  if (event.fSync) {
379  if (res.Write(&fNotificationListenPipe) < 0) {
380  jack_error("JackWinNamedPipeClientChannel write fail");
381  goto error;
382  }
383  }
384  return true;
385 
386 error:
387  // Close the pipes, server wont be able to create them otherwise.
388  fNotificationListenPipe.Close();
389  fRequestPipe.Close();
390  fClient->ShutDown();
391  return false;
392 }
393 
394 } // end of namespace
395 
396