main.cpp 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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::cout << "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::cout << "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::cout << "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::cout << "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::cout << "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::cout << "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::cout << "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::cout << "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. std::string socket_path("unix://tmp/test.sock");
  68. SimpleTestImpl service;
  69. grpc::ServerBuilder builder;
  70. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  71. builder.AddListeningPort(socket_path, grpc::InsecureServerCredentials());
  72. builder.RegisterService(&service);
  73. std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
  74. std::cout << "Server listening on " << server_address << std::endl;
  75. server->Wait();
  76. }