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.

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