main.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. };
  58. int main(int, char *[])
  59. {
  60. std::string server_address("localhost:50051");
  61. SimpleTestImpl service;
  62. grpc::ServerBuilder builder;
  63. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  64. builder.RegisterService(&service);
  65. std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
  66. std::cout << "Server listening on " << server_address << std::endl;
  67. server->Wait();
  68. }