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 "JackWinNamedPipeClientChannel.h"
22 #include "JackRequest.h"
23 #include "JackClient.h"
24 #include "JackGlobals.h"
25 #include "JackError.h"
30 JackWinNamedPipeClientChannel::JackWinNamedPipeClientChannel():fThread(this)
35 JackWinNamedPipeClientChannel::~JackWinNamedPipeClientChannel()
38 int JackWinNamedPipeClientChannel::ServerCheck(const char* server_name
)
40 jack_log("JackWinNamedPipeClientChannel::ServerCheck = %s", server_name
);
43 if (fRequestPipe
.Connect(jack_server_dir
, server_name
, 0) < 0) {
44 jack_error("Cannot connect to server pipe");
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
)
54 jack_log("JackWinNamedPipeClientChannel::Open name = %s", name
);
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");
64 if (fRequestPipe
.Connect(jack_server_dir
, server_name
, 0) < 0) {
65 jack_error("Cannot connect to server pipe");
69 // Check name in server
70 ClientCheck(name
, uuid
, name_res
, JACK_PROTOCOL_VERSION
, (int)options
, (int*)status
, &result
);
72 jack_error("Client name = %s conflits with another running client", name
);
76 if (fNotificationListenPipe
.Bind(jack_client_dir
, name_res
, 0) < 0) {
77 jack_error("Cannot bind pipe");
86 fNotificationListenPipe
.Close();
90 void JackWinNamedPipeClientChannel::Close()
93 fNotificationListenPipe
.Close();
94 // Here the thread will correctly stop when the pipe are closed
98 int JackWinNamedPipeClientChannel::Start()
100 jack_log("JackWinNamedPipeClientChannel::Start");
102 To be sure notification thread is started before ClientOpen is called.
104 if (fThread
.StartSync() != 0) {
105 jack_error("Cannot start Jack client listener");
112 void JackWinNamedPipeClientChannel::Stop()
114 jack_log("JackWinNamedPipeClientChannel::Stop");
115 fThread
.Kill(); // Unsafe on WIN32... TODO : solve WIN32 thread Kill issue
118 void JackWinNamedPipeClientChannel::ServerSyncCall(JackRequest
* req
, JackResult
* res
, int* result
)
120 if (req
->Write(&fRequestPipe
) < 0) {
121 jack_error("Could not write request type = %ld", req
->fType
);
126 if (res
->Read(&fRequestPipe
) < 0) {
127 jack_error("Could not read result type = %ld", req
->fType
);
132 *result
= res
->fResult
;
135 void JackWinNamedPipeClientChannel::ServerAsyncCall(JackRequest
* req
, JackResult
* res
, int* result
)
137 if (req
->Write(&fRequestPipe
) < 0) {
138 jack_error("Could not write request type = %ld", req
->fType
);
145 void JackWinNamedPipeClientChannel::ClientCheck(const char* name
, int uuid
, char* name_res
, int protocol
, int options
, int* status
, int* result
)
147 JackClientCheckRequest
req(name
, protocol
, options
, uuid
);
148 JackClientCheckResult res
;
149 ServerSyncCall(&req
, &res
, result
);
150 *status
= res
.fStatus
;
151 strcpy(name_res
, res
.fName
);
154 void JackWinNamedPipeClientChannel::ClientOpen(const char* name
, int pid
, int uuid
, int* shared_engine
, int* shared_client
, int* shared_graph
, int* result
)
156 JackClientOpenRequest
req(name
, pid
, uuid
);
157 JackClientOpenResult res
;
158 ServerSyncCall(&req
, &res
, result
);
159 *shared_engine
= res
.fSharedEngine
;
160 *shared_client
= res
.fSharedClient
;
161 *shared_graph
= res
.fSharedGraph
;
164 void JackWinNamedPipeClientChannel::ClientClose(int refnum
, int* result
)
166 JackClientCloseRequest
req(refnum
);
168 ServerSyncCall(&req
, &res
, result
);
171 void JackWinNamedPipeClientChannel::ClientActivate(int refnum
, int is_real_time
, int* result
)
173 JackActivateRequest
req(refnum
, is_real_time
);
175 ServerSyncCall(&req
, &res
, result
);
178 void JackWinNamedPipeClientChannel::ClientDeactivate(int refnum
, int* result
)
180 JackDeactivateRequest
req(refnum
);
182 ServerSyncCall(&req
, &res
, result
);
185 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
)
187 JackPortRegisterRequest
req(refnum
, name
, type
, flags
, buffer_size
);
188 JackPortRegisterResult res
;
189 ServerSyncCall(&req
, &res
, result
);
190 *port_index
= res
.fPortIndex
;
193 void JackWinNamedPipeClientChannel::PortUnRegister(int refnum
, jack_port_id_t port_index
, int* result
)
195 JackPortUnRegisterRequest
req(refnum
, port_index
);
197 ServerSyncCall(&req
, &res
, result
);
200 void JackWinNamedPipeClientChannel::PortConnect(int refnum
, const char* src
, const char* dst
, int* result
)
202 JackPortConnectNameRequest
req(refnum
, src
, dst
);
204 ServerSyncCall(&req
, &res
, result
);
207 void JackWinNamedPipeClientChannel::PortDisconnect(int refnum
, const char* src
, const char* dst
, int* result
)
209 JackPortDisconnectNameRequest
req(refnum
, src
, dst
);
211 ServerSyncCall(&req
, &res
, result
);
214 void JackWinNamedPipeClientChannel::PortConnect(int refnum
, jack_port_id_t src
, jack_port_id_t dst
, int* result
)
216 JackPortConnectRequest
req(refnum
, src
, dst
);
218 ServerSyncCall(&req
, &res
, result
);
221 void JackWinNamedPipeClientChannel::PortDisconnect(int refnum
, jack_port_id_t src
, jack_port_id_t dst
, int* result
)
223 JackPortDisconnectRequest
req(refnum
, src
, dst
);
225 ServerSyncCall(&req
, &res
, result
);
228 void JackWinNamedPipeClientChannel::PortRename(int refnum
, jack_port_id_t port
, const char* name
, int* result
)
230 JackPortRenameRequest
req(refnum
, port
, name
);
232 ServerSyncCall(&req
, &res
, result
);
235 void JackWinNamedPipeClientChannel::SetBufferSize(jack_nframes_t buffer_size
, int* result
)
237 JackSetBufferSizeRequest
req(buffer_size
);
239 ServerSyncCall(&req
, &res
, result
);
242 void JackWinNamedPipeClientChannel::SetFreewheel(int onoff
, int* result
)
244 JackSetFreeWheelRequest
req(onoff
);
246 ServerSyncCall(&req
, &res
, result
);
249 void JackWinNamedPipeClientChannel::ComputeTotalLatencies(int* result
)
251 JackComputeTotalLatenciesRequest req
;
253 ServerSyncCall(&req
, &res
, result
);
256 void JackWinNamedPipeClientChannel::SessionNotify(int refnum
, const char* target
, jack_session_event_type_t type
, const char* path
, jack_session_command_t
** result
)
258 JackSessionNotifyRequest
req(refnum
, path
, type
, target
);
259 JackSessionNotifyResult res
;
261 ServerSyncCall(&req
, &res
, &intresult
);
263 jack_session_command_t
* session_command
= (jack_session_command_t
*)malloc(sizeof(jack_session_command_t
) * (res
.fCommandList
.size() + 1));
266 for (std::list
<JackSessionCommand
>::iterator ci
=res
.fCommandList
.begin(); ci
!=res
.fCommandList
.end(); ci
++) {
267 session_command
[i
].uuid
= strdup( ci
->fUUID
);
268 session_command
[i
].client_name
= strdup( ci
->fClientName
);
269 session_command
[i
].command
= strdup( ci
->fCommand
);
270 session_command
[i
].flags
= ci
->fFlags
;
274 session_command
[i
].uuid
= NULL
;
275 session_command
[i
].client_name
= NULL
;
276 session_command
[i
].command
= NULL
;
277 session_command
[i
].flags
= (jack_session_flags_t
)0;
279 *result
= session_command
;
282 void JackWinNamedPipeClientChannel::SessionReply(int refnum
, int* result
)
284 JackSessionReplyRequest
req(refnum
);
286 ServerSyncCall(&req
, &res
, result
);
289 void JackWinNamedPipeClientChannel::GetUUIDForClientName(int refnum
, const char* client_name
, char* uuid_res
, int* result
)
291 JackGetUUIDRequest
req(client_name
);
293 ServerSyncCall(&req
, &res
, result
);
294 strncpy(uuid_res
, res
.fUUID
, JACK_UUID_SIZE
);
297 void JackWinNamedPipeClientChannel::GetClientNameForUUID(int refnum
, const char* uuid
, char* name_res
, int* result
)
299 JackGetClientNameRequest
req(uuid
);
300 JackClientNameResult res
;
301 ServerSyncCall(&req
, &res
, result
);
302 strncpy(name_res
, res
.fName
, JACK_CLIENT_NAME_SIZE
);
305 void JackWinNamedPipeClientChannel::ClientHasSessionCallback(const char* client_name
, int* result
)
307 JackClientHasSessionCallbackRequest
req(client_name
);
309 ServerSyncCall(&req
, &res
, result
);
312 void JackWinNamedPipeClientChannel::ReserveClientName(int refnum
, const char* client_name
, const char* uuid
, int* result
)
314 JackReserveNameRequest
req(refnum
, client_name
, uuid
);
316 ServerSyncCall(&req
, &res
, result
);
319 void JackWinNamedPipeClientChannel::ReleaseTimebase(int refnum
, int* result
)
321 JackReleaseTimebaseRequest
req(refnum
);
323 ServerSyncCall(&req
, &res
, result
);
326 void JackWinNamedPipeClientChannel::SetTimebaseCallback(int refnum
, int conditional
, int* result
)
328 JackSetTimebaseCallbackRequest
req(refnum
, conditional
);
330 ServerSyncCall(&req
, &res
, result
);
333 void JackWinNamedPipeClientChannel::GetInternalClientName(int refnum
, int int_ref
, char* name_res
, int* result
)
335 JackGetInternalClientNameRequest
req(refnum
, int_ref
);
336 JackGetInternalClientNameResult res
;
337 ServerSyncCall(&req
, &res
, result
);
338 strcpy(name_res
, res
.fName
);
341 void JackWinNamedPipeClientChannel::InternalClientHandle(int refnum
, const char* client_name
, int* status
, int* int_ref
, int* result
)
343 JackInternalClientHandleRequest
req(refnum
, client_name
);
344 JackInternalClientHandleResult res
;
345 ServerSyncCall(&req
, &res
, result
);
346 *int_ref
= res
.fIntRefNum
;
347 *status
= res
.fStatus
;
350 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
)
352 JackInternalClientLoadRequest
req(refnum
, client_name
, so_name
, objet_data
, options
, uuid
);
353 JackInternalClientLoadResult res
;
354 ServerSyncCall(&req
, &res
, result
);
355 *int_ref
= res
.fIntRefNum
;
356 *status
= res
.fStatus
;
359 void JackWinNamedPipeClientChannel::InternalClientUnload(int refnum
, int int_ref
, int* status
, int* result
)
361 JackInternalClientUnloadRequest
req(refnum
, int_ref
);
362 JackInternalClientUnloadResult res
;
363 ServerSyncCall(&req
, &res
, result
);
364 *status
= res
.fStatus
;
367 bool JackWinNamedPipeClientChannel::Init()
369 jack_log("JackWinNamedPipeClientChannel::Init");
371 if (!fNotificationListenPipe
.Accept()) {
372 jack_error("JackWinNamedPipeClientChannel: cannot establish notification pipe");
379 bool JackWinNamedPipeClientChannel::Execute()
381 JackClientNotification event
;
384 if (event
.Read(&fNotificationListenPipe
) < 0) {
385 jack_error("JackWinNamedPipeClientChannel read fail");
389 res
.fResult
= fClient
->ClientNotify(event
.fRefNum
, event
.fName
, event
.fNotify
, event
.fSync
, event
.fMessage
, event
.fValue1
, event
.fValue2
);
392 if (res
.Write(&fNotificationListenPipe
) < 0) {
393 jack_error("JackWinNamedPipeClientChannel write fail");
400 // Close the pipes, server wont be able to create them otherwise.
401 fNotificationListenPipe
.Close();
402 fRequestPipe
.Close();
407 } // end of namespace