Update Server.
This commit is contained in:
@@ -71,52 +71,29 @@ void read_stdin(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
|
||||
}
|
||||
|
||||
Server::Server() {
|
||||
handlers.try_emplace("initialize", [](json::Value& id, json::Value& value) {
|
||||
auto params = value.getAsObject();
|
||||
auto result = instance.initialize(json::deserialize<protocol::InitializeParams>(*params));
|
||||
|
||||
json::Object response;
|
||||
response.try_emplace("jsonrpc", "2.0");
|
||||
response.try_emplace("id", id);
|
||||
response.try_emplace("result", json::serialize(result));
|
||||
|
||||
json::Value responseValue = std::move(response);
|
||||
|
||||
std::string s;
|
||||
llvm::raw_string_ostream stream(s);
|
||||
stream << responseValue;
|
||||
stream.flush();
|
||||
|
||||
s = "Content-Length: " + std::to_string(s.size()) + "\r\n\r\n" + s;
|
||||
|
||||
static uv_buf_t buf = uv_buf_init(s.data(), s.size());
|
||||
static uv_write_t req;
|
||||
auto state = uv_write(&req, (uv_stream_t*)&stdout_pipe, &buf, 1, NULL);
|
||||
if(state < 0) {
|
||||
spdlog::error("Error writing to stdout: {}", uv_strerror(state));
|
||||
}
|
||||
handlers.try_emplace("initialize", [](json::Value id, json::Value value) {
|
||||
auto result = instance.initialize(json::deserialize<protocol::InitializeParams>(value));
|
||||
instance.response(std::move(id), json::serialize(result));
|
||||
});
|
||||
}
|
||||
|
||||
auto Server::initialize(protocol::InitializeParams params) -> protocol::InitializeResult {
|
||||
// TODO:
|
||||
|
||||
// instance.option.parse()
|
||||
|
||||
return protocol::InitializeResult();
|
||||
}
|
||||
|
||||
int Server::run(int argc, const char** argv) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||
argc = argc;
|
||||
argv = argv;
|
||||
// set logger
|
||||
option.argc = argc;
|
||||
option.argv = argv;
|
||||
|
||||
llvm::SmallString<128> temp;
|
||||
// 获取父路径
|
||||
temp.append(path::parent_path(path::parent_path(argv[0])));
|
||||
path::append(temp, "logs");
|
||||
|
||||
// 确保路径绝对化
|
||||
auto error = llvm::sys::fs::make_absolute(temp);
|
||||
|
||||
// 获取当前时间并格式化为字符串
|
||||
auto now = std::chrono::system_clock::now();
|
||||
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
|
||||
std::tm now_tm = *std::localtime(&now_c);
|
||||
@@ -124,11 +101,9 @@ int Server::run(int argc, const char** argv) {
|
||||
std::ostringstream timeStream;
|
||||
timeStream << std::put_time(&now_tm, "%Y-%m-%d_%H-%M-%S"); // 格式化为 "YYYY-MM-DD_HH-MM-SS"
|
||||
|
||||
// 将时间戳加入到日志文件名
|
||||
std::string logFileName = "clice_" + timeStream.str() + ".log";
|
||||
path::append(temp, logFileName);
|
||||
|
||||
// 创建 logger
|
||||
auto logger = spdlog::basic_logger_mt("clice", std::string(temp.str()));
|
||||
logger->flush_on(spdlog::level::trace);
|
||||
spdlog::set_default_logger(logger);
|
||||
@@ -168,4 +143,27 @@ void Server::handleMessage(std::string_view message) {
|
||||
}
|
||||
}
|
||||
|
||||
void Server::response(json::Value id, json::Value result) {
|
||||
json::Object response;
|
||||
response.try_emplace("jsonrpc", "2.0");
|
||||
response.try_emplace("id", std::move(id));
|
||||
response.try_emplace("result", result);
|
||||
|
||||
json::Value responseValue = std::move(response);
|
||||
|
||||
std::string s;
|
||||
llvm::raw_string_ostream stream(s);
|
||||
stream << responseValue;
|
||||
stream.flush();
|
||||
|
||||
s = "Content-Length: " + std::to_string(s.size()) + "\r\n\r\n" + s;
|
||||
|
||||
static uv_buf_t buf = uv_buf_init(s.data(), s.size());
|
||||
static uv_write_t req;
|
||||
auto state = uv_write(&req, (uv_stream_t*)&stdout_pipe, &buf, 1, NULL);
|
||||
if(state < 0) {
|
||||
spdlog::error("Error writing to stdout: {}", uv_strerror(state));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace clice
|
||||
|
||||
Reference in New Issue
Block a user