mirror of
https://github.com/Kitware/CMake.git
synced 2025-10-15 12:16:40 +08:00
server: Add support for connections that aren't event based
This commit is contained in:
@@ -14,18 +14,19 @@ struct write_req_t
|
||||
uv_buf_t buf;
|
||||
};
|
||||
|
||||
void cmConnection::on_alloc_buffer(uv_handle_t* handle, size_t suggested_size,
|
||||
uv_buf_t* buf)
|
||||
void cmEventBasedConnection::on_alloc_buffer(uv_handle_t* handle,
|
||||
size_t suggested_size,
|
||||
uv_buf_t* buf)
|
||||
{
|
||||
(void)(handle);
|
||||
char* rawBuffer = new char[suggested_size];
|
||||
*buf = uv_buf_init(rawBuffer, static_cast<unsigned int>(suggested_size));
|
||||
}
|
||||
|
||||
void cmConnection::on_read(uv_stream_t* stream, ssize_t nread,
|
||||
const uv_buf_t* buf)
|
||||
void cmEventBasedConnection::on_read(uv_stream_t* stream, ssize_t nread,
|
||||
const uv_buf_t* buf)
|
||||
{
|
||||
auto conn = reinterpret_cast<cmConnection*>(stream->data);
|
||||
auto conn = reinterpret_cast<cmEventBasedConnection*>(stream->data);
|
||||
if (conn) {
|
||||
if (nread >= 0) {
|
||||
conn->ReadData(std::string(buf->base, buf->base + nread));
|
||||
@@ -37,16 +38,16 @@ void cmConnection::on_read(uv_stream_t* stream, ssize_t nread,
|
||||
delete[](buf->base);
|
||||
}
|
||||
|
||||
void cmConnection::on_close_delete(uv_handle_t* handle)
|
||||
void cmEventBasedConnection::on_close_delete(uv_handle_t* handle)
|
||||
{
|
||||
delete handle;
|
||||
}
|
||||
|
||||
void cmConnection::on_close(uv_handle_t*)
|
||||
void cmEventBasedConnection::on_close(uv_handle_t* /*handle*/)
|
||||
{
|
||||
}
|
||||
|
||||
void cmConnection::on_write(uv_write_t* req, int status)
|
||||
void cmEventBasedConnection::on_write(uv_write_t* req, int status)
|
||||
{
|
||||
(void)(status);
|
||||
|
||||
@@ -56,22 +57,22 @@ void cmConnection::on_write(uv_write_t* req, int status)
|
||||
delete wr;
|
||||
}
|
||||
|
||||
void cmConnection::on_new_connection(uv_stream_t* stream, int status)
|
||||
void cmEventBasedConnection::on_new_connection(uv_stream_t* stream, int status)
|
||||
{
|
||||
(void)(status);
|
||||
auto conn = reinterpret_cast<cmConnection*>(stream->data);
|
||||
auto conn = reinterpret_cast<cmEventBasedConnection*>(stream->data);
|
||||
|
||||
if (conn) {
|
||||
conn->Connect(stream);
|
||||
}
|
||||
}
|
||||
|
||||
bool cmConnection::IsOpen() const
|
||||
bool cmEventBasedConnection::IsOpen() const
|
||||
{
|
||||
return this->WriteStream != CM_NULLPTR;
|
||||
}
|
||||
|
||||
void cmConnection::WriteData(const std::string& data)
|
||||
void cmEventBasedConnection::WriteData(const std::string& data)
|
||||
{
|
||||
assert(this->WriteStream);
|
||||
|
||||
@@ -86,12 +87,7 @@ void cmConnection::WriteData(const std::string& data)
|
||||
on_write);
|
||||
}
|
||||
|
||||
cmConnection::~cmConnection()
|
||||
{
|
||||
OnServerShuttingDown();
|
||||
}
|
||||
|
||||
void cmConnection::ReadData(const std::string& data)
|
||||
void cmEventBasedConnection::ReadData(const std::string& data)
|
||||
{
|
||||
this->RawReadBuffer += data;
|
||||
if (BufferStrategy) {
|
||||
@@ -107,21 +103,39 @@ void cmConnection::ReadData(const std::string& data)
|
||||
}
|
||||
}
|
||||
|
||||
void cmConnection::SetServer(cmServerBase* s)
|
||||
{
|
||||
Server = s;
|
||||
}
|
||||
|
||||
cmConnection::cmConnection(cmConnectionBufferStrategy* bufferStrategy)
|
||||
cmEventBasedConnection::cmEventBasedConnection(
|
||||
cmConnectionBufferStrategy* bufferStrategy)
|
||||
: BufferStrategy(bufferStrategy)
|
||||
{
|
||||
}
|
||||
|
||||
void cmConnection::Connect(uv_stream_t*)
|
||||
void cmEventBasedConnection::Connect(uv_stream_t* server)
|
||||
{
|
||||
(void)server;
|
||||
Server->OnConnected(nullptr);
|
||||
}
|
||||
|
||||
void cmEventBasedConnection::OnDisconnect(int onerror)
|
||||
{
|
||||
(void)onerror;
|
||||
this->OnConnectionShuttingDown();
|
||||
this->Server->OnDisconnect(this);
|
||||
}
|
||||
|
||||
cmConnection::~cmConnection()
|
||||
{
|
||||
}
|
||||
|
||||
bool cmConnection::OnConnectionShuttingDown()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void cmConnection::SetServer(cmServerBase* s)
|
||||
{
|
||||
Server = s;
|
||||
}
|
||||
|
||||
void cmConnection::ProcessRequest(const std::string& request)
|
||||
{
|
||||
Server->ProcessRequest(this, request);
|
||||
@@ -133,13 +147,7 @@ bool cmConnection::OnServeStart(std::string* errString)
|
||||
return true;
|
||||
}
|
||||
|
||||
void cmConnection::OnDisconnect(int errorCode)
|
||||
{
|
||||
(void)errorCode;
|
||||
this->Server->OnDisconnect(this);
|
||||
}
|
||||
|
||||
bool cmConnection::OnServerShuttingDown()
|
||||
bool cmEventBasedConnection::OnConnectionShuttingDown()
|
||||
{
|
||||
this->WriteStream->data = nullptr;
|
||||
this->ReadStream->data = nullptr;
|
||||
|
@@ -46,43 +46,57 @@ public:
|
||||
// TODO: There should be a callback / flag set for errors
|
||||
};
|
||||
|
||||
/***
|
||||
* Abstraction of a connection; ties in event callbacks from libuv and notifies
|
||||
* the server when appropriate
|
||||
*/
|
||||
class cmConnection
|
||||
{
|
||||
CM_DISABLE_COPY(cmConnection)
|
||||
|
||||
public:
|
||||
cmConnection() {}
|
||||
|
||||
virtual void WriteData(const std::string& data) = 0;
|
||||
|
||||
virtual ~cmConnection();
|
||||
|
||||
virtual bool OnConnectionShuttingDown();
|
||||
|
||||
virtual bool IsOpen() const = 0;
|
||||
|
||||
virtual void SetServer(cmServerBase* s);
|
||||
|
||||
virtual void ProcessRequest(const std::string& request);
|
||||
|
||||
virtual bool OnServeStart(std::string* pString);
|
||||
|
||||
protected:
|
||||
cmServerBase* Server = nullptr;
|
||||
};
|
||||
|
||||
/***
|
||||
* Abstraction of a connection; ties in event callbacks from libuv and notifies
|
||||
* the server when appropriate
|
||||
*/
|
||||
class cmEventBasedConnection : public cmConnection
|
||||
{
|
||||
|
||||
public:
|
||||
/***
|
||||
* @param bufferStrategy If no strategy is given, it will process the raw
|
||||
* chunks as they come in. The connection
|
||||
* owns the pointer given.
|
||||
*/
|
||||
cmConnection(cmConnectionBufferStrategy* bufferStrategy = nullptr);
|
||||
cmEventBasedConnection(cmConnectionBufferStrategy* bufferStrategy = nullptr);
|
||||
|
||||
virtual void Connect(uv_stream_t* server);
|
||||
|
||||
virtual void ReadData(const std::string& data);
|
||||
|
||||
virtual bool OnServeStart(std::string* errString);
|
||||
bool IsOpen() const override;
|
||||
|
||||
virtual bool OnServerShuttingDown();
|
||||
|
||||
virtual bool IsOpen() const;
|
||||
|
||||
virtual void WriteData(const std::string& data);
|
||||
|
||||
virtual void ProcessRequest(const std::string& request);
|
||||
|
||||
virtual void SetServer(cmServerBase* s);
|
||||
void WriteData(const std::string& data) override;
|
||||
bool OnConnectionShuttingDown() override;
|
||||
|
||||
virtual void OnDisconnect(int errorCode);
|
||||
uv_stream_t* ReadStream = nullptr;
|
||||
cmServerBase* Server = nullptr;
|
||||
uv_stream_t* WriteStream = nullptr;
|
||||
|
||||
static void on_close(uv_handle_t* handle);
|
||||
|
@@ -7,7 +7,7 @@
|
||||
|
||||
cmPipeConnection::cmPipeConnection(const std::string& name,
|
||||
cmConnectionBufferStrategy* bufferStrategy)
|
||||
: cmConnection(bufferStrategy)
|
||||
: cmEventBasedConnection(bufferStrategy)
|
||||
, PipeName(name)
|
||||
{
|
||||
}
|
||||
@@ -26,8 +26,7 @@ void cmPipeConnection::Connect(uv_stream_t* server)
|
||||
|
||||
this->ClientPipe = new uv_pipe_t();
|
||||
uv_pipe_init(this->Server->GetLoop(), this->ClientPipe, 0);
|
||||
this->ClientPipe->data = static_cast<cmConnection*>(this);
|
||||
|
||||
this->ClientPipe->data = static_cast<cmEventBasedConnection*>(this);
|
||||
auto client = reinterpret_cast<uv_stream_t*>(this->ClientPipe);
|
||||
if (uv_accept(server, client) != 0) {
|
||||
uv_close(reinterpret_cast<uv_handle_t*>(client), &on_close_delete);
|
||||
@@ -45,7 +44,7 @@ bool cmPipeConnection::OnServeStart(std::string* errorMessage)
|
||||
{
|
||||
this->ServerPipe = new uv_pipe_t();
|
||||
uv_pipe_init(this->Server->GetLoop(), this->ServerPipe, 0);
|
||||
this->ServerPipe->data = static_cast<cmConnection*>(this);
|
||||
this->ServerPipe->data = static_cast<cmEventBasedConnection*>(this);
|
||||
|
||||
int r;
|
||||
if ((r = uv_pipe_bind(this->ServerPipe, this->PipeName.c_str())) != 0) {
|
||||
@@ -63,7 +62,7 @@ bool cmPipeConnection::OnServeStart(std::string* errorMessage)
|
||||
return cmConnection::OnServeStart(errorMessage);
|
||||
}
|
||||
|
||||
bool cmPipeConnection::OnServerShuttingDown()
|
||||
bool cmPipeConnection::OnConnectionShuttingDown()
|
||||
{
|
||||
if (this->ClientPipe) {
|
||||
uv_close(reinterpret_cast<uv_handle_t*>(this->ClientPipe),
|
||||
@@ -77,5 +76,5 @@ bool cmPipeConnection::OnServerShuttingDown()
|
||||
this->WriteStream = nullptr;
|
||||
this->ReadStream = nullptr;
|
||||
|
||||
return cmConnection::OnServerShuttingDown();
|
||||
return cmConnection::OnConnectionShuttingDown();
|
||||
}
|
||||
|
@@ -9,7 +9,7 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
class cmPipeConnection : public cmConnection
|
||||
class cmPipeConnection : public cmEventBasedConnection
|
||||
{
|
||||
public:
|
||||
cmPipeConnection(const std::string& name,
|
||||
@@ -17,7 +17,7 @@ public:
|
||||
|
||||
bool OnServeStart(std::string* pString) override;
|
||||
|
||||
bool OnServerShuttingDown() override;
|
||||
bool OnConnectionShuttingDown() override;
|
||||
|
||||
void Connect(uv_stream_t* server) override;
|
||||
|
||||
|
@@ -28,7 +28,7 @@ static void on_walk_to_shutdown(uv_handle_t* handle, void* arg)
|
||||
{
|
||||
(void)arg;
|
||||
if (!uv_is_closing(handle)) {
|
||||
uv_close(handle, &cmConnection::on_close);
|
||||
uv_close(handle, &cmEventBasedConnection::on_close);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -478,7 +478,7 @@ void cmServerBase::StartShutDown()
|
||||
}
|
||||
|
||||
for (auto& connection : Connections) {
|
||||
connection->OnServerShuttingDown();
|
||||
connection->OnConnectionShuttingDown();
|
||||
}
|
||||
Connections.clear();
|
||||
|
||||
|
@@ -7,7 +7,7 @@
|
||||
|
||||
cmStdIoConnection::cmStdIoConnection(
|
||||
cmConnectionBufferStrategy* bufferStrategy)
|
||||
: cmConnection(bufferStrategy)
|
||||
: cmEventBasedConnection(bufferStrategy)
|
||||
, Input()
|
||||
, Output()
|
||||
{
|
||||
@@ -23,13 +23,13 @@ void cmStdIoConnection::SetServer(cmServerBase* s)
|
||||
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->Input.tty->data = static_cast<cmEventBasedConnection*>(this);
|
||||
this->ReadStream = reinterpret_cast<uv_stream_t*>(this->Input.tty);
|
||||
|
||||
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->Output.tty->data = static_cast<cmEventBasedConnection*>(this);
|
||||
this->WriteStream = reinterpret_cast<uv_stream_t*>(this->Output.tty);
|
||||
} else {
|
||||
usesTty = false;
|
||||
@@ -37,13 +37,13 @@ void cmStdIoConnection::SetServer(cmServerBase* s)
|
||||
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->Input.pipe->data = static_cast<cmEventBasedConnection*>(this);
|
||||
this->ReadStream = reinterpret_cast<uv_stream_t*>(this->Input.pipe);
|
||||
|
||||
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->Output.pipe->data = static_cast<cmEventBasedConnection*>(this);
|
||||
this->WriteStream = reinterpret_cast<uv_stream_t*>(this->Output.pipe);
|
||||
}
|
||||
}
|
||||
@@ -55,9 +55,9 @@ bool cmStdIoConnection::OnServeStart(std::string* pString)
|
||||
return cmConnection::OnServeStart(pString);
|
||||
}
|
||||
|
||||
bool cmStdIoConnection::OnServerShuttingDown()
|
||||
bool cmStdIoConnection::OnConnectionShuttingDown()
|
||||
{
|
||||
cmConnection::OnServerShuttingDown();
|
||||
cmEventBasedConnection::OnConnectionShuttingDown();
|
||||
|
||||
if (usesTty) {
|
||||
uv_read_stop(reinterpret_cast<uv_stream_t*>(this->Input.tty));
|
||||
|
@@ -32,14 +32,14 @@ private:
|
||||
/***
|
||||
* Generic connection over std io interfaces -- tty
|
||||
*/
|
||||
class cmStdIoConnection : public cmConnection
|
||||
class cmStdIoConnection : public cmEventBasedConnection
|
||||
{
|
||||
public:
|
||||
cmStdIoConnection(cmConnectionBufferStrategy* bufferStrategy);
|
||||
|
||||
void SetServer(cmServerBase* s) override;
|
||||
|
||||
bool OnServerShuttingDown() override;
|
||||
bool OnConnectionShuttingDown() override;
|
||||
|
||||
bool OnServeStart(std::string* pString) override;
|
||||
|
||||
|
Reference in New Issue
Block a user