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. */
|
2016-09-09 08:01:44 +00:00
|
|
|
#include "cmServerConnection.h"
|
|
|
|
|
2016-09-09 08:01:46 +00:00
|
|
|
#include "cmServer.h"
|
2017-05-05 21:21:39 +00:00
|
|
|
#include "cmServerDictionary.h"
|
2016-09-09 08:01:44 +00:00
|
|
|
|
2017-03-25 03:38:52 +00:00
|
|
|
cmStdIoConnection::cmStdIoConnection(
|
|
|
|
cmConnectionBufferStrategy* bufferStrategy)
|
|
|
|
: cmConnection(bufferStrategy)
|
|
|
|
, Input()
|
|
|
|
, Output()
|
2016-09-09 08:01:44 +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);
|
2016-09-09 08:01:44 +00:00
|
|
|
|
2017-03-25 03:38:52 +00:00
|
|
|
if (uv_guess_handle(1) == UV_TTY) {
|
|
|
|
usesTty = true;
|
2016-09-09 08:01:44 +00:00
|
|
|
|
2017-03-25 03:38:52 +00:00
|
|
|
this->Input.tty = new uv_tty_t();
|
|
|
|
uv_tty_init(this->Server->GetLoop(), this->Input.tty, 0, 1);
|
|
|
|
uv_tty_set_mode(this->Input.tty, UV_TTY_MODE_NORMAL);
|
|
|
|
this->Input.tty->data = static_cast<cmConnection*>(this);
|
|
|
|
this->ReadStream = reinterpret_cast<uv_stream_t*>(this->Input.tty);
|
2016-09-09 08:01:44 +00:00
|
|
|
|
2017-03-25 03:38:52 +00:00
|
|
|
this->Output.tty = new uv_tty_t();
|
|
|
|
uv_tty_init(this->Server->GetLoop(), this->Output.tty, 1, 0);
|
|
|
|
uv_tty_set_mode(this->Output.tty, UV_TTY_MODE_NORMAL);
|
|
|
|
this->Output.tty->data = static_cast<cmConnection*>(this);
|
|
|
|
this->WriteStream = reinterpret_cast<uv_stream_t*>(this->Output.tty);
|
|
|
|
} else {
|
|
|
|
usesTty = false;
|
2016-10-04 11:28:38 +00:00
|
|
|
|
2017-03-25 03:38:52 +00:00
|
|
|
this->Input.pipe = new uv_pipe_t();
|
|
|
|
uv_pipe_init(this->Server->GetLoop(), this->Input.pipe, 0);
|
|
|
|
uv_pipe_open(this->Input.pipe, 0);
|
|
|
|
this->Input.pipe->data = static_cast<cmConnection*>(this);
|
|
|
|
this->ReadStream = reinterpret_cast<uv_stream_t*>(this->Input.pipe);
|
2016-10-04 11:28:38 +00:00
|
|
|
|
2017-03-25 03:38:52 +00:00
|
|
|
this->Output.pipe = new uv_pipe_t();
|
|
|
|
uv_pipe_init(this->Server->GetLoop(), this->Output.pipe, 0);
|
|
|
|
uv_pipe_open(this->Output.pipe, 1);
|
|
|
|
this->Output.pipe->data = static_cast<cmConnection*>(this);
|
|
|
|
this->WriteStream = reinterpret_cast<uv_stream_t*>(this->Output.pipe);
|
|
|
|
}
|
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
|
|
|
uv_read_start(this->ReadStream, on_alloc_buffer, on_read);
|
|
|
|
Server->OnConnected(this);
|
|
|
|
return cmConnection::OnServeStart(pString);
|
2016-10-04 11:28:38 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 03:38:52 +00:00
|
|
|
bool cmStdIoConnection::OnServerShuttingDown()
|
2016-09-09 08:01:44 +00:00
|
|
|
{
|
2017-03-25 03:38:52 +00:00
|
|
|
cmConnection::OnServerShuttingDown();
|
2016-10-04 11:28:38 +00:00
|
|
|
|
2017-03-25 03:38:52 +00:00
|
|
|
if (usesTty) {
|
|
|
|
uv_read_stop(reinterpret_cast<uv_stream_t*>(this->Input.tty));
|
|
|
|
uv_close(reinterpret_cast<uv_handle_t*>(this->Input.tty),
|
|
|
|
&on_close_delete);
|
|
|
|
uv_close(reinterpret_cast<uv_handle_t*>(this->Output.tty),
|
|
|
|
&on_close_delete);
|
|
|
|
} else {
|
|
|
|
uv_close(reinterpret_cast<uv_handle_t*>(this->Input.pipe),
|
|
|
|
&on_close_delete);
|
|
|
|
uv_close(reinterpret_cast<uv_handle_t*>(this->Output.pipe),
|
|
|
|
&on_close_delete);
|
2016-09-09 08:01:44 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-03-25 03:38:52 +00:00
|
|
|
cmConnectionBufferStrategy::~cmConnectionBufferStrategy()
|
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-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
|
|
|
}
|
|
|
|
}
|