2 Copyright (C) 2004-2008 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "JackWinEvent.h"
22 #include "JackTools.h"
23 #include "JackError.h"
26 // http://www.codeproject.com/win32/Win32_Event_Handling.asp
27 // http://www.codeproject.com/threads/Synchronization.asp
32 void JackWinEvent::BuildName(const char* name
, const char* server_name
, char* res
, int size
)
34 snprintf(res
, size
, "jack_pipe.%s_%s", server_name
, name
);
37 bool JackWinEvent::Signal()
45 if (!(res
= SetEvent(fEvent
))) {
46 jack_error("JackWinEvent::Signal name = %s err = %ld", fName
, GetLastError());
52 bool JackWinEvent::SignalAll()
60 if (!(res
= SetEvent(fEvent
))) {
61 jack_error("JackWinEvent::SignalAll name = %s err = %ld", fName
, GetLastError());
67 bool JackWinEvent::Wait()
71 if ((res
= WaitForSingleObject(fEvent
, INFINITE
)) == WAIT_TIMEOUT
) {
72 jack_error("JackWinEvent::TimedWait name = %s time_out", fName
);
75 return (res
== WAIT_OBJECT_0
);
78 bool JackWinEvent::TimedWait(long usec
)
82 if ((res
= WaitForSingleObject(fEvent
, usec
/ 1000)) == WAIT_TIMEOUT
) {
83 jack_error("JackWinEvent::TimedWait name = %s time_out", fName
);
86 return (res
== WAIT_OBJECT_0
);
89 // Client side : get the published semaphore from server
90 bool JackWinEvent::ConnectInput(const char* name
, const char* server_name
)
92 BuildName(name
, server_name
, fName
, sizeof(fName
));
93 jack_log("JackWinEvent::Connect %s", fName
);
97 jack_log("Already connected name = %s", name
);
101 if ((fEvent
= OpenEvent(EVENT_ALL_ACCESS
, FALSE
, fName
)) == NULL
) {
102 jack_error("Connect: can't check in named event name = %s err = %ld", fName
, GetLastError());
109 bool JackWinEvent::Connect(const char* name
, const char* server_name
)
111 return ConnectInput(name
, server_name
);
114 bool JackWinEvent::ConnectOutput(const char* name
, const char* server_name
)
116 return ConnectInput(name
, server_name
);
119 bool JackWinEvent::Disconnect()
122 jack_log("JackWinEvent::Disconnect %s", fName
);
131 bool JackWinEvent::Allocate(const char* name
, const char* server_name
, int value
)
133 BuildName(name
, server_name
, fName
, sizeof(fName
));
134 jack_log("JackWinEvent::Allocate name = %s val = %ld", fName
, value
);
136 /* create an auto reset event */
137 if ((fEvent
= CreateEvent(NULL
, FALSE
, FALSE
, fName
)) == NULL
) {
138 jack_error("Allocate: can't check in named event name = %s err = %ld", fName
, GetLastError());
140 } else if (GetLastError() == ERROR_ALREADY_EXISTS
) {
141 jack_error("Allocate: named event already exist name = %s", fName
);
150 void JackWinEvent::Destroy()
152 if (fEvent
!= NULL
) {
153 jack_log("JackWinEvent::Destroy %s", fName
);
157 jack_error("JackWinEvent::Destroy synchro == NULL");
162 } // end of namespace