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 name = %s err = %ld", fName, 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 name = %s err = %ld", fName, GetLastError());
82 int JackWinNamedPipeClient::ConnectAux()
84 jack_log(
"Connect: fName %s", fName);
88 fNamedPipe = CreateFile(fName,
98 if (fNamedPipe != INVALID_HANDLE_VALUE) {
103 if (GetLastError() != ERROR_PIPE_BUSY) {
104 jack_error(
"Cannot connect to named pipe = %s err = %ld", fName, GetLastError());
109 if (!WaitNamedPipe(fName, 2000)) {
110 jack_error(
"Cannot connect to named pipe = %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 void JackWinNamedPipeClient::SetWriteTimeOut(
long sec)
145 JackWinAsyncNamedPipeClient::JackWinAsyncNamedPipeClient()
146 : JackWinNamedPipeClient(), fPendingIO(false), fIOState(kIdle)
149 fOverlap.hEvent = CreateEvent(NULL,
155 JackWinAsyncNamedPipeClient::JackWinAsyncNamedPipeClient(HANDLE pipe,
const char* name,
bool pending)
156 : JackWinNamedPipeClient(pipe, name), fPendingIO(pending), fIOState(kIdle)
158 fOverlap.hEvent = CreateEvent(NULL,
164 SetEvent(fOverlap.hEvent);
167 fIOState = (fPendingIO) ? kConnecting : kReading;
170 JackWinAsyncNamedPipeClient::~JackWinAsyncNamedPipeClient()
172 CloseHandle(fOverlap.hEvent);
175 int JackWinAsyncNamedPipeClient::FinishIO()
178 success = GetOverlappedResult(fNamedPipe,
196 if (!success || ret == 0) {
203 if (!success || ret == 0) {
216 int JackWinAsyncNamedPipeClient::Read(
void* data,
int len)
219 jack_log(
"JackWinNamedPipeClient::Read len = %ld", len);
220 BOOL res = ReadFile(fNamedPipe, data, len, &read, &fOverlap);
222 if (res && read != 0) {
226 }
else if (!res && GetLastError() == ERROR_IO_PENDING) {
230 jack_error(
"Cannot read named pipe err = %ld", GetLastError());
235 int JackWinAsyncNamedPipeClient::Write(
void* data,
int len)
238 jack_log(
"JackWinNamedPipeClient::Write len = %ld", len);
239 BOOL res = WriteFile(fNamedPipe, data, len, &written, &fOverlap);
241 if (res && written != 0) {
245 }
else if (!res && GetLastError() == ERROR_IO_PENDING) {
249 jack_error(
"Cannot write named pipe err = %ld", GetLastError());
255 int JackWinNamedPipeServer::BindAux()
259 if ((fNamedPipe = CreateNamedPipe(fName,
262 PIPE_READMODE_MESSAGE |
264 PIPE_UNLIMITED_INSTANCES,
268 NULL)) == INVALID_HANDLE_VALUE) {
269 jack_error(
"Cannot bind server to pipe err = %ld", GetLastError());
276 int JackWinNamedPipeServer::Bind(
const char* dir,
int which)
278 snprintf(fName,
sizeof(fName),
"\\\\.\\pipe\\%s_jack_%d", dir, which);
282 int JackWinNamedPipeServer::Bind(
const char* dir,
const char* name,
int which)
284 snprintf(fName,
sizeof(fName),
"\\\\.\\pipe\\%s_jack_%s_%d", dir, name, which);
288 bool JackWinNamedPipeServer::Accept()
290 if (ConnectNamedPipe(fNamedPipe, NULL)) {
293 jack_error(
"Cannot bind server pipe name = %s err = %ld", fName, GetLastError());
294 if (GetLastError() == ERROR_PIPE_CONNECTED) {
295 jack_error(
"pipe already connnected = %s ", fName);
303 JackWinNamedPipeClient* JackWinNamedPipeServer::AcceptClient()
305 if (ConnectNamedPipe(fNamedPipe, NULL)) {
306 JackWinNamedPipeClient* client =
new JackWinNamedPipeClient(fNamedPipe, fName);
308 fNamedPipe = INVALID_HANDLE_VALUE;
311 switch (GetLastError()) {
313 case ERROR_PIPE_CONNECTED:
314 return new JackWinNamedPipeClient(fNamedPipe, fName);
317 jack_error(
"Cannot connect server pipe name = %s err = %ld", fName, GetLastError());
324 int JackWinNamedPipeServer::Close()
326 jack_log(
"JackWinNamedPipeServer::Close");
328 if (fNamedPipe != INVALID_HANDLE_VALUE) {
329 DisconnectNamedPipe(fNamedPipe);
330 CloseHandle(fNamedPipe);
331 fNamedPipe = INVALID_HANDLE_VALUE;
340 int JackWinAsyncNamedPipeServer::BindAux()
344 if ((fNamedPipe = CreateNamedPipe(fName,
345 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
347 PIPE_READMODE_MESSAGE |
349 PIPE_UNLIMITED_INSTANCES,
353 NULL)) == INVALID_HANDLE_VALUE) {
354 jack_error(
"Cannot bind server to pipe err = %ld", GetLastError());
361 int JackWinAsyncNamedPipeServer::Bind(
const char* dir,
int which)
363 snprintf(fName,
sizeof(fName),
"\\\\.\\pipe\\%s_jack_%d", dir, which);
367 int JackWinAsyncNamedPipeServer::Bind(
const char* dir,
const char* name,
int which)
369 snprintf(fName,
sizeof(fName),
"\\\\.\\pipe\\%s_jack_%s_%d", dir, name, which);
373 bool JackWinAsyncNamedPipeServer::Accept()
378 JackWinNamedPipeClient* JackWinAsyncNamedPipeServer::AcceptClient()
380 if (ConnectNamedPipe(fNamedPipe, NULL)) {
381 return new JackWinAsyncNamedPipeClient(fNamedPipe, fName,
false);
383 switch (GetLastError()) {
385 case ERROR_IO_PENDING:
386 return new JackWinAsyncNamedPipeClient(fNamedPipe, fName,
true);
388 case ERROR_PIPE_CONNECTED:
389 return new JackWinAsyncNamedPipeClient(fNamedPipe, fName,
false);
392 jack_error(
"Cannot connect server pipe name = %s err = %ld", fName, GetLastError());