cmPipeConnection.cxx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 "cmPipeConnection.h"
  4. #include <algorithm>
  5. #include "cmServer.h"
  6. cmPipeConnection::cmPipeConnection(const std::string& name,
  7. cmConnectionBufferStrategy* bufferStrategy)
  8. : cmEventBasedConnection(bufferStrategy)
  9. , PipeName(name)
  10. {
  11. }
  12. void cmPipeConnection::Connect(uv_stream_t* server)
  13. {
  14. if (this->WriteStream.get()) {
  15. // Accept and close all pipes but the first:
  16. cm::uv_pipe_ptr rejectPipe;
  17. rejectPipe.init(*this->Server->GetLoop(), 0);
  18. uv_accept(server, rejectPipe);
  19. return;
  20. }
  21. cm::uv_pipe_ptr ClientPipe;
  22. ClientPipe.init(*this->Server->GetLoop(), 0,
  23. static_cast<cmEventBasedConnection*>(this));
  24. if (uv_accept(server, ClientPipe) != 0) {
  25. return;
  26. }
  27. uv_read_start(ClientPipe, on_alloc_buffer, on_read);
  28. WriteStream = std::move(ClientPipe);
  29. Server->OnConnected(this);
  30. }
  31. bool cmPipeConnection::OnServeStart(std::string* errorMessage)
  32. {
  33. this->ServerPipe.init(*this->Server->GetLoop(), 0,
  34. static_cast<cmEventBasedConnection*>(this));
  35. int r;
  36. if ((r = uv_pipe_bind(this->ServerPipe, this->PipeName.c_str())) != 0) {
  37. *errorMessage = std::string("Internal Error with ") + this->PipeName +
  38. ": " + uv_err_name(r);
  39. return false;
  40. }
  41. if ((r = uv_listen(this->ServerPipe, 1, on_new_connection)) != 0) {
  42. *errorMessage = std::string("Internal Error listening on ") +
  43. this->PipeName + ": " + uv_err_name(r);
  44. return false;
  45. }
  46. return cmConnection::OnServeStart(errorMessage);
  47. }
  48. bool cmPipeConnection::OnConnectionShuttingDown()
  49. {
  50. if (this->WriteStream.get()) {
  51. this->WriteStream->data = nullptr;
  52. }
  53. this->ServerPipe.reset();
  54. return cmEventBasedConnection::OnConnectionShuttingDown();
  55. }