1
#include "named_pipe_client.h"
4
# include <sys/types.h>
5
# include <sys/socket.h>
10
clNamedPipeClient::clNamedPipeClient(const char* path)
16
clNamedPipeClient::~clNamedPipeClient(void)
22
bool clNamedPipeClient::connect(long )
25
this->setLastError(ZNP_OK);
28
_pipeHandle = CreateFile(
29
getPipePath(), // pipe name
30
GENERIC_READ | // read and write access
33
NULL, // default security attributes
34
OPEN_EXISTING, // opens existing pipe
35
0, // default attributes
36
NULL); // no template file
38
if (_pipeHandle != INVALID_PIPE_HANDLE)
41
// Exit if an error other than ERROR_PIPE_BUSY occurs.
42
DWORD err = GetLastError();
43
if (err != ERROR_PIPE_BUSY) {
44
this->setLastError(ZNP_CONNECT_ERROR);
48
// All pipe instances are busy, so wait for 20 seconds.
50
if (!WaitNamedPipe(getPipePath(), 20000)) {
51
DWORD err = GetLastError();
52
if (err == WAIT_TIMEOUT) {
53
this->setLastError(ZNP_TIMEOUT);
55
this->setLastError(ZNP_CONNECT_WAIT_ERROR);
61
struct sockaddr_un server;
63
_pipeHandle = socket(AF_UNIX, SOCK_STREAM, 0);
64
if (_pipeHandle < 0) {
69
server.sun_family = AF_UNIX;
70
strcpy(server.sun_path, getPipePath());
72
if (::connect(_pipeHandle, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
81
bool clNamedPipeClient::isConnected()
83
return (_pipeHandle != INVALID_PIPE_HANDLE);
87
void clNamedPipeClient::disconnect()
90
this->setLastError(ZNP_OK);
92
if (_pipeHandle != INVALID_PIPE_HANDLE) {
93
CloseHandle(_pipeHandle);
94
_pipeHandle = INVALID_PIPE_HANDLE;
97
if ( _pipeHandle != INVALID_PIPE_HANDLE ) {
99
shutdown(_pipeHandle, SHUT_RDWR);
100
_pipeHandle = INVALID_PIPE_HANDLE;