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.

166 lines
4.0KB

  1. /*
  2. Copyright (C) 2004-2005 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. #if defined(HAVE_CONFIG_H)
  16. #include "config.h"
  17. #endif
  18. #include "JackWinEvent.h"
  19. #include "JackError.h"
  20. #include <assert.h>
  21. // http://www.codeproject.com/win32/Win32_Event_Handling.asp
  22. // http://www.codeproject.com/threads/Synchronization.asp
  23. namespace Jack
  24. {
  25. void JackWinEvent::BuildName(const char* name, const char* server_name, char* res)
  26. {
  27. sprintf(res, "jack_pipe.%s_%s", server_name, name);
  28. }
  29. bool JackWinEvent::Signal()
  30. {
  31. BOOL res;
  32. assert(fEvent);
  33. if (fFlush)
  34. return true;
  35. if (!(res = SetEvent(fEvent))) {
  36. jack_error("JackWinEvent::Signal name = %s err = %ld", fName, GetLastError());
  37. }
  38. return res;
  39. }
  40. bool JackWinEvent::SignalAll()
  41. {
  42. BOOL res;
  43. assert(fEvent);
  44. if (fFlush)
  45. return true;
  46. if (!(res = SetEvent(fEvent))) {
  47. jack_error("JackWinEvent::SignalAll name = %s err = %ld", fName, GetLastError());
  48. }
  49. return res;
  50. }
  51. bool JackWinEvent::Wait()
  52. {
  53. DWORD res;
  54. if ((res = WaitForSingleObject(fEvent, INFINITE)) == WAIT_TIMEOUT) {
  55. jack_error("JackWinEvent::TimedWait name = %s time_out", fName);
  56. }
  57. return (res == WAIT_OBJECT_0);
  58. }
  59. bool JackWinEvent::TimedWait(long usec)
  60. {
  61. DWORD res;
  62. if ((res = WaitForSingleObject(fEvent, usec / 1000)) == WAIT_TIMEOUT) {
  63. jack_error("JackWinEvent::TimedWait name = %s time_out", fName);
  64. }
  65. return (res == WAIT_OBJECT_0);
  66. }
  67. // Client side : get the published semaphore from server
  68. bool JackWinEvent::ConnectInput(const char* name, const char* server_name)
  69. {
  70. BuildName(name, server_name, fName);
  71. jack_log("JackWinEvent::Connect %s", fName);
  72. // Temporary...
  73. if (fEvent) {
  74. jack_log("Already connected name = %s", name);
  75. return true;
  76. }
  77. if ((fEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, fName)) == NULL) {
  78. jack_error("Connect: can't check in named event name = %s err = %ld", fName, GetLastError());
  79. return false;
  80. } else {
  81. return true;
  82. }
  83. }
  84. bool JackWinEvent::Connect(const char* name, const char* server_name)
  85. {
  86. return ConnectInput(name, server_name);
  87. }
  88. bool JackWinEvent::ConnectOutput(const char* name, const char* server_name)
  89. {
  90. return ConnectInput(name, server_name);
  91. }
  92. bool JackWinEvent::Disconnect()
  93. {
  94. if (fEvent) {
  95. jack_log("JackWinEvent::Disconnect %s", fName);
  96. CloseHandle(fEvent);
  97. fEvent = NULL;
  98. return true;
  99. } else {
  100. return false;
  101. }
  102. }
  103. bool JackWinEvent::Allocate(const char* name, const char* server_name, int value)
  104. {
  105. BuildName(name, server_name, fName);
  106. jack_log("JackWinEvent::Allocate name = %s val = %ld", fName, value);
  107. /* create an auto reset event */
  108. if ((fEvent = CreateEvent(NULL, FALSE, FALSE, fName)) == NULL) {
  109. jack_error("Allocate: can't check in named event name = %s err = %ld", fName, GetLastError());
  110. return false;
  111. } else if (GetLastError() == ERROR_ALREADY_EXISTS) {
  112. jack_error("Allocate: named event already exist name = %s", fName);
  113. CloseHandle(fEvent);
  114. fEvent = NULL;
  115. return false;
  116. } else {
  117. return true;
  118. }
  119. }
  120. void JackWinEvent::Destroy()
  121. {
  122. if (fEvent != NULL) {
  123. jack_log("JackWinEvent::Destroy %s", fName);
  124. CloseHandle(fEvent);
  125. fEvent = NULL;
  126. } else {
  127. jack_error("JackWinEvent::Destroy synchro == NULL");
  128. }
  129. }
  130. } // end of namespace