#include #include #include // Добавили для работы с потоками #include "imgui.h" #include "imgui_impl_glfw.h" #include "imgui_impl_opengl3.h" #include #include #include "include/SerialApp.h" #include // работает только после первой генерации #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; 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); IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void)io; ImGui::StyleColorsDark(); 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(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_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); app.renderUI(); if (app.isClosing()) { glfwSetWindowShouldClose(window, GLFW_TRUE); } ImGui::Render(); int display_w, display_h; glfwGetFramebufferSize(window, &display_w, &display_h); glViewport(0, 0, display_w, display_h); glClearColor(0.45f, 0.55f, 0.60f, 1.00f); glClear(GL_COLOR_BUFFER_BIT); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); 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(); glfwDestroyWindow(window); glfwTerminate(); return 0; }