soem fix.

This commit is contained in:
ykiko
2024-07-07 20:22:36 +08:00
parent 0f8a930289
commit 69872ab834
8 changed files with 150 additions and 94 deletions

View File

@@ -1,39 +0,0 @@
#pragma once
#include <vector>
#include <string_view>
namespace clice {
class MessageBuffer {
std::vector<char> buffer;
std::size_t max = 0;
public:
void write(std::string_view message) { buffer.insert(buffer.end(), message.begin(), message.end()); }
std::string_view read() {
std::string_view view = std::string_view(buffer.data(), buffer.size());
auto start = view.find("Content-Length: ") + 16;
auto end = view.find("\r\n\r\n");
if(start != std::string_view::npos || end != std::string_view::npos) {
std::size_t length = std::stoul(std::string(view.substr(start, end - start)));
if(view.size() >= length + end + 4) {
this->max = length + end + 4;
return view.substr(end + 4, length);
}
}
return {};
}
void clear() {
if(max != 0) {
buffer.erase(buffer.begin(), buffer.begin() + max);
max = 0;
}
}
};
} // namespace clice

View File

@@ -1,9 +1,7 @@
#pragma once
#include <uv.h>
#include <string_view>
#include "Protocol.h"
#include "Transport.h"
namespace clice {
@@ -13,11 +11,10 @@ extern class Server server;
/// core class responsible for starting the server
class Server {
uv_loop_t* loop;
uv_pipe_t stdin_pipe;
uv_pipe_t stdout_pipe;
std::unique_ptr<Transport> transport;
public:
int start();
int run();
int exit();
void handle_message(std::string_view message);

44
include/LSP/Transport.h Normal file
View File

@@ -0,0 +1,44 @@
#pragma once
#include <uv.h>
namespace clice {
struct Transport {
virtual void send(std::string_view) = 0;
virtual ~Transport() = default;
};
class Pipe : public Transport {
private:
uv_pipe_t stdin_pipe;
uv_pipe_t stdout_pipe;
private:
static void alloc_buffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
static void on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf);
public:
Pipe() {
// initialize the pipe
uv_loop_t* loop = uv_default_loop();
uv_pipe_init(loop, &stdin_pipe, 0);
uv_pipe_init(loop, &stdout_pipe, 0);
// bind to stdin and stdout
uv_pipe_open(&stdin_pipe, 0);
uv_pipe_open(&stdout_pipe, 1);
// set callback for reading from stdin
uv_read_start((uv_stream_t*)&stdin_pipe, alloc_buffer, on_read);
}
void send(std::string_view message) override;
~Pipe() override {
uv_close((uv_handle_t*)&stdin_pipe, nullptr);
uv_close((uv_handle_t*)&stdout_pipe, nullptr);
}
};
} // namespace clice