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.

269 lines
7.3KB

  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 "JackWinNamedPipeServerChannel.h"
  16. #include "JackNotification.h"
  17. #include "JackRequest.h"
  18. #include "JackServer.h"
  19. #include "JackLockedEngine.h"
  20. #include "JackGlobals.h"
  21. #include "JackClient.h"
  22. #include "JackNotification.h"
  23. #include "JackException.h"
  24. #include <assert.h>
  25. using namespace std;
  26. namespace Jack
  27. {
  28. HANDLE JackClientPipeThread::fMutex = NULL; // Never released....
  29. // fRefNum = -1 correspond to already removed client
  30. JackClientPipeThread::JackClientPipeThread(JackWinNamedPipeClient* pipe)
  31. :fPipe(pipe), fDecoder(NULL), fServer(NULL), fThread(this), fRefNum(0)
  32. {
  33. // First one allocated the static fMutex
  34. if (fMutex == NULL) {
  35. fMutex = CreateMutex(NULL, FALSE, NULL);
  36. }
  37. }
  38. JackClientPipeThread::~JackClientPipeThread()
  39. {
  40. jack_log("JackClientPipeThread::~JackClientPipeThread");
  41. delete fPipe;
  42. }
  43. int JackClientPipeThread::Open(JackServer* server) // Open the Server/Client connection
  44. {
  45. // Start listening
  46. if (fThread.Start() != 0) {
  47. jack_error("Cannot start Jack server listener\n");
  48. return -1;
  49. } else {
  50. fDecoder = new JackRequestDecoder(server, this);
  51. fServer = server;
  52. return 0;
  53. }
  54. }
  55. void JackClientPipeThread::Close() // Close the Server/Client connection
  56. {
  57. jack_log("JackClientPipeThread::Close 0 %x %ld", this, fRefNum);
  58. //fThread.Kill();
  59. fPipe->Close();
  60. fRefNum = -1;
  61. delete fDecoder;
  62. fDecoder = NULL;
  63. }
  64. bool JackClientPipeThread::Execute()
  65. {
  66. try {
  67. jack_log("JackClientPipeThread::Execute %x", this);
  68. JackRequest header;
  69. int res = header.Read(fPipe);
  70. bool ret = true;
  71. // Lock the global mutex
  72. if (WaitForSingleObject(fMutex, INFINITE) == WAIT_FAILED) {
  73. jack_error("JackClientPipeThread::Execute : mutex wait error");
  74. }
  75. // Decode header
  76. if (res < 0) {
  77. jack_log("JackClientPipeThread::Execute : cannot decode header");
  78. ClientKill();
  79. ret = false;
  80. // Decode request
  81. } else if (fDecoder->HandleRequest(fPipe, header.fType) < 0) {
  82. ret = false;
  83. }
  84. // Unlock the global mutex
  85. if (!ReleaseMutex(fMutex)) {
  86. jack_error("JackClientPipeThread::Execute : mutex release error");
  87. }
  88. return ret;
  89. } catch (JackQuitException& e) {
  90. jack_log("JackClientPipeThread::Execute : JackQuitException");
  91. return false;
  92. }
  93. }
  94. void JackClientPipeThread::ClientAdd(detail::JackChannelTransactionInterface* socket, JackClientOpenRequest* req, JackClientOpenResult *res)
  95. {
  96. jack_log("JackClientPipeThread::ClientAdd %x %s", this, req->fName);
  97. fRefNum = -1;
  98. res->fResult = fServer->GetEngine()->ClientExternalOpen(req->fName, req->fPID, req->fUUID, &fRefNum, &res->fSharedEngine, &res->fSharedClient, &res->fSharedGraph);
  99. }
  100. void JackClientPipeThread::ClientRemove(detail::JackChannelTransactionInterface* socket_aux, int refnum)
  101. {
  102. jack_log("JackClientPipeThread::ClientRemove ref = %d", refnum);
  103. Close();
  104. }
  105. void JackClientPipeThread::ClientKill()
  106. {
  107. jack_log("JackClientPipeThread::ClientKill ref = %d", fRefNum);
  108. if (fRefNum == -1) { // Correspond to an already removed client.
  109. jack_log("Kill a closed client %x", this);
  110. } else if (fRefNum == 0) { // Correspond to a still not opened client.
  111. jack_log("Kill a not opened client %x", this);
  112. } else {
  113. fServer->GetEngine()->ClientKill(fRefNum);
  114. }
  115. Close();
  116. }
  117. JackWinNamedPipeServerChannel::JackWinNamedPipeServerChannel():fThread(this)
  118. {}
  119. JackWinNamedPipeServerChannel::~JackWinNamedPipeServerChannel()
  120. {
  121. std::list<JackClientPipeThread*>::iterator it;
  122. for (it = fClientList.begin(); it != fClientList.end(); it++) {
  123. JackClientPipeThread* client = *it;
  124. client->Close();
  125. delete client;
  126. }
  127. }
  128. int JackWinNamedPipeServerChannel::Open(const char* server_name, JackServer* server)
  129. {
  130. jack_log("JackWinNamedPipeServerChannel::Open");
  131. snprintf(fServerName, sizeof(fServerName), server_name);
  132. // Needed for internal connection from JackWinNamedPipeServerNotifyChannel object
  133. if (ClientListen()) {
  134. fServer = server;
  135. return 0;
  136. } else {
  137. jack_error("JackWinNamedPipeServerChannel::Open : cannot create result listen pipe");
  138. return -1;
  139. }
  140. }
  141. void JackWinNamedPipeServerChannel::Close()
  142. {
  143. /* TODO : solve WIN32 thread Kill issue
  144. This would hang the server... since we are quitting it, its not really problematic,
  145. all ressources will be deallocated at the end.
  146. fRequestListenPipe.Close();
  147. fThread.Stop();
  148. */
  149. fRequestListenPipe.Close();
  150. }
  151. int JackWinNamedPipeServerChannel::Start()
  152. {
  153. if (fThread.Start() != 0) {
  154. jack_error("Cannot start Jack server listener");
  155. return -1;
  156. } else {
  157. return 0;
  158. }
  159. }
  160. void JackWinNamedPipeServerChannel::Stop()
  161. {
  162. fThread.Kill();
  163. }
  164. bool JackWinNamedPipeServerChannel::Init()
  165. {
  166. jack_log("JackWinNamedPipeServerChannel::Init");
  167. // Accept first client, that is the JackWinNamedPipeServerNotifyChannel object
  168. return ClientAccept();
  169. }
  170. bool JackWinNamedPipeServerChannel::ClientListen()
  171. {
  172. if (fRequestListenPipe.Bind(jack_server_dir, fServerName, 0) < 0) {
  173. jack_error("JackWinNamedPipeServerChannel::ClientListen : cannot create result listen pipe");
  174. return false;
  175. } else {
  176. return true;
  177. }
  178. }
  179. bool JackWinNamedPipeServerChannel::ClientAccept()
  180. {
  181. JackWinNamedPipeClient* pipe;
  182. if ((pipe = fRequestListenPipe.AcceptClient()) == NULL) {
  183. jack_error("JackWinNamedPipeServerChannel::ClientAccept : cannot connect pipe");
  184. return false;
  185. } else {
  186. ClientAdd(pipe);
  187. return true;
  188. }
  189. }
  190. bool JackWinNamedPipeServerChannel::Execute()
  191. {
  192. if (!ClientListen()) {
  193. return false;
  194. }
  195. return ClientAccept();
  196. }
  197. void JackWinNamedPipeServerChannel::ClientAdd(JackWinNamedPipeClient* pipe)
  198. {
  199. // Remove dead (= not running anymore) clients.
  200. std::list<JackClientPipeThread*>::iterator it = fClientList.begin();
  201. JackClientPipeThread* client;
  202. jack_log("JackWinNamedPipeServerChannel::ClientAdd size %ld", fClientList.size());
  203. while (it != fClientList.end()) {
  204. client = *it;
  205. if (client->IsRunning()) {
  206. it++;
  207. } else {
  208. it = fClientList.erase(it);
  209. delete client;
  210. }
  211. }
  212. client = new JackClientPipeThread(pipe);
  213. client->Open(fServer);
  214. // Here we are sure that the client is running (because it's thread is in "running" state).
  215. fClientList.push_back(client);
  216. }
  217. } // end of namespace