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 "JackWinNamedPipe.h"
22 #include "JackError.h"
31 int JackWinNamedPipeAux::ReadAux(void* data
, int len
)
34 BOOL res
= ReadFile(fNamedPipe
, data
, len
, &read
, NULL
);
35 if (res
&& read
== (DWORD
)len
) {
38 jack_log("Cannot read named pipe name = %s err = %ld", fName
, GetLastError());
43 int JackWinNamedPipeAux::WriteAux(void* data
, int len
)
46 BOOL res
= WriteFile(fNamedPipe
, data
, len
, &written
, NULL
);
47 if (res
&& written
== (DWORD
)len
) {
50 jack_log("Cannot write named pipe name = %s err = %ld", fName
, GetLastError());
57 http://answers.google.com/answers/threadview?id=430173
58 http://msdn.microsoft.com/en-us/library/windows/desktop/aa365800(v=vs.85).aspx
62 int JackWinNamedPipeClient::ConnectAux()
64 fNamedPipe = CreateFile(fName, // pipe name
65 GENERIC_READ | // read and write access
68 NULL, // default security attributes
69 OPEN_EXISTING, // opens existing pipe
70 0, // default attributes
71 NULL); // no template file
73 if (fNamedPipe == INVALID_HANDLE_VALUE) {
74 jack_error("Cannot connect to named pipe = %s err = %ld", fName, GetLastError());
82 int JackWinNamedPipeClient::ConnectAux()
84 jack_log("JackWinNamedPipeClient::ConnectAux : fName %s", fName
);
88 fNamedPipe
= CreateFile(fName
, // pipe name
89 GENERIC_READ
| // read and write access
92 NULL
, // default security attributes
93 OPEN_EXISTING
, // opens existing pipe
94 0, // default attributes
95 NULL
); // no template file
97 // Break if the pipe handle is valid.
98 if (fNamedPipe
!= INVALID_HANDLE_VALUE
) {
102 // Exit if an error other than ERROR_PIPE_BUSY or ERROR_FILE_NOT_FOUND occurs.
103 if ((GetLastError() != ERROR_PIPE_BUSY
) && (GetLastError() != ERROR_FILE_NOT_FOUND
)) {
104 jack_error("Cannot connect to named pipe = %s err = %ld", fName
, GetLastError());
108 // All pipe instances are busy, so wait for 2 seconds.
109 if (!WaitNamedPipe(fName
, 2000)) {
110 jack_error("Cannot connect to named pipe after wait = %s err = %ld", fName
, GetLastError());
116 int JackWinNamedPipeClient::Connect(const char* dir
, int which
)
118 snprintf(fName
, sizeof(fName
), "\\\\.\\pipe\\%s_jack_%d", dir
, which
);
122 int JackWinNamedPipeClient::Connect(const char* dir
, const char* name
, int which
)
124 snprintf(fName
, sizeof(fName
), "\\\\.\\pipe\\%s_jack_%s_%d", dir
, name
, which
);
128 int JackWinNamedPipeClient::Close()
130 if (fNamedPipe
!= INVALID_HANDLE_VALUE
) {
131 CloseHandle(fNamedPipe
);
132 fNamedPipe
= INVALID_HANDLE_VALUE
;
139 void JackWinNamedPipeClient::SetReadTimeOut(long sec
)
142 COMMTIMEOUTS timeout;
143 if (GetCommTimeouts(fNamedPipe, &timeout)) {
144 jack_info("JackWinNamedPipeClient::SetReadTimeOut ReadIntervalTimeout = %d", timeout.ReadIntervalTimeout);
145 jack_info("JackWinNamedPipeClient::SetReadTimeOut ReadTotalTimeoutMultiplier = %d", timeout.ReadTotalTimeoutMultiplier);
146 jack_info("JackWinNamedPipeClient::SetReadTimeOut ReadTotalTimeoutConstant = %d", timeout.ReadTotalTimeoutConstant);
148 jack_error("JackWinNamedPipeClient::SetReadTimeOut err %d", GetLastError());
153 void JackWinNamedPipeClient::SetWriteTimeOut(long sec
)
156 COMMTIMEOUTS timeout;
157 if (GetCommTimeouts(fNamedPipe, &timeout)) {
158 jack_info("JackWinNamedPipeClient::SetWriteTimeOut WriteTotalTimeoutMultiplier = %d", timeout.WriteTotalTimeoutMultiplier);
159 jack_info("JackWinNamedPipeClient::SetWriteTimeOut WriteTotalTimeoutConstant = %d", timeout.WriteTotalTimeoutConstant);
164 void JackWinNamedPipeClient::SetNonBlocking(bool onoff
)
167 JackWinAsyncNamedPipeClient::JackWinAsyncNamedPipeClient()
168 : JackWinNamedPipeClient(), fPendingIO(false), fIOState(kIdle
)
171 fOverlap
.hEvent
= CreateEvent(NULL
, // default security attribute
172 TRUE
, // manual-reset event
173 TRUE
, // initial state = signaled
174 NULL
); // unnamed event object
177 JackWinAsyncNamedPipeClient::JackWinAsyncNamedPipeClient(HANDLE pipe
, const char* name
, bool pending
)
178 : JackWinNamedPipeClient(pipe
, name
), fPendingIO(pending
), fIOState(kIdle
)
180 fOverlap
.hEvent
= CreateEvent(NULL
, // default security attribute
181 TRUE
, // manual-reset event
182 TRUE
, // initial state = signaled
183 NULL
); // unnamed event object
186 SetEvent(fOverlap
.hEvent
);
189 fIOState
= (fPendingIO
) ? kConnecting
: kReading
;
192 JackWinAsyncNamedPipeClient::~JackWinAsyncNamedPipeClient()
194 CloseHandle(fOverlap
.hEvent
);
197 int JackWinAsyncNamedPipeClient::FinishIO()
200 success
= GetOverlappedResult(fNamedPipe
, // handle to pipe
201 &fOverlap
, // OVERLAPPED structure
202 &ret
, // bytes transferred
203 FALSE
); // do not wait
209 jack_error("Connection error");
213 // Prepare connection for new client ??
218 if (!success
|| ret
== 0) {
225 if (!success
|| ret
== 0) {
238 int JackWinAsyncNamedPipeClient::Read(void* data
, int len
)
241 jack_log("JackWinNamedPipeClient::Read len = %ld", len
);
242 BOOL res
= ReadFile(fNamedPipe
, data
, len
, &read
, &fOverlap
);
244 if (res
&& read
!= 0) {
248 } else if (!res
&& GetLastError() == ERROR_IO_PENDING
) {
252 jack_error("Cannot read named pipe err = %ld", GetLastError());
257 int JackWinAsyncNamedPipeClient::Write(void* data
, int len
)
260 jack_log("JackWinNamedPipeClient::Write len = %ld", len
);
261 BOOL res
= WriteFile(fNamedPipe
, data
, len
, &written
, &fOverlap
);
263 if (res
&& written
!= 0) {
267 } else if (!res
&& GetLastError() == ERROR_IO_PENDING
) {
271 jack_error("Cannot write named pipe err = %ld", GetLastError());
277 int JackWinNamedPipeServer::BindAux()
279 jack_log("JackWinNamedPipeServer::BindAux : fName %s", fName
);
281 if ((fNamedPipe
= CreateNamedPipe(fName
,
282 PIPE_ACCESS_DUPLEX
, // read/write access
283 PIPE_TYPE_MESSAGE
| // message type pipe
284 PIPE_READMODE_MESSAGE
| // message-read mode
285 PIPE_WAIT
, // blocking mode
286 PIPE_UNLIMITED_INSTANCES
, // max. instances
287 BUFSIZE
, // output buffer size
288 BUFSIZE
, // input buffer size
289 INFINITE
, // client time-out
290 NULL
)) == INVALID_HANDLE_VALUE
) { // no security
291 jack_error("Cannot bind server to pipe err = %ld", GetLastError());
298 int JackWinNamedPipeServer::Bind(const char* dir
, int which
)
300 snprintf(fName
, sizeof(fName
), "\\\\.\\pipe\\%s_jack_%d", dir
, which
);
304 int JackWinNamedPipeServer::Bind(const char* dir
, const char* name
, int which
)
306 snprintf(fName
, sizeof(fName
), "\\\\.\\pipe\\%s_jack_%s_%d", dir
, name
, which
);
310 bool JackWinNamedPipeServer::Accept()
312 if (ConnectNamedPipe(fNamedPipe
, NULL
)) {
315 jack_error("Cannot connect server pipe name = %s err = %ld", fName
, GetLastError());
316 if (GetLastError() == ERROR_PIPE_CONNECTED
) {
317 jack_error("Pipe already connected = %s", fName
);
325 JackWinNamedPipeClient
* JackWinNamedPipeServer::AcceptClient()
327 if (ConnectNamedPipe(fNamedPipe
, NULL
)) {
328 JackWinNamedPipeClient
* client
= new JackWinNamedPipeClient(fNamedPipe
, fName
);
329 // Init the pipe to the default value
330 fNamedPipe
= INVALID_HANDLE_VALUE
;
333 switch (GetLastError()) {
335 case ERROR_PIPE_CONNECTED
:
336 return new JackWinNamedPipeClient(fNamedPipe
, fName
);
339 jack_error("Cannot connect server pipe name = %s err = %ld", fName
, GetLastError());
345 int JackWinNamedPipeServer::Close()
347 jack_log("JackWinNamedPipeServer::Close");
349 if (fNamedPipe
!= INVALID_HANDLE_VALUE
) {
350 DisconnectNamedPipe(fNamedPipe
);
351 CloseHandle(fNamedPipe
);
352 fNamedPipe
= INVALID_HANDLE_VALUE
;
361 int JackWinAsyncNamedPipeServer::BindAux()
363 jack_log("JackWinAsyncNamedPipeServer::BindAux : fName %s", fName
);
365 if ((fNamedPipe
= CreateNamedPipe(fName
,
366 PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
, // read/write access
367 PIPE_TYPE_MESSAGE
| // message type pipe
368 PIPE_READMODE_MESSAGE
| // message-read mode
369 PIPE_WAIT
, // blocking mode
370 PIPE_UNLIMITED_INSTANCES
, // max. instances
371 BUFSIZE
, // output buffer size
372 BUFSIZE
, // input buffer size
373 INFINITE
, // client time-out
374 NULL
)) == INVALID_HANDLE_VALUE
) { // no security a
375 jack_error("Cannot bind server to pipe err = %ld", GetLastError());
382 int JackWinAsyncNamedPipeServer::Bind(const char* dir
, int which
)
384 snprintf(fName
, sizeof(fName
), "\\\\.\\pipe\\%s_jack_%d", dir
, which
);
388 int JackWinAsyncNamedPipeServer::Bind(const char* dir
, const char* name
, int which
)
390 snprintf(fName
, sizeof(fName
), "\\\\.\\pipe\\%s_jack_%s_%d", dir
, name
, which
);
394 bool JackWinAsyncNamedPipeServer::Accept()
399 JackWinNamedPipeClient
* JackWinAsyncNamedPipeServer::AcceptClient()
401 if (ConnectNamedPipe(fNamedPipe
, NULL
)) {
402 return new JackWinAsyncNamedPipeClient(fNamedPipe
, fName
, false);
404 switch (GetLastError()) {
406 case ERROR_IO_PENDING
:
407 return new JackWinAsyncNamedPipeClient(fNamedPipe
, fName
, true);
409 case ERROR_PIPE_CONNECTED
:
410 return new JackWinAsyncNamedPipeClient(fNamedPipe
, fName
, false);
413 jack_error("Cannot connect server pipe name = %s err = %ld", fName
, GetLastError());
420 } // end of namespace