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.

333 lines
9.7KB

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