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 "cmServerConnection.h"
  4. #include "cmConfigure.h"
  5. #include "cmServer.h"
  6. #include "cmServerDictionary.h"
  7. #include "cm_uv.h"
  8. #include <algorithm>
  9. #ifdef _WIN32
  10. #include "io.h"
  11. #else
  12. #include <unistd.h>
  13. #endif
  14. #include <cassert>
  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()
  106. {
  107. }
  108. void cmConnectionBufferStrategy::clear()
  109. {
  110. }
  111. std::string cmServerBufferStrategy::BufferOutMessage(
  112. const std::string& rawBuffer) const
  113. {
  114. return std::string("\n") + kSTART_MAGIC + std::string("\n") + rawBuffer +
  115. kEND_MAGIC + std::string("\n");
  116. }
  117. std::string cmServerBufferStrategy::BufferMessage(std::string& RawReadBuffer)
  118. {
  119. for (;;) {
  120. auto needle = RawReadBuffer.find('\n');
  121. if (needle == std::string::npos) {
  122. return "";
  123. }
  124. std::string line = RawReadBuffer.substr(0, needle);
  125. const auto ls = line.size();
  126. if (ls > 1 && line.at(ls - 1) == '\r') {
  127. line.erase(ls - 1, 1);
  128. }
  129. RawReadBuffer.erase(RawReadBuffer.begin(),
  130. RawReadBuffer.begin() + static_cast<long>(needle) + 1);
  131. if (line == kSTART_MAGIC) {
  132. RequestBuffer.clear();
  133. continue;
  134. }
  135. if (line == kEND_MAGIC) {
  136. std::string rtn;
  137. rtn.swap(this->RequestBuffer);
  138. return rtn;
  139. }
  140. this->RequestBuffer += line;
  141. this->RequestBuffer += "\n";
  142. }
  143. }