main.cpp 3.5 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. #include <fstream>
  9. #include <sstream>
  10. class SimpleTestImpl final : public qtprotobufnamespace::tests::TestService::Service {
  11. public:
  12. ::grpc::Status testMethod(grpc::ServerContext *, const qtprotobufnamespace::tests::SimpleStringMessage *request, qtprotobufnamespace::tests::SimpleStringMessage *response) override
  13. {
  14. std::cerr << "testMethod called" << std::endl << request->testfieldstring() << std::endl;
  15. response->set_testfieldstring(request->testfieldstring());
  16. if (request->testfieldstring() == "sleep") {
  17. std::this_thread::sleep_for(std::chrono::seconds(3));
  18. }
  19. return ::grpc::Status();
  20. }
  21. ::grpc::Status testMethodServerStream(grpc::ServerContext *, const qtprotobufnamespace::tests::SimpleStringMessage *request,
  22. ::grpc::ServerWriter<qtprotobufnamespace::tests::SimpleStringMessage> *writer) override
  23. {
  24. std::cerr << "testMethodServerStream called" << std::endl << request->testfieldstring() << std::endl;
  25. qtprotobufnamespace::tests::SimpleStringMessage msg;
  26. msg.set_testfieldstring(request->testfieldstring() + "1");
  27. std::this_thread::sleep_for(std::chrono::seconds(3));
  28. std::cerr << "send back " << (request->testfieldstring() + "1") << std::endl;
  29. writer->Write(msg);
  30. msg.set_testfieldstring(request->testfieldstring() + "2");
  31. std::this_thread::sleep_for(std::chrono::seconds(3));
  32. std::cerr << "send back " << (request->testfieldstring() + "2") << std::endl;
  33. writer->Write(msg);
  34. msg.set_testfieldstring(request->testfieldstring() + "3");
  35. std::this_thread::sleep_for(std::chrono::seconds(3));
  36. std::cerr << "send back " << (request->testfieldstring() + "3") << std::endl;
  37. writer->Write(msg);
  38. msg.set_testfieldstring(request->testfieldstring() + "4");
  39. std::this_thread::sleep_for(std::chrono::seconds(3));
  40. std::cerr << "send back " << (request->testfieldstring() + "4") << std::endl;
  41. writer->WriteLast(msg, grpc::WriteOptions());
  42. return ::grpc::Status();
  43. }
  44. ::grpc::Status testMethodBlobServerStream(grpc::ServerContext *, const qtprotobufnamespace::tests::BlobMessage *request,
  45. ::grpc::ServerWriter<qtprotobufnamespace::tests::BlobMessage> *writer) override
  46. {
  47. std::cerr << "testMethodBlobServerStream called" << std::endl;
  48. qtprotobufnamespace::tests::BlobMessage msg;
  49. msg.set_allocated_testbytes(new std::string(request->testbytes()));
  50. writer->Write(msg);
  51. return ::grpc::Status();
  52. }
  53. };
  54. int main(int, char *[])
  55. {
  56. std::ifstream tfile("cert.pem");
  57. std::stringstream cert;
  58. cert << tfile.rdbuf();
  59. tfile.close();
  60. tfile.open("key.pem");
  61. std::stringstream key;
  62. key << tfile.rdbuf();
  63. tfile.close();
  64. grpc::ServerBuilder builder;
  65. grpc::SslServerCredentialsOptions opts(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE);
  66. opts.pem_key_cert_pairs.push_back({key.str(), cert.str()});
  67. std::string server_uri("localhost:60051");
  68. builder.AddListeningPort(server_uri, grpc::SslServerCredentials(opts));
  69. SimpleTestImpl service;
  70. builder.RegisterService(&service);
  71. std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
  72. std::cout << "Server listening on " << server_uri << std::endl;
  73. server->Wait();
  74. }