cmServerConnection.cxx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #include "cmServerConnection.h"
  12. #include <cmServer.h>
  13. #include <assert.h>
  14. namespace {
  15. static const std::string kSTART_MAGIC = "[== CMake Server ==[";
  16. static const std::string kEND_MAGIC = "]== CMake Server ==]";
  17. struct write_req_t
  18. {
  19. uv_write_t req;
  20. uv_buf_t buf;
  21. };
  22. void on_alloc_buffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf)
  23. {
  24. (void)(handle);
  25. char* rawBuffer = new char[suggested_size];
  26. *buf = uv_buf_init(rawBuffer, static_cast<unsigned int>(suggested_size));
  27. }
  28. void on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf)
  29. {
  30. auto conn = reinterpret_cast<cmServerConnection*>(stream->data);
  31. if (nread >= 0) {
  32. conn->ReadData(std::string(buf->base, buf->base + nread));
  33. } else {
  34. conn->HandleEof();
  35. }
  36. delete[](buf->base);
  37. }
  38. void on_write(uv_write_t* req, int status)
  39. {
  40. (void)(status);
  41. auto conn = reinterpret_cast<cmServerConnection*>(req->data);
  42. // Free req and buffer
  43. write_req_t* wr = reinterpret_cast<write_req_t*>(req);
  44. delete[](wr->buf.base);
  45. delete wr;
  46. conn->ProcessNextRequest();
  47. }
  48. void on_new_connection(uv_stream_t* stream, int status)
  49. {
  50. (void)(status);
  51. auto conn = reinterpret_cast<cmServerConnection*>(stream->data);
  52. conn->Connect(stream);
  53. }
  54. } // namespace
  55. class LoopGuard
  56. {
  57. public:
  58. LoopGuard(cmServerConnection* connection)
  59. : Connection(connection)
  60. {
  61. Connection->mLoop = uv_default_loop();
  62. }
  63. ~LoopGuard()
  64. {
  65. uv_loop_close(Connection->mLoop);
  66. Connection->mLoop = nullptr;
  67. }
  68. private:
  69. cmServerConnection* Connection;
  70. };
  71. cmServerConnection::cmServerConnection()
  72. {
  73. }
  74. cmServerConnection::~cmServerConnection()
  75. {
  76. }
  77. void cmServerConnection::SetServer(cmServer* s)
  78. {
  79. this->Server = s;
  80. }
  81. bool cmServerConnection::ProcessEvents(std::string* errorMessage)
  82. {
  83. assert(this->Server);
  84. errorMessage->clear();
  85. this->RawReadBuffer.clear();
  86. this->RequestBuffer.clear();
  87. LoopGuard guard(this);
  88. (void)(guard);
  89. if (!this->mLoop) {
  90. *errorMessage = "Internal Error: Failed to create event loop.";
  91. return false;
  92. }
  93. if (!DoSetup(errorMessage)) {
  94. return false;
  95. }
  96. if (uv_run(this->mLoop, UV_RUN_DEFAULT) != 0) {
  97. *errorMessage = "Internal Error: Event loop stopped in unclean state.";
  98. return false;
  99. }
  100. // These need to be cleaned up by now:
  101. assert(!this->ReadStream);
  102. assert(!this->WriteStream);
  103. this->RawReadBuffer.clear();
  104. this->RequestBuffer.clear();
  105. return true;
  106. }
  107. void cmServerConnection::ReadData(const std::string& data)
  108. {
  109. this->RawReadBuffer += data;
  110. for (;;) {
  111. auto needle = this->RawReadBuffer.find('\n');
  112. if (needle == std::string::npos) {
  113. return;
  114. }
  115. std::string line = this->RawReadBuffer.substr(0, needle);
  116. const auto ls = line.size();
  117. if (ls > 1 && line.at(ls - 1) == '\r')
  118. line.erase(ls - 1, 1);
  119. this->RawReadBuffer.erase(this->RawReadBuffer.begin(),
  120. this->RawReadBuffer.begin() +
  121. static_cast<long>(needle) + 1);
  122. if (line == kSTART_MAGIC) {
  123. this->RequestBuffer.clear();
  124. continue;
  125. }
  126. if (line == kEND_MAGIC) {
  127. this->Server->QueueRequest(this->RequestBuffer);
  128. this->RequestBuffer.clear();
  129. } else {
  130. this->RequestBuffer += line;
  131. this->RequestBuffer += "\n";
  132. }
  133. }
  134. }
  135. void cmServerConnection::HandleEof()
  136. {
  137. this->TearDown();
  138. }
  139. void cmServerConnection::WriteData(const std::string& data)
  140. {
  141. assert(this->WriteStream);
  142. auto ds = data.size();
  143. write_req_t* req = new write_req_t;
  144. req->req.data = this;
  145. req->buf = uv_buf_init(new char[ds], static_cast<unsigned int>(ds));
  146. memcpy(req->buf.base, data.c_str(), ds);
  147. uv_write(reinterpret_cast<uv_write_t*>(req),
  148. static_cast<uv_stream_t*>(this->WriteStream), &req->buf, 1,
  149. on_write);
  150. }
  151. void cmServerConnection::ProcessNextRequest()
  152. {
  153. Server->PopOne();
  154. }
  155. void cmServerConnection::SendGreetings()
  156. {
  157. Server->PrintHello();
  158. }
  159. bool cmServerStdIoConnection::DoSetup(std::string* errorMessage)
  160. {
  161. (void)(errorMessage);
  162. if (uv_guess_handle(1) == UV_TTY) {
  163. uv_tty_init(this->Loop(), &this->Input.tty, 0, 1);
  164. uv_tty_set_mode(&this->Input.tty, UV_TTY_MODE_NORMAL);
  165. Input.tty.data = this;
  166. this->ReadStream = reinterpret_cast<uv_stream_t*>(&this->Input.tty);
  167. uv_tty_init(this->Loop(), &this->Output.tty, 1, 0);
  168. uv_tty_set_mode(&this->Output.tty, UV_TTY_MODE_NORMAL);
  169. Output.tty.data = this;
  170. this->WriteStream = reinterpret_cast<uv_stream_t*>(&this->Output.tty);
  171. } else {
  172. uv_pipe_init(this->Loop(), &this->Input.pipe, 0);
  173. uv_pipe_open(&this->Input.pipe, 0);
  174. Input.pipe.data = this;
  175. this->ReadStream = reinterpret_cast<uv_stream_t*>(&this->Input.pipe);
  176. uv_pipe_init(this->Loop(), &this->Output.pipe, 0);
  177. uv_pipe_open(&this->Output.pipe, 1);
  178. Output.pipe.data = this;
  179. this->WriteStream = reinterpret_cast<uv_stream_t*>(&this->Output.pipe);
  180. }
  181. SendGreetings();
  182. uv_read_start(this->ReadStream, on_alloc_buffer, on_read);
  183. return true;
  184. }
  185. void cmServerStdIoConnection::TearDown()
  186. {
  187. uv_close(reinterpret_cast<uv_handle_t*>(this->ReadStream), nullptr);
  188. this->ReadStream = nullptr;
  189. uv_close(reinterpret_cast<uv_handle_t*>(this->WriteStream), nullptr);
  190. this->WriteStream = nullptr;
  191. }
  192. cmServerPipeConnection::cmServerPipeConnection(const std::string& name)
  193. : PipeName(name)
  194. {
  195. this->ServerPipe.data = nullptr;
  196. this->ClientPipe.data = nullptr;
  197. }
  198. bool cmServerPipeConnection::DoSetup(std::string* errorMessage)
  199. {
  200. uv_pipe_init(this->Loop(), &this->ServerPipe, 0);
  201. this->ServerPipe.data = this;
  202. int r;
  203. if ((r = uv_pipe_bind(&this->ServerPipe, this->PipeName.c_str())) != 0) {
  204. *errorMessage = std::string("Internal Error with ") + this->PipeName +
  205. ": " + uv_err_name(r);
  206. return false;
  207. }
  208. auto serverStream = reinterpret_cast<uv_stream_t*>(&this->ServerPipe);
  209. serverStream->data = this;
  210. if ((r = uv_listen(serverStream, 1, on_new_connection)) != 0) {
  211. *errorMessage = std::string("Internal Error with ") + this->PipeName +
  212. ": " + uv_err_name(r);
  213. return false;
  214. }
  215. return true;
  216. }
  217. void cmServerPipeConnection::TearDown()
  218. {
  219. if (this->WriteStream->data) {
  220. uv_close(reinterpret_cast<uv_handle_t*>(this->WriteStream), nullptr);
  221. this->WriteStream->data = nullptr;
  222. }
  223. uv_close(reinterpret_cast<uv_handle_t*>(&this->ServerPipe), nullptr);
  224. this->WriteStream = nullptr;
  225. this->ReadStream = nullptr;
  226. }
  227. void cmServerPipeConnection::Connect(uv_stream_t* server)
  228. {
  229. if (this->ClientPipe.data == this) {
  230. // Accept and close all pipes but the first:
  231. uv_pipe_t rejectPipe;
  232. uv_pipe_init(this->Loop(), &rejectPipe, 0);
  233. auto rejecter = reinterpret_cast<uv_stream_t*>(&rejectPipe);
  234. uv_accept(server, rejecter);
  235. uv_close(reinterpret_cast<uv_handle_t*>(rejecter), nullptr);
  236. return;
  237. }
  238. uv_pipe_init(this->Loop(), &this->ClientPipe, 0);
  239. this->ClientPipe.data = this;
  240. auto client = reinterpret_cast<uv_stream_t*>(&this->ClientPipe);
  241. if (uv_accept(server, client) != 0) {
  242. uv_close(reinterpret_cast<uv_handle_t*>(client), nullptr);
  243. return;
  244. }
  245. this->ReadStream = client;
  246. this->WriteStream = client;
  247. uv_read_start(this->ReadStream, on_alloc_buffer, on_read);
  248. this->SendGreetings();
  249. }