cmDebuggerWindowsPipeConnection.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <condition_variable>
  6. #include <cstddef>
  7. #include <future>
  8. #include <memory>
  9. #include <mutex>
  10. #include <string>
  11. #include <thread>
  12. #include <windows.h>
  13. #include <cm3p/cppdap/io.h>
  14. #include "cmDebuggerAdapter.h"
  15. namespace cmDebugger {
  16. #ifdef _WIN32
  17. class DuplexPipe_WIN32
  18. {
  19. public:
  20. DuplexPipe_WIN32(HANDLE read);
  21. ~DuplexPipe_WIN32();
  22. void close();
  23. size_t read(void* buffer, size_t n);
  24. bool write(void const* buffer, size_t n);
  25. bool WaitForConnection();
  26. private:
  27. HANDLE hPipe;
  28. OVERLAPPED readOp;
  29. OVERLAPPED writeOp;
  30. };
  31. class cmDebuggerPipeConnection_WIN32
  32. : public dap::ReaderWriter
  33. , public cmDebuggerConnection
  34. , public std::enable_shared_from_this<cmDebuggerPipeConnection_WIN32>
  35. {
  36. public:
  37. cmDebuggerPipeConnection_WIN32(std::string name);
  38. ~cmDebuggerPipeConnection_WIN32() override;
  39. void WaitForConnection() override;
  40. bool StartListening(std::string& errorMessage) override;
  41. std::shared_ptr<dap::Reader> GetReader() override;
  42. std::shared_ptr<dap::Writer> GetWriter() override;
  43. // dap::ReaderWriter implementation
  44. bool isOpen() override;
  45. void close() override;
  46. size_t read(void* buffer, size_t n) override;
  47. bool write(void const* buffer, size_t n) override;
  48. // Used for unit test synchronization
  49. std::promise<void> StartedListening;
  50. private:
  51. void CloseConnection();
  52. std::string GetErrorMessage(DWORD errorCode);
  53. std::string const PipeName;
  54. std::unique_ptr<DuplexPipe_WIN32> pipes;
  55. };
  56. using cmDebuggerPipeConnection = cmDebuggerPipeConnection_WIN32;
  57. class cmDebuggerPipeClient_WIN32
  58. : public dap::ReaderWriter
  59. , public std::enable_shared_from_this<cmDebuggerPipeClient_WIN32>
  60. {
  61. public:
  62. cmDebuggerPipeClient_WIN32(std::string name);
  63. ~cmDebuggerPipeClient_WIN32();
  64. void WaitForConnection();
  65. bool isOpen() override;
  66. void close() override;
  67. size_t read(void* buffer, size_t n) override;
  68. bool write(void const* buffer, size_t n) override;
  69. private:
  70. std::string const PipeName;
  71. std::unique_ptr<DuplexPipe_WIN32> pipes;
  72. };
  73. using cmDebuggerPipeClient = cmDebuggerPipeClient_WIN32;
  74. #endif // _WIN32
  75. } // namespace cmDebugger