Update XCode project.
[jack2.git] / posix / JackSocketClientChannel.cpp
blobd2d4b676db0fb89e9f5b242099a89b5ebbe39d7b
1 /*
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.
20 #include "JackSocketClientChannel.h"
21 #include "JackRequest.h"
22 #include "JackClient.h"
23 #include "JackGlobals.h"
25 namespace Jack
28 JackSocketClientChannel::JackSocketClientChannel():
29 fThread(this)
31 fNotificationSocket = NULL;
32 fClient = NULL;
35 JackSocketClientChannel::~JackSocketClientChannel()
37 delete fNotificationSocket;
40 int JackSocketClientChannel::ServerCheck(const char* server_name)
42 jack_log("JackSocketClientChannel::ServerCheck = %s", server_name);
44 // Connect to server
45 if (fRequestSocket.Connect(jack_server_dir, server_name, 0) < 0) {
46 jack_error("Cannot connect to server socket");
47 fRequestSocket.Close();
48 return -1;
49 } else {
50 return 0;
54 int JackSocketClientChannel::Open(const char* server_name, const char* name, char* name_res, JackClient* obj, jack_options_t options, jack_status_t* status)
56 int result = 0;
57 jack_log("JackSocketClientChannel::Open name = %s", name);
59 if (fRequestSocket.Connect(jack_server_dir, server_name, 0) < 0) {
60 jack_error("Cannot connect to server socket");
61 goto error;
64 // Check name in server
65 ClientCheck(name, name_res, JACK_PROTOCOL_VERSION, (int)options, (int*)status, &result);
66 if (result < 0) {
67 int status1 = *status;
68 if (status1 & JackVersionError)
69 jack_error("JACK protocol mismatch %d", JACK_PROTOCOL_VERSION);
70 else
71 jack_error("Client name = %s conflits with another running client", name);
72 goto error;
75 if (fNotificationListenSocket.Bind(jack_client_dir, name_res, 0) < 0) {
76 jack_error("Cannot bind socket");
77 goto error;
80 fClient = obj;
81 return 0;
83 error:
84 fRequestSocket.Close();
85 fNotificationListenSocket.Close();
86 return -1;
89 void JackSocketClientChannel::Close()
91 fRequestSocket.Close();
92 fNotificationListenSocket.Close();
93 if (fNotificationSocket)
94 fNotificationSocket->Close();
97 int JackSocketClientChannel::Start()
99 jack_log("JackSocketClientChannel::Start");
101 To be sure notification thread is started before ClientOpen is called.
103 if (fThread.StartSync() != 0) {
104 jack_error("Cannot start Jack client listener");
105 return -1;
106 } else {
107 return 0;
112 void JackSocketClientChannel::Stop()
114 jack_log("JackSocketClientChannel::Stop");
115 fThread.Kill();
118 void JackSocketClientChannel::ServerSyncCall(JackRequest* req, JackResult* res, int* result)
120 if (req->Write(&fRequestSocket) < 0) {
121 jack_error("Could not write request type = %ld", req->fType);
122 *result = -1;
123 return;
126 if (res->Read(&fRequestSocket) < 0) {
127 jack_error("Could not read result type = %ld", req->fType);
128 *result = -1;
129 return;
132 *result = res->fResult;
135 void JackSocketClientChannel::ServerAsyncCall(JackRequest* req, JackResult* res, int* result)
137 if (req->Write(&fRequestSocket) < 0) {
138 jack_error("Could not write request type = %ld", req->fType);
139 *result = -1;
140 } else {
141 *result = 0;
145 void JackSocketClientChannel::ClientCheck(const char* name, char* name_res, int protocol, int options, int* status, int* result)
147 JackClientCheckRequest req(name, protocol, options);
148 JackClientCheckResult res;
149 ServerSyncCall(&req, &res, result);
150 *status = res.fStatus;
151 strcpy(name_res, res.fName);
154 void JackSocketClientChannel::ClientOpen(const char* name, int pid, int* shared_engine, int* shared_client, int* shared_graph, int* result)
156 JackClientOpenRequest req(name, pid);
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 JackSocketClientChannel::ClientClose(int refnum, int* result)
166 JackClientCloseRequest req(refnum);
167 JackResult res;
168 ServerSyncCall(&req, &res, result);
171 void JackSocketClientChannel::ClientActivate(int refnum, int is_real_time, int* result)
173 JackActivateRequest req(refnum, is_real_time);
174 JackResult res;
175 ServerSyncCall(&req, &res, result);
178 void JackSocketClientChannel::ClientDeactivate(int refnum, int* result)
180 JackDeactivateRequest req(refnum);
181 JackResult res;
182 ServerSyncCall(&req, &res, result);
185 void JackSocketClientChannel::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 JackSocketClientChannel::PortUnRegister(int refnum, jack_port_id_t port_index, int* result)
195 JackPortUnRegisterRequest req(refnum, port_index);
196 JackResult res;
197 ServerSyncCall(&req, &res, result);
200 void JackSocketClientChannel::PortConnect(int refnum, const char* src, const char* dst, int* result)
202 JackPortConnectNameRequest req(refnum, src, dst);
203 JackResult res;
204 ServerSyncCall(&req, &res, result);
207 void JackSocketClientChannel::PortDisconnect(int refnum, const char* src, const char* dst, int* result)
209 JackPortDisconnectNameRequest req(refnum, src, dst);
210 JackResult res;
211 ServerSyncCall(&req, &res, result);
214 void JackSocketClientChannel::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
216 JackPortConnectRequest req(refnum, src, dst);
217 JackResult res;
218 ServerSyncCall(&req, &res, result);
221 void JackSocketClientChannel::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
223 JackPortDisconnectRequest req(refnum, src, dst);
224 JackResult res;
225 ServerSyncCall(&req, &res, result);
228 void JackSocketClientChannel::PortRename(int refnum, jack_port_id_t port, const char* name, int* result)
230 JackPortRenameRequest req(refnum, port, name);
231 JackResult res;
232 ServerSyncCall(&req, &res, result);
235 void JackSocketClientChannel::SetBufferSize(jack_nframes_t buffer_size, int* result)
237 JackSetBufferSizeRequest req(buffer_size);
238 JackResult res;
239 ServerSyncCall(&req, &res, result);
242 void JackSocketClientChannel::SetFreewheel(int onoff, int* result)
244 JackSetFreeWheelRequest req(onoff);
245 JackResult res;
246 ServerSyncCall(&req, &res, result);
249 void JackSocketClientChannel::ReleaseTimebase(int refnum, int* result)
251 JackReleaseTimebaseRequest req(refnum);
252 JackResult res;
253 ServerSyncCall(&req, &res, result);
256 void JackSocketClientChannel::SetTimebaseCallback(int refnum, int conditional, int* result)
258 JackSetTimebaseCallbackRequest req(refnum, conditional);
259 JackResult res;
260 ServerSyncCall(&req, &res, result);
263 void JackSocketClientChannel::GetInternalClientName(int refnum, int int_ref, char* name_res, int* result)
265 JackGetInternalClientNameRequest req(refnum, int_ref);
266 JackGetInternalClientNameResult res;
267 ServerSyncCall(&req, &res, result);
268 strcpy(name_res, res.fName);
271 void JackSocketClientChannel::InternalClientHandle(int refnum, const char* client_name, int* status, int* int_ref, int* result)
273 JackInternalClientHandleRequest req(refnum, client_name);
274 JackInternalClientHandleResult res;
275 ServerSyncCall(&req, &res, result);
276 *int_ref = res.fIntRefNum;
277 *status = res.fStatus;
280 void JackSocketClientChannel::InternalClientLoad(int refnum, const char* client_name, const char* so_name, const char* objet_data, int options, int* status, int* int_ref, int* result)
282 JackInternalClientLoadRequest req(refnum, client_name, so_name, objet_data, options);
283 JackInternalClientLoadResult res;
284 ServerSyncCall(&req, &res, result);
285 *int_ref = res.fIntRefNum;
286 *status = res.fStatus;
289 void JackSocketClientChannel::InternalClientUnload(int refnum, int int_ref, int* status, int* result)
291 JackInternalClientUnloadRequest req(refnum, int_ref);
292 JackInternalClientUnloadResult res;
293 ServerSyncCall(&req, &res, result);
294 *status = res.fStatus;
297 bool JackSocketClientChannel::Init()
299 jack_log("JackSocketClientChannel::Init");
300 fNotificationSocket = fNotificationListenSocket.Accept();
301 // No more needed
302 fNotificationListenSocket.Close();
304 if (!fNotificationSocket) {
305 jack_error("JackSocketClientChannel: cannot establish notication socket");
306 return false;
307 } else {
308 return fClient->Init();
312 bool JackSocketClientChannel::Execute()
314 JackClientNotification event;
315 JackResult res;
317 if (event.Read(fNotificationSocket) < 0) {
318 fNotificationSocket->Close();
319 jack_error("JackSocketClientChannel read fail");
320 goto error;
323 res.fResult = fClient->ClientNotify(event.fRefNum, event.fName, event.fNotify, event.fSync, event.fMessage, event.fValue1, event.fValue2);
325 if (event.fSync) {
326 if (res.Write(fNotificationSocket) < 0) {
327 fNotificationSocket->Close();
328 jack_error("JackSocketClientChannel write fail");
329 goto error;
332 return true;
334 error:
335 fClient->ShutDown();
336 return false;
339 } // end of namespace