| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | |
| 6 | #ifndef TwoWayPipe_H |
| 7 | #define TwoWayPipe_H |
| 8 | |
| 9 | #include "processdescriptor.h" |
| 10 | |
| 11 | #ifdef FEATURE_PAL |
| 12 | #define INVALID_PIPE -1 |
| 13 | #else |
| 14 | #define INVALID_PIPE INVALID_HANDLE_VALUE |
| 15 | #endif |
| 16 | |
| 17 | // This file contains definition of a simple IPC mechanism - bidirectional named pipe. |
| 18 | // It is implemented on top of two one-directional names pipes (fifos on UNIX) |
| 19 | |
| 20 | // One Windows it is possible to ask OS to create a bidirectional pipe, but it is not the case on UNIX. |
| 21 | // In order to unify implementation we use two pipes on all systems. |
| 22 | |
| 23 | // This all methods of this class are *NOT* thread safe: it is assumed the caller provides synchronization at a higher level. |
| 24 | class TwoWayPipe |
| 25 | { |
| 26 | public: |
| 27 | enum State |
| 28 | { |
| 29 | NotInitialized, // Object didn't create or connect to any pipes. |
| 30 | Created, // Server side of the pipe has been created, but didn't bind it to a client. |
| 31 | ServerConnected, // Server side of the pipe is connected to a client |
| 32 | ClientConnected, // Client side of the pipe is connected to a server. |
| 33 | }; |
| 34 | |
| 35 | TwoWayPipe() |
| 36 | :m_state(NotInitialized), |
| 37 | m_inboundPipe(INVALID_PIPE), |
| 38 | m_outboundPipe(INVALID_PIPE) |
| 39 | {} |
| 40 | |
| 41 | |
| 42 | ~TwoWayPipe() |
| 43 | { |
| 44 | Disconnect(); |
| 45 | } |
| 46 | |
| 47 | // Creates a server side of the pipe. |
| 48 | // pd is used to create pipes names and uniquely identify the pipe on the machine. |
| 49 | // true - success, false - failure (use GetLastError() for more details) |
| 50 | bool CreateServer(const ProcessDescriptor& pd); |
| 51 | |
| 52 | // Connects to a previously opened server side of the pipe. |
| 53 | // pd is used to locate the pipe on the machine. |
| 54 | // true - success, false - failure (use GetLastError() for more details) |
| 55 | bool Connect(const ProcessDescriptor& pd); |
| 56 | |
| 57 | // Waits for incoming client connections, assumes GetState() == Created |
| 58 | // true - success, false - failure (use GetLastError() for more details) |
| 59 | bool WaitForConnection(); |
| 60 | |
| 61 | // Reads data from pipe. Returns number of bytes read or a negative number in case of an error. |
| 62 | // use GetLastError() for more details |
| 63 | int Read(void *buffer, DWORD bufferSize); |
| 64 | |
| 65 | // Writes data to pipe. Returns number of bytes written or a negative number in case of an error. |
| 66 | // use GetLastError() for more details |
| 67 | int Write(const void *data, DWORD dataSize); |
| 68 | |
| 69 | // Disconnects server or client side of the pipe. |
| 70 | // true - success, false - failure (use GetLastError() for more details) |
| 71 | bool Disconnect(); |
| 72 | |
| 73 | State GetState() |
| 74 | { |
| 75 | return m_state; |
| 76 | } |
| 77 | |
| 78 | // Used by debugger side (RS) to cleanup the target (LS) named pipes |
| 79 | // and semaphores when the debugger detects the debuggee process exited. |
| 80 | void CleanupTargetProcess(); |
| 81 | |
| 82 | private: |
| 83 | |
| 84 | State m_state; |
| 85 | |
| 86 | #ifdef FEATURE_PAL |
| 87 | |
| 88 | int m_inboundPipe, m_outboundPipe; // two one sided pipes used for communication |
| 89 | char m_inPipeName[MAX_DEBUGGER_TRANSPORT_PIPE_NAME_LENGTH]; // filename of the inbound pipe |
| 90 | char m_outPipeName[MAX_DEBUGGER_TRANSPORT_PIPE_NAME_LENGTH]; // filename of the outbound pipe |
| 91 | |
| 92 | #else |
| 93 | // Connects to a one sided pipe previously created by CreateOneWayPipe. |
| 94 | // In order to successfully connect id and inbound flag should be the same. |
| 95 | HANDLE OpenOneWayPipe(DWORD id, bool inbound); |
| 96 | |
| 97 | // Creates a one way pipe, id and inboud flag are used for naming. |
| 98 | // Created pipe is supposed to be connected to by OpenOneWayPipe. |
| 99 | HANDLE CreateOneWayPipe(DWORD id, bool inbound); |
| 100 | |
| 101 | HANDLE m_inboundPipe, m_outboundPipe; //two one sided pipes used for communication |
| 102 | #endif //FEATURE_PAL |
| 103 | }; |
| 104 | |
| 105 | #endif //TwoWayPipe_H |
| 106 | |