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.

350 lines
10KB

  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. #include "JackWinNamedPipeClientChannel.h"
  16. #include "JackRequest.h"
  17. #include "JackClient.h"
  18. #include "JackGlobals.h"
  19. #include "JackError.h"
  20. namespace Jack
  21. {
  22. JackWinNamedPipeClientChannel::JackWinNamedPipeClientChannel():fThread(this)
  23. {
  24. fClient = NULL;
  25. }
  26. JackWinNamedPipeClientChannel::~JackWinNamedPipeClientChannel()
  27. {}
  28. int JackWinNamedPipeClientChannel::ServerCheck(const char* server_name)
  29. {
  30. jack_log("JackWinNamedPipeClientChannel::ServerCheck = %s", server_name);
  31. // Connect to server
  32. if (fRequestPipe.Connect(jack_server_dir, server_name, 0) < 0) {
  33. jack_error("Cannot connect to server pipe");
  34. return -1;
  35. } else {
  36. return 0;
  37. }
  38. }
  39. 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)
  40. {
  41. int result = 0;
  42. jack_log("JackWinNamedPipeClientChannel::Open name = %s", name);
  43. /*
  44. 16/08/07: was called before doing "fRequestPipe.Connect" .... still necessary?
  45. if (fNotificationListenPipe.Bind(jack_client_dir, name, 0) < 0) {
  46. jack_error("Cannot bind pipe");
  47. goto error;
  48. }
  49. */
  50. if (fRequestPipe.Connect(jack_server_dir, server_name, 0) < 0) {
  51. jack_error("Cannot connect to server pipe");
  52. goto error;
  53. }
  54. // Check name in server
  55. ClientCheck(name, uuid, name_res, JACK_PROTOCOL_VERSION, (int)options, (int*)status, &result);
  56. if (result < 0) {
  57. jack_error("Client name = %s conflits with another running client", name);
  58. goto error;
  59. }
  60. if (fNotificationListenPipe.Bind(jack_client_dir, name_res, 0) < 0) {
  61. jack_error("Cannot bind pipe");
  62. goto error;
  63. }
  64. fClient = obj;
  65. return 0;
  66. error:
  67. fRequestPipe.Close();
  68. fNotificationListenPipe.Close();
  69. return -1;
  70. }
  71. void JackWinNamedPipeClientChannel::Close()
  72. {
  73. fRequestPipe.Close();
  74. fNotificationListenPipe.Close();
  75. // Here the thread will correctly stop when the pipe are closed
  76. fThread.Stop();
  77. }
  78. int JackWinNamedPipeClientChannel::Start()
  79. {
  80. jack_log("JackWinNamedPipeClientChannel::Start");
  81. /*
  82. To be sure notification thread is started before ClientOpen is called.
  83. */
  84. if (fThread.StartSync() != 0) {
  85. jack_error("Cannot start Jack client listener");
  86. return -1;
  87. } else {
  88. return 0;
  89. }
  90. }
  91. void JackWinNamedPipeClientChannel::Stop()
  92. {
  93. jack_log("JackWinNamedPipeClientChannel::Stop");
  94. fThread.Kill(); // Unsafe on WIN32... TODO : solve WIN32 thread Kill issue
  95. }
  96. void JackWinNamedPipeClientChannel::ServerSyncCall(JackRequest* req, JackResult* res, int* result)
  97. {
  98. if (req->Write(&fRequestPipe) < 0) {
  99. jack_error("Could not write request type = %ld", req->fType);
  100. *result = -1;
  101. return ;
  102. }
  103. if (res->Read(&fRequestPipe) < 0) {
  104. jack_error("Could not read result type = %ld", req->fType);
  105. *result = -1;
  106. return ;
  107. }
  108. *result = res->fResult;
  109. }
  110. void JackWinNamedPipeClientChannel::ServerAsyncCall(JackRequest* req, JackResult* res, int* result)
  111. {
  112. if (req->Write(&fRequestPipe) < 0) {
  113. jack_error("Could not write request type = %ld", req->fType);
  114. *result = -1;
  115. } else {
  116. *result = 0;
  117. }
  118. }
  119. void JackWinNamedPipeClientChannel::ClientCheck(const char* name, int uuid, char* name_res, int protocol, int options, int* status, int* result)
  120. {
  121. JackClientCheckRequest req(name, protocol, options, uuid);
  122. JackClientCheckResult res;
  123. ServerSyncCall(&req, &res, result);
  124. *status = res.fStatus;
  125. strcpy(name_res, res.fName);
  126. }
  127. void JackWinNamedPipeClientChannel::ClientOpen(const char* name, int pid, int uuid, int* shared_engine, int* shared_client, int* shared_graph, int* result)
  128. {
  129. JackClientOpenRequest req(name, pid, uuid);
  130. JackClientOpenResult res;
  131. ServerSyncCall(&req, &res, result);
  132. *shared_engine = res.fSharedEngine;
  133. *shared_client = res.fSharedClient;
  134. *shared_graph = res.fSharedGraph;
  135. }
  136. void JackWinNamedPipeClientChannel::ClientClose(int refnum, int* result)
  137. {
  138. JackClientCloseRequest req(refnum);
  139. JackResult res;
  140. ServerSyncCall(&req, &res, result);
  141. }
  142. void JackWinNamedPipeClientChannel::ClientActivate(int refnum, int is_real_time, int* result)
  143. {
  144. JackActivateRequest req(refnum, is_real_time);
  145. JackResult res;
  146. ServerSyncCall(&req, &res, result);
  147. }
  148. void JackWinNamedPipeClientChannel::ClientDeactivate(int refnum, int* result)
  149. {
  150. JackDeactivateRequest req(refnum);
  151. JackResult res;
  152. ServerSyncCall(&req, &res, result);
  153. }
  154. 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)
  155. {
  156. JackPortRegisterRequest req(refnum, name, type, flags, buffer_size);
  157. JackPortRegisterResult res;
  158. ServerSyncCall(&req, &res, result);
  159. *port_index = res.fPortIndex;
  160. }
  161. void JackWinNamedPipeClientChannel::PortUnRegister(int refnum, jack_port_id_t port_index, int* result)
  162. {
  163. JackPortUnRegisterRequest req(refnum, port_index);
  164. JackResult res;
  165. ServerSyncCall(&req, &res, result);
  166. }
  167. void JackWinNamedPipeClientChannel::PortConnect(int refnum, const char* src, const char* dst, int* result)
  168. {
  169. JackPortConnectNameRequest req(refnum, src, dst);
  170. JackResult res;
  171. ServerSyncCall(&req, &res, result);
  172. }
  173. void JackWinNamedPipeClientChannel::PortDisconnect(int refnum, const char* src, const char* dst, int* result)
  174. {
  175. JackPortDisconnectNameRequest req(refnum, src, dst);
  176. JackResult res;
  177. ServerSyncCall(&req, &res, result);
  178. }
  179. void JackWinNamedPipeClientChannel::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
  180. {
  181. JackPortConnectRequest req(refnum, src, dst);
  182. JackResult res;
  183. ServerSyncCall(&req, &res, result);
  184. }
  185. void JackWinNamedPipeClientChannel::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
  186. {
  187. JackPortDisconnectRequest req(refnum, src, dst);
  188. JackResult res;
  189. ServerSyncCall(&req, &res, result);
  190. }
  191. void JackWinNamedPipeClientChannel::PortRename(int refnum, jack_port_id_t port, const char* name, int* result)
  192. {
  193. JackPortRenameRequest req(refnum, port, name);
  194. JackResult res;
  195. ServerSyncCall(&req, &res, result);
  196. }
  197. void JackWinNamedPipeClientChannel::SetBufferSize(jack_nframes_t buffer_size, int* result)
  198. {
  199. JackSetBufferSizeRequest req(buffer_size);
  200. JackResult res;
  201. ServerSyncCall(&req, &res, result);
  202. }
  203. void JackWinNamedPipeClientChannel::SetFreewheel(int onoff, int* result)
  204. {
  205. JackSetFreeWheelRequest req(onoff);
  206. JackResult res;
  207. ServerSyncCall(&req, &res, result);
  208. }
  209. void JackWinNamedPipeClientChannel::SessionNotify(int refnum, const char* target, jack_session_event_type_t type, const char* path, jack_session_command_t** result)
  210. {
  211. JackSessionNotifyRequest req(refnum, target, type, path);
  212. JackResult res;
  213. int intresult;
  214. ServerSyncCall(&req, &res, &intresult);
  215. *result = NULL;
  216. }
  217. void JackWinNamedPipeClientChannel::ReleaseTimebase(int refnum, int* result)
  218. {
  219. JackReleaseTimebaseRequest req(refnum);
  220. JackResult res;
  221. ServerSyncCall(&req, &res, result);
  222. }
  223. void JackWinNamedPipeClientChannel::SetTimebaseCallback(int refnum, int conditional, int* result)
  224. {
  225. JackSetTimebaseCallbackRequest req(refnum, conditional);
  226. JackResult res;
  227. ServerSyncCall(&req, &res, result);
  228. }
  229. void JackWinNamedPipeClientChannel::GetInternalClientName(int refnum, int int_ref, char* name_res, int* result)
  230. {
  231. JackGetInternalClientNameRequest req(refnum, int_ref);
  232. JackGetInternalClientNameResult res;
  233. ServerSyncCall(&req, &res, result);
  234. strcpy(name_res, res.fName);
  235. }
  236. void JackWinNamedPipeClientChannel::InternalClientHandle(int refnum, const char* client_name, int* status, int* int_ref, int* result)
  237. {
  238. JackInternalClientHandleRequest req(refnum, client_name);
  239. JackInternalClientHandleResult res;
  240. ServerSyncCall(&req, &res, result);
  241. *int_ref = res.fIntRefNum;
  242. *status = res.fStatus;
  243. }
  244. 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)
  245. {
  246. JackInternalClientLoadRequest req(refnum, client_name, so_name, objet_data, options, uuid);
  247. JackInternalClientLoadResult res;
  248. ServerSyncCall(&req, &res, result);
  249. *int_ref = res.fIntRefNum;
  250. *status = res.fStatus;
  251. }
  252. void JackWinNamedPipeClientChannel::InternalClientUnload(int refnum, int int_ref, int* status, int* result)
  253. {
  254. JackInternalClientUnloadRequest req(refnum, int_ref);
  255. JackInternalClientUnloadResult res;
  256. ServerSyncCall(&req, &res, result);
  257. *status = res.fStatus;
  258. }
  259. bool JackWinNamedPipeClientChannel::Init()
  260. {
  261. jack_log("JackWinNamedPipeClientChannel::Init");
  262. if (!fNotificationListenPipe.Accept()) {
  263. jack_error("JackWinNamedPipeClientChannel: cannot establish notification pipe");
  264. return false;
  265. } else {
  266. return fClient->Init();
  267. }
  268. }
  269. bool JackWinNamedPipeClientChannel::Execute()
  270. {
  271. JackClientNotification event;
  272. JackResult res;
  273. if (event.Read(&fNotificationListenPipe) < 0) {
  274. jack_error("JackWinNamedPipeClientChannel read fail");
  275. goto error;
  276. }
  277. res.fResult = fClient->ClientNotify(event.fRefNum, event.fName, event.fNotify, event.fSync, event.fMessage, event.fValue1, event.fValue2);
  278. if (event.fSync) {
  279. if (res.Write(&fNotificationListenPipe) < 0) {
  280. jack_error("JackWinNamedPipeClientChannel write fail");
  281. goto error;
  282. }
  283. }
  284. return true;
  285. error:
  286. // Close the pipes, server wont be able to create them otherwise.
  287. fNotificationListenPipe.Close();
  288. fRequestPipe.Close();
  289. fClient->ShutDown();
  290. return false;
  291. }
  292. } // end of namespace