cmake/Source/cmPipeConnection.cxx

72 lines
1.9 KiB
C++
Raw Normal View History

2018-01-26 17:06:56 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmPipeConnection.h"
2019-11-11 23:01:05 +01:00
#include <utility>
2018-04-23 21:13:27 +02:00
2018-01-26 17:06:56 +01:00
#include "cmServer.h"
2019-11-11 23:01:05 +01:00
cmPipeConnection::cmPipeConnection(std::string name,
2018-01-26 17:06:56 +01:00
cmConnectionBufferStrategy* bufferStrategy)
: cmEventBasedConnection(bufferStrategy)
2019-11-11 23:01:05 +01:00
, PipeName(std::move(name))
2018-01-26 17:06:56 +01:00
{
}
void cmPipeConnection::Connect(uv_stream_t* server)
{
2018-04-23 21:13:27 +02:00
if (this->WriteStream.get()) {
2018-01-26 17:06:56 +01:00
// Accept and close all pipes but the first:
2018-04-23 21:13:27 +02:00
cm::uv_pipe_ptr rejectPipe;
rejectPipe.init(*this->Server->GetLoop(), 0);
uv_accept(server, rejectPipe);
2018-01-26 17:06:56 +01:00
return;
}
2018-04-23 21:13:27 +02:00
cm::uv_pipe_ptr ClientPipe;
ClientPipe.init(*this->Server->GetLoop(), 0,
static_cast<cmEventBasedConnection*>(this));
if (uv_accept(server, ClientPipe) != 0) {
2018-01-26 17:06:56 +01:00
return;
}
2018-04-23 21:13:27 +02:00
uv_read_start(ClientPipe, on_alloc_buffer, on_read);
WriteStream = std::move(ClientPipe);
2018-01-26 17:06:56 +01:00
Server->OnConnected(this);
}
bool cmPipeConnection::OnServeStart(std::string* errorMessage)
{
2018-04-23 21:13:27 +02:00
this->ServerPipe.init(*this->Server->GetLoop(), 0,
static_cast<cmEventBasedConnection*>(this));
2018-01-26 17:06:56 +01:00
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;
}
2018-04-23 21:13:27 +02:00
if ((r = uv_listen(this->ServerPipe, 1, on_new_connection)) != 0) {
2018-01-26 17:06:56 +01:00
*errorMessage = std::string("Internal Error listening on ") +
this->PipeName + ": " + uv_err_name(r);
return false;
}
return cmConnection::OnServeStart(errorMessage);
}
bool cmPipeConnection::OnConnectionShuttingDown()
{
2018-04-23 21:13:27 +02:00
if (this->WriteStream.get()) {
2018-01-26 17:06:56 +01:00
this->WriteStream->data = nullptr;
}
2018-04-23 21:13:27 +02:00
this->ServerPipe.reset();
2018-01-26 17:06:56 +01:00
return cmEventBasedConnection::OnConnectionShuttingDown();
}