1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
/*
Copyright (C) 2004-2008 Grame
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "JackWinEvent.h"
#include "JackTools.h"
#include "JackError.h"
#include <assert.h>
// http://www.codeproject.com/win32/Win32_Event_Handling.asp
// http://www.codeproject.com/threads/Synchronization.asp
namespace Jack
{
void JackWinEvent::BuildName(const char* name, const char* server_name, char* res, int size)
{
snprintf(res, size, "jack_pipe.%s_%s", server_name, name);
}
bool JackWinEvent::Signal()
{
BOOL res;
assert(fEvent);
if (fFlush)
return true;
if (!(res = SetEvent(fEvent))) {
jack_error("JackWinEvent::Signal name = %s err = %ld", fName, GetLastError());
}
return res;
}
bool JackWinEvent::SignalAll()
{
BOOL res;
assert(fEvent);
if (fFlush)
return true;
if (!(res = SetEvent(fEvent))) {
jack_error("JackWinEvent::SignalAll name = %s err = %ld", fName, GetLastError());
}
return res;
}
bool JackWinEvent::Wait()
{
DWORD res;
if ((res = WaitForSingleObject(fEvent, INFINITE)) == WAIT_TIMEOUT) {
jack_error("JackWinEvent::TimedWait name = %s time_out", fName);
}
return (res == WAIT_OBJECT_0);
}
bool JackWinEvent::TimedWait(long usec)
{
DWORD res;
if ((res = WaitForSingleObject(fEvent, usec / 1000)) == WAIT_TIMEOUT) {
jack_error("JackWinEvent::TimedWait name = %s time_out", fName);
}
return (res == WAIT_OBJECT_0);
}
// Client side : get the published semaphore from server
bool JackWinEvent::ConnectInput(const char* name, const char* server_name)
{
BuildName(name, server_name, fName, sizeof(fName));
jack_log("JackWinEvent::Connect %s", fName);
// Temporary...
if (fEvent) {
jack_log("Already connected name = %s", name);
return true;
}
if ((fEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, fName)) == NULL) {
jack_error("Connect: can't check in named event name = %s err = %ld", fName, GetLastError());
return false;
} else {
return true;
}
}
bool JackWinEvent::Connect(const char* name, const char* server_name)
{
return ConnectInput(name, server_name);
}
bool JackWinEvent::ConnectOutput(const char* name, const char* server_name)
{
return ConnectInput(name, server_name);
}
bool JackWinEvent::Disconnect()
{
if (fEvent) {
jack_log("JackWinEvent::Disconnect %s", fName);
CloseHandle(fEvent);
fEvent = NULL;
return true;
} else {
return false;
}
}
bool JackWinEvent::Allocate(const char* name, const char* server_name, int value)
{
BuildName(name, server_name, fName, sizeof(fName));
jack_log("JackWinEvent::Allocate name = %s val = %ld", fName, value);
/* create an auto reset event */
if ((fEvent = CreateEvent(NULL, FALSE, FALSE, fName)) == NULL) {
jack_error("Allocate: can't check in named event name = %s err = %ld", fName, GetLastError());
return false;
} else if (GetLastError() == ERROR_ALREADY_EXISTS) {
jack_error("Allocate: named event already exist name = %s", fName);
CloseHandle(fEvent);
fEvent = NULL;
return false;
} else {
return true;
}
}
void JackWinEvent::Destroy()
{
if (fEvent != NULL) {
jack_log("JackWinEvent::Destroy %s", fName);
CloseHandle(fEvent);
fEvent = NULL;
} else {
jack_error("JackWinEvent::Destroy synchro == NULL");
}
}
} // end of namespace
|