main.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <iostream>
  2. #include <grpc++/grpc++.h>
  3. #include <testservice.pb.h>
  4. #include <testservice.grpc.pb.h>
  5. #include <simpletest.pb.h>
  6. #include <simpletest.grpc.pb.h>
  7. #include <thread>
  8. class SimpleTestImpl final : public qtprotobufnamespace::tests::TestService::Service {
  9. public:
  10. ::grpc::Status testMethod(grpc::ServerContext *, const qtprotobufnamespace::tests::SimpleStringMessage *request, qtprotobufnamespace::tests::SimpleStringMessage *response) override
  11. {
  12. std::cerr << "testMethod called" << std::endl << request->testfieldstring() << std::endl;
  13. response->set_testfieldstring(request->testfieldstring());
  14. if(request->testfieldstring() == "sleep") {
  15. std::this_thread::sleep_for(std::chrono::seconds(3));
  16. }
  17. return ::grpc::Status();
  18. }
  19. ::grpc::Status testMethodServerStream(grpc::ServerContext *, const qtprotobufnamespace::tests::SimpleStringMessage *request,
  20. ::grpc::ServerWriter<qtprotobufnamespace::tests::SimpleStringMessage> *writer) override
  21. {
  22. std::cerr << "testMethodServerStream called" << std::endl << request->testfieldstring() << std::endl;
  23. qtprotobufnamespace::tests::SimpleStringMessage msg;
  24. msg.set_testfieldstring(request->testfieldstring() + "1");
  25. std::this_thread::sleep_for(std::chrono::seconds(3));
  26. std::cerr << "send back " << (request->testfieldstring() + "1") << std::endl;
  27. writer->Write(msg);
  28. msg.set_testfieldstring(request->testfieldstring() + "2");
  29. std::this_thread::sleep_for(std::chrono::seconds(3));
  30. std::cerr << "send back " << (request->testfieldstring() + "2") << std::endl;
  31. writer->Write(msg);
  32. msg.set_testfieldstring(request->testfieldstring() + "3");
  33. std::this_thread::sleep_for(std::chrono::seconds(3));
  34. std::cerr << "send back " << (request->testfieldstring() + "3") << std::endl;
  35. writer->Write(msg);
  36. msg.set_testfieldstring(request->testfieldstring() + "4");
  37. std::this_thread::sleep_for(std::chrono::seconds(3));
  38. std::cerr << "send back " << (request->testfieldstring() + "4") << std::endl;
  39. writer->WriteLast(msg, grpc::WriteOptions());
  40. return ::grpc::Status();
  41. }
  42. ::grpc::Status testMethodBlobServerStream(grpc::ServerContext *, const qtprotobufnamespace::tests::BlobMessage *request,
  43. ::grpc::ServerWriter<qtprotobufnamespace::tests::BlobMessage> *writer) override
  44. {
  45. std::cerr << "testMethodBlobServerStream called" << std::endl;
  46. qtprotobufnamespace::tests::BlobMessage msg;
  47. msg.set_allocated_testbytes(new std::string(request->testbytes()));
  48. writer->Write(msg);
  49. return ::grpc::Status();
  50. }
  51. };
  52. int main(int, char *[])
  53. {
  54. std::string server_address("localhost:50051");
  55. SimpleTestImpl service;
  56. grpc::ServerBuilder builder;
  57. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  58. builder.RegisterService(&service);
  59. std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
  60. std::cout << "Server listening on " << server_address << std::endl;
  61. server->Wait();
  62. }