summaryrefslogtreecommitdiff
path: root/src/debug/debug-pal/win/twowaypipe.cpp
blob: 49876871c0860109146817f8c5c44dc55df5badb (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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

#include <windows.h>
#include <stdio.h> 
#include <wchar.h>
#include <assert.h>
#include "twowaypipe.h"

#define _ASSERTE assert

// This file contains implementation of a simple IPC mechanism - bidirectional named pipe.
// It is implemented on top of two one-directional names pipes (fifos on UNIX)


// Creates a server side of the pipe. 
// Id is used to create pipes names and uniquely identify the pipe on the machine. 
// true - success, false - failure (use GetLastError() for more details)
bool TwoWayPipe::CreateServer(DWORD id)
{
    _ASSERTE(m_state == NotInitialized);
    if (m_state != NotInitialized)
        return false;

    m_inboundPipe = CreateOneWayPipe(id, true);
    if (m_inboundPipe == INVALID_HANDLE_VALUE)
    {
        return false;
    }

    m_outboundPipe = CreateOneWayPipe(id, false);
    if (m_outboundPipe == INVALID_HANDLE_VALUE)
    {
        CloseHandle(m_inboundPipe);
        m_inboundPipe = INVALID_HANDLE_VALUE;
        return false;
    }

    m_state = Created;
    return true;
}


// Connects to a previously opened server side of the pipe.
// Id is used to locate the pipe on the machine. 
// true - success, false - failure (use GetLastError() for more details)
bool TwoWayPipe::Connect(DWORD id)
{
    _ASSERTE(m_state == NotInitialized);
    if (m_state != NotInitialized)
        return false;

    m_inboundPipe = OpenOneWayPipe(id, true);
    if (m_inboundPipe == INVALID_HANDLE_VALUE)
    {
        return false;
    }

    m_outboundPipe = OpenOneWayPipe(id, false);
    if (m_outboundPipe == INVALID_HANDLE_VALUE)
    {
        CloseHandle(m_inboundPipe);
        m_inboundPipe = INVALID_HANDLE_VALUE;
        return false;
    }

    m_state = ClientConnected;
    return true;

}

// Waits for incoming client connections, assumes GetState() == Created
// true - success, false - failure (use GetLastError() for more details)
bool TwoWayPipe::WaitForConnection()
{
    _ASSERTE(m_state == Created);
    if (m_state != Created)
        return false;

    if (!ConnectNamedPipe(m_inboundPipe, NULL))
    {
        auto error = GetLastError();
        if (error != ERROR_PIPE_CONNECTED)
            return false;
    }

    if (!ConnectNamedPipe(m_outboundPipe, NULL))
    {
        auto error = GetLastError();
        if (error != ERROR_PIPE_CONNECTED)
            return false;
    }

    m_state = ServerConnected;
    return true;
}

// Reads data from pipe. Returns number of bytes read or a negative number in case of an error.
// use GetLastError() for more details
int TwoWayPipe::Read(void *buffer, DWORD bufferSize)
{
    _ASSERTE(m_state == ServerConnected || m_state == ClientConnected);
    DWORD bytesRead;
    BOOL ok = ReadFile(m_inboundPipe, buffer, bufferSize, &bytesRead, NULL);

    if (ok)
    {
        return (int)bytesRead;
    }
    else
    {
        return -1;
    }
}

// Writes data to pipe. Returns number of bytes written or a negative number in case of an error.
// use GetLastError() for more details
int TwoWayPipe::Write(const void *data, DWORD dataSize)
{
    _ASSERTE(m_state == ServerConnected || m_state == ClientConnected);
    DWORD bytesWritten;
    BOOL ok = WriteFile(m_outboundPipe, data, dataSize, &bytesWritten, NULL);

    if (ok)
    {
        FlushFileBuffers(m_outboundPipe);
        return (int)bytesWritten;
    }
    else
    {
        return -1;
    }
}

// Disconnect server or client side of the pipe.
// true - success, false - failure (use GetLastError() for more details)
bool TwoWayPipe::Disconnect()
{
    if (m_state == ServerConnected)
    {
        DisconnectNamedPipe(m_outboundPipe);
        DisconnectNamedPipe(m_inboundPipe);
        CloseHandle(m_outboundPipe);
        m_outboundPipe = INVALID_HANDLE_VALUE;
        CloseHandle(m_inboundPipe);
        m_inboundPipe = INVALID_HANDLE_VALUE;
        m_state = NotInitialized;
        return true;
    }
    else if (m_state == ClientConnected)
    {
        CloseHandle(m_outboundPipe);
        m_outboundPipe = INVALID_HANDLE_VALUE;
        CloseHandle(m_inboundPipe);
        m_inboundPipe = INVALID_HANDLE_VALUE;
        m_state = NotInitialized;
        return true;
    } 
    else 
    {
        // nothign to do
        return true;
    }
}

#define PIPE_NAME_FORMAT_STR L"\\\\.\\pipe\\clr-debug-pipe-%d-%s"

// Connects to a one sided pipe previously created by CreateOneWayPipe.
// In order to successfully connect id and inbound flag should be the same.
HANDLE TwoWayPipe::OpenOneWayPipe(DWORD id, bool inbound)
{
    WCHAR fullName[MAX_PATH];
    // "in" and "out" are deliberately switched because we're opening a client side connection
    int chars = swprintf_s(fullName, MAX_PATH, PIPE_NAME_FORMAT_STR, id, inbound ? L"out" : L"in");
    _ASSERTE(chars > 0);

    HANDLE handle = CreateFileW(
        fullName,
        inbound ? GENERIC_READ : GENERIC_WRITE,
        0,              // no sharing 
        NULL,           // default security attributes
        OPEN_EXISTING,  // opens existing pipe 
        0,              // default attributes 
        NULL);          // no template file 

    return handle;
}


// Creates a one way pipe, id and inboud flag are used for naming.
// Created pipe is supposed to be connected to by OpenOneWayPipe.
HANDLE TwoWayPipe::CreateOneWayPipe(DWORD id, bool inbound)
{
    WCHAR fullName[MAX_PATH];
    int chars = swprintf_s(fullName, MAX_PATH, PIPE_NAME_FORMAT_STR, id, inbound ? L"in" : L"out");
    _ASSERTE(chars > 0);

    HANDLE handle = CreateNamedPipeW(fullName,
        (inbound ? PIPE_ACCESS_INBOUND : PIPE_ACCESS_OUTBOUND) | FILE_FLAG_FIRST_PIPE_INSTANCE,
        PIPE_TYPE_BYTE | PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS,
        1,    // max number of instances
        4000, //in buffer size
        4000, //out buffer size
        0,    // default timeout
        NULL); // default security

    return handle;
}