cmake/Source/cmServerConnection.cxx

166 lines
4.1 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2020-02-01 23:06:01 +01:00
#include "cmConfigure.h"
2016-10-30 18:24:19 +01:00
#include "cmServerConnection.h"
2020-08-30 11:54:41 +02:00
#include <cm3p/uv.h>
2020-02-01 23:06:01 +01:00
2016-10-30 18:24:19 +01:00
#include "cmServer.h"
2017-07-20 19:35:53 +02:00
#include "cmServerDictionary.h"
2018-04-23 21:13:27 +02:00
2018-01-26 17:06:56 +01:00
#ifdef _WIN32
2018-08-09 18:06:22 +02:00
# include "io.h"
2018-01-26 17:06:56 +01:00
#else
2018-08-09 18:06:22 +02:00
# include <unistd.h>
2018-01-26 17:06:56 +01:00
#endif
#include <cassert>
2019-11-11 23:01:05 +01:00
#include <utility>
2016-10-30 18:24:19 +01:00
2018-01-26 17:06:56 +01:00
cmStdIoConnection::cmStdIoConnection(
cmConnectionBufferStrategy* bufferStrategy)
: cmEventBasedConnection(bufferStrategy)
2016-10-30 18:24:19 +01:00
{
}
2018-04-23 21:13:27 +02:00
cm::uv_stream_ptr cmStdIoConnection::SetupStream(int file_id)
2016-10-30 18:24:19 +01:00
{
2018-01-26 17:06:56 +01:00
switch (uv_guess_handle(file_id)) {
case UV_TTY: {
2018-04-23 21:13:27 +02:00
cm::uv_tty_ptr tty;
tty.init(*this->Server->GetLoop(), file_id, file_id == 0,
static_cast<cmEventBasedConnection*>(this));
2018-01-26 17:06:56 +01:00
uv_tty_set_mode(tty, UV_TTY_MODE_NORMAL);
2019-11-11 23:01:05 +01:00
return { std::move(tty) };
2018-01-26 17:06:56 +01:00
}
case UV_FILE:
if (file_id == 0) {
2018-04-23 21:13:27 +02:00
return nullptr;
2018-01-26 17:06:56 +01:00
}
// Intentional fallthrough; stdin can _not_ be treated as a named
// pipe, however stdout can be.
CM_FALLTHROUGH;
case UV_NAMED_PIPE: {
2018-04-23 21:13:27 +02:00
cm::uv_pipe_ptr pipe;
pipe.init(*this->Server->GetLoop(), 0,
static_cast<cmEventBasedConnection*>(this));
2018-01-26 17:06:56 +01:00
uv_pipe_open(pipe, file_id);
2019-11-11 23:01:05 +01:00
return { std::move(pipe) };
2018-01-26 17:06:56 +01:00
}
default:
assert(false && "Unable to determine stream type");
2018-04-23 21:13:27 +02:00
return nullptr;
2016-10-30 18:24:19 +01:00
}
}
2018-01-26 17:06:56 +01:00
void cmStdIoConnection::SetServer(cmServerBase* s)
2016-10-30 18:24:19 +01:00
{
2018-01-26 17:06:56 +01:00
cmConnection::SetServer(s);
if (!s) {
return;
}
2016-10-30 18:24:19 +01:00
2018-04-23 21:13:27 +02:00
this->ReadStream = SetupStream(0);
this->WriteStream = SetupStream(1);
2016-10-30 18:24:19 +01:00
}
2018-01-26 17:06:56 +01:00
void shutdown_connection(uv_prepare_t* prepare)
2016-10-30 18:24:19 +01:00
{
2018-01-26 17:06:56 +01:00
cmStdIoConnection* connection =
2018-04-23 21:13:27 +02:00
static_cast<cmStdIoConnection*>(prepare->data);
2016-10-30 18:24:19 +01:00
2018-01-26 17:06:56 +01:00
if (!uv_is_closing(reinterpret_cast<uv_handle_t*>(prepare))) {
uv_close(reinterpret_cast<uv_handle_t*>(prepare),
&cmEventBasedConnection::on_close_delete<uv_prepare_t>);
}
connection->OnDisconnect(0);
2016-10-30 18:24:19 +01:00
}
2018-01-26 17:06:56 +01:00
bool cmStdIoConnection::OnServeStart(std::string* pString)
2016-10-30 18:24:19 +01:00
{
2018-01-26 17:06:56 +01:00
Server->OnConnected(this);
2018-04-23 21:13:27 +02:00
if (this->ReadStream.get()) {
2018-01-26 17:06:56 +01:00
uv_read_start(this->ReadStream, on_alloc_buffer, on_read);
} else if (uv_guess_handle(0) == UV_FILE) {
char buffer[1024];
while (auto len = read(0, buffer, sizeof(buffer))) {
ReadData(std::string(buffer, buffer + len));
}
2016-10-30 18:24:19 +01:00
2018-01-26 17:06:56 +01:00
// We can't start the disconnect from here, add a prepare hook to do that
// for us
auto prepare = new uv_prepare_t();
prepare->data = this;
uv_prepare_init(Server->GetLoop(), prepare);
uv_prepare_start(prepare, shutdown_connection);
}
return cmConnection::OnServeStart(pString);
2016-10-30 18:24:19 +01:00
}
2018-01-26 17:06:56 +01:00
bool cmStdIoConnection::OnConnectionShuttingDown()
2016-10-30 18:24:19 +01:00
{
2018-04-23 21:13:27 +02:00
if (ReadStream.get()) {
2018-01-26 17:06:56 +01:00
uv_read_stop(ReadStream);
2018-04-23 21:13:27 +02:00
ReadStream->data = nullptr;
2016-10-30 18:24:19 +01:00
}
2018-04-23 21:13:27 +02:00
this->ReadStream.reset();
2016-10-30 18:24:19 +01:00
2018-01-26 17:06:56 +01:00
cmEventBasedConnection::OnConnectionShuttingDown();
2016-10-30 18:24:19 +01:00
2018-01-26 17:06:56 +01:00
return true;
}
2016-10-30 18:24:19 +01:00
2018-01-26 17:06:56 +01:00
cmServerPipeConnection::cmServerPipeConnection(const std::string& name)
: cmPipeConnection(name, new cmServerBufferStrategy)
2016-10-30 18:24:19 +01:00
{
}
2018-01-26 17:06:56 +01:00
cmServerStdIoConnection::cmServerStdIoConnection()
: cmStdIoConnection(new cmServerBufferStrategy)
2016-10-30 18:24:19 +01:00
{
}
2019-11-11 23:01:05 +01:00
cmConnectionBufferStrategy::~cmConnectionBufferStrategy() = default;
2016-10-30 18:24:19 +01:00
2018-01-26 17:06:56 +01:00
void cmConnectionBufferStrategy::clear()
2016-10-30 18:24:19 +01:00
{
}
2018-01-26 17:06:56 +01:00
std::string cmServerBufferStrategy::BufferOutMessage(
const std::string& rawBuffer) const
2016-10-30 18:24:19 +01:00
{
2018-01-26 17:06:56 +01:00
return std::string("\n") + kSTART_MAGIC + std::string("\n") + rawBuffer +
kEND_MAGIC + std::string("\n");
}
2016-10-30 18:24:19 +01:00
2018-01-26 17:06:56 +01:00
std::string cmServerBufferStrategy::BufferMessage(std::string& RawReadBuffer)
{
2016-10-30 18:24:19 +01:00
for (;;) {
2018-01-26 17:06:56 +01:00
auto needle = RawReadBuffer.find('\n');
2016-10-30 18:24:19 +01:00
if (needle == std::string::npos) {
2018-01-26 17:06:56 +01:00
return "";
2016-10-30 18:24:19 +01:00
}
2018-01-26 17:06:56 +01:00
std::string line = RawReadBuffer.substr(0, needle);
2016-10-30 18:24:19 +01:00
const auto ls = line.size();
if (ls > 1 && line.at(ls - 1) == '\r') {
line.erase(ls - 1, 1);
}
2018-01-26 17:06:56 +01:00
RawReadBuffer.erase(RawReadBuffer.begin(),
RawReadBuffer.begin() + static_cast<long>(needle) + 1);
2016-10-30 18:24:19 +01:00
if (line == kSTART_MAGIC) {
2018-01-26 17:06:56 +01:00
RequestBuffer.clear();
2016-10-30 18:24:19 +01:00
continue;
}
if (line == kEND_MAGIC) {
2018-01-26 17:06:56 +01:00
std::string rtn;
rtn.swap(this->RequestBuffer);
return rtn;
2016-10-30 18:24:19 +01:00
}
2018-01-26 17:06:56 +01:00
this->RequestBuffer += line;
this->RequestBuffer += "\n";
2016-10-30 18:24:19 +01:00
}
}