main.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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(1));
  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(1));
  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(1));
  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(1));
  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(1));
  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. ::grpc::Status testMethodStatusMessage(::grpc::ServerContext*,
  52. const ::qtprotobufnamespace::tests::SimpleStringMessage* request,
  53. ::qtprotobufnamespace::tests::SimpleStringMessage*) override
  54. {
  55. return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, request->testfieldstring());
  56. }
  57. ::grpc::Status testMethodNonCompatibleArgRet(grpc::ServerContext *, const qtprotobufnamespace::tests::SimpleIntMessage *request, qtprotobufnamespace::tests::SimpleStringMessage *response) override
  58. {
  59. std::cerr << "testMethodNonCompatibleArgRet called" << std::endl << request->testfield() << std::endl;
  60. response->set_testfieldstring(std::to_string(request->testfield()));
  61. return ::grpc::Status();
  62. }
  63. };
  64. int main(int, char *[])
  65. {
  66. std::string server_address("localhost:50051");
  67. SimpleTestImpl service;
  68. grpc::ServerBuilder builder;
  69. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  70. builder.RegisterService(&service);
  71. std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
  72. std::cout << "Server listening on " << server_address << std::endl;
  73. server->Wait();
  74. }