~ubuntu-branches/ubuntu/maverick/codelite/maverick

« back to all changes in this revision

Viewing changes to sdk/codelite_indexer/network/named_pipe_client.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2009-02-10 02:27:55 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090210022755-m5692nfc1t5uf1w9
Tags: 1.0.2759+dfsg-0ubuntu1
* New upstream release (LP: #327216).
* debian/patches/series, debian/patches/00_fix-ia64-build.patch:
  + Dropped, applied upstream already.
* debian/patches/02_fix-desktop.patch,
  debian/patches/03_fix-sh.patch:
  + Refreshed to patch cleanly.
* debian/rules:
  + Make get-orig-source honour UPSTREAM_VERSION if set.
* debian/ctags-le.1,
  debian/codelite_indexer.1,
  debian/codelite.manpages:
  + Dropped ctags-le manpage, since ctags-le was replaced by
    codelite_indexer.
  + Added codelite_indexer manpage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "named_pipe_client.h"
 
2
 
 
3
#ifndef __WXMSW__
 
4
# include <sys/types.h>
 
5
# include <sys/socket.h>
 
6
# include <sys/un.h>
 
7
# include <stdio.h>
 
8
#endif
 
9
 
 
10
clNamedPipeClient::clNamedPipeClient(const char* path)
 
11
                : clNamedPipe(path)
 
12
{
 
13
}
 
14
 
 
15
 
 
16
clNamedPipeClient::~clNamedPipeClient(void)
 
17
{
 
18
        disconnect();
 
19
}
 
20
 
 
21
 
 
22
bool clNamedPipeClient::connect(long )
 
23
{
 
24
#ifdef __WXMSW__
 
25
        this->setLastError(ZNP_OK);
 
26
 
 
27
        while (1) {
 
28
                _pipeHandle = CreateFile(
 
29
                                  getPipePath(),  // pipe name
 
30
                                  GENERIC_READ |  // read and write access
 
31
                                  GENERIC_WRITE,
 
32
                                  0,              // no sharing
 
33
                                  NULL,           // default security attributes
 
34
                                  OPEN_EXISTING,  // opens existing pipe
 
35
                                  0,              // default attributes
 
36
                                  NULL);          // no template file
 
37
 
 
38
                if (_pipeHandle != INVALID_PIPE_HANDLE)
 
39
                        return true;
 
40
 
 
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);
 
45
                        return false;
 
46
                }
 
47
 
 
48
                // All pipe instances are busy, so wait for 20 seconds.
 
49
 
 
50
                if (!WaitNamedPipe(getPipePath(), 20000)) {
 
51
                        DWORD err = GetLastError();
 
52
                        if (err == WAIT_TIMEOUT) {
 
53
                                this->setLastError(ZNP_TIMEOUT);
 
54
                        } else {
 
55
                                this->setLastError(ZNP_CONNECT_WAIT_ERROR);
 
56
                        }
 
57
                        return false;
 
58
                }
 
59
        }
 
60
#else // MSW
 
61
        struct sockaddr_un server;
 
62
        
 
63
        _pipeHandle = socket(AF_UNIX, SOCK_STREAM, 0);
 
64
        if (_pipeHandle < 0) {
 
65
                perror("ERROR");
 
66
                return false;
 
67
        }
 
68
        
 
69
        server.sun_family = AF_UNIX;
 
70
        strcpy(server.sun_path, getPipePath());
 
71
 
 
72
        if (::connect(_pipeHandle, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
 
73
                perror("ERROR");
 
74
                disconnect();
 
75
                return false;
 
76
        }
 
77
        return true;
 
78
#endif
 
79
}
 
80
 
 
81
bool clNamedPipeClient::isConnected()
 
82
{
 
83
        return (_pipeHandle != INVALID_PIPE_HANDLE);
 
84
}
 
85
 
 
86
 
 
87
void clNamedPipeClient::disconnect()
 
88
{
 
89
#ifdef __WXMSW__
 
90
        this->setLastError(ZNP_OK);
 
91
 
 
92
        if (_pipeHandle != INVALID_PIPE_HANDLE) {
 
93
                CloseHandle(_pipeHandle);
 
94
                _pipeHandle = INVALID_PIPE_HANDLE;
 
95
        }
 
96
#else
 
97
        if ( _pipeHandle != INVALID_PIPE_HANDLE ) {
 
98
                close(_pipeHandle);
 
99
                shutdown(_pipeHandle, SHUT_RDWR);
 
100
                _pipeHandle = INVALID_PIPE_HANDLE;
 
101
        }
 
102
#endif
 
103
}