Jack2: JackWinEvent.cpp Source File

Jack2 1.9.12

JackWinEvent.cpp
1 /*
2 Copyright (C) 2004-2008 Grame
3
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.
8
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.
13
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.
17
18 */
19
20
21 #include "JackWinEvent.h"
22 #include "JackTools.h"
23 #include "JackError.h"
24 #include <assert.h>
25
26 // http://www.codeproject.com/win32/Win32_Event_Handling.asp
27 // http://www.codeproject.com/threads/Synchronization.asp
28
29 namespace Jack
30 {
31
32 void JackWinEvent::BuildName(const char* name, const char* server_name, char* res, int size)
33 {
34 snprintf(res, size, "jack_pipe.%s_%s", server_name, name);
35 }
36
37 bool JackWinEvent::Signal()
38 {
39 BOOL res;
40 assert(fEvent);
41
42 if (fFlush)
43 return true;
44
45 if (!(res = SetEvent(fEvent))) {
46 jack_error("JackWinEvent::Signal name = %s err = %ld", fName, GetLastError());
47 }
48
49 return res;
50 }
51
52 bool JackWinEvent::SignalAll()
53 {
54 BOOL res;
55 assert(fEvent);
56
57 if (fFlush)
58 return true;
59
60 if (!(res = SetEvent(fEvent))) {
61 jack_error("JackWinEvent::SignalAll name = %s err = %ld", fName, GetLastError());
62 }
63
64 return res;
65 }
66
67 bool JackWinEvent::Wait()
68 {
69 DWORD res;
70
71 if ((res = WaitForSingleObject(fEvent, INFINITE)) == WAIT_TIMEOUT) {
72 jack_error("JackWinEvent::TimedWait name = %s time_out", fName);
73 }
74
75 return (res == WAIT_OBJECT_0);
76 }
77
78 bool JackWinEvent::TimedWait(long usec)
79 {
80 DWORD res;
81
82 if ((res = WaitForSingleObject(fEvent, usec / 1000)) == WAIT_TIMEOUT) {
83 jack_error("JackWinEvent::TimedWait name = %s time_out", fName);
84 }
85
86 return (res == WAIT_OBJECT_0);
87 }
88
89 // Client side : get the published semaphore from server
90 bool JackWinEvent::ConnectInput(const char* name, const char* server_name)
91 {
92 BuildName(name, server_name, fName, sizeof(fName));
93 jack_log("JackWinEvent::Connect %s", fName);
94
95 // Temporary...
96 if (fEvent) {
97 jack_log("Already connected name = %s", name);
98 return true;
99 }
100
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());
103 return false;
104 } else {
105 return true;
106 }
107 }
108
109 bool JackWinEvent::Connect(const char* name, const char* server_name)
110 {
111 return ConnectInput(name, server_name);
112 }
113
114 bool JackWinEvent::ConnectOutput(const char* name, const char* server_name)
115 {
116 return ConnectInput(name, server_name);
117 }
118
119 bool JackWinEvent::Disconnect()
120 {
121 if (fEvent) {
122 jack_log("JackWinEvent::Disconnect %s", fName);
123 CloseHandle(fEvent);
124 fEvent = NULL;
125 return true;
126 } else {
127 return false;
128 }
129 }
130
131 bool JackWinEvent::Allocate(const char* name, const char* server_name, int value)
132 {
133 BuildName(name, server_name, fName, sizeof(fName));
134 jack_log("JackWinEvent::Allocate name = %s val = %ld", fName, value);
135
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());
139 return false;
140 } else if (GetLastError() == ERROR_ALREADY_EXISTS) {
141 jack_error("Allocate: named event already exist name = %s", fName);
142 CloseHandle(fEvent);
143 fEvent = NULL;
144 return false;
145 } else {
146 return true;
147 }
148 }
149
150 void JackWinEvent::Destroy()
151 {
152 if (fEvent != NULL) {
153 jack_log("JackWinEvent::Destroy %s", fName);
154 CloseHandle(fEvent);
155 fEvent = NULL;
156 } else {
157 jack_error("JackWinEvent::Destroy synchro == NULL");
158 }
159 }
160
161
162 } // end of namespace
163
jack_log
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108
jack_error
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92