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.

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