Skip to content

Commit 8cf76e5

Browse files
committed
UPD | grpc
1 parent de342fe commit 8cf76e5

4 files changed

Lines changed: 105 additions & 54 deletions

File tree

README.md

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ class GreeterServiceImpl final : public helloworld::Greeter::CallbackService {
270270
{"http", "1.1"},
271271
{"verify_peer", false},
272272
{"verify_host", false}
273-
}, manapi::async::timeout_cancellation(2000));
273+
}, ctokens::timeout(2000));
274274

275275
if (status.ok()) {
276276
auto response = status.unwrap();
@@ -354,7 +354,6 @@ private:
354354

355355
int main () {
356356
...
357-
std::atomic<bool> grpc_is_running = false;
358357
auto grpc_server_ctx = manapi::net::wgrpc::server_ctx::create().unwrap();
359358

360359
grpc::EnableDefaultHealthCheckService(true);
@@ -363,48 +362,46 @@ int main () {
363362
std::shared_ptr<GreeterServiceImpl> service;
364363
manapi::net::wgrpc::server grpc_server;
365364

366-
if (!grpc_is_running.exchange(true) {
367-
service = std::make_shared<GreeterServiceImpl>();
368-
369-
grpc_server = manapi::net::wgrpc::server::create (grpc_server_ctx).unwrap();
370-
manapi::async::run([grpc_server, service] () mutable -> manapi::future<> {
371-
auto res = co_await grpc_server.config_object({
372-
{"ssl", {
373-
{"cert", "cert.crt"},
374-
{"key", "cert.key"},
375-
{"verify_peer", false}
376-
}},
377-
{"address", "localhost"},
378-
{"port", "8080"}
379-
});
365+
service = std::make_shared<GreeterServiceImpl>();
366+
367+
grpc_server = manapi::net::wgrpc::server::create (grpc_server_ctx).unwrap();
368+
manapi::async::run([grpc_server, service] () mutable -> manapi::future<> {
369+
auto res = co_await grpc_server.config_object({
370+
{"ssl", {
371+
{"cert", "cert.crt"},
372+
{"key", "cert.key"},
373+
{"verify_peer", false}
374+
}},
375+
{"address", "localhost"},
376+
{"port", "8080"}
377+
});
378+
379+
res.log();
380+
res.unwrap();
381+
382+
res = co_await grpc_server.start([&] (grpc::ServerBuilder &builder) -> manapi::status {
383+
builder.RegisterService(service.get());
384+
return manapi::status_ok();
385+
});
380386
381-
res.log();
382-
res.unwrap();
383-
384-
res = co_await grpc_server.start([&] (grpc::ServerBuilder &builder) -> manapi::status {
385-
builder.RegisterService(service.get());
386-
return manapi::status_ok();
387-
});
388-
389-
res.log();
390-
res.unwrap();
391-
392-
manapi::async::current()->timerpool()->append_interval_async(100, [] (const manapi::timer &t) -> manapi::future<> {
393-
auto creds = co_await manapi::net::wgrpc::secure_channel_credentials("/home/Timur/Documents/ssl/quic/cert.crt");
394-
if (!creds.ok()) {
395-
creds.err().log();
396-
co_return;
397-
}
398-
GreeterClient greeter(grpc::CreateChannel("localhost:8080", creds.unwrap()));
399-
std::string user = "Xiadnoring Client";
400-
auto res = co_await greeter.SayHello(user);
401-
if (res.ok())
402-
std::cout << res.unwrap() << "\n";
403-
else
404-
res.err().log();
405-
});
387+
res.log();
388+
res.unwrap();
389+
390+
manapi::async::current()->timerpool()->append_interval_async(100, [] (const manapi::timer &t) -> manapi::future<> {
391+
auto creds = co_await manapi::net::wgrpc::secure_channel_credentials("/home/Timur/Documents/ssl/quic/cert.crt");
392+
if (!creds.ok()) {
393+
creds.err().log();
394+
co_return;
395+
}
396+
GreeterClient greeter(grpc::CreateChannel("localhost:8080", creds.unwrap()));
397+
std::string user = "Xiadnoring Client";
398+
auto res = co_await greeter.SayHello(user);
399+
if (res.ok())
400+
std::cout << res.unwrap() << "\n";
401+
else
402+
res.err().log();
406403
});
407-
}
404+
});
408405
...
409406
}
410407
```

include/ManapiGrpc.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ namespace manapi::net::wgrpc {
4242
struct data_t;
4343

4444
public:
45+
server ();
46+
4547
server (wgrpc::server_ctx ctx);
4648

4749
static manapi::status_or<server> create (wgrpc::server_ctx ctx) MANAPIHTTP_NOEXCEPT;

main.cpp

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,85 @@
11
#include <iostream>
2+
#include <ManapiGrpc.hpp>
23

34
#include <ManapiHttp.hpp>
45
#include <ManapiInitTools.hpp>
6+
#include <grpcpp/health_check_service_interface.h>
7+
#include <grpcpp/ext/proto_server_reflection_plugin.h>
8+
9+
#include "ManapiFetch2.hpp"
510
#include "ext/pq/AsyncPostgreClient.hpp"
611
#include "ext/pq/AsyncPostgrePool.hpp"
12+
13+
#include "protobuf/helloworld.grpc.pb.h"
14+
715
#define FOLDER "/home/Timur/Downloads/anime-main/"
816

17+
18+
// Logic and data behind the server's behavior.
19+
class GreeterServiceImpl final : public helloworld::Greeter::CallbackService {
20+
grpc::ServerUnaryReactor *SayHello(grpc::CallbackServerContext* context, const helloworld::HelloRequest* request,
21+
helloworld::HelloReply* reply) override {
22+
grpc::ServerUnaryReactor* reactor = context->DefaultReactor();
23+
manapi::async::run ([reactor, reply, request] () -> manapi::future<> {
24+
try {
25+
reply->set_message(std::format("Hello, {}! Fact: {}", request->name(), "im happy"));
26+
}
27+
catch (std::exception const &e) {
28+
reply->set_message(std::format("Hello, {}! Something gets wrong: {}", request->name(), e.what()));
29+
}
30+
reactor->Finish(grpc::Status::OK);
31+
});
32+
return reactor;
33+
}
34+
};
35+
936
int main() {
1037
manapi::init_tools::log_trace_init(manapi::debug::LOG_TRACE_HARD);
1138

1239
manapi::async::context::threadpoolfs(4);
1340
manapi::async::context::gbs(manapi::async::context::blockedsignals());
1441

15-
auto ctx = manapi::async::context::create(0).unwrap();
42+
grpc::EnableDefaultHealthCheckService(true);
43+
grpc::reflection::InitProtoReflectionServerBuilderPlugin();
44+
45+
auto ctx = manapi::async::context::create(2).unwrap();
46+
auto grpc_server_ctx = manapi::net::wgrpc::server_ctx::create().unwrap();
1647

1748
auto server_ctx = manapi::net::http::server_ctx::create().unwrap();
18-
ctx->run(0, [server_ctx] (auto cb) -> void {
49+
ctx->run(2, [grpc_server_ctx, server_ctx] (auto cb) -> void {
1950
using http = manapi::net::http::server;
2051

21-
manapi::before_delete bd ([] () -> void {
22-
manapi::async::current()->etaskpool()->append_task([] () -> void {
23-
std::cout << "hello world!\n";
52+
std::shared_ptr<GreeterServiceImpl> service;
53+
manapi::net::wgrpc::server grpc_server;
54+
55+
auto route = manapi::net::http::server::create(server_ctx).unwrap();
56+
auto db = manapi::ext::pq::db::create().unwrap();
57+
58+
service = std::make_shared<GreeterServiceImpl>();
59+
60+
grpc_server = manapi::net::wgrpc::server::create (grpc_server_ctx).unwrap();
61+
manapi::async::run([grpc_server, service] () mutable -> manapi::future<> {
62+
auto res = co_await grpc_server.config_object({
63+
{"ssl", {
64+
{"cert", "/home/Timur/Documents/ssl/quic/cert.crt"},
65+
{"key", "/home/Timur/Documents/ssl/quic/cert.key"},
66+
{"verify_peer", false}
67+
}},
68+
{"address", "localhost"},
69+
{"port", "8080"}
2470
});
25-
});
2671

27-
manapi::async::current()->eventloop()->subscribe_finish(500, [] () -> manapi::future<> {
72+
res.log();
73+
res.unwrap();
2874

29-
std::cout << "finish it!\n";
30-
co_return;
31-
});
75+
res = co_await grpc_server.start([&] (grpc::ServerBuilder &builder) -> manapi::status {
76+
builder.RegisterService(service.get());
77+
return manapi::status_ok();
78+
});
3279

33-
auto route = manapi::net::http::server::create(server_ctx).unwrap();
34-
auto db = manapi::ext::pq::db::create().unwrap();
80+
res.log();
81+
res.unwrap();
82+
});
3583

3684
route.GET ("/", [db] (http::req &req, http::resp &resp) mutable
3785
-> manapi::future<> {

src/ManapiGrpc.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,10 @@ manapi::net::wgrpc::server::server(wgrpc::server_ctx ctx) {
15191519
this->data_ = std::make_shared<data_t>(std::move(ctx), nullptr);
15201520
}
15211521

1522+
manapi::net::wgrpc::server::server() {
1523+
this->data_ = nullptr;
1524+
}
1525+
15221526
manapi::status_or<manapi::net::wgrpc::server> manapi::net::wgrpc::server::create (wgrpc::server_ctx ctx) MANAPIHTTP_NOEXCEPT {
15231527
try {
15241528
return server(std::move(ctx));

0 commit comments

Comments
 (0)