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 JackWinNamedPipe::Read(void* data
, int len
)
34 BOOL res
= ReadFile(fNamedPipe
, data
, len
, &read
, NULL
);
35 if (res
&& read
== (DWORD
)len
) {
38 jack_error("Cannot read named pipe err = %ld", GetLastError());
43 int JackWinNamedPipe::Write(void* data
, int len
)
46 BOOL res
= WriteFile(fNamedPipe
, data
, len
, &written
, NULL
);
47 if (res
&& written
== (DWORD
)len
) {
50 jack_error("Cannot write named pipe err = %ld", GetLastError());
55 int JackWinNamedPipeClient::Connect(const char* dir
, int which
)
57 sprintf(fName
, "\\\\.\\pipe\\%s_jack_%d", dir
, which
);
58 jack_log("Connect: fName %s", fName
);
60 fNamedPipe
= CreateFile(fName
, // pipe name
61 GENERIC_READ
| // read and write access
64 NULL
, // default security attributes
65 OPEN_EXISTING
, // opens existing pipe
66 0, // default attributes
67 NULL
); // no template file
69 if (fNamedPipe
== INVALID_HANDLE_VALUE
) {
70 jack_error("Cannot connect to named pipe = %s err = %ld", fName
, GetLastError());
77 int JackWinNamedPipeClient::Connect(const char* dir
, const char* name
, int which
)
79 sprintf(fName
, "\\\\.\\pipe\\%s_jack_%s_%d", dir
, name
, which
);
80 jack_log("Connect: fName %s", fName
);
82 fNamedPipe
= CreateFile(fName
, // pipe name
83 GENERIC_READ
| // read and write access
86 NULL
, // default security attributes
87 OPEN_EXISTING
, // opens existing pipe
88 0, // default attributes
89 NULL
); // no template file
91 if (fNamedPipe
== INVALID_HANDLE_VALUE
) {
92 jack_error("Cannot connect to named pipe = %s err = %ld", fName
, GetLastError());
99 int JackWinNamedPipeClient::Close()
101 if (fNamedPipe
!= INVALID_HANDLE_VALUE
) {
102 CloseHandle(fNamedPipe
);
103 fNamedPipe
= INVALID_HANDLE_VALUE
;
110 void JackWinNamedPipeClient::SetReadTimeOut(long sec
)
113 void JackWinNamedPipeClient::SetWriteTimeOut(long sec
)
116 JackWinAsyncNamedPipeClient::JackWinAsyncNamedPipeClient()
117 : JackWinNamedPipeClient(), fPendingIO(false), fIOState(kIdle
)
120 fOverlap
.hEvent
= CreateEvent(NULL
, // default security attribute
121 TRUE
, // manual-reset event
122 TRUE
, // initial state = signaled
123 NULL
); // unnamed event object
126 JackWinAsyncNamedPipeClient::JackWinAsyncNamedPipeClient(HANDLE pipe
, bool pending
)
127 : JackWinNamedPipeClient(pipe
), fPendingIO(pending
), fIOState(kIdle
)
129 fOverlap
.hEvent
= CreateEvent(NULL
, // default security attribute
130 TRUE
, // manual-reset event
131 TRUE
, // initial state = signaled
132 NULL
); // unnamed event object
135 SetEvent(fOverlap
.hEvent
);
137 fIOState
= (fPendingIO
) ? kConnecting
: kReading
;
140 JackWinAsyncNamedPipeClient::~JackWinAsyncNamedPipeClient()
142 CloseHandle(fOverlap
.hEvent
);
145 int JackWinAsyncNamedPipeClient::FinishIO()
148 success
= GetOverlappedResult(fNamedPipe
, // handle to pipe
149 &fOverlap
, // OVERLAPPED structure
150 &ret
, // bytes transferred
151 FALSE
); // do not wait
157 jack_error("Conection error");
161 // Prepare connection for new client ??
166 if (!success
|| ret
== 0) {
173 if (!success
|| ret
== 0) {
186 int JackWinAsyncNamedPipeClient::Read(void* data
, int len
)
189 jack_log("JackWinNamedPipeClient::Read len = %ld", len
);
190 BOOL res
= ReadFile(fNamedPipe
, data
, len
, &read
, &fOverlap
);
191 jack_log("JackWinNamedPipeClient::Read res = %ld read %ld", res
, read
);
193 if (res
&& read
!= 0) {
197 } else if (!res
&& GetLastError() == ERROR_IO_PENDING
) {
201 jack_error("Cannot read named pipe err = %ld", GetLastError());
206 int JackWinAsyncNamedPipeClient::Write(void* data
, int len
)
209 jack_log("JackWinNamedPipeClient::Write len = %ld", len
);
210 BOOL res
= WriteFile(fNamedPipe
, data
, len
, &written
, &fOverlap
);
212 if (res
&& written
!= 0) {
216 } else if (!res
&& GetLastError() == ERROR_IO_PENDING
) {
220 jack_error("Cannot write named pipe err = %ld", GetLastError());
227 int JackWinNamedPipeServer::Bind(const char* dir
, int which
)
229 sprintf(fName
, "\\\\.\\pipe\\%s_jack_%d", dir
, which
);
230 jack_log("Bind: fName %s", fName
);
232 if ((fNamedPipe
= CreateNamedPipe(fName
,
233 PIPE_ACCESS_DUPLEX
, // read/write access
234 PIPE_TYPE_MESSAGE
| // message type pipe
235 PIPE_READMODE_MESSAGE
| // message-read mode
236 PIPE_WAIT
, // blocking mode
237 PIPE_UNLIMITED_INSTANCES
, // max. instances
238 BUFSIZE
, // output buffer size
239 BUFSIZE
, // input buffer size
240 INFINITE
, // client time-out
241 NULL
)) == INVALID_HANDLE_VALUE
) { // no security a
242 jack_error("Cannot bind server to pipe err = %ld", GetLastError());
249 int JackWinNamedPipeServer::Bind(const char* dir
, const char* name
, int which
)
251 sprintf(fName
, "\\\\.\\pipe\\%s_jack_%s_%d", dir
, name
, which
);
252 jack_log("Bind: fName %s", fName
);
254 if ((fNamedPipe
= CreateNamedPipe(fName
,
255 PIPE_ACCESS_DUPLEX
, // read/write access
256 PIPE_TYPE_MESSAGE
| // message type pipe
257 PIPE_READMODE_MESSAGE
| // message-read mode
258 PIPE_WAIT
, // blocking mode
259 PIPE_UNLIMITED_INSTANCES
, // max. instances
260 BUFSIZE
, // output buffer size
261 BUFSIZE
, // input buffer size
262 INFINITE
, // client time-out
263 NULL
)) == INVALID_HANDLE_VALUE
) { // no security a
264 jack_error("Cannot bind server to pipe err = %ld", GetLastError());
271 bool JackWinNamedPipeServer::Accept()
273 if (ConnectNamedPipe(fNamedPipe
, NULL
)) {
276 jack_error("Cannot bind server pipe name = %s err = %ld", fName
, GetLastError());
277 if (GetLastError() == ERROR_PIPE_CONNECTED
) {
278 jack_error("pipe already connnected = %s ", fName
);
286 JackWinNamedPipeClient
* JackWinNamedPipeServer::AcceptClient()
288 if (ConnectNamedPipe(fNamedPipe
, NULL
)) {
289 JackWinNamedPipeClient
* client
= new JackWinNamedPipeClient(fNamedPipe
);
290 // Init the pipe to the default value
291 fNamedPipe
= INVALID_HANDLE_VALUE
;
294 switch (GetLastError()) {
296 case ERROR_PIPE_CONNECTED
:
297 return new JackWinNamedPipeClient(fNamedPipe
);
300 jack_error("Cannot connect server pipe name = %s err = %ld", fName
, GetLastError());
307 int JackWinNamedPipeServer::Close()
309 jack_log("JackWinNamedPipeServer::Close");
311 if (fNamedPipe
!= INVALID_HANDLE_VALUE
) {
312 DisconnectNamedPipe(fNamedPipe
);
313 CloseHandle(fNamedPipe
);
314 fNamedPipe
= INVALID_HANDLE_VALUE
;
323 int JackWinAsyncNamedPipeServer::Bind(const char* dir
, int which
)
325 sprintf(fName
, "\\\\.\\pipe\\%s_jack_%d", dir
, which
);
326 jack_log("Bind: fName %s", fName
);
328 if ((fNamedPipe
= CreateNamedPipe(fName
,
329 PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
, // read/write access
330 PIPE_TYPE_MESSAGE
| // message type pipe
331 PIPE_READMODE_MESSAGE
| // message-read mode
332 PIPE_WAIT
, // blocking mode
333 PIPE_UNLIMITED_INSTANCES
, // max. instances
334 BUFSIZE
, // output buffer size
335 BUFSIZE
, // input buffer size
336 INFINITE
, // client time-out
337 NULL
)) == INVALID_HANDLE_VALUE
) { // no security a
338 jack_error("Cannot bind server to pipe err = %ld", GetLastError());
345 int JackWinAsyncNamedPipeServer::Bind(const char* dir
, const char* name
, int which
)
347 sprintf(fName
, "\\\\.\\pipe\\%s_jack_%s_%d", dir
, name
, which
);
348 jack_log("Bind: fName %s", fName
);
350 if ((fNamedPipe
= CreateNamedPipe(fName
,
351 PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
, // read/write access
352 PIPE_TYPE_MESSAGE
| // message type pipe
353 PIPE_READMODE_MESSAGE
| // message-read mode
354 PIPE_WAIT
, // blocking mode
355 PIPE_UNLIMITED_INSTANCES
, // max. instances
356 BUFSIZE
, // output buffer size
357 BUFSIZE
, // input buffer size
358 INFINITE
, // client time-out
359 NULL
)) == INVALID_HANDLE_VALUE
) { // no security a
360 jack_error("Cannot bind server to pipe err = %ld", GetLastError());
367 bool JackWinAsyncNamedPipeServer::Accept()
372 JackWinNamedPipeClient
* JackWinAsyncNamedPipeServer::AcceptClient()
374 if (ConnectNamedPipe(fNamedPipe
, NULL
)) {
375 return new JackWinAsyncNamedPipeClient(fNamedPipe
, false);
377 switch (GetLastError()) {
379 case ERROR_IO_PENDING
:
380 return new JackWinAsyncNamedPipeClient(fNamedPipe
, true);
382 case ERROR_PIPE_CONNECTED
:
383 return new JackWinAsyncNamedPipeClient(fNamedPipe
, false);
386 jack_error("Cannot connect server pipe name = %s err = %ld", fName
, GetLastError());
393 } // end of namespace