2016-09-27 19:01:08 +00:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2019-09-30 14:46:28 +00:00
|
|
|
#include "cmConfigure.h"
|
|
|
|
|
2016-09-09 08:01:44 +00:00
|
|
|
#include "cmServerConnection.h"
|
|
|
|
|
2019-09-30 14:46:28 +00:00
|
|
|
#include "cm_uv.h"
|
|
|
|
|
2016-09-09 08:01:46 +00:00
|
|
|
#include "cmServer.h"
|
2017-05-05 21:21:39 +00:00
|
|
|
#include "cmServerDictionary.h"
|
2017-07-23 18:31:13 +00:00
|
|
|
|
2017-07-21 15:35:41 +00:00
|
|
|
#ifdef _WIN32
|
2018-06-01 13:53:41 +00:00
|
|
|
# include "io.h"
|
2017-07-21 15:35:41 +00:00
|
|
|
#else
|
2018-06-01 13:53:41 +00:00
|
|
|
# include <unistd.h>
|
2017-07-21 15:35:41 +00:00
|
|
|
#endif
|
|
|
|
#include <cassert>
|
2019-01-15 14:59:46 +00:00
|
|
|
#include <utility>
|
2016-09-09 08:01:44 +00:00
|
|
|
|
2017-03-25 03:38:52 +00:00
|
|
|
cmStdIoConnection::cmStdIoConnection(
|
|
|
|
cmConnectionBufferStrategy* bufferStrategy)
|
2017-02-25 22:06:34 +00:00
|
|
|
: cmEventBasedConnection(bufferStrategy)
|
2016-09-09 08:01:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-07-23 18:31:13 +00:00
|
|
|
cm::uv_stream_ptr cmStdIoConnection::SetupStream(int file_id)
|
2017-07-21 15:35:41 +00:00
|
|
|
{
|
|
|
|
switch (uv_guess_handle(file_id)) {
|
|
|
|
case UV_TTY: {
|
2017-07-23 18:31:13 +00:00
|
|
|
cm::uv_tty_ptr tty;
|
|
|
|
tty.init(*this->Server->GetLoop(), file_id, file_id == 0,
|
|
|
|
static_cast<cmEventBasedConnection*>(this));
|
2017-07-21 15:35:41 +00:00
|
|
|
uv_tty_set_mode(tty, UV_TTY_MODE_NORMAL);
|
2019-02-20 23:33:51 +00:00
|
|
|
return { std::move(tty) };
|
2017-07-21 15:35:41 +00:00
|
|
|
}
|
|
|
|
case UV_FILE:
|
|
|
|
if (file_id == 0) {
|
2017-07-23 18:31:13 +00:00
|
|
|
return nullptr;
|
2017-07-21 15:35:41 +00:00
|
|
|
}
|
|
|
|
// Intentional fallthrough; stdin can _not_ be treated as a named
|
|
|
|
// pipe, however stdout can be.
|
|
|
|
CM_FALLTHROUGH;
|
|
|
|
case UV_NAMED_PIPE: {
|
2017-07-23 18:31:13 +00:00
|
|
|
cm::uv_pipe_ptr pipe;
|
|
|
|
pipe.init(*this->Server->GetLoop(), 0,
|
|
|
|
static_cast<cmEventBasedConnection*>(this));
|
2017-07-21 15:35:41 +00:00
|
|
|
uv_pipe_open(pipe, file_id);
|
2019-02-20 23:33:51 +00:00
|
|
|
return { std::move(pipe) };
|
2017-07-21 15:35:41 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
assert(false && "Unable to determine stream type");
|
2017-07-23 18:31:13 +00:00
|
|
|
return nullptr;
|
2017-07-21 15:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 03:38:52 +00:00
|
|
|
void cmStdIoConnection::SetServer(cmServerBase* s)
|
2016-09-09 08:01:44 +00:00
|
|
|
{
|
2017-03-25 03:38:52 +00:00
|
|
|
cmConnection::SetServer(s);
|
2017-07-21 03:18:41 +00:00
|
|
|
if (!s) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-09 08:01:44 +00:00
|
|
|
|
2017-07-23 18:31:13 +00:00
|
|
|
this->ReadStream = SetupStream(0);
|
|
|
|
this->WriteStream = SetupStream(1);
|
2017-07-21 15:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void shutdown_connection(uv_prepare_t* prepare)
|
|
|
|
{
|
|
|
|
cmStdIoConnection* connection =
|
2017-07-23 18:31:13 +00:00
|
|
|
static_cast<cmStdIoConnection*>(prepare->data);
|
2017-07-21 15:35:41 +00: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>);
|
2017-03-25 03:38:52 +00:00
|
|
|
}
|
2017-07-21 15:35:41 +00:00
|
|
|
connection->OnDisconnect(0);
|
2016-10-04 11:28:38 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 03:38:52 +00:00
|
|
|
bool cmStdIoConnection::OnServeStart(std::string* pString)
|
2016-10-04 11:28:38 +00:00
|
|
|
{
|
2017-03-25 03:38:52 +00:00
|
|
|
Server->OnConnected(this);
|
2017-07-23 18:31:13 +00:00
|
|
|
if (this->ReadStream.get()) {
|
2017-07-21 15:35:41 +00: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));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2017-03-25 03:38:52 +00:00
|
|
|
return cmConnection::OnServeStart(pString);
|
2016-10-04 11:28:38 +00:00
|
|
|
}
|
|
|
|
|
2017-07-21 15:35:41 +00:00
|
|
|
bool cmStdIoConnection::OnConnectionShuttingDown()
|
|
|
|
{
|
2017-07-23 18:31:13 +00:00
|
|
|
if (ReadStream.get()) {
|
2017-07-21 15:35:41 +00:00
|
|
|
uv_read_stop(ReadStream);
|
2017-07-23 18:31:13 +00:00
|
|
|
ReadStream->data = nullptr;
|
2016-09-09 08:01:44 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 18:31:13 +00:00
|
|
|
this->ReadStream.reset();
|
2017-07-21 15:35:41 +00:00
|
|
|
|
|
|
|
cmEventBasedConnection::OnConnectionShuttingDown();
|
|
|
|
|
2017-03-25 03:38:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-09-09 08:01:44 +00:00
|
|
|
|
2017-03-25 03:38:52 +00:00
|
|
|
cmServerPipeConnection::cmServerPipeConnection(const std::string& name)
|
|
|
|
: cmPipeConnection(name, new cmServerBufferStrategy)
|
2016-09-09 08:01:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-03-25 03:38:52 +00:00
|
|
|
cmServerStdIoConnection::cmServerStdIoConnection()
|
|
|
|
: cmStdIoConnection(new cmServerBufferStrategy)
|
2016-09-09 08:01:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-22 22:44:50 +00:00
|
|
|
cmConnectionBufferStrategy::~cmConnectionBufferStrategy() = default;
|
2016-09-09 08:01:44 +00:00
|
|
|
|
2017-03-25 03:38:52 +00:00
|
|
|
void cmConnectionBufferStrategy::clear()
|
2016-09-09 08:01:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-07-20 02:23:34 +00:00
|
|
|
std::string cmServerBufferStrategy::BufferOutMessage(
|
|
|
|
const std::string& rawBuffer) const
|
|
|
|
{
|
|
|
|
return std::string("\n") + kSTART_MAGIC + std::string("\n") + rawBuffer +
|
|
|
|
kEND_MAGIC + std::string("\n");
|
|
|
|
}
|
|
|
|
|
2017-03-25 03:38:52 +00:00
|
|
|
std::string cmServerBufferStrategy::BufferMessage(std::string& RawReadBuffer)
|
2016-09-09 08:01:44 +00:00
|
|
|
{
|
|
|
|
for (;;) {
|
2017-03-25 03:38:52 +00:00
|
|
|
auto needle = RawReadBuffer.find('\n');
|
2016-09-09 08:01:44 +00:00
|
|
|
|
|
|
|
if (needle == std::string::npos) {
|
2017-03-25 03:38:52 +00:00
|
|
|
return "";
|
2016-09-09 08:01:44 +00:00
|
|
|
}
|
2017-03-25 03:38:52 +00:00
|
|
|
std::string line = RawReadBuffer.substr(0, needle);
|
2016-09-09 08:01:44 +00:00
|
|
|
const auto ls = line.size();
|
2016-09-23 20:43:36 +00:00
|
|
|
if (ls > 1 && line.at(ls - 1) == '\r') {
|
2016-09-09 08:01:44 +00:00
|
|
|
line.erase(ls - 1, 1);
|
2016-09-23 20:43:36 +00:00
|
|
|
}
|
2017-03-25 03:38:52 +00:00
|
|
|
RawReadBuffer.erase(RawReadBuffer.begin(),
|
|
|
|
RawReadBuffer.begin() + static_cast<long>(needle) + 1);
|
2016-09-09 08:01:44 +00:00
|
|
|
if (line == kSTART_MAGIC) {
|
2017-03-25 03:38:52 +00:00
|
|
|
RequestBuffer.clear();
|
2016-09-09 08:01:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (line == kEND_MAGIC) {
|
2017-03-25 03:38:52 +00:00
|
|
|
std::string rtn;
|
|
|
|
rtn.swap(this->RequestBuffer);
|
|
|
|
return rtn;
|
2016-09-09 08:01:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 03:38:52 +00:00
|
|
|
this->RequestBuffer += line;
|
|
|
|
this->RequestBuffer += "\n";
|
2016-09-09 08:01:44 +00:00
|
|
|
}
|
|
|
|
}
|