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.

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