cmServerConnection.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmServerConnection.h"
  4. #include "cmServer.h"
  5. #include "cmServerDictionary.h"
  6. cmStdIoConnection::cmStdIoConnection(
  7. cmConnectionBufferStrategy* bufferStrategy)
  8. : cmConnection(bufferStrategy)
  9. , Input()
  10. , Output()
  11. {
  12. }
  13. void cmStdIoConnection::SetServer(cmServerBase* s)
  14. {
  15. cmConnection::SetServer(s);
  16. if (uv_guess_handle(1) == UV_TTY) {
  17. usesTty = true;
  18. this->Input.tty = new uv_tty_t();
  19. uv_tty_init(this->Server->GetLoop(), this->Input.tty, 0, 1);
  20. uv_tty_set_mode(this->Input.tty, UV_TTY_MODE_NORMAL);
  21. this->Input.tty->data = static_cast<cmConnection*>(this);
  22. this->ReadStream = reinterpret_cast<uv_stream_t*>(this->Input.tty);
  23. this->Output.tty = new uv_tty_t();
  24. uv_tty_init(this->Server->GetLoop(), this->Output.tty, 1, 0);
  25. uv_tty_set_mode(this->Output.tty, UV_TTY_MODE_NORMAL);
  26. this->Output.tty->data = static_cast<cmConnection*>(this);
  27. this->WriteStream = reinterpret_cast<uv_stream_t*>(this->Output.tty);
  28. } else {
  29. usesTty = false;
  30. this->Input.pipe = new uv_pipe_t();
  31. uv_pipe_init(this->Server->GetLoop(), this->Input.pipe, 0);
  32. uv_pipe_open(this->Input.pipe, 0);
  33. this->Input.pipe->data = static_cast<cmConnection*>(this);
  34. this->ReadStream = reinterpret_cast<uv_stream_t*>(this->Input.pipe);
  35. this->Output.pipe = new uv_pipe_t();
  36. uv_pipe_init(this->Server->GetLoop(), this->Output.pipe, 0);
  37. uv_pipe_open(this->Output.pipe, 1);
  38. this->Output.pipe->data = static_cast<cmConnection*>(this);
  39. this->WriteStream = reinterpret_cast<uv_stream_t*>(this->Output.pipe);
  40. }
  41. }
  42. bool cmStdIoConnection::OnServeStart(std::string* pString)
  43. {
  44. uv_read_start(this->ReadStream, on_alloc_buffer, on_read);
  45. Server->OnConnected(this);
  46. return cmConnection::OnServeStart(pString);
  47. }
  48. bool cmStdIoConnection::OnServerShuttingDown()
  49. {
  50. cmConnection::OnServerShuttingDown();
  51. if (usesTty) {
  52. uv_read_stop(reinterpret_cast<uv_stream_t*>(this->Input.tty));
  53. uv_close(reinterpret_cast<uv_handle_t*>(this->Input.tty),
  54. &on_close_delete);
  55. uv_close(reinterpret_cast<uv_handle_t*>(this->Output.tty),
  56. &on_close_delete);
  57. } else {
  58. uv_close(reinterpret_cast<uv_handle_t*>(this->Input.pipe),
  59. &on_close_delete);
  60. uv_close(reinterpret_cast<uv_handle_t*>(this->Output.pipe),
  61. &on_close_delete);
  62. }
  63. return true;
  64. }
  65. cmServerPipeConnection::cmServerPipeConnection(const std::string& name)
  66. : cmPipeConnection(name, new cmServerBufferStrategy)
  67. {
  68. }
  69. cmServerStdIoConnection::cmServerStdIoConnection()
  70. : cmStdIoConnection(new cmServerBufferStrategy)
  71. {
  72. }
  73. cmConnectionBufferStrategy::~cmConnectionBufferStrategy()
  74. {
  75. }
  76. void cmConnectionBufferStrategy::clear()
  77. {
  78. }
  79. std::string cmServerBufferStrategy::BufferMessage(std::string& RawReadBuffer)
  80. {
  81. for (;;) {
  82. auto needle = RawReadBuffer.find('\n');
  83. if (needle == std::string::npos) {
  84. return "";
  85. }
  86. std::string line = RawReadBuffer.substr(0, needle);
  87. const auto ls = line.size();
  88. if (ls > 1 && line.at(ls - 1) == '\r') {
  89. line.erase(ls - 1, 1);
  90. }
  91. RawReadBuffer.erase(RawReadBuffer.begin(),
  92. RawReadBuffer.begin() + static_cast<long>(needle) + 1);
  93. if (line == kSTART_MAGIC) {
  94. RequestBuffer.clear();
  95. continue;
  96. }
  97. if (line == kEND_MAGIC) {
  98. std::string rtn;
  99. rtn.swap(this->RequestBuffer);
  100. return rtn;
  101. }
  102. this->RequestBuffer += line;
  103. this->RequestBuffer += "\n";
  104. }
  105. }