summaryrefslogtreecommitdiffstats
path: root/client/windows/named_pipe.cpp
blob: 78361474e304648424a192ba8ca8d95096a65921 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
   Copyright (C) 2009 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#include "common.h"
#include "named_pipe.h"
#include "utils.h"
#include "debug.h"

PipeBuffer::PipeBuffer(HANDLE pipe, ProcessLoop& process_loop)
    : _handler (NULL)
    , _pipe (pipe)
    , _start (0)
    , _end (0)
    , _pending (false)
    , _process_loop(process_loop)
{
    ZeroMemory(&_overlap, sizeof(_overlap));
    _overlap.hEvent = this->get_handle();
    _process_loop.add_handle(*this);
}

PipeBuffer::~PipeBuffer()
{
    _process_loop.remove_handle(*this);
}

DWORD PipeBuffer::get_overlapped_bytes()
{
    DWORD bytes = 0;

    if (!GetOverlappedResult(_pipe, &_overlap, &bytes, FALSE) || bytes == 0) {
        _pending = false;
        _handler->on_data();
    }
    return bytes;
}

int32_t PipeReader::read(uint8_t *buf, int32_t size)
{
    ASSERT(buf && size >= 0);

    if (_start < _end) {
        int32_t bytes_read = 0;
        bytes_read = MIN(_end - _start, (uint32_t)size);
        CopyMemory(buf, _data + _start, bytes_read);
        _start += bytes_read;
        if (_start == _end) {
            _start = _end = 0;
        }
        return bytes_read;
    }
    if (_pending) {
        return 0;
    }
    if (!ReadFile(_pipe, _data + _end, sizeof(_data) - _end, NULL, &_overlap) &&
                                              GetLastError() != ERROR_IO_PENDING) {
        DBG(0, "ReadFile() failed %u", GetLastError());
        return -1;
    }
    _pending = true;
    return 0;
}

void PipeReader::on_event()
{
    ASSERT(_pending);
    DWORD bytes = get_overlapped_bytes();

    if (!bytes) {
        return;
    }
    _end += bytes;
    _pending = false;
    _handler->on_data();
}

int32_t PipeWriter::write(const uint8_t *buf, int32_t size)
{
    int32_t bytes_written = 0;
    ASSERT(buf && size >= 0);

    if (!_pending && _start == _end) {
        _start = _end = 0;
    }
    if (_end < sizeof(_data)) {
        bytes_written = MIN(sizeof(_data) - _end, (uint32_t)size);
        CopyMemory(_data + _end, buf, bytes_written);
        _end += bytes_written;
    }
    if (!_pending && _start < _end) {
        if (!WriteFile(_pipe, _data + _start, _end - _start, NULL, &_overlap) &&
                                              GetLastError() != ERROR_IO_PENDING) {
            DBG(0, "WriteFile() failed %u", GetLastError());
            return -1;
        }
        _pending = true;
    }
    return bytes_written;
}

void PipeWriter::on_event()
{
    ASSERT(_pending);
    DWORD bytes = get_overlapped_bytes();
    if (!bytes) {
        return;
    }
    _start += bytes;
    _pending = false;
    if (_start == sizeof(_data)) {
        _handler->on_data();
    }
}

WinConnection::WinConnection(HANDLE pipe, ProcessLoop& process_loop)
    : _pipe (pipe)
    , _writer (pipe, process_loop)
    , _reader (pipe, process_loop)
{
}

WinConnection::~WinConnection()
{
    if (!DisconnectNamedPipe(_pipe)) {
        DBG(0, "DisconnectNamedPipe failed %d", GetLastError());
    }
    CloseHandle(_pipe);
}

int32_t WinConnection::read(uint8_t *buf, int32_t size)
{
    return _reader.read(buf, size);
}

int32_t WinConnection::write(const uint8_t *buf, int32_t size)
{
    return _writer.write(buf, size);
}

void WinConnection::set_handler(NamedPipe::ConnectionInterface* handler)
{
    _reader.set_handler(handler);
    _writer.set_handler(handler);
}

WinListener::WinListener(const char *name, NamedPipe::ListenerInterface &listener_interface,
                         ProcessLoop& process_loop)
    : _listener_interface (listener_interface)
    , _pipe (0)
    , _process_loop (process_loop)
{
    _pipename = new TCHAR[PIPE_MAX_NAME_LEN];
    swprintf_s(_pipename, PIPE_MAX_NAME_LEN, L"%s%S", PIPE_PREFIX, name);
    ZeroMemory(&_overlap, sizeof(_overlap));
    _overlap.hEvent = this->get_handle();
    _process_loop.add_handle(*this);
    create_pipe();
}

WinListener::~WinListener()
{
    CancelIo(_pipe);
    _process_loop.remove_handle(*this);
    delete[] _pipename;
}

void WinListener::on_event()
{
    DWORD bytes;

    if (!GetOverlappedResult(_pipe, &_overlap, &bytes, FALSE)) {
        DBG(0, "GetOverlappedResult() failed %u", GetLastError());
        return;
    }
    DBG(0, "Pipe connected 0x%p", _pipe);
    WinConnection *con = new WinConnection(_pipe, _process_loop);
    NamedPipe::ConnectionInterface &con_interface = _listener_interface.create();
    con->set_handler(&con_interface);
    con_interface.bind((NamedPipe::ConnectionRef)con);
    create_pipe();
}

void WinListener::create_pipe()
{
    _pipe = CreateNamedPipe(_pipename, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
                            PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
                            PIPE_UNLIMITED_INSTANCES, PIPE_BUF_SIZE, PIPE_BUF_SIZE,
                            PIPE_TIMEOUT, NULL);
    if (_pipe == INVALID_HANDLE_VALUE) {
        THROW("CreateNamedPipe() failed %u", GetLastError());
    }
    if (ConnectNamedPipe(_pipe, &_overlap)) {
        THROW("ConnectNamedPipe() is not pending");
    }
    switch (GetLastError()) {
    case ERROR_IO_PENDING:
        DBG(0, "Pipe waits for connection");
        break;
    case ERROR_PIPE_CONNECTED: {
        DBG(0, "Pipe already connected");
        WinConnection *con = new WinConnection(_pipe, _process_loop);
        NamedPipe::ConnectionInterface &con_interface = _listener_interface.create();
        con->set_handler(&con_interface);
        con_interface.bind((NamedPipe::ConnectionRef)con);
        create_pipe();
        break;
    }
    default:
        THROW("ConnectNamedPipe() failed %u", GetLastError());
    }
}