JackWinEvent.cpp

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

Generated on Thu Feb 14 11:16:02 2008 for Jackdmp by  doxygen 1.5.1