jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

342 lines
9.6KB

  1. /*
  2. Copyright (C) 2004-2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #if defined(HAVE_CONFIG_H)
  16. #include "config.h"
  17. #endif
  18. #include "JackSocketClientChannel.h"
  19. #include "JackRequest.h"
  20. #include "JackClient.h"
  21. #include "JackGlobals.h"
  22. namespace Jack
  23. {
  24. JackSocketClientChannel::JackSocketClientChannel():
  25. fThread(this)
  26. {
  27. fNotificationSocket = NULL;
  28. fClient = NULL;
  29. }
  30. JackSocketClientChannel::~JackSocketClientChannel()
  31. {
  32. delete fNotificationSocket;
  33. }
  34. int JackSocketClientChannel::ServerCheck(const char* server_name)
  35. {
  36. jack_log("JackSocketClientChannel::ServerCheck = %s", server_name);
  37. // Connect to server
  38. if (fRequestSocket.Connect(jack_server_dir, server_name, 0) < 0) {
  39. jack_error("Cannot connect to server socket");
  40. fRequestSocket.Close();
  41. return -1;
  42. } else {
  43. return 0;
  44. }
  45. }
  46. int JackSocketClientChannel::Open(const char* server_name, const char* name, char* name_res, JackClient* obj, jack_options_t options, jack_status_t* status)
  47. {
  48. int result = 0;
  49. jack_log("JackSocketClientChannel::Open name = %s", name);
  50. if (fRequestSocket.Connect(jack_server_dir, server_name, 0) < 0) {
  51. jack_error("Cannot connect to server socket");
  52. goto error;
  53. }
  54. // Check name in server
  55. ClientCheck(name, name_res, JACK_PROTOCOL_VERSION, (int)options, (int*)status, &result);
  56. if (result < 0) {
  57. int status1 = *status;
  58. if (status1 & JackVersionError)
  59. jack_error("JACK protocol mismatch %d", JACK_PROTOCOL_VERSION);
  60. else
  61. jack_error("Client name = %s conflits with another running client", name);
  62. goto error;
  63. }
  64. if (fNotificationListenSocket.Bind(jack_client_dir, name_res, 0) < 0) {
  65. jack_error("Cannot bind socket");
  66. goto error;
  67. }
  68. fClient = obj;
  69. return 0;
  70. error:
  71. fRequestSocket.Close();
  72. fNotificationListenSocket.Close();
  73. return -1;
  74. }
  75. void JackSocketClientChannel::Close()
  76. {
  77. fRequestSocket.Close();
  78. fNotificationListenSocket.Close();
  79. if (fNotificationSocket)
  80. fNotificationSocket->Close();
  81. }
  82. int JackSocketClientChannel::Start()
  83. {
  84. jack_log("JackSocketClientChannel::Start");
  85. /*
  86. To be sure notification thread is started before ClientOpen is called.
  87. */
  88. if (fThread.StartSync() != 0) {
  89. jack_error("Cannot start Jack client listener");
  90. return -1;
  91. } else {
  92. return 0;
  93. }
  94. }
  95. void JackSocketClientChannel::Stop()
  96. {
  97. jack_log("JackSocketClientChannel::Stop");
  98. fThread.Kill();
  99. }
  100. void JackSocketClientChannel::ServerSyncCall(JackRequest* req, JackResult* res, int* result)
  101. {
  102. if (req->Write(&fRequestSocket) < 0) {
  103. jack_error("Could not write request type = %ld", req->fType);
  104. *result = -1;
  105. return ;
  106. }
  107. if (res->Read(&fRequestSocket) < 0) {
  108. jack_error("Could not read result type = %ld", req->fType);
  109. *result = -1;
  110. return ;
  111. }
  112. *result = res->fResult;
  113. }
  114. void JackSocketClientChannel::ServerAsyncCall(JackRequest* req, JackResult* res, int* result)
  115. {
  116. if (req->Write(&fRequestSocket) < 0) {
  117. jack_error("Could not write request type = %ld", req->fType);
  118. *result = -1;
  119. } else {
  120. *result = 0;
  121. }
  122. }
  123. void JackSocketClientChannel::ClientCheck(const char* name, char* name_res, int protocol, int options, int* status, int* result)
  124. {
  125. JackClientCheckRequest req(name, protocol, options);
  126. JackClientCheckResult res;
  127. ServerSyncCall(&req, &res, result);
  128. *status = res.fStatus;
  129. strcpy(name_res, res.fName);
  130. }
  131. void JackSocketClientChannel::ClientOpen(const char* name, int pid, int* shared_engine, int* shared_client, int* shared_graph, int* result)
  132. {
  133. JackClientOpenRequest req(name, pid);
  134. JackClientOpenResult res;
  135. ServerSyncCall(&req, &res, result);
  136. *shared_engine = res.fSharedEngine;
  137. *shared_client = res.fSharedClient;
  138. *shared_graph = res.fSharedGraph;
  139. }
  140. void JackSocketClientChannel::ClientClose(int refnum, int* result)
  141. {
  142. JackClientCloseRequest req(refnum);
  143. JackResult res;
  144. ServerSyncCall(&req, &res, result);
  145. }
  146. void JackSocketClientChannel::ClientActivate(int refnum, int state, int* result)
  147. {
  148. JackActivateRequest req(refnum, state);
  149. JackResult res;
  150. ServerSyncCall(&req, &res, result);
  151. }
  152. void JackSocketClientChannel::ClientDeactivate(int refnum, int* result)
  153. {
  154. JackDeactivateRequest req(refnum);
  155. JackResult res;
  156. ServerSyncCall(&req, &res, result);
  157. }
  158. void JackSocketClientChannel::PortRegister(int refnum, const char* name, const char* type, unsigned int flags, unsigned int buffer_size, unsigned int* port_index, int* result)
  159. {
  160. JackPortRegisterRequest req(refnum, name, type, flags, buffer_size);
  161. JackPortRegisterResult res;
  162. ServerSyncCall(&req, &res, result);
  163. *port_index = res.fPortIndex;
  164. }
  165. void JackSocketClientChannel::PortUnRegister(int refnum, jack_port_id_t port_index, int* result)
  166. {
  167. JackPortUnRegisterRequest req(refnum, port_index);
  168. JackResult res;
  169. ServerSyncCall(&req, &res, result);
  170. }
  171. void JackSocketClientChannel::PortConnect(int refnum, const char* src, const char* dst, int* result)
  172. {
  173. JackPortConnectNameRequest req(refnum, src, dst);
  174. JackResult res;
  175. ServerSyncCall(&req, &res, result);
  176. }
  177. void JackSocketClientChannel::PortDisconnect(int refnum, const char* src, const char* dst, int* result)
  178. {
  179. JackPortDisconnectNameRequest req(refnum, src, dst);
  180. JackResult res;
  181. ServerSyncCall(&req, &res, result);
  182. }
  183. void JackSocketClientChannel::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
  184. {
  185. JackPortConnectRequest req(refnum, src, dst);
  186. JackResult res;
  187. ServerSyncCall(&req, &res, result);
  188. }
  189. void JackSocketClientChannel::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
  190. {
  191. JackPortDisconnectRequest req(refnum, src, dst);
  192. JackResult res;
  193. ServerSyncCall(&req, &res, result);
  194. }
  195. void JackSocketClientChannel::SetBufferSize(jack_nframes_t buffer_size, int* result)
  196. {
  197. JackSetBufferSizeRequest req(buffer_size);
  198. JackResult res;
  199. ServerSyncCall(&req, &res, result);
  200. }
  201. void JackSocketClientChannel::SetFreewheel(int onoff, int* result)
  202. {
  203. JackSetFreeWheelRequest req(onoff);
  204. JackResult res;
  205. ServerSyncCall(&req, &res, result);
  206. }
  207. void JackSocketClientChannel::ReleaseTimebase(int refnum, int* result)
  208. {
  209. JackReleaseTimebaseRequest req(refnum);
  210. JackResult res;
  211. ServerSyncCall(&req, &res, result);
  212. }
  213. void JackSocketClientChannel::SetTimebaseCallback(int refnum, int conditional, int* result)
  214. {
  215. JackSetTimebaseCallbackRequest req(refnum, conditional);
  216. JackResult res;
  217. ServerSyncCall(&req, &res, result);
  218. }
  219. void JackSocketClientChannel::GetInternalClientName(int refnum, int int_ref, char* name_res, int* result)
  220. {
  221. JackGetInternalClientNameRequest req(refnum, int_ref);
  222. JackGetInternalClientNameResult res;
  223. ServerSyncCall(&req, &res, result);
  224. strcpy(name_res, res.fName);
  225. }
  226. void JackSocketClientChannel::InternalClientHandle(int refnum, const char* client_name, int* status, int* int_ref, int* result)
  227. {
  228. JackInternalClientHandleRequest req(refnum, client_name);
  229. JackInternalClientHandleResult res;
  230. ServerSyncCall(&req, &res, result);
  231. *int_ref = res.fIntRefNum;
  232. *status = res.fStatus;
  233. }
  234. 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)
  235. {
  236. JackInternalClientLoadRequest req(refnum, client_name, so_name, objet_data, options);
  237. JackInternalClientLoadResult res;
  238. ServerSyncCall(&req, &res, result);
  239. *int_ref = res.fIntRefNum;
  240. *status = res.fStatus;
  241. }
  242. void JackSocketClientChannel::InternalClientUnload(int refnum, int int_ref, int* status, int* result)
  243. {
  244. JackInternalClientUnloadRequest req(refnum, int_ref);
  245. JackInternalClientUnloadResult res;
  246. ServerSyncCall(&req, &res, result);
  247. *status = res.fStatus;
  248. }
  249. bool JackSocketClientChannel::Init()
  250. {
  251. jack_log("JackSocketClientChannel::Init");
  252. fNotificationSocket = fNotificationListenSocket.Accept();
  253. // No more needed
  254. fNotificationListenSocket.Close();
  255. if (!fNotificationSocket) {
  256. jack_error("JackSocketClientChannel: cannot establish notication socket");
  257. return false;
  258. } else {
  259. return fClient->Init();
  260. }
  261. }
  262. bool JackSocketClientChannel::Execute()
  263. {
  264. JackClientNotification event;
  265. JackResult res;
  266. if (event.Read(fNotificationSocket) < 0) {
  267. fNotificationSocket->Close();
  268. jack_error("JackSocketClientChannel read fail");
  269. goto error;
  270. }
  271. res.fResult = fClient->ClientNotify(event.fRefNum, event.fName, event.fNotify, event.fSync, event.fValue1, event.fValue2);
  272. if (event.fSync) {
  273. if (res.Write(fNotificationSocket) < 0) {
  274. fNotificationSocket->Close();
  275. jack_error("JackSocketClientChannel write fail");
  276. goto error;
  277. }
  278. }
  279. return true;
  280. error:
  281. fClient->ShutDown();
  282. return false;
  283. }
  284. } // end of namespace