Jack2 1.9.6
|
00001 /* 00002 Copyright (C) 2004-2008 Grame 00003 00004 This program is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU Lesser General Public License as published by 00006 the Free Software Foundation; either version 2.1 of the License, or 00007 (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU Lesser General Public License for more details. 00013 00014 You should have received a copy of the GNU Lesser General Public License 00015 along with this program; if not, write to the Free Software 00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 00018 */ 00019 00020 00021 #include "JackWinEvent.h" 00022 #include "JackTools.h" 00023 #include "JackError.h" 00024 #include <assert.h> 00025 00026 // http://www.codeproject.com/win32/Win32_Event_Handling.asp 00027 // http://www.codeproject.com/threads/Synchronization.asp 00028 00029 namespace Jack 00030 { 00031 00032 void JackWinEvent::BuildName(const char* name, const char* server_name, char* res) 00033 { 00034 sprintf(res, "jack_pipe.%s_%s", server_name, name); 00035 } 00036 00037 bool JackWinEvent::Signal() 00038 { 00039 BOOL res; 00040 assert(fEvent); 00041 00042 if (fFlush) 00043 return true; 00044 00045 if (!(res = SetEvent(fEvent))) { 00046 jack_error("JackWinEvent::Signal name = %s err = %ld", fName, GetLastError()); 00047 } 00048 00049 return res; 00050 } 00051 00052 bool JackWinEvent::SignalAll() 00053 { 00054 BOOL res; 00055 assert(fEvent); 00056 00057 if (fFlush) 00058 return true; 00059 00060 if (!(res = SetEvent(fEvent))) { 00061 jack_error("JackWinEvent::SignalAll name = %s err = %ld", fName, GetLastError()); 00062 } 00063 00064 return res; 00065 } 00066 00067 bool JackWinEvent::Wait() 00068 { 00069 DWORD res; 00070 00071 if ((res = WaitForSingleObject(fEvent, INFINITE)) == WAIT_TIMEOUT) { 00072 jack_error("JackWinEvent::TimedWait name = %s time_out", fName); 00073 } 00074 00075 return (res == WAIT_OBJECT_0); 00076 } 00077 00078 bool JackWinEvent::TimedWait(long usec) 00079 { 00080 DWORD res; 00081 00082 if ((res = WaitForSingleObject(fEvent, usec / 1000)) == WAIT_TIMEOUT) { 00083 jack_error("JackWinEvent::TimedWait name = %s time_out", fName); 00084 } 00085 00086 return (res == WAIT_OBJECT_0); 00087 } 00088 00089 // Client side : get the published semaphore from server 00090 bool JackWinEvent::ConnectInput(const char* name, const char* server_name) 00091 { 00092 BuildName(name, server_name, fName); 00093 jack_log("JackWinEvent::Connect %s", fName); 00094 00095 // Temporary... 00096 if (fEvent) { 00097 jack_log("Already connected name = %s", name); 00098 return true; 00099 } 00100 00101 if ((fEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, fName)) == NULL) { 00102 jack_error("Connect: can't check in named event name = %s err = %ld", fName, GetLastError()); 00103 return false; 00104 } else { 00105 return true; 00106 } 00107 } 00108 00109 bool JackWinEvent::Connect(const char* name, const char* server_name) 00110 { 00111 return ConnectInput(name, server_name); 00112 } 00113 00114 bool JackWinEvent::ConnectOutput(const char* name, const char* server_name) 00115 { 00116 return ConnectInput(name, server_name); 00117 } 00118 00119 bool JackWinEvent::Disconnect() 00120 { 00121 if (fEvent) { 00122 jack_log("JackWinEvent::Disconnect %s", fName); 00123 CloseHandle(fEvent); 00124 fEvent = NULL; 00125 return true; 00126 } else { 00127 return false; 00128 } 00129 } 00130 00131 bool JackWinEvent::Allocate(const char* name, const char* server_name, int value) 00132 { 00133 BuildName(name, server_name, fName); 00134 jack_log("JackWinEvent::Allocate name = %s val = %ld", fName, value); 00135 00136 /* create an auto reset event */ 00137 if ((fEvent = CreateEvent(NULL, FALSE, FALSE, fName)) == NULL) { 00138 jack_error("Allocate: can't check in named event name = %s err = %ld", fName, GetLastError()); 00139 return false; 00140 } else if (GetLastError() == ERROR_ALREADY_EXISTS) { 00141 jack_error("Allocate: named event already exist name = %s", fName); 00142 CloseHandle(fEvent); 00143 fEvent = NULL; 00144 return false; 00145 } else { 00146 return true; 00147 } 00148 } 00149 00150 void JackWinEvent::Destroy() 00151 { 00152 if (fEvent != NULL) { 00153 jack_log("JackWinEvent::Destroy %s", fName); 00154 CloseHandle(fEvent); 00155 fEvent = NULL; 00156 } else { 00157 jack_error("JackWinEvent::Destroy synchro == NULL"); 00158 } 00159 } 00160 00161 00162 } // end of namespace 00163