-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
twowaypipe.h
104 lines (78 loc) · 3.61 KB
/
twowaypipe.h
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#ifndef TwoWayPipe_H
#define TwoWayPipe_H
#include "processdescriptor.h"
#ifdef HOST_UNIX
#define INVALID_PIPE -1
#else
#define INVALID_PIPE INVALID_HANDLE_VALUE
#endif
// This file contains definition of a simple IPC mechanism - bidirectional named pipe.
// It is implemented on top of two one-directional names pipes (fifos on UNIX)
// One Windows it is possible to ask OS to create a bidirectional pipe, but it is not the case on UNIX.
// In order to unify implementation we use two pipes on all systems.
// This all methods of this class are *NOT* thread safe: it is assumed the caller provides synchronization at a higher level.
class TwoWayPipe
{
public:
enum State
{
NotInitialized, // Object didn't create or connect to any pipes.
Created, // Server side of the pipe has been created, but didn't bind it to a client.
ServerConnected, // Server side of the pipe is connected to a client
ClientConnected, // Client side of the pipe is connected to a server.
};
TwoWayPipe()
:m_state(NotInitialized),
m_inboundPipe(INVALID_PIPE),
m_outboundPipe(INVALID_PIPE)
{}
~TwoWayPipe()
{
Disconnect();
}
// Creates a server side of the pipe.
// pd is used to create pipes names and uniquely identify the pipe on the machine.
// true - success, false - failure (use GetLastError() for more details)
bool CreateServer(const ProcessDescriptor& pd);
// Connects to a previously opened server side of the pipe.
// pd is used to locate the pipe on the machine.
// true - success, false - failure (use GetLastError() for more details)
bool Connect(const ProcessDescriptor& pd);
// Waits for incoming client connections, assumes GetState() == Created
// true - success, false - failure (use GetLastError() for more details)
bool WaitForConnection();
// Reads data from pipe. Returns number of bytes read or a negative number in case of an error.
// use GetLastError() for more details
int Read(void *buffer, DWORD bufferSize);
// Writes data to pipe. Returns number of bytes written or a negative number in case of an error.
// use GetLastError() for more details
int Write(const void *data, DWORD dataSize);
// Disconnects server or client side of the pipe.
// true - success, false - failure (use GetLastError() for more details)
bool Disconnect();
State GetState()
{
return m_state;
}
// Used by debugger side (RS) to cleanup the target (LS) named pipes
// and semaphores when the debugger detects the debuggee process exited.
void CleanupTargetProcess();
private:
State m_state;
#ifdef HOST_UNIX
int m_inboundPipe, m_outboundPipe; // two one sided pipes used for communication
char m_inPipeName[MAX_DEBUGGER_TRANSPORT_PIPE_NAME_LENGTH]; // filename of the inbound pipe
char m_outPipeName[MAX_DEBUGGER_TRANSPORT_PIPE_NAME_LENGTH]; // filename of the outbound pipe
#else
// Connects to a one sided pipe previously created by CreateOneWayPipe.
// In order to successfully connect id and inbound flag should be the same.
HANDLE OpenOneWayPipe(DWORD id, bool inbound);
// Creates a one way pipe, id and inboud flag are used for naming.
// Created pipe is supposed to be connected to by OpenOneWayPipe.
HANDLE CreateOneWayPipe(DWORD id, bool inbound);
HANDLE m_inboundPipe, m_outboundPipe; //two one sided pipes used for communication
#endif //HOST_UNIX
};
#endif //TwoWayPipe_H