cmPipeConnection.cxx 1.87 KB
Newer Older
1 2 3 4
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#include "cmPipeConnection.h"

5
#include <utility>
6

7 8
#include "cmServer.h"

wahikihiki's avatar
wahikihiki committed
9
cmPipeConnection::cmPipeConnection(std::string name,
10
                                   cmConnectionBufferStrategy* bufferStrategy)
11
  : cmEventBasedConnection(bufferStrategy)
wahikihiki's avatar
wahikihiki committed
12
  , PipeName(std::move(name))
13 14 15 16 17
{
}

void cmPipeConnection::Connect(uv_stream_t* server)
{
18
  if (this->WriteStream.get()) {
19
    // Accept and close all pipes but the first:
20 21 22 23
    cm::uv_pipe_ptr rejectPipe;

    rejectPipe.init(*this->Server->GetLoop(), 0);
    uv_accept(server, rejectPipe);
24 25 26 27

    return;
  }

28 29 30 31 32
  cm::uv_pipe_ptr ClientPipe;
  ClientPipe.init(*this->Server->GetLoop(), 0,
                  static_cast<cmEventBasedConnection*>(this));

  if (uv_accept(server, ClientPipe) != 0) {
33 34 35
    return;
  }

36 37
  uv_read_start(ClientPipe, on_alloc_buffer, on_read);
  WriteStream = std::move(ClientPipe);
38 39 40 41 42
  Server->OnConnected(this);
}

bool cmPipeConnection::OnServeStart(std::string* errorMessage)
{
43 44
  this->ServerPipe.init(*this->Server->GetLoop(), 0,
                        static_cast<cmEventBasedConnection*>(this));
45 46 47 48 49 50 51

  int r;
  if ((r = uv_pipe_bind(this->ServerPipe, this->PipeName.c_str())) != 0) {
    *errorMessage = std::string("Internal Error with ") + this->PipeName +
      ": " + uv_err_name(r);
    return false;
  }
52 53

  if ((r = uv_listen(this->ServerPipe, 1, on_new_connection)) != 0) {
54 55 56 57 58 59 60 61
    *errorMessage = std::string("Internal Error listening on ") +
      this->PipeName + ": " + uv_err_name(r);
    return false;
  }

  return cmConnection::OnServeStart(errorMessage);
}

62
bool cmPipeConnection::OnConnectionShuttingDown()
63
{
64
  if (this->WriteStream.get()) {
65 66 67
    this->WriteStream->data = nullptr;
  }

68
  this->ServerPipe.reset();
69

70
  return cmEventBasedConnection::OnConnectionShuttingDown();
71
}