cmServerConnection.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "cmConfigure.h"
  4. #include "cmServerConnection.h"
  5. #include <cm3p/uv.h>
  6. #include "cmServer.h"
  7. #include "cmServerDictionary.h"
  8. #ifdef _WIN32
  9. # include "io.h"
  10. #else
  11. # include <unistd.h>
  12. #endif
  13. #include <cassert>
  14. #include <utility>
  15. cmStdIoConnection::cmStdIoConnection(
  16. cmConnectionBufferStrategy* bufferStrategy)
  17. : cmEventBasedConnection(bufferStrategy)
  18. {
  19. }
  20. cm::uv_stream_ptr cmStdIoConnection::SetupStream(int file_id)
  21. {
  22. switch (uv_guess_handle(file_id)) {
  23. case UV_TTY: {
  24. cm::uv_tty_ptr tty;
  25. tty.init(*this->Server->GetLoop(), file_id, file_id == 0,
  26. static_cast<cmEventBasedConnection*>(this));
  27. uv_tty_set_mode(tty, UV_TTY_MODE_NORMAL);
  28. return { std::move(tty) };
  29. }
  30. case UV_FILE:
  31. if (file_id == 0) {
  32. return nullptr;
  33. }
  34. // Intentional fallthrough; stdin can _not_ be treated as a named
  35. // pipe, however stdout can be.
  36. CM_FALLTHROUGH;
  37. case UV_NAMED_PIPE: {
  38. cm::uv_pipe_ptr pipe;
  39. pipe.init(*this->Server->GetLoop(), 0,
  40. static_cast<cmEventBasedConnection*>(this));
  41. uv_pipe_open(pipe, file_id);
  42. return { std::move(pipe) };
  43. }
  44. default:
  45. assert(false && "Unable to determine stream type");
  46. return nullptr;
  47. }
  48. }
  49. void cmStdIoConnection::SetServer(cmServerBase* s)
  50. {
  51. cmConnection::SetServer(s);
  52. if (!s) {
  53. return;
  54. }
  55. this->ReadStream = SetupStream(0);
  56. this->WriteStream = SetupStream(1);
  57. }
  58. void shutdown_connection(uv_prepare_t* prepare)
  59. {
  60. cmStdIoConnection* connection =
  61. static_cast<cmStdIoConnection*>(prepare->data);
  62. if (!uv_is_closing(reinterpret_cast<uv_handle_t*>(prepare))) {
  63. uv_close(reinterpret_cast<uv_handle_t*>(prepare),
  64. &cmEventBasedConnection::on_close_delete<uv_prepare_t>);
  65. }
  66. connection->OnDisconnect(0);
  67. }
  68. bool cmStdIoConnection::OnServeStart(std::string* pString)
  69. {
  70. Server->OnConnected(this);
  71. if (this->ReadStream.get()) {
  72. uv_read_start(this->ReadStream, on_alloc_buffer, on_read);
  73. } else if (uv_guess_handle(0) == UV_FILE) {
  74. char buffer[1024];
  75. while (auto len = read(0, buffer, sizeof(buffer))) {
  76. ReadData(std::string(buffer, buffer + len));
  77. }
  78. // We can't start the disconnect from here, add a prepare hook to do that
  79. // for us
  80. auto prepare = new uv_prepare_t();
  81. prepare->data = this;
  82. uv_prepare_init(Server->GetLoop(), prepare);
  83. uv_prepare_start(prepare, shutdown_connection);
  84. }
  85. return cmConnection::OnServeStart(pString);
  86. }
  87. bool cmStdIoConnection::OnConnectionShuttingDown()
  88. {
  89. if (ReadStream.get()) {
  90. uv_read_stop(ReadStream);
  91. ReadStream->data = nullptr;
  92. }
  93. this->ReadStream.reset();
  94. cmEventBasedConnection::OnConnectionShuttingDown();
  95. return true;
  96. }
  97. cmServerPipeConnection::cmServerPipeConnection(const std::string& name)
  98. : cmPipeConnection(name, new cmServerBufferStrategy)
  99. {
  100. }
  101. cmServerStdIoConnection::cmServerStdIoConnection()
  102. : cmStdIoConnection(new cmServerBufferStrategy)
  103. {
  104. }
  105. cmConnectionBufferStrategy::~cmConnectionBufferStrategy() = default;
  106. void cmConnectionBufferStrategy::clear()
  107. {
  108. }
  109. std::string cmServerBufferStrategy::BufferOutMessage(
  110. const std::string& rawBuffer) const
  111. {
  112. return std::string("\n") + kSTART_MAGIC + std::string("\n") + rawBuffer +
  113. kEND_MAGIC + std::string("\n");
  114. }
  115. std::string cmServerBufferStrategy::BufferMessage(std::string& RawReadBuffer)
  116. {
  117. for (;;) {
  118. auto needle = RawReadBuffer.find('\n');
  119. if (needle == std::string::npos) {
  120. return "";
  121. }
  122. std::string line = RawReadBuffer.substr(0, needle);
  123. const auto ls = line.size();
  124. if (ls > 1 && line.at(ls - 1) == '\r') {
  125. line.erase(ls - 1, 1);
  126. }
  127. RawReadBuffer.erase(RawReadBuffer.begin(),
  128. RawReadBuffer.begin() + static_cast<long>(needle) + 1);
  129. if (line == kSTART_MAGIC) {
  130. RequestBuffer.clear();
  131. continue;
  132. }
  133. if (line == kEND_MAGIC) {
  134. std::string rtn;
  135. rtn.swap(this->RequestBuffer);
  136. return rtn;
  137. }
  138. this->RequestBuffer += line;
  139. this->RequestBuffer += "\n";
  140. }
  141. }