MCPcopy Create free account
hub / github.com/Tradias/asio-grpc / main

Function main

example/multi-threaded-server.cpp:62–104  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

60}
61
62int main(int argc, const char** argv)
63{
64 const auto port = argc >= 2 ? argv[1] : "50051";
65 const auto host = std::string("0.0.0.0:") + port;
66 const auto thread_count = std::thread::hardware_concurrency();
67
68 helloworld::Greeter::AsyncService service;
69 std::unique_ptr<grpc::Server> server;
70 std::vector<std::unique_ptr<agrpc::GrpcContext>> grpc_contexts;
71
72 {
73 grpc::ServerBuilder builder;
74 for (size_t i = 0; i < thread_count; ++i)
75 {
76 grpc_contexts.emplace_back(std::make_unique<agrpc::GrpcContext>(builder.AddCompletionQueue()));
77 }
78 builder.AddListeningPort(host, grpc::InsecureServerCredentials());
79 builder.RegisterService(&service);
80 agrpc::add_health_check_service(builder);
81 server = builder.BuildAndStart();
82 agrpc::start_health_check_service(*server, *grpc_contexts.front());
83 }
84
85 example::ServerShutdown shutdown{*server, *grpc_contexts.front()};
86
87 // Create one thread per GrpcContext.
88 std::vector<std::thread> threads;
89 for (size_t i = 0; i < thread_count; ++i)
90 {
91 threads.emplace_back(
92 [&, i]
93 {
94 auto& grpc_context = *grpc_contexts[i];
95 register_request_handler(grpc_context, service, shutdown);
96 grpc_context.run();
97 });
98 }
99
100 for (auto& thread : threads)
101 {
102 thread.join();
103 }
104}

Callers

nothing calls this directly

Calls 3

register_request_handlerFunction · 0.70
runMethod · 0.45

Tested by

no test coverage detected