cmServerConnection.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2015 Stephen Kelly <[email protected]>
  4. Copyright 2016 Tobias Hunger <[email protected]>
  5. Distributed under the OSI-approved BSD License (the "License");
  6. see accompanying file Copyright.txt for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even the
  8. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. See the License for more information.
  10. ============================================================================*/
  11. #pragma once
  12. #include <string>
  13. #include <vector>
  14. #if defined(CMAKE_BUILD_WITH_CMAKE)
  15. #include "cm_uv.h"
  16. #endif
  17. class cmServer;
  18. class LoopGuard;
  19. class cmServerConnection
  20. {
  21. public:
  22. cmServerConnection();
  23. virtual ~cmServerConnection();
  24. void SetServer(cmServer* s);
  25. bool ProcessEvents(std::string* errorMessage);
  26. void ReadData(const std::string& data);
  27. void HandleEof();
  28. void WriteData(const std::string& data);
  29. void ProcessNextRequest();
  30. virtual void Connect(uv_stream_t* server) { (void)(server); }
  31. protected:
  32. virtual bool DoSetup(std::string* errorMessage) = 0;
  33. virtual void TearDown() = 0;
  34. void SendGreetings();
  35. uv_loop_t* Loop() const { return mLoop; }
  36. protected:
  37. std::string RawReadBuffer;
  38. std::string RequestBuffer;
  39. uv_stream_t* ReadStream = nullptr;
  40. uv_stream_t* WriteStream = nullptr;
  41. private:
  42. uv_loop_t* mLoop = nullptr;
  43. cmServer* Server = nullptr;
  44. friend class LoopGuard;
  45. };
  46. class cmServerStdIoConnection : public cmServerConnection
  47. {
  48. public:
  49. bool DoSetup(std::string* errorMessage) override;
  50. void TearDown() override;
  51. private:
  52. typedef union
  53. {
  54. uv_tty_t tty;
  55. uv_pipe_t pipe;
  56. } InOutUnion;
  57. InOutUnion Input;
  58. InOutUnion Output;
  59. };
  60. class cmServerPipeConnection : public cmServerConnection
  61. {
  62. public:
  63. cmServerPipeConnection(const std::string& name);
  64. bool DoSetup(std::string* errorMessage) override;
  65. void TearDown() override;
  66. void Connect(uv_stream_t* server) override;
  67. private:
  68. const std::string PipeName;
  69. uv_pipe_t ServerPipe;
  70. uv_pipe_t ClientPipe;
  71. };