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.

169 lines
4.4KB

  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 "JackSocketClientChannel.h"
  16. #include "JackRequest.h"
  17. #include "JackClient.h"
  18. #include "JackGlobals.h"
  19. #include "JackError.h"
  20. namespace Jack
  21. {
  22. JackSocketClientChannel::JackSocketClientChannel()
  23. :JackGenericClientChannel(), fThread(this)
  24. {
  25. fRequest = new JackClientSocket();
  26. fNotificationSocket = NULL;
  27. }
  28. JackSocketClientChannel::~JackSocketClientChannel()
  29. {
  30. delete fRequest;
  31. delete fNotificationSocket;
  32. }
  33. int JackSocketClientChannel::Open(const char* server_name, const char* name, jack_uuid_t uuid, char* name_res, JackClient* client, jack_options_t options, jack_status_t* status)
  34. {
  35. int result = 0;
  36. jack_log("JackSocketClientChannel::Open name = %s", name);
  37. // Before any server/client call
  38. fClient = client;
  39. if (fRequest->Connect(jack_server_dir, server_name, 0) < 0) {
  40. jack_error("Cannot connect to server socket");
  41. goto error;
  42. }
  43. // OK so server is there...
  44. JackGlobals::fServerRunning = true;
  45. // Check name in server
  46. ClientCheck(name, uuid, name_res, JACK_PROTOCOL_VERSION, (int)options, (int*)status, &result, true);
  47. if (result < 0) {
  48. int status1 = *status;
  49. if (status1 & JackVersionError) {
  50. jack_error("JACK protocol mismatch %d", JACK_PROTOCOL_VERSION);
  51. } else {
  52. jack_error("Client name = %s conflits with another running client", name);
  53. }
  54. goto error;
  55. }
  56. if (fNotificationListenSocket.Bind(jack_client_dir, name_res, 0) < 0) {
  57. jack_error("Cannot bind socket");
  58. goto error;
  59. }
  60. return 0;
  61. error:
  62. fRequest->Close();
  63. fNotificationListenSocket.Close();
  64. return -1;
  65. }
  66. void JackSocketClientChannel::Close()
  67. {
  68. fRequest->Close();
  69. fNotificationListenSocket.Close();
  70. if (fNotificationSocket) {
  71. fNotificationSocket->Close();
  72. }
  73. }
  74. int JackSocketClientChannel::Start()
  75. {
  76. jack_log("JackSocketClientChannel::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 JackSocketClientChannel::Stop()
  88. {
  89. jack_log("JackSocketClientChannel::Stop");
  90. fThread.Kill();
  91. }
  92. bool JackSocketClientChannel::Init()
  93. {
  94. jack_log("JackSocketClientChannel::Init");
  95. fNotificationSocket = fNotificationListenSocket.Accept();
  96. // No more needed
  97. fNotificationListenSocket.Close();
  98. // Setup context
  99. if (!jack_tls_set(JackGlobals::fNotificationThread, this)) {
  100. jack_error("Failed to set thread notification key");
  101. }
  102. if (!fNotificationSocket) {
  103. jack_error("JackSocketClientChannel: cannot establish notification socket");
  104. return false;
  105. } else {
  106. return true;
  107. }
  108. }
  109. bool JackSocketClientChannel::Execute()
  110. {
  111. JackClientNotification event;
  112. JackResult res;
  113. int err = event.Read(fNotificationSocket);
  114. if (err < 0) {
  115. // aborted reading due to shutdown
  116. if (err != JACK_REQUEST_ERR_ABORTED) {
  117. jack_error("JackSocketClientChannel read fail");
  118. }
  119. goto error;
  120. }
  121. res.fResult = fClient->ClientNotify(event.fRefNum, event.fName, event.fNotify, event.fSync, event.fMessage, event.fValue1, event.fValue2);
  122. if (event.fSync) {
  123. if (res.Write(fNotificationSocket) < 0) {
  124. jack_error("JackSocketClientChannel write fail");
  125. goto error;
  126. }
  127. }
  128. return true;
  129. error:
  130. fNotificationSocket->Close();
  131. fClient->ShutDown(jack_status_t(JackFailure | JackServerError), JACK_SERVER_FAILURE);
  132. return false;
  133. }
  134. } // end of namespace