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.

312 lines
11KB

  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 General Public License as published by
  5. the Free Software Foundation; either version 2 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 General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include "JackMachClientChannel.h"
  16. #include "JackRPCEngine.h"
  17. #include "JackRPCClientServer.c"
  18. #include "JackError.h"
  19. #include "JackLibClient.h"
  20. #include "JackLibGlobals.h"
  21. #include "JackMachThread.h"
  22. #include "JackConstants.h"
  23. namespace Jack
  24. {
  25. JackMachClientChannel::JackMachClientChannel()
  26. {
  27. fThread = new JackMachThread(this);
  28. }
  29. JackMachClientChannel::~JackMachClientChannel()
  30. {
  31. delete fThread;
  32. }
  33. // Server <===> client
  34. int JackMachClientChannel::ServerCheck(const char* server_name)
  35. {
  36. JackLog("JackMachClientChannel::ServerCheck = %s\n", server_name);
  37. char jack_server_entry_name[512];
  38. snprintf(jack_server_entry_name, sizeof(jack_server_entry_name), "%s_%s", jack_server_entry, server_name);
  39. // Connect to server
  40. if (!fServerPort.ConnectPort(jack_server_entry_name)) {
  41. jack_error("Cannot connect to server Mach port");
  42. return -1;
  43. } else {
  44. return 0;
  45. }
  46. }
  47. int JackMachClientChannel::Open(const char* server_name, const char* name, char* name_res, JackClient* client, jack_options_t options, jack_status_t* status)
  48. {
  49. JackLog("JackMachClientChannel::Open name = %s\n", name);
  50. char jack_server_entry_name[512];
  51. snprintf(jack_server_entry_name, sizeof(jack_server_entry_name), "%s_%s", jack_server_entry, server_name);
  52. // Connect to server
  53. if (!fServerPort.ConnectPort(jack_server_entry_name)) {
  54. jack_error("Cannot connect to server Mach port");
  55. return -1;
  56. }
  57. // Check name in server
  58. int result = 0;
  59. ClientCheck(name, name_res, JACK_PROTOCOL_VERSION, (int)options, (int*)status, &result);
  60. if (result < 0) {
  61. int status1 = *status;
  62. if (status1 & JackVersionError)
  63. jack_error("JACK protocol mismatch %d", JACK_PROTOCOL_VERSION);
  64. else
  65. jack_error("Client name = %s conflits with another running client", name);
  66. return -1;
  67. }
  68. // Prepare local port using client name
  69. char buf[JACK_CLIENT_NAME_SIZE];
  70. snprintf(buf, sizeof(buf) - 1, "%s:%s", jack_client_entry, name_res);
  71. if (!fClientPort.AllocatePort(buf, 16)) {
  72. jack_error("Cannot allocate client Mach port");
  73. return -1;
  74. }
  75. JackLibGlobals::fGlobals->fClientTable[fClientPort.GetPort()] = client;
  76. return 0;
  77. }
  78. void JackMachClientChannel::Close()
  79. {
  80. JackLog("JackMachClientChannel::Close\n");
  81. JackLibGlobals::fGlobals->fClientTable.erase(fClientPort.GetPort());
  82. fServerPort.DisconnectPort();
  83. fClientPort.DestroyPort();
  84. // TO CHECK
  85. kern_return_t res;
  86. if ((res = mach_port_destroy(mach_task_self(), fPrivatePort)) != KERN_SUCCESS) {
  87. jack_error("JackMachClientChannel::Close err = %s", mach_error_string(res));
  88. }
  89. }
  90. int JackMachClientChannel::Start()
  91. {
  92. JackLog("JackMachClientChannel::Start\n");
  93. if (fThread->Start() != 0) {
  94. jack_error("Cannot start Jack client listener");
  95. return -1;
  96. } else {
  97. return 0;
  98. }
  99. }
  100. void JackMachClientChannel::Stop()
  101. {
  102. JackLog("JackMachClientChannel::Stop\n");
  103. fThread->Kill();
  104. }
  105. void JackMachClientChannel::ClientCheck(const char* name, char* name_res, int protocol, int options, int* status, int* result)
  106. {
  107. kern_return_t res = rpc_jack_client_check(fServerPort.GetPort(), (char*)name, name_res, protocol, options, status, result);
  108. if (res != KERN_SUCCESS) {
  109. *result = -1;
  110. jack_error("JackMachClientChannel::ClientCheck err = %s", mach_error_string(res));
  111. }
  112. }
  113. void JackMachClientChannel::ClientOpen(const char* name, int* shared_engine, int* shared_client, int* shared_graph, int* result)
  114. {
  115. kern_return_t res = rpc_jack_client_open(fServerPort.GetPort(), (char*)name, &fPrivatePort, shared_engine, shared_client, shared_graph, result);
  116. if (res != KERN_SUCCESS) {
  117. *result = -1;
  118. jack_error("JackMachClientChannel::ClientOpen err = %s", mach_error_string(res));
  119. }
  120. }
  121. void JackMachClientChannel::ClientClose(int refnum, int* result)
  122. {
  123. kern_return_t res = rpc_jack_client_close(fPrivatePort, refnum, result);
  124. if (res != KERN_SUCCESS) {
  125. *result = -1;
  126. jack_error("JackMachClientChannel::ClientClose err = %s", mach_error_string(res));
  127. }
  128. }
  129. void JackMachClientChannel::ClientActivate(int refnum, int* result)
  130. {
  131. kern_return_t res = rpc_jack_client_activate(fPrivatePort, refnum, result);
  132. if (res != KERN_SUCCESS) {
  133. *result = -1;
  134. jack_error("JackMachClientChannel::ClientActivate err = %s", mach_error_string(res));
  135. }
  136. }
  137. void JackMachClientChannel::ClientDeactivate(int refnum, int* result)
  138. {
  139. kern_return_t res = rpc_jack_client_deactivate(fPrivatePort, refnum, result);
  140. if (res != KERN_SUCCESS) {
  141. *result = -1;
  142. jack_error("JackMachClientChannel::ClientDeactivate err = %s", mach_error_string(res));
  143. }
  144. }
  145. void JackMachClientChannel::PortRegister(int refnum, const char* name, const char* type, unsigned int flags, unsigned int buffer_size, unsigned int* port_index, int* result)
  146. {
  147. kern_return_t res = rpc_jack_port_register(fPrivatePort, refnum, (char*)name, (char*)type, flags, buffer_size, port_index, result);
  148. if (res != KERN_SUCCESS) {
  149. *result = -1;
  150. jack_error("JackMachClientChannel::PortRegister err = %s", mach_error_string(res));
  151. }
  152. }
  153. void JackMachClientChannel::PortUnRegister(int refnum, jack_port_id_t port_index, int* result)
  154. {
  155. kern_return_t res = rpc_jack_port_unregister(fPrivatePort, refnum, port_index, result);
  156. if (res != KERN_SUCCESS) {
  157. *result = -1;
  158. jack_error("JackMachClientChannel::PortUnRegister err = %s", mach_error_string(res));
  159. }
  160. }
  161. void JackMachClientChannel::PortConnect(int refnum, const char* src, const char* dst, int* result)
  162. {
  163. kern_return_t res = rpc_jack_port_connect_name(fPrivatePort, refnum, (char*)src, (char*)dst, result);
  164. if (res != KERN_SUCCESS) {
  165. jack_error("JackMachClientChannel::PortConnect err = %s", mach_error_string(res));
  166. }
  167. }
  168. void JackMachClientChannel::PortDisconnect(int refnum, const char* src, const char* dst, int* result)
  169. {
  170. kern_return_t res = rpc_jack_port_disconnect_name(fPrivatePort, refnum, (char*)src, (char*)dst, result);
  171. if (res != KERN_SUCCESS) {
  172. *result = -1;
  173. jack_error("JackMachClientChannel::PortDisconnect err = %s", mach_error_string(res));
  174. }
  175. }
  176. void JackMachClientChannel::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
  177. {
  178. kern_return_t res = rpc_jack_port_connect(fPrivatePort, refnum, src, dst, result);
  179. if (res != KERN_SUCCESS) {
  180. *result = -1;
  181. jack_error("JackMachClientChannel::PortConnect err = %s", mach_error_string(res));
  182. }
  183. }
  184. void JackMachClientChannel::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
  185. {
  186. kern_return_t res = rpc_jack_port_disconnect(fPrivatePort, refnum, src, dst, result);
  187. if (res != KERN_SUCCESS) {
  188. *result = -1;
  189. jack_error("JackMachClientChannel::PortDisconnect err = %s", mach_error_string(res));
  190. }
  191. }
  192. void JackMachClientChannel::SetBufferSize(jack_nframes_t buffer_size, int* result)
  193. {
  194. kern_return_t res = rpc_jack_set_buffer_size(fPrivatePort, buffer_size, result);
  195. if (res != KERN_SUCCESS) {
  196. *result = -1;
  197. jack_error("JackMachClientChannel::SetBufferSize err = %s", mach_error_string(res));
  198. }
  199. }
  200. void JackMachClientChannel::SetFreewheel(int onoff, int* result)
  201. {
  202. kern_return_t res = rpc_jack_set_freewheel(fPrivatePort, onoff, result);
  203. if (res != KERN_SUCCESS) {
  204. *result = -1;
  205. jack_error("JackMachClientChannel::SetFreewheel err = %s", mach_error_string(res));
  206. }
  207. }
  208. void JackMachClientChannel::ReleaseTimebase(int refnum, int* result)
  209. {
  210. kern_return_t res = rpc_jack_release_timebase(fPrivatePort, refnum, result);
  211. if (res != KERN_SUCCESS) {
  212. *result = -1;
  213. jack_error("JackMachClientChannel::ReleaseTimebase err = %s", mach_error_string(res));
  214. }
  215. }
  216. void JackMachClientChannel::SetTimebaseCallback(int refnum, int conditional, int* result)
  217. {
  218. kern_return_t res = rpc_jack_set_timebase_callback(fPrivatePort, refnum, conditional, result);
  219. if (res != KERN_SUCCESS) {
  220. *result = -1;
  221. jack_error("JackMachClientChannel::SetTimebaseCallback err = %s", mach_error_string(res));
  222. }
  223. }
  224. void JackMachClientChannel::GetInternalClientName(int refnum, int int_ref, char* name_res, int* result)
  225. {
  226. kern_return_t res = rpc_jack_get_internal_clientname(fPrivatePort, refnum, int_ref, name_res, result);
  227. if (res != KERN_SUCCESS) {
  228. *result = -1;
  229. jack_error("JackMachClientChannel::GetInternalClientName err = %s", mach_error_string(res));
  230. }
  231. }
  232. void JackMachClientChannel::InternalClientHandle(int refnum, const char* client_name, int* status, int* int_ref, int* result)
  233. {
  234. kern_return_t res = rpc_jack_internal_clienthandle(fPrivatePort, refnum, (char*)client_name, status, int_ref, result);
  235. if (res != KERN_SUCCESS) {
  236. *result = -1;
  237. jack_error("JackMachClientChannel::InternalClientHandle err = %s", mach_error_string(res));
  238. }
  239. }
  240. void JackMachClientChannel::InternalClientLoad(int refnum, const char* client_name, const char* so_name, const char* objet_data, int options, int* status, int* int_ref, int* result)
  241. {
  242. kern_return_t res = rpc_jack_internal_clientload(fPrivatePort, refnum, (char*)client_name, (char*)so_name, (char*)objet_data, options, status, int_ref, result);
  243. if (res != KERN_SUCCESS) {
  244. *result = -1;
  245. jack_error("JackMachClientChannel::InternalClientLoad err = %s", mach_error_string(res));
  246. }
  247. }
  248. void JackMachClientChannel::InternalClientUnload(int refnum, int int_ref, int* status, int* result)
  249. {
  250. kern_return_t res = rpc_jack_internal_clientunload(fPrivatePort, refnum, int_ref, status, result);
  251. if (res != KERN_SUCCESS) {
  252. *result = -1;
  253. jack_error("JackMachClientChannel::InternalClientUnload err = %s", mach_error_string(res));
  254. }
  255. }
  256. bool JackMachClientChannel::Execute()
  257. {
  258. kern_return_t res;
  259. if ((res = mach_msg_server(JackRPCClient_server, 1024, fClientPort.GetPort(), 0)) != KERN_SUCCESS) {
  260. jack_error("JackMachClientChannel::Execute err = %s", mach_error_string(res));
  261. //fClient->ShutDown();
  262. return false;
  263. } else {
  264. return true;
  265. }
  266. }
  267. } // end of namespace