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.

155 lines
4.2KB

  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()
  23. :JackGenericClientChannel(),fThread(this)
  24. {
  25. fRequest = new JackWinNamedPipeClient();
  26. }
  27. JackWinNamedPipeClientChannel::~JackWinNamedPipeClientChannel()
  28. {
  29. delete fRequest;
  30. }
  31. 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)
  32. {
  33. int result = 0;
  34. jack_log("JackWinNamedPipeClientChannel::Open name = %s", name);
  35. /*
  36. 16/08/07: was called before doing "fRequest->Connect" .... still necessary?
  37. if (fNotificationListenPipe.Bind(jack_client_dir, name, 0) < 0) {
  38. jack_error("Cannot bind pipe");
  39. goto error;
  40. }
  41. */
  42. if (fRequest->Connect(jack_server_dir, server_name, 0) < 0) {
  43. jack_error("Cannot connect to server pipe");
  44. goto error;
  45. }
  46. // Check name in server
  47. ClientCheck(name, uuid, name_res, JACK_PROTOCOL_VERSION, (int)options, (int*)status, &result, true);
  48. if (result < 0) {
  49. int status1 = *status;
  50. if (status1 & JackVersionError) {
  51. jack_error("JACK protocol mismatch %d", JACK_PROTOCOL_VERSION);
  52. } else {
  53. jack_error("Client name = %s conflits with another running client", name);
  54. }
  55. }
  56. if (fNotificationListenPipe.Bind(jack_client_dir, name_res, 0) < 0) {
  57. jack_error("Cannot bind pipe");
  58. goto error;
  59. }
  60. fClient = obj;
  61. return 0;
  62. error:
  63. fRequest->Close();
  64. fNotificationListenPipe.Close();
  65. return -1;
  66. }
  67. void JackWinNamedPipeClientChannel::Close()
  68. {
  69. fRequest->Close();
  70. fNotificationListenPipe.Close();
  71. // Here the thread will correctly stop when the pipe are closed
  72. fThread.Stop();
  73. }
  74. int JackWinNamedPipeClientChannel::Start()
  75. {
  76. jack_log("JackWinNamedPipeClientChannel::Start");
  77. /*
  78. To be sure notification thread is started before ClientOpen is called.
  79. */
  80. if (fThread.StartSync() != 0) {
  81. jack_error("Cannot start Jack client listener");
  82. return -1;
  83. } else {
  84. return 0;
  85. }
  86. }
  87. void JackWinNamedPipeClientChannel::Stop()
  88. {
  89. jack_log("JackWinNamedPipeClientChannel::Stop");
  90. fThread.Kill(); // Unsafe on WIN32... TODO : solve WIN32 thread Kill issue
  91. }
  92. bool JackWinNamedPipeClientChannel::Init()
  93. {
  94. jack_log("JackWinNamedPipeClientChannel::Init");
  95. if (!fNotificationListenPipe.Accept()) {
  96. jack_error("JackWinNamedPipeClientChannel: cannot establish notification pipe");
  97. return false;
  98. } else {
  99. return true;
  100. }
  101. }
  102. bool JackWinNamedPipeClientChannel::Execute()
  103. {
  104. JackClientNotification event;
  105. JackResult res;
  106. if (event.Read(&fNotificationListenPipe) < 0) {
  107. jack_error("JackWinNamedPipeClientChannel read fail");
  108. goto error;
  109. }
  110. res.fResult = fClient->ClientNotify(event.fRefNum, event.fName, event.fNotify, event.fSync, event.fMessage, event.fValue1, event.fValue2);
  111. if (event.fSync) {
  112. if (res.Write(&fNotificationListenPipe) < 0) {
  113. jack_error("JackWinNamedPipeClientChannel write fail");
  114. goto error;
  115. }
  116. }
  117. return true;
  118. error:
  119. // Close the pipes, server wont be able to create them otherwise.
  120. fNotificationListenPipe.Close();
  121. fRequest->Close();
  122. fClient->ShutDown();
  123. return false;
  124. }
  125. } // end of namespace