protobuf server
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <memory>
|
||||
#include <thread> // Добавили для работы с потоками
|
||||
#include "imgui.h"
|
||||
#include "imgui_impl_glfw.h"
|
||||
#include "imgui_impl_opengl3.h"
|
||||
@@ -7,56 +8,79 @@
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include "include/SerialApp.h"
|
||||
#include <grpcpp/grpcpp.h>
|
||||
|
||||
// работает только после первой генерации
|
||||
#include "service.grpc.pb.h"
|
||||
|
||||
static void glfw_error_callback(int error, const char* description) {
|
||||
fprintf(stderr, "GLFW Error %d: %s\n", error, description);
|
||||
}
|
||||
|
||||
// Простейшая реализация вашего сервиса gRPC (для примера)
|
||||
class MyServiceImpl final : public serial_sample::Greeter::Service {
|
||||
grpc::Status SayHello(grpc::ServerContext* context,
|
||||
const serial_sample::HelloRequest* request,
|
||||
serial_sample::HelloReply* reply) override {
|
||||
reply->set_message("Hello, " + request->name() + "!");
|
||||
return grpc::Status::OK;
|
||||
}
|
||||
};
|
||||
|
||||
int main(int, char**) {
|
||||
glfwSetErrorCallback(glfw_error_callback);
|
||||
if (!glfwInit()) return 1;
|
||||
|
||||
// Настройка версии OpenGL (3.3 Core)
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
// Создание окна ОС
|
||||
GLFWwindow* window = glfwCreateWindow(1280, 720, "Serial App", nullptr, nullptr);
|
||||
if (window == nullptr) return 1;
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSwapInterval(1); // Включение вертикальной синхронизации (V-Sync)
|
||||
glfwSwapInterval(1);
|
||||
|
||||
// Инициализация контекста ImGui
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO& io = ImGui::GetIO(); (void)io;
|
||||
ImGui::StyleColorsDark();
|
||||
|
||||
// Инициализация платформных бэкендов внутри ImGui
|
||||
ImGui_ImplGlfw_InitForOpenGL(window, true);
|
||||
ImGui_ImplOpenGL3_Init("#version 130");
|
||||
|
||||
// Создаем и инициализируем наше изолированное приложение
|
||||
SerialApp app;
|
||||
app.initialize();
|
||||
|
||||
// Главный цикл приложения
|
||||
// === НАЧАЛО ИНИЦИАЛИЗАЦИИ gRPC СЕРВЕРА ===
|
||||
std::string server_address("0.0.0.0:50051");
|
||||
MyServiceImpl grpc_service;
|
||||
|
||||
grpc::ServerBuilder builder;
|
||||
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
|
||||
builder.RegisterService(&grpc_service);
|
||||
|
||||
std::unique_ptr<grpc::Server> grpc_server(builder.BuildAndStart());
|
||||
std::cout << "gRPC Server listening on " << server_address << std::endl;
|
||||
|
||||
// Запускаем сервер в фоновом потоке
|
||||
std::thread grpc_thread([&grpc_server]() {
|
||||
grpc_server->Wait(); // Блокирует фоновый поток, пока сервер работает
|
||||
});
|
||||
// =========================================
|
||||
|
||||
// Главный цикл приложения (GUI поток)
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
glfwPollEvents();
|
||||
|
||||
// Старт нового кадра ImGui
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui_ImplGlfw_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
// Рендерим наше кастомное UI
|
||||
app.renderUI();
|
||||
if (app.isClosing()) {
|
||||
glfwSetWindowShouldClose(window, GLFW_TRUE);
|
||||
}
|
||||
|
||||
// Рендеринг графики
|
||||
ImGui::Render();
|
||||
int display_w, display_h;
|
||||
glfwGetFramebufferSize(window, &display_w, &display_h);
|
||||
@@ -68,7 +92,18 @@ int main(int, char**) {
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
|
||||
// Очистка ресурсов перед выходом
|
||||
// === КОРРЕКТНОЕ ВЫКЛЮЧЕНИЕ gRPC СЕРВЕРА ===
|
||||
std::cout << "Shutting down gRPC server..." << std::endl;
|
||||
// Останавливаем сервер (это разблокирует метод Wait() в фоновом потоке)
|
||||
grpc_server->Shutdown();
|
||||
|
||||
// Обязательно дожидаемся завершения фонового потока перед выходом из main
|
||||
if (grpc_thread.joinable()) {
|
||||
grpc_thread.join();
|
||||
}
|
||||
std::cout << "gRPC server thread joined." << std::endl;
|
||||
// =========================================
|
||||
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
ImGui_ImplGlfw_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
|
||||
Reference in New Issue
Block a user