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());
82 int JackWinNamedPipeClient::ConnectAux()
84 jack_log(
"JackWinNamedPipeClient::ConnectAux : fName %s", fName);
88 fNamedPipe = CreateFile(fName,
98 if (fNamedPipe != INVALID_HANDLE_VALUE) {
103 if ((GetLastError() != ERROR_PIPE_BUSY) && (GetLastError() != ERROR_FILE_NOT_FOUND)) {
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 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)
153 void JackWinNamedPipeClient::SetWriteTimeOut(
long sec)
164 void JackWinNamedPipeClient::SetNonBlocking(
bool onoff)
167 JackWinAsyncNamedPipeClient::JackWinAsyncNamedPipeClient()
168 : JackWinNamedPipeClient(), fPendingIO(false), fIOState(kIdle)
171 fOverlap.hEvent = CreateEvent(NULL,
177 JackWinAsyncNamedPipeClient::JackWinAsyncNamedPipeClient(HANDLE pipe,
const char* name,
bool pending)
178 : JackWinNamedPipeClient(pipe, name), fPendingIO(pending), fIOState(kIdle)
180 fOverlap.hEvent = CreateEvent(NULL,
186 SetEvent(fOverlap.hEvent);
189 fIOState = (fPendingIO) ? kConnecting : kReading;
192 JackWinAsyncNamedPipeClient::~JackWinAsyncNamedPipeClient()
194 CloseHandle(fOverlap.hEvent);
197 int JackWinAsyncNamedPipeClient::FinishIO()
200 success = GetOverlappedResult(fNamedPipe,
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,
284 PIPE_READMODE_MESSAGE |
286 PIPE_UNLIMITED_INSTANCES,
290 NULL)) == INVALID_HANDLE_VALUE) {
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 connnected = %s", fName);
325 JackWinNamedPipeClient* JackWinNamedPipeServer::AcceptClient()
327 if (ConnectNamedPipe(fNamedPipe, NULL)) {
328 JackWinNamedPipeClient* client =
new JackWinNamedPipeClient(fNamedPipe, fName);
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,
368 PIPE_READMODE_MESSAGE |
370 PIPE_UNLIMITED_INSTANCES,
374 NULL)) == INVALID_HANDLE_VALUE) {
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());
SERVER_EXPORT void jack_error(const char *fmt,...)
SERVER_EXPORT void jack_log(const char *fmt,...)