├── SpellCheck.dic ├── common ├── LogHelper.h ├── GrpcType.h ├── GrpcType.cc ├── Performance.h └── LogHelper.cc ├── protobuf ├── generate.sh ├── greeting.proto ├── CMakeLists.txt ├── greeting.grpc.pb.cc ├── greeting.grpc.pb.h ├── greeting.pb.h └── greeting.pb.cc ├── .gitignore ├── .clang-format ├── server ├── main.cc ├── GreetingSession.h ├── GreetingServer.h ├── GreetingSession.cc └── GreetingServer.cc ├── README.md ├── client ├── main.cc ├── GreetingClient.h └── GreetingClient.cc └── CMakeLists.txt /SpellCheck.dic: -------------------------------------------------------------------------------- 1 | grpc 2 | easylogging 3 | pubsub -------------------------------------------------------------------------------- /common/LogHelper.h: -------------------------------------------------------------------------------- 1 | #ifndef GRPC_EXAMPLE_LOG_HELPER_H 2 | #define GRPC_EXAMPLE_LOG_HELPER_H 3 | 4 | #include 5 | 6 | bool initLog(int argc, const char **argv, const std::string& logFileName); 7 | 8 | #endif // GRPC_EXAMPLE_LOG_HELPER_H 9 | -------------------------------------------------------------------------------- /protobuf/generate.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | export LC_ALL=C 4 | cd "$(dirname "$0")" || exit 1 5 | 6 | # generate grpc files by manual, 7 | # only used for some special scenes, like remote debug for Clion 8 | # generally just generate by cmake 9 | 10 | echo "now generate protobuf and grpc source code, protoc version: " && protoc --version 11 | 12 | protoc -I=./ --cpp_out=. --grpc_out=. --plugin=protoc-gen-grpc=/usr/local/bin/grpc_cpp_plugin ./greeting.proto 13 | -------------------------------------------------------------------------------- /protobuf/greeting.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package grpc.example; 3 | 4 | service GreetingService { 5 | rpc SubscribeGreetingBySecond (stream RequestSubscribe) returns (stream ReplyGreeting) { 6 | } 7 | } 8 | 9 | message RequestSubscribe { 10 | // request multi times to change name 11 | string name = 1; 12 | uint64 current_nanosecond = 2; 13 | } 14 | 15 | message ReplyGreeting { 16 | // reply: "name: count" 17 | string message = 1; 18 | uint64 current_nanosecond = 2; 19 | } -------------------------------------------------------------------------------- /common/GrpcType.h: -------------------------------------------------------------------------------- 1 | #ifndef GRPC_EXAMPLE_GRPC_TYPE_H 2 | #define GRPC_EXAMPLE_GRPC_TYPE_H 3 | #include 4 | 5 | #define GRPC_EVENT_MASK 0x3u 6 | #define GRPC_EVENT_BIT_LENGTH 2u 7 | 8 | enum GrpcEvent { 9 | GRPC_EVENT_CONNECTED = 0, 10 | GRPC_EVENT_READ_DONE = 1, 11 | GRPC_EVENT_WRITE_DONE = 2, 12 | GRPC_EVENT_FINISHED = 3 13 | }; 14 | std::ostream& operator<<(std::ostream& os, GrpcEvent event); 15 | 16 | enum class GrpcSessionStatus { WAIT_CONNECT, READY_TO_WRITE, WAIT_WRITE_DONE, FINISHED }; 17 | std::ostream& operator<<(std::ostream& os, GrpcSessionStatus sessionStatus); 18 | 19 | #endif // GRPC_EXAMPLE_GRPC_TYPE_H 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | codeship.aes 3 | 4 | # Mac 5 | .DS_Store 6 | .AppleDouble 7 | .LSOverride 8 | 9 | # Prerequisites 10 | *.d 11 | 12 | # Compiled Object files 13 | *.slo 14 | *.lo 15 | *.o 16 | *.obj 17 | 18 | # Precompiled Headers 19 | *.gch 20 | *.pch 21 | 22 | # Compiled Dynamic libraries 23 | *.so 24 | *.dylib 25 | *.dll 26 | 27 | # Fortran module files 28 | *.mod 29 | *.smod 30 | 31 | # Compiled Static libraries 32 | *.lai 33 | *.la 34 | *.a 35 | *.lib 36 | 37 | # Executables 38 | *.exe 39 | *.out 40 | *.app 41 | 42 | build/ 43 | 44 | # Clion 45 | .idea/ 46 | cmake-build-*/ 47 | .clion.source.upload.marker 48 | 49 | # vscode 50 | .vscode -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | # see https://clang.llvm.org/docs/ClangFormatStyleOptions.html 2 | 3 | # clang format not support insert blank lines for now, see: 4 | # https://stackoverflow.com/questions/27121457/line-breaks-between-function-definitions 5 | --- 6 | Language: Cpp 7 | BasedOnStyle: Google 8 | Standard: Cpp11 9 | AccessModifierOffset: -2 10 | AlignAfterOpenBracket: Align 11 | AlignConsecutiveAssignments: false 12 | AlignEscapedNewlines: Left 13 | AlignOperands: true 14 | AlignTrailingComments: true 15 | AllowShortBlocksOnASingleLine: true 16 | AllowShortFunctionsOnASingleLine: InlineOnly 17 | AlwaysBreakBeforeMultilineStrings: false 18 | BinPackArguments: false 19 | BinPackParameters: false 20 | IndentWidth: 4 21 | ColumnLimit: 110 -------------------------------------------------------------------------------- /server/main.cc: -------------------------------------------------------------------------------- 1 | #include "GreetingServer.h" 2 | #include "LogHelper.h" 3 | 4 | void handler(int sig) { 5 | std::cout << "get signal: " << sig << std::endl; 6 | GreetingServer::getInstance().stop(); 7 | } 8 | 9 | int main(int argc, const char **argv) { 10 | signal(SIGTERM, handler); 11 | signal(SIGINT, handler); 12 | 13 | if (!initLog(argc, argv, "server")) { 14 | std::cout << "init log failed" << std::endl; 15 | return 1; 16 | } 17 | 18 | try { 19 | if (!GreetingServer::getInstance().init("0.0.0.0:6666")) { 20 | LOG(INFO) << "init failure"; 21 | return 1; 22 | } 23 | GreetingServer::getInstance().run(); 24 | } catch (std::exception &e) { 25 | LOG(INFO) << "exception: " << e.what(); 26 | return 1; 27 | } 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /protobuf/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | message(STATUS "** sub directory protobuf") 2 | 3 | find_package(Protobuf CONFIG REQUIRED) 4 | message(STATUS "protobuf version: ${Protobuf_VERSION}") 5 | find_program(_PROGRAM_PROTOC protoc) 6 | 7 | find_package(gRPC CONFIG REQUIRED) 8 | message(STATUS "grpc version: ${gRPC_VERSION}") 9 | find_program(_PROGRAM_GRPC_CPP_PLUGIN grpc_cpp_plugin) 10 | 11 | find_package(Threads REQUIRED) 12 | 13 | execute_process( 14 | COMMAND ${_PROGRAM_PROTOC} -I=./ --cpp_out=. --grpc_out=. --plugin=protoc-gen-grpc=${_PROGRAM_GRPC_CPP_PLUGIN} ./greeting.proto 15 | WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/protobuf 16 | ) 17 | 18 | file(GLOB LIB_GENERATED_SOURCES ${PROJECT_SOURCE_DIR}/protobuf/*.cc) 19 | 20 | add_library(grpc_greeting STATIC ${LIB_GENERATED_SOURCES}) 21 | target_link_libraries(grpc_greeting gRPC::grpc++_unsecure protobuf::libprotobuf) 22 | 23 | message(STATUS "** sub directory protobuf done") -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Grpc async bi-direction stream example 2 | 3 | A simple example about grpc async C++ api 4 | 5 | ## Key words 6 | 7 | * async C++ api 8 | * bi-direction stream 9 | * state machine based event callback 10 | * pubsub / subscriber 11 | * shutdown server and client gracefully 12 | * one service one completion queue(one thread) 13 | * performance test 14 | 15 | ## Compile and run 16 | 17 | ```shell script 18 | # complie 19 | mkdir -p build 20 | cd build 21 | cmake -DCMAKE_BUILD_TYPE=Debug .. 22 | make 23 | # run 24 | ## simple mode 25 | ./server 26 | ./client 27 | ## change log level, default debug 28 | LOG_LEVEL=release ./server 29 | LOG_LEVEL=release ./client 30 | ## specify client name 31 | LOG_LEVEL=debug ./client client_name 32 | ## specify server address 33 | LOG_LEVEL=release ./client client_name localhost:6666 34 | ``` 35 | 36 | ## Test platform 37 | 38 | * gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0 39 | * protobuf version: 3.11.2.0 40 | * grpc version: 1.27.3 -------------------------------------------------------------------------------- /common/GrpcType.cc: -------------------------------------------------------------------------------- 1 | #include "GrpcType.h" 2 | 3 | std::ostream& operator<<(std::ostream& os, GrpcEvent event) { 4 | // omit default case to trigger compiler warning for missing cases 5 | switch (event) { 6 | case GrpcEvent::GRPC_EVENT_CONNECTED: 7 | return os << "GRPC_EVENT_CONNECTED"; 8 | case GrpcEvent::GRPC_EVENT_READ_DONE: 9 | return os << "GRPC_EVENT_READ_DONE"; 10 | case GrpcEvent::GRPC_EVENT_WRITE_DONE: 11 | return os << "GRPC_EVENT_WRITE_DONE"; 12 | case GrpcEvent::GRPC_EVENT_FINISHED: 13 | return os << "GRPC_EVENT_FINISHED"; 14 | } 15 | } 16 | 17 | std::ostream& operator<<(std::ostream& os, GrpcSessionStatus sessionStatus) { 18 | // omit default case to trigger compiler warning for missing cases 19 | switch (sessionStatus) { 20 | case GrpcSessionStatus::WAIT_CONNECT: 21 | return os << "WAIT_CONNECT"; 22 | case GrpcSessionStatus::READY_TO_WRITE: 23 | return os << "READY_TO_WRITE"; 24 | case GrpcSessionStatus::WAIT_WRITE_DONE: 25 | return os << "WAIT_WRITE_DONE"; 26 | case GrpcSessionStatus::FINISHED: 27 | return os << "FINISHED"; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /client/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "GreetingClient.h" 5 | 6 | void handler(int sig) { 7 | LOG(INFO) << "get signal: " << sig; 8 | GreetingClient::getInstance().stop(); 9 | } 10 | 11 | int main(int argc, const char **argv) { 12 | signal(SIGTERM, handler); 13 | signal(SIGINT, handler); 14 | 15 | if (!initLog(argc, argv, "client")) { 16 | std::cout << "init log failed" << std::endl; 17 | return 1; 18 | } 19 | 20 | std::string client_name{"client_name"}; 21 | if (argc >= 2) { 22 | client_name = argv[1]; 23 | LOG(INFO) << "client name: " << client_name; 24 | } 25 | 26 | std::string server_address{"localhost:6666"}; 27 | if (argc >= 3) { 28 | server_address = argv[2]; 29 | LOG(INFO) << "server address: " << server_address; 30 | } 31 | 32 | try { 33 | if (!GreetingClient::getInstance().init(client_name, server_address)) { 34 | LOG(INFO) << "GreetingClient init failed"; 35 | return 1; 36 | } 37 | GreetingClient::getInstance().run(); 38 | } catch (std::exception &e) { 39 | LOG(INFO) << "exception: " << e.what(); 40 | return 1; 41 | } 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /server/GreetingSession.h: -------------------------------------------------------------------------------- 1 | #ifndef GRPC_EXAMPLE_TIME_SERVER_SESSION_H 2 | #define GRPC_EXAMPLE_TIME_SERVER_SESSION_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "GrpcType.h" 14 | #include "Performance.h" 15 | 16 | class GreetingSession { 17 | public: 18 | GreetingSession(uint64_t sessionId) : session_id_(sessionId){}; 19 | 20 | bool init(); 21 | 22 | void process(GrpcEvent event); 23 | 24 | void reply(); 25 | 26 | void finish(); 27 | 28 | public: 29 | const uint64_t session_id_{0}; 30 | std::mutex mutex_{}; 31 | GrpcSessionStatus status_{GrpcSessionStatus::WAIT_CONNECT}; 32 | 33 | ::grpc::ServerContext server_context_{}; 34 | ::grpc::ServerAsyncReaderWriter<::grpc::example::ReplyGreeting, ::grpc::example::RequestSubscribe> 35 | subscribe_stream{&server_context_}; 36 | ::grpc::example::RequestSubscribe request_{}; 37 | 38 | std::string name_{}; 39 | std::deque> message_queue_{}; 40 | Performance performance_{}; 41 | uint64_t reply_times_{0}; 42 | }; 43 | 44 | #endif // GRPC_EXAMPLE_TIME_SERVER_SESSION_H 45 | -------------------------------------------------------------------------------- /common/Performance.h: -------------------------------------------------------------------------------- 1 | #ifndef GRPC_EXAMPLE_PERFORMANCE_H 2 | #define GRPC_EXAMPLE_PERFORMANCE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "LogHelper.h" 12 | 13 | #define COUNT_FOR_PRINT_PERFORMANCE 10000u 14 | 15 | class Performance { 16 | public: 17 | explicit Performance() = default; 18 | 19 | void setName(std::string name) { name_ = std::move(name); } 20 | 21 | void add(uint64_t timeDifference) { 22 | time_diff_set_.insert(timeDifference); 23 | if (time_diff_set_.size() > COUNT_FOR_PRINT_PERFORMANCE) { 24 | uint64_t time_diff_average = 25 | std::accumulate(time_diff_set_.begin(), time_diff_set_.end(), uint64_t(0u)) / 26 | time_diff_set_.size(); 27 | LOG(INFO) << "[performance]name: " << name_ << ", average: " << time_diff_average 28 | << ", min: " << *time_diff_set_.begin() << ", max: " << *time_diff_set_.rbegin(); 29 | time_diff_set_.clear(); 30 | } 31 | LOG(DEBUG) << "[performance]time difference: " << timeDifference; 32 | } 33 | 34 | private: 35 | std::set time_diff_set_{}; 36 | std::string name_{}; 37 | }; 38 | 39 | #endif // GRPC_EXAMPLE_PERFORMANCE_H 40 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | 3 | project(grpc_example VERSION 1.0 LANGUAGES C CXX) 4 | 5 | set(CMAKE_CXX_STANDARD 14) 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 7 | set(CMAKE_CXX_EXTENSIONS OFF) 8 | 9 | # cmake -DCMAKE_BUILD_TYPE=Debug .. 10 | # cmake -DCMAKE_BUILD_TYPE=Release .. 11 | # cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo .. (default for docker release build) 12 | message("CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}") 13 | 14 | add_subdirectory(${PROJECT_SOURCE_DIR}/protobuf) 15 | 16 | include_directories( 17 | ${PROJECT_SOURCE_DIR}/libs 18 | ${PROJECT_SOURCE_DIR}/protobuf 19 | ${PROJECT_SOURCE_DIR}/common) 20 | 21 | # init easylogging 22 | add_definitions(-DELPP_THREAD_SAFE) 23 | add_library(easylogging STATIC ${PROJECT_SOURCE_DIR}/libs/easylogging/easylogging++.cc) 24 | 25 | set(THIRD_LIBRARIES grpc_greeting easylogging) 26 | 27 | file(GLOB_RECURSE SERVER_SOURCES common/*.cc ${PROJECT_SOURCE_DIR}/server/*.cc) 28 | message("SERVER_SOURCES = ${SERVER_SOURCES}") 29 | add_executable(server ${SERVER_SOURCES}) 30 | target_link_libraries(server ${THIRD_LIBRARIES}) 31 | 32 | file(GLOB_RECURSE CLIENT_SOURCES common/*.cc ${PROJECT_SOURCE_DIR}/client/*.cc) 33 | message("CLIENT_SOURCES = ${CLIENT_SOURCES}") 34 | add_executable(client ${CLIENT_SOURCES}) 35 | target_link_libraries(client ${THIRD_LIBRARIES}) -------------------------------------------------------------------------------- /client/GreetingClient.h: -------------------------------------------------------------------------------- 1 | #ifndef GRPC_EXAMPLE_GREETING_CLIENT_H 2 | #define GRPC_EXAMPLE_GREETING_CLIENT_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | #include "Performance.h" 11 | 12 | class GreetingClient { 13 | public: 14 | GreetingClient(const GreetingClient &) = delete; 15 | 16 | GreetingClient(GreetingClient &&) = delete; 17 | 18 | GreetingClient &operator=(const GreetingClient &) = delete; 19 | 20 | GreetingClient &operator=(GreetingClient &&) = delete; 21 | 22 | // only for C++11, return static local var reference is thread safe 23 | static GreetingClient &getInstance() { 24 | static GreetingClient kSingleInstance; 25 | return kSingleInstance; 26 | } 27 | 28 | bool init(const std::string &name, const std::string &server); 29 | 30 | void run(); 31 | 32 | void stop(); 33 | 34 | private: 35 | GreetingClient() = default; 36 | 37 | virtual ~GreetingClient() = default; 38 | 39 | public: 40 | std::atomic_bool running_{false}; 41 | 42 | std::unique_ptr<::grpc::example::GreetingService::Stub> stub_; 43 | ::grpc::ClientContext context_{}; 44 | std::unique_ptr< 45 | ::grpc::ClientReaderWriter<::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>> 46 | stream_{}; 47 | Performance performance_{}; 48 | std::string name_{}; 49 | }; 50 | 51 | #endif // GRPC_EXAMPLE_GREETING_CLIENT_H 52 | -------------------------------------------------------------------------------- /common/LogHelper.cc: -------------------------------------------------------------------------------- 1 | #include "LogHelper.h" 2 | 3 | INITIALIZE_EASYLOGGINGPP 4 | 5 | bool initLog(int argc, const char** argv, const std::string& logFileName) { 6 | // init log 7 | { 8 | START_EASYLOGGINGPP(argc, argv); 9 | el::Configurations defaultConf; 10 | defaultConf.setToDefault(); 11 | 12 | std::string log_level = "debug"; 13 | const char* env_log_level = getenv("LOG_LEVEL"); 14 | if (env_log_level != nullptr && strncmp(env_log_level, "", sizeof("")) != 0) { 15 | log_level = env_log_level; 16 | } 17 | 18 | std::cout << "LOG_LEVEL: " << log_level << std::endl; 19 | defaultConf.setGlobally(el::ConfigurationType::ToStandardOutput, "true"); 20 | defaultConf.setGlobally(el::ConfigurationType::ToFile, "true"); 21 | 22 | if (log_level == "release") { 23 | el::Loggers::setLoggingLevel(el::Level::Info); 24 | } else { 25 | el::Loggers::setLoggingLevel(el::Level::Debug); 26 | } 27 | 28 | std::string full_log_file_name = "./" + logFileName + ".log"; 29 | defaultConf.setGlobally(el::ConfigurationType::Format, "%datetime %level %fbase-%line] %msg"); 30 | defaultConf.setGlobally(el::ConfigurationType::MaxLogFileSize, "10485760"); // 10MB 31 | defaultConf.setGlobally(el::ConfigurationType::Filename, full_log_file_name); 32 | 33 | el::Loggers::reconfigureLogger("default", defaultConf); 34 | el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck); 35 | el::Loggers::addFlag(el::LoggingFlag::HierarchicalLogging); 36 | el::Loggers::addFlag(el::LoggingFlag::DisableApplicationAbortOnFatalLog); 37 | } 38 | return true; 39 | } -------------------------------------------------------------------------------- /server/GreetingServer.h: -------------------------------------------------------------------------------- 1 | #ifndef GRPC_EXAMPLE_TIME_SERVER_H 2 | #define GRPC_EXAMPLE_TIME_SERVER_H 3 | 4 | #include 5 | #include 6 | 7 | #include "GreetingSession.h" 8 | #include "GrpcType.h" 9 | 10 | class GreetingServer { 11 | public: 12 | GreetingServer(const GreetingServer &) = delete; 13 | 14 | GreetingServer(GreetingServer &&) = delete; 15 | 16 | GreetingServer &operator=(const GreetingServer &) = delete; 17 | 18 | GreetingServer &operator=(GreetingServer &&) = delete; 19 | 20 | // only for C++11, return static local var reference is thread safe 21 | static GreetingServer &getInstance() { 22 | static GreetingServer kSingleInstance; 23 | return kSingleInstance; 24 | } 25 | 26 | bool init(const std::string &server); 27 | 28 | void run(); 29 | 30 | void stop(); 31 | 32 | std::shared_ptr addSession(); 33 | 34 | void removeSession(uint64_t sessionId); 35 | 36 | std::shared_ptr getSession(uint64_t sessionId); 37 | 38 | private: 39 | GreetingServer() = default; 40 | 41 | virtual ~GreetingServer() = default; 42 | 43 | public: 44 | std::atomic_bool running_{false}; 45 | std::atomic_uint64_t session_id_allocator_{0}; 46 | 47 | std::unique_ptr<::grpc::Server> server_{}; 48 | ::grpc::example::GreetingService::AsyncService greeting_async_service_{}; 49 | 50 | // subscribe_greeting 51 | std::unique_ptr<::grpc::ServerCompletionQueue> completion_queue_call_{}; 52 | std::unique_ptr<::grpc::ServerCompletionQueue> completion_queue_notification_{}; 53 | // better choice for production environment: boost::shared_mutex or std::shared_mutex(since C++17) 54 | std::mutex mutex_sessions_{}; 55 | std::unordered_map> sessions_{}; 56 | }; 57 | 58 | #endif // GRPC_EXAMPLE_TIME_SERVER_H 59 | -------------------------------------------------------------------------------- /client/GreetingClient.cc: -------------------------------------------------------------------------------- 1 | #include "GreetingClient.h" 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | #include "LogHelper.h" 9 | 10 | bool GreetingClient::init(const std::string &name, const std::string &server) { 11 | name_ = name; 12 | performance_.setName(name_); 13 | stub_ = ::grpc::example::GreetingService::NewStub( 14 | grpc::CreateChannel(server, grpc::InsecureChannelCredentials())); 15 | return true; 16 | } 17 | 18 | void GreetingClient::run() { 19 | running_ = true; 20 | stream_ = stub_->SubscribeGreetingBySecond(&context_); 21 | 22 | std::thread thread_for_reply{[this] { 23 | LOG(INFO) << "thread_for_reply run"; 24 | ::grpc::example::ReplyGreeting reply; 25 | while (stream_->Read(&reply)) { 26 | performance_.add(std::chrono::duration_cast( 27 | std::chrono::system_clock::now().time_since_epoch()) 28 | .count() - 29 | reply.current_nanosecond()); 30 | LOG(DEBUG) << "receive reply: " << reply.ShortDebugString(); 31 | } 32 | LOG(INFO) << "thread_for_reply stop"; 33 | }}; 34 | 35 | std::thread thread_for_request{[this] { 36 | LOG(INFO) << "thread_for_request run"; 37 | uint64_t increase_name{0}; 38 | ::grpc::example::RequestSubscribe request; 39 | 40 | while (running_) { 41 | request.set_name(name_); 42 | request.set_current_nanosecond(std::chrono::duration_cast( 43 | std::chrono::system_clock::now().time_since_epoch()) 44 | .count()); 45 | if (!stream_->Write(request)) { 46 | LOG(INFO) << "session closed"; 47 | break; 48 | } 49 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); 50 | } 51 | LOG(INFO) << "thread_for_request stop"; 52 | }}; 53 | 54 | if (thread_for_reply.joinable()) { thread_for_reply.join(); } 55 | if (thread_for_request.joinable()) { thread_for_request.join(); } 56 | } 57 | 58 | void GreetingClient::stop() { 59 | if (!running_) { return; } 60 | running_ = false; 61 | LOG(INFO) << "context_->TryCancel() begin"; 62 | context_.TryCancel(); 63 | LOG(INFO) << "context_->TryCancel() end"; 64 | } 65 | -------------------------------------------------------------------------------- /server/GreetingSession.cc: -------------------------------------------------------------------------------- 1 | #include "GreetingSession.h" 2 | 3 | #include "GreetingServer.h" 4 | #include "LogHelper.h" 5 | 6 | bool GreetingSession::init() { 7 | GreetingServer::getInstance().greeting_async_service_.RequestSubscribeGreetingBySecond( 8 | &server_context_, 9 | &subscribe_stream, 10 | GreetingServer::getInstance().completion_queue_call_.get(), 11 | GreetingServer::getInstance().completion_queue_notification_.get(), 12 | reinterpret_cast(session_id_ << GRPC_EVENT_BIT_LENGTH | GRPC_EVENT_CONNECTED)); 13 | return true; 14 | } 15 | 16 | void GreetingSession::process(GrpcEvent event) { 17 | LOG(DEBUG) << "session_id_: " << session_id_ << ", current status: " << status_ << ", event: " << event; 18 | switch (event) { 19 | case GRPC_EVENT_CONNECTED: 20 | subscribe_stream.Read( 21 | &request_, 22 | reinterpret_cast(session_id_ << GRPC_EVENT_BIT_LENGTH | GRPC_EVENT_READ_DONE)); 23 | status_ = GrpcSessionStatus::READY_TO_WRITE; 24 | GreetingServer::getInstance().addSession(); 25 | return; 26 | case GRPC_EVENT_READ_DONE: 27 | LOG(DEBUG) << "session_id_: " << session_id_ << ", new request: " << request_.ShortDebugString(); 28 | performance_.setName(name_); 29 | performance_.add(std::chrono::duration_cast( 30 | std::chrono::system_clock::now().time_since_epoch()) 31 | .count() - 32 | request_.current_nanosecond()); 33 | name_ = request_.name(); 34 | subscribe_stream.Read( 35 | &request_, 36 | reinterpret_cast(session_id_ << GRPC_EVENT_BIT_LENGTH | GRPC_EVENT_READ_DONE)); 37 | reply(); 38 | return; 39 | case GRPC_EVENT_WRITE_DONE: 40 | if (!message_queue_.empty()) { 41 | status_ = GrpcSessionStatus::WAIT_WRITE_DONE; 42 | subscribe_stream.Write( 43 | *message_queue_.front(), 44 | reinterpret_cast(session_id_ << GRPC_EVENT_BIT_LENGTH | GRPC_EVENT_WRITE_DONE)); 45 | message_queue_.pop_front(); 46 | } else { 47 | status_ = GrpcSessionStatus::READY_TO_WRITE; 48 | } 49 | return; 50 | default: 51 | LOG(DEBUG) << "session_id_: " << session_id_ << ", unhandled event: " << event; 52 | return; 53 | } 54 | } 55 | 56 | void GreetingSession::reply() { 57 | if (status_ != GrpcSessionStatus::READY_TO_WRITE && status_ != GrpcSessionStatus::WAIT_WRITE_DONE) { 58 | return; 59 | } 60 | auto new_message = std::make_shared<::grpc::example::ReplyGreeting>(); 61 | new_message->set_message(name_ + ": " + std::to_string(++reply_times_)); 62 | new_message->set_current_nanosecond(std::chrono::duration_cast( 63 | std::chrono::system_clock::now().time_since_epoch()) 64 | .count()); 65 | if (status_ == GrpcSessionStatus::READY_TO_WRITE) { 66 | status_ = GrpcSessionStatus::WAIT_WRITE_DONE; 67 | subscribe_stream.Write( 68 | *new_message, 69 | reinterpret_cast(session_id_ << GRPC_EVENT_BIT_LENGTH | GRPC_EVENT_WRITE_DONE)); 70 | } else { 71 | message_queue_.emplace_back(new_message); 72 | } 73 | } 74 | 75 | void GreetingSession::finish() { 76 | if (status_ == GrpcSessionStatus::WAIT_CONNECT) { return; } 77 | subscribe_stream.Finish( 78 | ::grpc::Status::CANCELLED, 79 | reinterpret_cast(session_id_ << GRPC_EVENT_BIT_LENGTH | GRPC_EVENT_FINISHED)); 80 | } -------------------------------------------------------------------------------- /protobuf/greeting.grpc.pb.cc: -------------------------------------------------------------------------------- 1 | // Generated by the gRPC C++ plugin. 2 | // If you make any local change, they will be lost. 3 | // source: greeting.proto 4 | 5 | #include "greeting.pb.h" 6 | #include "greeting.grpc.pb.h" 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | namespace grpc { 23 | namespace example { 24 | 25 | static const char* GreetingService_method_names[] = { 26 | "/grpc.example.GreetingService/SubscribeGreetingBySecond", 27 | }; 28 | 29 | std::unique_ptr< GreetingService::Stub> GreetingService::NewStub(const std::shared_ptr< ::grpc::ChannelInterface>& channel, const ::grpc::StubOptions& options) { 30 | (void)options; 31 | std::unique_ptr< GreetingService::Stub> stub(new GreetingService::Stub(channel)); 32 | return stub; 33 | } 34 | 35 | GreetingService::Stub::Stub(const std::shared_ptr< ::grpc::ChannelInterface>& channel) 36 | : channel_(channel), rpcmethod_SubscribeGreetingBySecond_(GreetingService_method_names[0], ::grpc::internal::RpcMethod::BIDI_STREAMING, channel) 37 | {} 38 | 39 | ::grpc::ClientReaderWriter< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>* GreetingService::Stub::SubscribeGreetingBySecondRaw(::grpc::ClientContext* context) { 40 | return ::grpc_impl::internal::ClientReaderWriterFactory< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>::Create(channel_.get(), rpcmethod_SubscribeGreetingBySecond_, context); 41 | } 42 | 43 | void GreetingService::Stub::experimental_async::SubscribeGreetingBySecond(::grpc::ClientContext* context, ::grpc::experimental::ClientBidiReactor< ::grpc::example::RequestSubscribe,::grpc::example::ReplyGreeting>* reactor) { 44 | ::grpc_impl::internal::ClientCallbackReaderWriterFactory< ::grpc::example::RequestSubscribe,::grpc::example::ReplyGreeting>::Create(stub_->channel_.get(), stub_->rpcmethod_SubscribeGreetingBySecond_, context, reactor); 45 | } 46 | 47 | ::grpc::ClientAsyncReaderWriter< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>* GreetingService::Stub::AsyncSubscribeGreetingBySecondRaw(::grpc::ClientContext* context, ::grpc::CompletionQueue* cq, void* tag) { 48 | return ::grpc_impl::internal::ClientAsyncReaderWriterFactory< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>::Create(channel_.get(), cq, rpcmethod_SubscribeGreetingBySecond_, context, true, tag); 49 | } 50 | 51 | ::grpc::ClientAsyncReaderWriter< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>* GreetingService::Stub::PrepareAsyncSubscribeGreetingBySecondRaw(::grpc::ClientContext* context, ::grpc::CompletionQueue* cq) { 52 | return ::grpc_impl::internal::ClientAsyncReaderWriterFactory< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>::Create(channel_.get(), cq, rpcmethod_SubscribeGreetingBySecond_, context, false, nullptr); 53 | } 54 | 55 | GreetingService::Service::Service() { 56 | AddMethod(new ::grpc::internal::RpcServiceMethod( 57 | GreetingService_method_names[0], 58 | ::grpc::internal::RpcMethod::BIDI_STREAMING, 59 | new ::grpc::internal::BidiStreamingHandler< GreetingService::Service, ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>( 60 | std::mem_fn(&GreetingService::Service::SubscribeGreetingBySecond), this))); 61 | } 62 | 63 | GreetingService::Service::~Service() { 64 | } 65 | 66 | ::grpc::Status GreetingService::Service::SubscribeGreetingBySecond(::grpc::ServerContext* context, ::grpc::ServerReaderWriter< ::grpc::example::ReplyGreeting, ::grpc::example::RequestSubscribe>* stream) { 67 | (void) context; 68 | (void) stream; 69 | return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, ""); 70 | } 71 | 72 | 73 | } // namespace grpc 74 | } // namespace example 75 | 76 | -------------------------------------------------------------------------------- /server/GreetingServer.cc: -------------------------------------------------------------------------------- 1 | #include "GreetingServer.h" 2 | 3 | #include 4 | 5 | #include 6 | 7 | #include "LogHelper.h" 8 | 9 | bool GreetingServer::init(const std::string &server) { 10 | ::grpc::ServerBuilder builder; 11 | // Listen on the given address without any authentication mechanism. 12 | builder.AddListeningPort(server, grpc::InsecureServerCredentials()); 13 | // Register "service_" as the instance through which we'll communicate with 14 | // clients. In this case it corresponds to an *asynchronous* service. 15 | builder.RegisterService(&greeting_async_service_); 16 | // Get hold of the completion queue used for the asynchronous communication 17 | // with the gRPC runtime. 18 | completion_queue_call_ = builder.AddCompletionQueue(); 19 | completion_queue_notification_ = builder.AddCompletionQueue(); 20 | // Finally assemble the server. 21 | server_ = builder.BuildAndStart(); 22 | LOG(INFO) << "GreetingServer listening on: " << server; 23 | return true; 24 | } 25 | 26 | void GreetingServer::run() { 27 | running_ = true; 28 | std::thread thread_completion_queue_call([this] { 29 | // spawn the first rpc call session 30 | addSession(); 31 | uint64_t session_id; // uniquely identifies a request. 32 | bool ok; 33 | // rpc event "read done / write done / close(already connected)" call-back by this completion queue 34 | while (completion_queue_call_->Next(reinterpret_cast(&session_id), &ok)) { 35 | auto event = static_cast(session_id & GRPC_EVENT_MASK); 36 | session_id = session_id >> GRPC_EVENT_BIT_LENGTH; 37 | LOG(DEBUG) << "session_id_: " << session_id << ", completion queue(call), event: " << event; 38 | if (event == GRPC_EVENT_FINISHED) { 39 | LOG(INFO) << "session_id_: " << session_id << ", event: " << event; 40 | removeSession(session_id); 41 | continue; 42 | } 43 | auto session = getSession(session_id); 44 | if (session == nullptr) { 45 | LOG(INFO) << "session_id_: " << session_id << ", have been removed"; 46 | continue; 47 | } 48 | if (!ok) { 49 | LOG(DEBUG) << "session_id_: " << session_id << ", rpc call closed"; 50 | removeSession(session_id); 51 | continue; 52 | } 53 | { 54 | std::lock_guard local_lock_guard{session->mutex_}; 55 | session->process(event); 56 | } 57 | } 58 | LOG(INFO) << "completion queue(call) exit"; 59 | }); 60 | std::thread thread_completion_queue_notification([this] { 61 | uint64_t session_id; // uniquely identifies a request. 62 | bool ok; 63 | // rpc event "new connection / close(waiting for connect)" call-back by this completion queue 64 | while (completion_queue_notification_->Next(reinterpret_cast(&session_id), &ok)) { 65 | auto event = static_cast(session_id & GRPC_EVENT_MASK); 66 | session_id = session_id >> GRPC_EVENT_BIT_LENGTH; 67 | LOG(INFO) << "session_id_: " << session_id 68 | << ", completion queue(notification), event: " << event; 69 | if (event == GRPC_EVENT_FINISHED) { 70 | removeSession(session_id); 71 | continue; 72 | } 73 | auto session = getSession(session_id); 74 | if (session == nullptr) { 75 | LOG(DEBUG) << "session_id_: " << session_id << ", have been removed"; 76 | continue; 77 | } 78 | if (!ok) { 79 | LOG(DEBUG) << "session_id_: " << session_id << ", rpc call closed"; 80 | removeSession(session_id); 81 | continue; 82 | } 83 | { 84 | std::lock_guard local_lock_guard{session->mutex_}; 85 | // deal request 86 | session->process(event); 87 | } 88 | } 89 | LOG(INFO) << "completion queue(notification) exit"; 90 | }); 91 | std::thread thread_publish_greeting([this] { 92 | while (running_) { 93 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); 94 | { 95 | std::lock_guard local_lock_guard{mutex_sessions_}; 96 | for (const auto &it : sessions_) { 97 | std::lock_guard inner_local_lock_guard{it.second->mutex_}; 98 | it.second->reply(); 99 | } 100 | } 101 | } 102 | }); 103 | if (thread_completion_queue_call.joinable()) { thread_completion_queue_call.join(); } 104 | if (thread_completion_queue_notification.joinable()) { thread_completion_queue_notification.join(); } 105 | if (thread_publish_greeting.joinable()) { thread_publish_greeting.join(); } 106 | LOG(INFO) << "greeting server run() exit"; 107 | } 108 | 109 | void GreetingServer::stop() { 110 | if (!running_) { return; } 111 | running_ = false; 112 | LOG(INFO) << "all sessions TryCancel() begin"; 113 | { 114 | std::lock_guard local_lock_guard{mutex_sessions_}; 115 | // send finish() will trigger error(pure virtual method called) for bi-di stream(grpc version: 1.27.3) 116 | // https://github.com/grpc/grpc/issues/17222 117 | // for (const auto &it : sessions_) { it.second->finish(); } 118 | for (const auto &it : sessions_) { 119 | std::lock_guard local_lock_guard_inner{it.second->mutex_}; 120 | if (it.second->status_ != GrpcSessionStatus::WAIT_CONNECT) { 121 | it.second->server_context_.TryCancel(); 122 | } 123 | } 124 | } 125 | LOG(INFO) << "server_->Shutdown() begin"; 126 | server_->Shutdown(); 127 | // Always shutdown the completion queue after the server. 128 | LOG(INFO) << "completion queue(call) Shutdown() begin"; 129 | completion_queue_call_->Shutdown(); 130 | LOG(INFO) << "completion queue(notification) Shutdown() begin"; 131 | completion_queue_notification_->Shutdown(); 132 | LOG(INFO) << "GreetingServer::stop() exit"; 133 | } 134 | 135 | std::shared_ptr GreetingServer::addSession() { 136 | auto new_session_id = session_id_allocator_++; 137 | auto new_session = std::make_shared(new_session_id); 138 | if (!new_session->init()) { 139 | LOG(ERROR) << "init new session failed"; 140 | return nullptr; 141 | } 142 | std::lock_guard local_lock_guard{mutex_sessions_}; 143 | sessions_[new_session_id] = new_session; 144 | LOG(INFO) << "session_id_: " << new_session_id << ", spawn new session and wait for connect"; 145 | return new_session; 146 | } 147 | 148 | void GreetingServer::removeSession(uint64_t sessionId) { 149 | std::lock_guard local_lock_guard{mutex_sessions_}; 150 | sessions_.erase(sessionId); 151 | } 152 | 153 | std::shared_ptr GreetingServer::getSession(uint64_t sessionId) { 154 | std::lock_guard local_lock_guard{mutex_sessions_}; 155 | auto it = sessions_.find(sessionId); 156 | if (it == sessions_.end()) { return nullptr; } 157 | return it->second; 158 | } 159 | -------------------------------------------------------------------------------- /protobuf/greeting.grpc.pb.h: -------------------------------------------------------------------------------- 1 | // Generated by the gRPC C++ plugin. 2 | // If you make any local change, they will be lost. 3 | // source: greeting.proto 4 | #ifndef GRPC_greeting_2eproto__INCLUDED 5 | #define GRPC_greeting_2eproto__INCLUDED 6 | 7 | #include "greeting.pb.h" 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | namespace grpc { 30 | namespace example { 31 | 32 | class GreetingService final { 33 | public: 34 | static constexpr char const* service_full_name() { 35 | return "grpc.example.GreetingService"; 36 | } 37 | class StubInterface { 38 | public: 39 | virtual ~StubInterface() {} 40 | std::unique_ptr< ::grpc::ClientReaderWriterInterface< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>> SubscribeGreetingBySecond(::grpc::ClientContext* context) { 41 | return std::unique_ptr< ::grpc::ClientReaderWriterInterface< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>>(SubscribeGreetingBySecondRaw(context)); 42 | } 43 | std::unique_ptr< ::grpc::ClientAsyncReaderWriterInterface< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>> AsyncSubscribeGreetingBySecond(::grpc::ClientContext* context, ::grpc::CompletionQueue* cq, void* tag) { 44 | return std::unique_ptr< ::grpc::ClientAsyncReaderWriterInterface< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>>(AsyncSubscribeGreetingBySecondRaw(context, cq, tag)); 45 | } 46 | std::unique_ptr< ::grpc::ClientAsyncReaderWriterInterface< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>> PrepareAsyncSubscribeGreetingBySecond(::grpc::ClientContext* context, ::grpc::CompletionQueue* cq) { 47 | return std::unique_ptr< ::grpc::ClientAsyncReaderWriterInterface< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>>(PrepareAsyncSubscribeGreetingBySecondRaw(context, cq)); 48 | } 49 | class experimental_async_interface { 50 | public: 51 | virtual ~experimental_async_interface() {} 52 | #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL 53 | virtual void SubscribeGreetingBySecond(::grpc::ClientContext* context, ::grpc::ClientBidiReactor< ::grpc::example::RequestSubscribe,::grpc::example::ReplyGreeting>* reactor) = 0; 54 | #else 55 | virtual void SubscribeGreetingBySecond(::grpc::ClientContext* context, ::grpc::experimental::ClientBidiReactor< ::grpc::example::RequestSubscribe,::grpc::example::ReplyGreeting>* reactor) = 0; 56 | #endif 57 | }; 58 | #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL 59 | typedef class experimental_async_interface async_interface; 60 | #endif 61 | #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL 62 | async_interface* async() { return experimental_async(); } 63 | #endif 64 | virtual class experimental_async_interface* experimental_async() { return nullptr; } 65 | private: 66 | virtual ::grpc::ClientReaderWriterInterface< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>* SubscribeGreetingBySecondRaw(::grpc::ClientContext* context) = 0; 67 | virtual ::grpc::ClientAsyncReaderWriterInterface< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>* AsyncSubscribeGreetingBySecondRaw(::grpc::ClientContext* context, ::grpc::CompletionQueue* cq, void* tag) = 0; 68 | virtual ::grpc::ClientAsyncReaderWriterInterface< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>* PrepareAsyncSubscribeGreetingBySecondRaw(::grpc::ClientContext* context, ::grpc::CompletionQueue* cq) = 0; 69 | }; 70 | class Stub final : public StubInterface { 71 | public: 72 | Stub(const std::shared_ptr< ::grpc::ChannelInterface>& channel); 73 | std::unique_ptr< ::grpc::ClientReaderWriter< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>> SubscribeGreetingBySecond(::grpc::ClientContext* context) { 74 | return std::unique_ptr< ::grpc::ClientReaderWriter< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>>(SubscribeGreetingBySecondRaw(context)); 75 | } 76 | std::unique_ptr< ::grpc::ClientAsyncReaderWriter< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>> AsyncSubscribeGreetingBySecond(::grpc::ClientContext* context, ::grpc::CompletionQueue* cq, void* tag) { 77 | return std::unique_ptr< ::grpc::ClientAsyncReaderWriter< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>>(AsyncSubscribeGreetingBySecondRaw(context, cq, tag)); 78 | } 79 | std::unique_ptr< ::grpc::ClientAsyncReaderWriter< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>> PrepareAsyncSubscribeGreetingBySecond(::grpc::ClientContext* context, ::grpc::CompletionQueue* cq) { 80 | return std::unique_ptr< ::grpc::ClientAsyncReaderWriter< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>>(PrepareAsyncSubscribeGreetingBySecondRaw(context, cq)); 81 | } 82 | class experimental_async final : 83 | public StubInterface::experimental_async_interface { 84 | public: 85 | #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL 86 | void SubscribeGreetingBySecond(::grpc::ClientContext* context, ::grpc::ClientBidiReactor< ::grpc::example::RequestSubscribe,::grpc::example::ReplyGreeting>* reactor) override; 87 | #else 88 | void SubscribeGreetingBySecond(::grpc::ClientContext* context, ::grpc::experimental::ClientBidiReactor< ::grpc::example::RequestSubscribe,::grpc::example::ReplyGreeting>* reactor) override; 89 | #endif 90 | private: 91 | friend class Stub; 92 | explicit experimental_async(Stub* stub): stub_(stub) { } 93 | Stub* stub() { return stub_; } 94 | Stub* stub_; 95 | }; 96 | class experimental_async_interface* experimental_async() override { return &async_stub_; } 97 | 98 | private: 99 | std::shared_ptr< ::grpc::ChannelInterface> channel_; 100 | class experimental_async async_stub_{this}; 101 | ::grpc::ClientReaderWriter< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>* SubscribeGreetingBySecondRaw(::grpc::ClientContext* context) override; 102 | ::grpc::ClientAsyncReaderWriter< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>* AsyncSubscribeGreetingBySecondRaw(::grpc::ClientContext* context, ::grpc::CompletionQueue* cq, void* tag) override; 103 | ::grpc::ClientAsyncReaderWriter< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>* PrepareAsyncSubscribeGreetingBySecondRaw(::grpc::ClientContext* context, ::grpc::CompletionQueue* cq) override; 104 | const ::grpc::internal::RpcMethod rpcmethod_SubscribeGreetingBySecond_; 105 | }; 106 | static std::unique_ptr NewStub(const std::shared_ptr< ::grpc::ChannelInterface>& channel, const ::grpc::StubOptions& options = ::grpc::StubOptions()); 107 | 108 | class Service : public ::grpc::Service { 109 | public: 110 | Service(); 111 | virtual ~Service(); 112 | virtual ::grpc::Status SubscribeGreetingBySecond(::grpc::ServerContext* context, ::grpc::ServerReaderWriter< ::grpc::example::ReplyGreeting, ::grpc::example::RequestSubscribe>* stream); 113 | }; 114 | template 115 | class WithAsyncMethod_SubscribeGreetingBySecond : public BaseClass { 116 | private: 117 | void BaseClassMustBeDerivedFromService(const Service* /*service*/) {} 118 | public: 119 | WithAsyncMethod_SubscribeGreetingBySecond() { 120 | ::grpc::Service::MarkMethodAsync(0); 121 | } 122 | ~WithAsyncMethod_SubscribeGreetingBySecond() override { 123 | BaseClassMustBeDerivedFromService(this); 124 | } 125 | // disable synchronous version of this method 126 | ::grpc::Status SubscribeGreetingBySecond(::grpc::ServerContext* /*context*/, ::grpc::ServerReaderWriter< ::grpc::example::ReplyGreeting, ::grpc::example::RequestSubscribe>* /*stream*/) override { 127 | abort(); 128 | return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, ""); 129 | } 130 | void RequestSubscribeGreetingBySecond(::grpc::ServerContext* context, ::grpc::ServerAsyncReaderWriter< ::grpc::example::ReplyGreeting, ::grpc::example::RequestSubscribe>* stream, ::grpc::CompletionQueue* new_call_cq, ::grpc::ServerCompletionQueue* notification_cq, void *tag) { 131 | ::grpc::Service::RequestAsyncBidiStreaming(0, context, stream, new_call_cq, notification_cq, tag); 132 | } 133 | }; 134 | typedef WithAsyncMethod_SubscribeGreetingBySecond AsyncService; 135 | template 136 | class ExperimentalWithCallbackMethod_SubscribeGreetingBySecond : public BaseClass { 137 | private: 138 | void BaseClassMustBeDerivedFromService(const Service* /*service*/) {} 139 | public: 140 | ExperimentalWithCallbackMethod_SubscribeGreetingBySecond() { 141 | #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL 142 | ::grpc::Service:: 143 | #else 144 | ::grpc::Service::experimental(). 145 | #endif 146 | MarkMethodCallback(0, 147 | new ::grpc_impl::internal::CallbackBidiHandler< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>( 148 | [this]( 149 | #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL 150 | ::grpc::CallbackServerContext* 151 | #else 152 | ::grpc::experimental::CallbackServerContext* 153 | #endif 154 | context) { return this->SubscribeGreetingBySecond(context); })); 155 | } 156 | ~ExperimentalWithCallbackMethod_SubscribeGreetingBySecond() override { 157 | BaseClassMustBeDerivedFromService(this); 158 | } 159 | // disable synchronous version of this method 160 | ::grpc::Status SubscribeGreetingBySecond(::grpc::ServerContext* /*context*/, ::grpc::ServerReaderWriter< ::grpc::example::ReplyGreeting, ::grpc::example::RequestSubscribe>* /*stream*/) override { 161 | abort(); 162 | return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, ""); 163 | } 164 | #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL 165 | virtual ::grpc::ServerBidiReactor< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>* SubscribeGreetingBySecond( 166 | ::grpc::CallbackServerContext* /*context*/) 167 | #else 168 | virtual ::grpc::experimental::ServerBidiReactor< ::grpc::example::RequestSubscribe, ::grpc::example::ReplyGreeting>* SubscribeGreetingBySecond( 169 | ::grpc::experimental::CallbackServerContext* /*context*/) 170 | #endif 171 | { return nullptr; } 172 | }; 173 | #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL 174 | typedef ExperimentalWithCallbackMethod_SubscribeGreetingBySecond CallbackService; 175 | #endif 176 | 177 | typedef ExperimentalWithCallbackMethod_SubscribeGreetingBySecond ExperimentalCallbackService; 178 | template 179 | class WithGenericMethod_SubscribeGreetingBySecond : public BaseClass { 180 | private: 181 | void BaseClassMustBeDerivedFromService(const Service* /*service*/) {} 182 | public: 183 | WithGenericMethod_SubscribeGreetingBySecond() { 184 | ::grpc::Service::MarkMethodGeneric(0); 185 | } 186 | ~WithGenericMethod_SubscribeGreetingBySecond() override { 187 | BaseClassMustBeDerivedFromService(this); 188 | } 189 | // disable synchronous version of this method 190 | ::grpc::Status SubscribeGreetingBySecond(::grpc::ServerContext* /*context*/, ::grpc::ServerReaderWriter< ::grpc::example::ReplyGreeting, ::grpc::example::RequestSubscribe>* /*stream*/) override { 191 | abort(); 192 | return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, ""); 193 | } 194 | }; 195 | template 196 | class WithRawMethod_SubscribeGreetingBySecond : public BaseClass { 197 | private: 198 | void BaseClassMustBeDerivedFromService(const Service* /*service*/) {} 199 | public: 200 | WithRawMethod_SubscribeGreetingBySecond() { 201 | ::grpc::Service::MarkMethodRaw(0); 202 | } 203 | ~WithRawMethod_SubscribeGreetingBySecond() override { 204 | BaseClassMustBeDerivedFromService(this); 205 | } 206 | // disable synchronous version of this method 207 | ::grpc::Status SubscribeGreetingBySecond(::grpc::ServerContext* /*context*/, ::grpc::ServerReaderWriter< ::grpc::example::ReplyGreeting, ::grpc::example::RequestSubscribe>* /*stream*/) override { 208 | abort(); 209 | return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, ""); 210 | } 211 | void RequestSubscribeGreetingBySecond(::grpc::ServerContext* context, ::grpc::ServerAsyncReaderWriter< ::grpc::ByteBuffer, ::grpc::ByteBuffer>* stream, ::grpc::CompletionQueue* new_call_cq, ::grpc::ServerCompletionQueue* notification_cq, void *tag) { 212 | ::grpc::Service::RequestAsyncBidiStreaming(0, context, stream, new_call_cq, notification_cq, tag); 213 | } 214 | }; 215 | template 216 | class ExperimentalWithRawCallbackMethod_SubscribeGreetingBySecond : public BaseClass { 217 | private: 218 | void BaseClassMustBeDerivedFromService(const Service* /*service*/) {} 219 | public: 220 | ExperimentalWithRawCallbackMethod_SubscribeGreetingBySecond() { 221 | #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL 222 | ::grpc::Service:: 223 | #else 224 | ::grpc::Service::experimental(). 225 | #endif 226 | MarkMethodRawCallback(0, 227 | new ::grpc_impl::internal::CallbackBidiHandler< ::grpc::ByteBuffer, ::grpc::ByteBuffer>( 228 | [this]( 229 | #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL 230 | ::grpc::CallbackServerContext* 231 | #else 232 | ::grpc::experimental::CallbackServerContext* 233 | #endif 234 | context) { return this->SubscribeGreetingBySecond(context); })); 235 | } 236 | ~ExperimentalWithRawCallbackMethod_SubscribeGreetingBySecond() override { 237 | BaseClassMustBeDerivedFromService(this); 238 | } 239 | // disable synchronous version of this method 240 | ::grpc::Status SubscribeGreetingBySecond(::grpc::ServerContext* /*context*/, ::grpc::ServerReaderWriter< ::grpc::example::ReplyGreeting, ::grpc::example::RequestSubscribe>* /*stream*/) override { 241 | abort(); 242 | return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, ""); 243 | } 244 | #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL 245 | virtual ::grpc::ServerBidiReactor< ::grpc::ByteBuffer, ::grpc::ByteBuffer>* SubscribeGreetingBySecond( 246 | ::grpc::CallbackServerContext* /*context*/) 247 | #else 248 | virtual ::grpc::experimental::ServerBidiReactor< ::grpc::ByteBuffer, ::grpc::ByteBuffer>* SubscribeGreetingBySecond( 249 | ::grpc::experimental::CallbackServerContext* /*context*/) 250 | #endif 251 | { return nullptr; } 252 | }; 253 | typedef Service StreamedUnaryService; 254 | typedef Service SplitStreamedService; 255 | typedef Service StreamedService; 256 | }; 257 | 258 | } // namespace example 259 | } // namespace grpc 260 | 261 | 262 | #endif // GRPC_greeting_2eproto__INCLUDED 263 | -------------------------------------------------------------------------------- /protobuf/greeting.pb.h: -------------------------------------------------------------------------------- 1 | // Generated by the protocol buffer compiler. DO NOT EDIT! 2 | // source: greeting.proto 3 | 4 | #ifndef GOOGLE_PROTOBUF_INCLUDED_greeting_2eproto 5 | #define GOOGLE_PROTOBUF_INCLUDED_greeting_2eproto 6 | 7 | #include 8 | #include 9 | 10 | #include 11 | #if PROTOBUF_VERSION < 3011000 12 | #error This file was generated by a newer version of protoc which is 13 | #error incompatible with your Protocol Buffer headers. Please update 14 | #error your headers. 15 | #endif 16 | #if 3011002 < PROTOBUF_MIN_PROTOC_VERSION 17 | #error This file was generated by an older version of protoc which is 18 | #error incompatible with your Protocol Buffer headers. Please 19 | #error regenerate this file with a newer version of protoc. 20 | #endif 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include // IWYU pragma: export 33 | #include // IWYU pragma: export 34 | #include 35 | // @@protoc_insertion_point(includes) 36 | #include 37 | #define PROTOBUF_INTERNAL_EXPORT_greeting_2eproto 38 | PROTOBUF_NAMESPACE_OPEN 39 | namespace internal { 40 | class AnyMetadata; 41 | } // namespace internal 42 | PROTOBUF_NAMESPACE_CLOSE 43 | 44 | // Internal implementation detail -- do not use these members. 45 | struct TableStruct_greeting_2eproto { 46 | static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] 47 | PROTOBUF_SECTION_VARIABLE(protodesc_cold); 48 | static const ::PROTOBUF_NAMESPACE_ID::internal::AuxillaryParseTableField aux[] 49 | PROTOBUF_SECTION_VARIABLE(protodesc_cold); 50 | static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[2] 51 | PROTOBUF_SECTION_VARIABLE(protodesc_cold); 52 | static const ::PROTOBUF_NAMESPACE_ID::internal::FieldMetadata field_metadata[]; 53 | static const ::PROTOBUF_NAMESPACE_ID::internal::SerializationTable serialization_table[]; 54 | static const ::PROTOBUF_NAMESPACE_ID::uint32 offsets[]; 55 | }; 56 | extern const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_greeting_2eproto; 57 | namespace grpc { 58 | namespace example { 59 | class ReplyGreeting; 60 | class ReplyGreetingDefaultTypeInternal; 61 | extern ReplyGreetingDefaultTypeInternal _ReplyGreeting_default_instance_; 62 | class RequestSubscribe; 63 | class RequestSubscribeDefaultTypeInternal; 64 | extern RequestSubscribeDefaultTypeInternal _RequestSubscribe_default_instance_; 65 | } // namespace example 66 | } // namespace grpc 67 | PROTOBUF_NAMESPACE_OPEN 68 | template<> ::grpc::example::ReplyGreeting* Arena::CreateMaybeMessage<::grpc::example::ReplyGreeting>(Arena*); 69 | template<> ::grpc::example::RequestSubscribe* Arena::CreateMaybeMessage<::grpc::example::RequestSubscribe>(Arena*); 70 | PROTOBUF_NAMESPACE_CLOSE 71 | namespace grpc { 72 | namespace example { 73 | 74 | // =================================================================== 75 | 76 | class RequestSubscribe : 77 | public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:grpc.example.RequestSubscribe) */ { 78 | public: 79 | RequestSubscribe(); 80 | virtual ~RequestSubscribe(); 81 | 82 | RequestSubscribe(const RequestSubscribe& from); 83 | RequestSubscribe(RequestSubscribe&& from) noexcept 84 | : RequestSubscribe() { 85 | *this = ::std::move(from); 86 | } 87 | 88 | inline RequestSubscribe& operator=(const RequestSubscribe& from) { 89 | CopyFrom(from); 90 | return *this; 91 | } 92 | inline RequestSubscribe& operator=(RequestSubscribe&& from) noexcept { 93 | if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { 94 | if (this != &from) InternalSwap(&from); 95 | } else { 96 | CopyFrom(from); 97 | } 98 | return *this; 99 | } 100 | 101 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { 102 | return GetDescriptor(); 103 | } 104 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { 105 | return GetMetadataStatic().descriptor; 106 | } 107 | static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { 108 | return GetMetadataStatic().reflection; 109 | } 110 | static const RequestSubscribe& default_instance(); 111 | 112 | static void InitAsDefaultInstance(); // FOR INTERNAL USE ONLY 113 | static inline const RequestSubscribe* internal_default_instance() { 114 | return reinterpret_cast( 115 | &_RequestSubscribe_default_instance_); 116 | } 117 | static constexpr int kIndexInFileMessages = 118 | 0; 119 | 120 | friend void swap(RequestSubscribe& a, RequestSubscribe& b) { 121 | a.Swap(&b); 122 | } 123 | inline void Swap(RequestSubscribe* other) { 124 | if (other == this) return; 125 | InternalSwap(other); 126 | } 127 | 128 | // implements Message ---------------------------------------------- 129 | 130 | inline RequestSubscribe* New() const final { 131 | return CreateMaybeMessage(nullptr); 132 | } 133 | 134 | RequestSubscribe* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { 135 | return CreateMaybeMessage(arena); 136 | } 137 | void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 138 | void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 139 | void CopyFrom(const RequestSubscribe& from); 140 | void MergeFrom(const RequestSubscribe& from); 141 | PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; 142 | bool IsInitialized() const final; 143 | 144 | size_t ByteSizeLong() const final; 145 | const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; 146 | ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( 147 | ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; 148 | int GetCachedSize() const final { return _cached_size_.Get(); } 149 | 150 | private: 151 | inline void SharedCtor(); 152 | inline void SharedDtor(); 153 | void SetCachedSize(int size) const final; 154 | void InternalSwap(RequestSubscribe* other); 155 | friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; 156 | static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { 157 | return "grpc.example.RequestSubscribe"; 158 | } 159 | private: 160 | inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { 161 | return nullptr; 162 | } 163 | inline void* MaybeArenaPtr() const { 164 | return nullptr; 165 | } 166 | public: 167 | 168 | ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; 169 | private: 170 | static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { 171 | ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_greeting_2eproto); 172 | return ::descriptor_table_greeting_2eproto.file_level_metadata[kIndexInFileMessages]; 173 | } 174 | 175 | public: 176 | 177 | // nested types ---------------------------------------------------- 178 | 179 | // accessors ------------------------------------------------------- 180 | 181 | enum : int { 182 | kNameFieldNumber = 1, 183 | kCurrentNanosecondFieldNumber = 2, 184 | }; 185 | // string name = 1; 186 | void clear_name(); 187 | const std::string& name() const; 188 | void set_name(const std::string& value); 189 | void set_name(std::string&& value); 190 | void set_name(const char* value); 191 | void set_name(const char* value, size_t size); 192 | std::string* mutable_name(); 193 | std::string* release_name(); 194 | void set_allocated_name(std::string* name); 195 | private: 196 | const std::string& _internal_name() const; 197 | void _internal_set_name(const std::string& value); 198 | std::string* _internal_mutable_name(); 199 | public: 200 | 201 | // uint64 current_nanosecond = 2; 202 | void clear_current_nanosecond(); 203 | ::PROTOBUF_NAMESPACE_ID::uint64 current_nanosecond() const; 204 | void set_current_nanosecond(::PROTOBUF_NAMESPACE_ID::uint64 value); 205 | private: 206 | ::PROTOBUF_NAMESPACE_ID::uint64 _internal_current_nanosecond() const; 207 | void _internal_set_current_nanosecond(::PROTOBUF_NAMESPACE_ID::uint64 value); 208 | public: 209 | 210 | // @@protoc_insertion_point(class_scope:grpc.example.RequestSubscribe) 211 | private: 212 | class _Internal; 213 | 214 | ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; 215 | ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr name_; 216 | ::PROTOBUF_NAMESPACE_ID::uint64 current_nanosecond_; 217 | mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; 218 | friend struct ::TableStruct_greeting_2eproto; 219 | }; 220 | // ------------------------------------------------------------------- 221 | 222 | class ReplyGreeting : 223 | public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:grpc.example.ReplyGreeting) */ { 224 | public: 225 | ReplyGreeting(); 226 | virtual ~ReplyGreeting(); 227 | 228 | ReplyGreeting(const ReplyGreeting& from); 229 | ReplyGreeting(ReplyGreeting&& from) noexcept 230 | : ReplyGreeting() { 231 | *this = ::std::move(from); 232 | } 233 | 234 | inline ReplyGreeting& operator=(const ReplyGreeting& from) { 235 | CopyFrom(from); 236 | return *this; 237 | } 238 | inline ReplyGreeting& operator=(ReplyGreeting&& from) noexcept { 239 | if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { 240 | if (this != &from) InternalSwap(&from); 241 | } else { 242 | CopyFrom(from); 243 | } 244 | return *this; 245 | } 246 | 247 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() { 248 | return GetDescriptor(); 249 | } 250 | static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() { 251 | return GetMetadataStatic().descriptor; 252 | } 253 | static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() { 254 | return GetMetadataStatic().reflection; 255 | } 256 | static const ReplyGreeting& default_instance(); 257 | 258 | static void InitAsDefaultInstance(); // FOR INTERNAL USE ONLY 259 | static inline const ReplyGreeting* internal_default_instance() { 260 | return reinterpret_cast( 261 | &_ReplyGreeting_default_instance_); 262 | } 263 | static constexpr int kIndexInFileMessages = 264 | 1; 265 | 266 | friend void swap(ReplyGreeting& a, ReplyGreeting& b) { 267 | a.Swap(&b); 268 | } 269 | inline void Swap(ReplyGreeting* other) { 270 | if (other == this) return; 271 | InternalSwap(other); 272 | } 273 | 274 | // implements Message ---------------------------------------------- 275 | 276 | inline ReplyGreeting* New() const final { 277 | return CreateMaybeMessage(nullptr); 278 | } 279 | 280 | ReplyGreeting* New(::PROTOBUF_NAMESPACE_ID::Arena* arena) const final { 281 | return CreateMaybeMessage(arena); 282 | } 283 | void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 284 | void MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) final; 285 | void CopyFrom(const ReplyGreeting& from); 286 | void MergeFrom(const ReplyGreeting& from); 287 | PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; 288 | bool IsInitialized() const final; 289 | 290 | size_t ByteSizeLong() const final; 291 | const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final; 292 | ::PROTOBUF_NAMESPACE_ID::uint8* _InternalSerialize( 293 | ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final; 294 | int GetCachedSize() const final { return _cached_size_.Get(); } 295 | 296 | private: 297 | inline void SharedCtor(); 298 | inline void SharedDtor(); 299 | void SetCachedSize(int size) const final; 300 | void InternalSwap(ReplyGreeting* other); 301 | friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata; 302 | static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() { 303 | return "grpc.example.ReplyGreeting"; 304 | } 305 | private: 306 | inline ::PROTOBUF_NAMESPACE_ID::Arena* GetArenaNoVirtual() const { 307 | return nullptr; 308 | } 309 | inline void* MaybeArenaPtr() const { 310 | return nullptr; 311 | } 312 | public: 313 | 314 | ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final; 315 | private: 316 | static ::PROTOBUF_NAMESPACE_ID::Metadata GetMetadataStatic() { 317 | ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&::descriptor_table_greeting_2eproto); 318 | return ::descriptor_table_greeting_2eproto.file_level_metadata[kIndexInFileMessages]; 319 | } 320 | 321 | public: 322 | 323 | // nested types ---------------------------------------------------- 324 | 325 | // accessors ------------------------------------------------------- 326 | 327 | enum : int { 328 | kMessageFieldNumber = 1, 329 | kCurrentNanosecondFieldNumber = 2, 330 | }; 331 | // string message = 1; 332 | void clear_message(); 333 | const std::string& message() const; 334 | void set_message(const std::string& value); 335 | void set_message(std::string&& value); 336 | void set_message(const char* value); 337 | void set_message(const char* value, size_t size); 338 | std::string* mutable_message(); 339 | std::string* release_message(); 340 | void set_allocated_message(std::string* message); 341 | private: 342 | const std::string& _internal_message() const; 343 | void _internal_set_message(const std::string& value); 344 | std::string* _internal_mutable_message(); 345 | public: 346 | 347 | // uint64 current_nanosecond = 2; 348 | void clear_current_nanosecond(); 349 | ::PROTOBUF_NAMESPACE_ID::uint64 current_nanosecond() const; 350 | void set_current_nanosecond(::PROTOBUF_NAMESPACE_ID::uint64 value); 351 | private: 352 | ::PROTOBUF_NAMESPACE_ID::uint64 _internal_current_nanosecond() const; 353 | void _internal_set_current_nanosecond(::PROTOBUF_NAMESPACE_ID::uint64 value); 354 | public: 355 | 356 | // @@protoc_insertion_point(class_scope:grpc.example.ReplyGreeting) 357 | private: 358 | class _Internal; 359 | 360 | ::PROTOBUF_NAMESPACE_ID::internal::InternalMetadataWithArena _internal_metadata_; 361 | ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr message_; 362 | ::PROTOBUF_NAMESPACE_ID::uint64 current_nanosecond_; 363 | mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; 364 | friend struct ::TableStruct_greeting_2eproto; 365 | }; 366 | // =================================================================== 367 | 368 | 369 | // =================================================================== 370 | 371 | #ifdef __GNUC__ 372 | #pragma GCC diagnostic push 373 | #pragma GCC diagnostic ignored "-Wstrict-aliasing" 374 | #endif // __GNUC__ 375 | // RequestSubscribe 376 | 377 | // string name = 1; 378 | inline void RequestSubscribe::clear_name() { 379 | name_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 380 | } 381 | inline const std::string& RequestSubscribe::name() const { 382 | // @@protoc_insertion_point(field_get:grpc.example.RequestSubscribe.name) 383 | return _internal_name(); 384 | } 385 | inline void RequestSubscribe::set_name(const std::string& value) { 386 | _internal_set_name(value); 387 | // @@protoc_insertion_point(field_set:grpc.example.RequestSubscribe.name) 388 | } 389 | inline std::string* RequestSubscribe::mutable_name() { 390 | // @@protoc_insertion_point(field_mutable:grpc.example.RequestSubscribe.name) 391 | return _internal_mutable_name(); 392 | } 393 | inline const std::string& RequestSubscribe::_internal_name() const { 394 | return name_.GetNoArena(); 395 | } 396 | inline void RequestSubscribe::_internal_set_name(const std::string& value) { 397 | 398 | name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); 399 | } 400 | inline void RequestSubscribe::set_name(std::string&& value) { 401 | 402 | name_.SetNoArena( 403 | &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); 404 | // @@protoc_insertion_point(field_set_rvalue:grpc.example.RequestSubscribe.name) 405 | } 406 | inline void RequestSubscribe::set_name(const char* value) { 407 | GOOGLE_DCHECK(value != nullptr); 408 | 409 | name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); 410 | // @@protoc_insertion_point(field_set_char:grpc.example.RequestSubscribe.name) 411 | } 412 | inline void RequestSubscribe::set_name(const char* value, size_t size) { 413 | 414 | name_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), 415 | ::std::string(reinterpret_cast(value), size)); 416 | // @@protoc_insertion_point(field_set_pointer:grpc.example.RequestSubscribe.name) 417 | } 418 | inline std::string* RequestSubscribe::_internal_mutable_name() { 419 | 420 | return name_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 421 | } 422 | inline std::string* RequestSubscribe::release_name() { 423 | // @@protoc_insertion_point(field_release:grpc.example.RequestSubscribe.name) 424 | 425 | return name_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 426 | } 427 | inline void RequestSubscribe::set_allocated_name(std::string* name) { 428 | if (name != nullptr) { 429 | 430 | } else { 431 | 432 | } 433 | name_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), name); 434 | // @@protoc_insertion_point(field_set_allocated:grpc.example.RequestSubscribe.name) 435 | } 436 | 437 | // uint64 current_nanosecond = 2; 438 | inline void RequestSubscribe::clear_current_nanosecond() { 439 | current_nanosecond_ = PROTOBUF_ULONGLONG(0); 440 | } 441 | inline ::PROTOBUF_NAMESPACE_ID::uint64 RequestSubscribe::_internal_current_nanosecond() const { 442 | return current_nanosecond_; 443 | } 444 | inline ::PROTOBUF_NAMESPACE_ID::uint64 RequestSubscribe::current_nanosecond() const { 445 | // @@protoc_insertion_point(field_get:grpc.example.RequestSubscribe.current_nanosecond) 446 | return _internal_current_nanosecond(); 447 | } 448 | inline void RequestSubscribe::_internal_set_current_nanosecond(::PROTOBUF_NAMESPACE_ID::uint64 value) { 449 | 450 | current_nanosecond_ = value; 451 | } 452 | inline void RequestSubscribe::set_current_nanosecond(::PROTOBUF_NAMESPACE_ID::uint64 value) { 453 | _internal_set_current_nanosecond(value); 454 | // @@protoc_insertion_point(field_set:grpc.example.RequestSubscribe.current_nanosecond) 455 | } 456 | 457 | // ------------------------------------------------------------------- 458 | 459 | // ReplyGreeting 460 | 461 | // string message = 1; 462 | inline void ReplyGreeting::clear_message() { 463 | message_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 464 | } 465 | inline const std::string& ReplyGreeting::message() const { 466 | // @@protoc_insertion_point(field_get:grpc.example.ReplyGreeting.message) 467 | return _internal_message(); 468 | } 469 | inline void ReplyGreeting::set_message(const std::string& value) { 470 | _internal_set_message(value); 471 | // @@protoc_insertion_point(field_set:grpc.example.ReplyGreeting.message) 472 | } 473 | inline std::string* ReplyGreeting::mutable_message() { 474 | // @@protoc_insertion_point(field_mutable:grpc.example.ReplyGreeting.message) 475 | return _internal_mutable_message(); 476 | } 477 | inline const std::string& ReplyGreeting::_internal_message() const { 478 | return message_.GetNoArena(); 479 | } 480 | inline void ReplyGreeting::_internal_set_message(const std::string& value) { 481 | 482 | message_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), value); 483 | } 484 | inline void ReplyGreeting::set_message(std::string&& value) { 485 | 486 | message_.SetNoArena( 487 | &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::move(value)); 488 | // @@protoc_insertion_point(field_set_rvalue:grpc.example.ReplyGreeting.message) 489 | } 490 | inline void ReplyGreeting::set_message(const char* value) { 491 | GOOGLE_DCHECK(value != nullptr); 492 | 493 | message_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), ::std::string(value)); 494 | // @@protoc_insertion_point(field_set_char:grpc.example.ReplyGreeting.message) 495 | } 496 | inline void ReplyGreeting::set_message(const char* value, size_t size) { 497 | 498 | message_.SetNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), 499 | ::std::string(reinterpret_cast(value), size)); 500 | // @@protoc_insertion_point(field_set_pointer:grpc.example.ReplyGreeting.message) 501 | } 502 | inline std::string* ReplyGreeting::_internal_mutable_message() { 503 | 504 | return message_.MutableNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 505 | } 506 | inline std::string* ReplyGreeting::release_message() { 507 | // @@protoc_insertion_point(field_release:grpc.example.ReplyGreeting.message) 508 | 509 | return message_.ReleaseNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 510 | } 511 | inline void ReplyGreeting::set_allocated_message(std::string* message) { 512 | if (message != nullptr) { 513 | 514 | } else { 515 | 516 | } 517 | message_.SetAllocatedNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), message); 518 | // @@protoc_insertion_point(field_set_allocated:grpc.example.ReplyGreeting.message) 519 | } 520 | 521 | // uint64 current_nanosecond = 2; 522 | inline void ReplyGreeting::clear_current_nanosecond() { 523 | current_nanosecond_ = PROTOBUF_ULONGLONG(0); 524 | } 525 | inline ::PROTOBUF_NAMESPACE_ID::uint64 ReplyGreeting::_internal_current_nanosecond() const { 526 | return current_nanosecond_; 527 | } 528 | inline ::PROTOBUF_NAMESPACE_ID::uint64 ReplyGreeting::current_nanosecond() const { 529 | // @@protoc_insertion_point(field_get:grpc.example.ReplyGreeting.current_nanosecond) 530 | return _internal_current_nanosecond(); 531 | } 532 | inline void ReplyGreeting::_internal_set_current_nanosecond(::PROTOBUF_NAMESPACE_ID::uint64 value) { 533 | 534 | current_nanosecond_ = value; 535 | } 536 | inline void ReplyGreeting::set_current_nanosecond(::PROTOBUF_NAMESPACE_ID::uint64 value) { 537 | _internal_set_current_nanosecond(value); 538 | // @@protoc_insertion_point(field_set:grpc.example.ReplyGreeting.current_nanosecond) 539 | } 540 | 541 | #ifdef __GNUC__ 542 | #pragma GCC diagnostic pop 543 | #endif // __GNUC__ 544 | // ------------------------------------------------------------------- 545 | 546 | 547 | // @@protoc_insertion_point(namespace_scope) 548 | 549 | } // namespace example 550 | } // namespace grpc 551 | 552 | // @@protoc_insertion_point(global_scope) 553 | 554 | #include 555 | #endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_greeting_2eproto 556 | -------------------------------------------------------------------------------- /protobuf/greeting.pb.cc: -------------------------------------------------------------------------------- 1 | // Generated by the protocol buffer compiler. DO NOT EDIT! 2 | // source: greeting.proto 3 | 4 | #include "greeting.pb.h" 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | // @@protoc_insertion_point(includes) 16 | #include 17 | namespace grpc { 18 | namespace example { 19 | class RequestSubscribeDefaultTypeInternal { 20 | public: 21 | ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; 22 | } _RequestSubscribe_default_instance_; 23 | class ReplyGreetingDefaultTypeInternal { 24 | public: 25 | ::PROTOBUF_NAMESPACE_ID::internal::ExplicitlyConstructed _instance; 26 | } _ReplyGreeting_default_instance_; 27 | } // namespace example 28 | } // namespace grpc 29 | static void InitDefaultsscc_info_ReplyGreeting_greeting_2eproto() { 30 | GOOGLE_PROTOBUF_VERIFY_VERSION; 31 | 32 | { 33 | void* ptr = &::grpc::example::_ReplyGreeting_default_instance_; 34 | new (ptr) ::grpc::example::ReplyGreeting(); 35 | ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); 36 | } 37 | ::grpc::example::ReplyGreeting::InitAsDefaultInstance(); 38 | } 39 | 40 | ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_ReplyGreeting_greeting_2eproto = 41 | {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, 0, InitDefaultsscc_info_ReplyGreeting_greeting_2eproto}, {}}; 42 | 43 | static void InitDefaultsscc_info_RequestSubscribe_greeting_2eproto() { 44 | GOOGLE_PROTOBUF_VERIFY_VERSION; 45 | 46 | { 47 | void* ptr = &::grpc::example::_RequestSubscribe_default_instance_; 48 | new (ptr) ::grpc::example::RequestSubscribe(); 49 | ::PROTOBUF_NAMESPACE_ID::internal::OnShutdownDestroyMessage(ptr); 50 | } 51 | ::grpc::example::RequestSubscribe::InitAsDefaultInstance(); 52 | } 53 | 54 | ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo<0> scc_info_RequestSubscribe_greeting_2eproto = 55 | {{ATOMIC_VAR_INIT(::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase::kUninitialized), 0, 0, InitDefaultsscc_info_RequestSubscribe_greeting_2eproto}, {}}; 56 | 57 | static ::PROTOBUF_NAMESPACE_ID::Metadata file_level_metadata_greeting_2eproto[2]; 58 | static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_greeting_2eproto = nullptr; 59 | static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_greeting_2eproto = nullptr; 60 | 61 | const ::PROTOBUF_NAMESPACE_ID::uint32 TableStruct_greeting_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { 62 | ~0u, // no _has_bits_ 63 | PROTOBUF_FIELD_OFFSET(::grpc::example::RequestSubscribe, _internal_metadata_), 64 | ~0u, // no _extensions_ 65 | ~0u, // no _oneof_case_ 66 | ~0u, // no _weak_field_map_ 67 | PROTOBUF_FIELD_OFFSET(::grpc::example::RequestSubscribe, name_), 68 | PROTOBUF_FIELD_OFFSET(::grpc::example::RequestSubscribe, current_nanosecond_), 69 | ~0u, // no _has_bits_ 70 | PROTOBUF_FIELD_OFFSET(::grpc::example::ReplyGreeting, _internal_metadata_), 71 | ~0u, // no _extensions_ 72 | ~0u, // no _oneof_case_ 73 | ~0u, // no _weak_field_map_ 74 | PROTOBUF_FIELD_OFFSET(::grpc::example::ReplyGreeting, message_), 75 | PROTOBUF_FIELD_OFFSET(::grpc::example::ReplyGreeting, current_nanosecond_), 76 | }; 77 | static const ::PROTOBUF_NAMESPACE_ID::internal::MigrationSchema schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { 78 | { 0, -1, sizeof(::grpc::example::RequestSubscribe)}, 79 | { 7, -1, sizeof(::grpc::example::ReplyGreeting)}, 80 | }; 81 | 82 | static ::PROTOBUF_NAMESPACE_ID::Message const * const file_default_instances[] = { 83 | reinterpret_cast(&::grpc::example::_RequestSubscribe_default_instance_), 84 | reinterpret_cast(&::grpc::example::_ReplyGreeting_default_instance_), 85 | }; 86 | 87 | const char descriptor_table_protodef_greeting_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = 88 | "\n\016greeting.proto\022\014grpc.example\"<\n\020Reques" 89 | "tSubscribe\022\014\n\004name\030\001 \001(\t\022\032\n\022current_nano" 90 | "second\030\002 \001(\004\"<\n\rReplyGreeting\022\017\n\007message" 91 | "\030\001 \001(\t\022\032\n\022current_nanosecond\030\002 \001(\0042q\n\017Gr" 92 | "eetingService\022^\n\031SubscribeGreetingBySeco" 93 | "nd\022\036.grpc.example.RequestSubscribe\032\033.grp" 94 | "c.example.ReplyGreeting\"\000(\0010\001b\006proto3" 95 | ; 96 | static const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable*const descriptor_table_greeting_2eproto_deps[1] = { 97 | }; 98 | static ::PROTOBUF_NAMESPACE_ID::internal::SCCInfoBase*const descriptor_table_greeting_2eproto_sccs[2] = { 99 | &scc_info_ReplyGreeting_greeting_2eproto.base, 100 | &scc_info_RequestSubscribe_greeting_2eproto.base, 101 | }; 102 | static ::PROTOBUF_NAMESPACE_ID::internal::once_flag descriptor_table_greeting_2eproto_once; 103 | static bool descriptor_table_greeting_2eproto_initialized = false; 104 | const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_greeting_2eproto = { 105 | &descriptor_table_greeting_2eproto_initialized, descriptor_table_protodef_greeting_2eproto, "greeting.proto", 277, 106 | &descriptor_table_greeting_2eproto_once, descriptor_table_greeting_2eproto_sccs, descriptor_table_greeting_2eproto_deps, 2, 0, 107 | schemas, file_default_instances, TableStruct_greeting_2eproto::offsets, 108 | file_level_metadata_greeting_2eproto, 2, file_level_enum_descriptors_greeting_2eproto, file_level_service_descriptors_greeting_2eproto, 109 | }; 110 | 111 | // Force running AddDescriptors() at dynamic initialization time. 112 | static bool dynamic_init_dummy_greeting_2eproto = ( ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_greeting_2eproto), true); 113 | namespace grpc { 114 | namespace example { 115 | 116 | // =================================================================== 117 | 118 | void RequestSubscribe::InitAsDefaultInstance() { 119 | } 120 | class RequestSubscribe::_Internal { 121 | public: 122 | }; 123 | 124 | RequestSubscribe::RequestSubscribe() 125 | : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { 126 | SharedCtor(); 127 | // @@protoc_insertion_point(constructor:grpc.example.RequestSubscribe) 128 | } 129 | RequestSubscribe::RequestSubscribe(const RequestSubscribe& from) 130 | : ::PROTOBUF_NAMESPACE_ID::Message(), 131 | _internal_metadata_(nullptr) { 132 | _internal_metadata_.MergeFrom(from._internal_metadata_); 133 | name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 134 | if (!from._internal_name().empty()) { 135 | name_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name_); 136 | } 137 | current_nanosecond_ = from.current_nanosecond_; 138 | // @@protoc_insertion_point(copy_constructor:grpc.example.RequestSubscribe) 139 | } 140 | 141 | void RequestSubscribe::SharedCtor() { 142 | ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_RequestSubscribe_greeting_2eproto.base); 143 | name_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 144 | current_nanosecond_ = PROTOBUF_ULONGLONG(0); 145 | } 146 | 147 | RequestSubscribe::~RequestSubscribe() { 148 | // @@protoc_insertion_point(destructor:grpc.example.RequestSubscribe) 149 | SharedDtor(); 150 | } 151 | 152 | void RequestSubscribe::SharedDtor() { 153 | name_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 154 | } 155 | 156 | void RequestSubscribe::SetCachedSize(int size) const { 157 | _cached_size_.Set(size); 158 | } 159 | const RequestSubscribe& RequestSubscribe::default_instance() { 160 | ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_RequestSubscribe_greeting_2eproto.base); 161 | return *internal_default_instance(); 162 | } 163 | 164 | 165 | void RequestSubscribe::Clear() { 166 | // @@protoc_insertion_point(message_clear_start:grpc.example.RequestSubscribe) 167 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 168 | // Prevent compiler warnings about cached_has_bits being unused 169 | (void) cached_has_bits; 170 | 171 | name_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 172 | current_nanosecond_ = PROTOBUF_ULONGLONG(0); 173 | _internal_metadata_.Clear(); 174 | } 175 | 176 | const char* RequestSubscribe::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { 177 | #define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure 178 | while (!ctx->Done(&ptr)) { 179 | ::PROTOBUF_NAMESPACE_ID::uint32 tag; 180 | ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); 181 | CHK_(ptr); 182 | switch (tag >> 3) { 183 | // string name = 1; 184 | case 1: 185 | if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { 186 | auto str = _internal_mutable_name(); 187 | ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); 188 | CHK_(::PROTOBUF_NAMESPACE_ID::internal::VerifyUTF8(str, "grpc.example.RequestSubscribe.name")); 189 | CHK_(ptr); 190 | } else goto handle_unusual; 191 | continue; 192 | // uint64 current_nanosecond = 2; 193 | case 2: 194 | if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 16)) { 195 | current_nanosecond_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); 196 | CHK_(ptr); 197 | } else goto handle_unusual; 198 | continue; 199 | default: { 200 | handle_unusual: 201 | if ((tag & 7) == 4 || tag == 0) { 202 | ctx->SetLastTag(tag); 203 | goto success; 204 | } 205 | ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); 206 | CHK_(ptr != nullptr); 207 | continue; 208 | } 209 | } // switch 210 | } // while 211 | success: 212 | return ptr; 213 | failure: 214 | ptr = nullptr; 215 | goto success; 216 | #undef CHK_ 217 | } 218 | 219 | ::PROTOBUF_NAMESPACE_ID::uint8* RequestSubscribe::_InternalSerialize( 220 | ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { 221 | // @@protoc_insertion_point(serialize_to_array_start:grpc.example.RequestSubscribe) 222 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 223 | (void) cached_has_bits; 224 | 225 | // string name = 1; 226 | if (this->name().size() > 0) { 227 | ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( 228 | this->_internal_name().data(), static_cast(this->_internal_name().length()), 229 | ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::SERIALIZE, 230 | "grpc.example.RequestSubscribe.name"); 231 | target = stream->WriteStringMaybeAliased( 232 | 1, this->_internal_name(), target); 233 | } 234 | 235 | // uint64 current_nanosecond = 2; 236 | if (this->current_nanosecond() != 0) { 237 | target = stream->EnsureSpace(target); 238 | target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteUInt64ToArray(2, this->_internal_current_nanosecond(), target); 239 | } 240 | 241 | if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { 242 | target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( 243 | _internal_metadata_.unknown_fields(), target, stream); 244 | } 245 | // @@protoc_insertion_point(serialize_to_array_end:grpc.example.RequestSubscribe) 246 | return target; 247 | } 248 | 249 | size_t RequestSubscribe::ByteSizeLong() const { 250 | // @@protoc_insertion_point(message_byte_size_start:grpc.example.RequestSubscribe) 251 | size_t total_size = 0; 252 | 253 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 254 | // Prevent compiler warnings about cached_has_bits being unused 255 | (void) cached_has_bits; 256 | 257 | // string name = 1; 258 | if (this->name().size() > 0) { 259 | total_size += 1 + 260 | ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( 261 | this->_internal_name()); 262 | } 263 | 264 | // uint64 current_nanosecond = 2; 265 | if (this->current_nanosecond() != 0) { 266 | total_size += 1 + 267 | ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::UInt64Size( 268 | this->_internal_current_nanosecond()); 269 | } 270 | 271 | if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { 272 | return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( 273 | _internal_metadata_, total_size, &_cached_size_); 274 | } 275 | int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); 276 | SetCachedSize(cached_size); 277 | return total_size; 278 | } 279 | 280 | void RequestSubscribe::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { 281 | // @@protoc_insertion_point(generalized_merge_from_start:grpc.example.RequestSubscribe) 282 | GOOGLE_DCHECK_NE(&from, this); 283 | const RequestSubscribe* source = 284 | ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( 285 | &from); 286 | if (source == nullptr) { 287 | // @@protoc_insertion_point(generalized_merge_from_cast_fail:grpc.example.RequestSubscribe) 288 | ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); 289 | } else { 290 | // @@protoc_insertion_point(generalized_merge_from_cast_success:grpc.example.RequestSubscribe) 291 | MergeFrom(*source); 292 | } 293 | } 294 | 295 | void RequestSubscribe::MergeFrom(const RequestSubscribe& from) { 296 | // @@protoc_insertion_point(class_specific_merge_from_start:grpc.example.RequestSubscribe) 297 | GOOGLE_DCHECK_NE(&from, this); 298 | _internal_metadata_.MergeFrom(from._internal_metadata_); 299 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 300 | (void) cached_has_bits; 301 | 302 | if (from.name().size() > 0) { 303 | 304 | name_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.name_); 305 | } 306 | if (from.current_nanosecond() != 0) { 307 | _internal_set_current_nanosecond(from._internal_current_nanosecond()); 308 | } 309 | } 310 | 311 | void RequestSubscribe::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { 312 | // @@protoc_insertion_point(generalized_copy_from_start:grpc.example.RequestSubscribe) 313 | if (&from == this) return; 314 | Clear(); 315 | MergeFrom(from); 316 | } 317 | 318 | void RequestSubscribe::CopyFrom(const RequestSubscribe& from) { 319 | // @@protoc_insertion_point(class_specific_copy_from_start:grpc.example.RequestSubscribe) 320 | if (&from == this) return; 321 | Clear(); 322 | MergeFrom(from); 323 | } 324 | 325 | bool RequestSubscribe::IsInitialized() const { 326 | return true; 327 | } 328 | 329 | void RequestSubscribe::InternalSwap(RequestSubscribe* other) { 330 | using std::swap; 331 | _internal_metadata_.Swap(&other->_internal_metadata_); 332 | name_.Swap(&other->name_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), 333 | GetArenaNoVirtual()); 334 | swap(current_nanosecond_, other->current_nanosecond_); 335 | } 336 | 337 | ::PROTOBUF_NAMESPACE_ID::Metadata RequestSubscribe::GetMetadata() const { 338 | return GetMetadataStatic(); 339 | } 340 | 341 | 342 | // =================================================================== 343 | 344 | void ReplyGreeting::InitAsDefaultInstance() { 345 | } 346 | class ReplyGreeting::_Internal { 347 | public: 348 | }; 349 | 350 | ReplyGreeting::ReplyGreeting() 351 | : ::PROTOBUF_NAMESPACE_ID::Message(), _internal_metadata_(nullptr) { 352 | SharedCtor(); 353 | // @@protoc_insertion_point(constructor:grpc.example.ReplyGreeting) 354 | } 355 | ReplyGreeting::ReplyGreeting(const ReplyGreeting& from) 356 | : ::PROTOBUF_NAMESPACE_ID::Message(), 357 | _internal_metadata_(nullptr) { 358 | _internal_metadata_.MergeFrom(from._internal_metadata_); 359 | message_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 360 | if (!from._internal_message().empty()) { 361 | message_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.message_); 362 | } 363 | current_nanosecond_ = from.current_nanosecond_; 364 | // @@protoc_insertion_point(copy_constructor:grpc.example.ReplyGreeting) 365 | } 366 | 367 | void ReplyGreeting::SharedCtor() { 368 | ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&scc_info_ReplyGreeting_greeting_2eproto.base); 369 | message_.UnsafeSetDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 370 | current_nanosecond_ = PROTOBUF_ULONGLONG(0); 371 | } 372 | 373 | ReplyGreeting::~ReplyGreeting() { 374 | // @@protoc_insertion_point(destructor:grpc.example.ReplyGreeting) 375 | SharedDtor(); 376 | } 377 | 378 | void ReplyGreeting::SharedDtor() { 379 | message_.DestroyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 380 | } 381 | 382 | void ReplyGreeting::SetCachedSize(int size) const { 383 | _cached_size_.Set(size); 384 | } 385 | const ReplyGreeting& ReplyGreeting::default_instance() { 386 | ::PROTOBUF_NAMESPACE_ID::internal::InitSCC(&::scc_info_ReplyGreeting_greeting_2eproto.base); 387 | return *internal_default_instance(); 388 | } 389 | 390 | 391 | void ReplyGreeting::Clear() { 392 | // @@protoc_insertion_point(message_clear_start:grpc.example.ReplyGreeting) 393 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 394 | // Prevent compiler warnings about cached_has_bits being unused 395 | (void) cached_has_bits; 396 | 397 | message_.ClearToEmptyNoArena(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited()); 398 | current_nanosecond_ = PROTOBUF_ULONGLONG(0); 399 | _internal_metadata_.Clear(); 400 | } 401 | 402 | const char* ReplyGreeting::_InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) { 403 | #define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure 404 | while (!ctx->Done(&ptr)) { 405 | ::PROTOBUF_NAMESPACE_ID::uint32 tag; 406 | ptr = ::PROTOBUF_NAMESPACE_ID::internal::ReadTag(ptr, &tag); 407 | CHK_(ptr); 408 | switch (tag >> 3) { 409 | // string message = 1; 410 | case 1: 411 | if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 10)) { 412 | auto str = _internal_mutable_message(); 413 | ptr = ::PROTOBUF_NAMESPACE_ID::internal::InlineGreedyStringParser(str, ptr, ctx); 414 | CHK_(::PROTOBUF_NAMESPACE_ID::internal::VerifyUTF8(str, "grpc.example.ReplyGreeting.message")); 415 | CHK_(ptr); 416 | } else goto handle_unusual; 417 | continue; 418 | // uint64 current_nanosecond = 2; 419 | case 2: 420 | if (PROTOBUF_PREDICT_TRUE(static_cast<::PROTOBUF_NAMESPACE_ID::uint8>(tag) == 16)) { 421 | current_nanosecond_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint(&ptr); 422 | CHK_(ptr); 423 | } else goto handle_unusual; 424 | continue; 425 | default: { 426 | handle_unusual: 427 | if ((tag & 7) == 4 || tag == 0) { 428 | ctx->SetLastTag(tag); 429 | goto success; 430 | } 431 | ptr = UnknownFieldParse(tag, &_internal_metadata_, ptr, ctx); 432 | CHK_(ptr != nullptr); 433 | continue; 434 | } 435 | } // switch 436 | } // while 437 | success: 438 | return ptr; 439 | failure: 440 | ptr = nullptr; 441 | goto success; 442 | #undef CHK_ 443 | } 444 | 445 | ::PROTOBUF_NAMESPACE_ID::uint8* ReplyGreeting::_InternalSerialize( 446 | ::PROTOBUF_NAMESPACE_ID::uint8* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { 447 | // @@protoc_insertion_point(serialize_to_array_start:grpc.example.ReplyGreeting) 448 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 449 | (void) cached_has_bits; 450 | 451 | // string message = 1; 452 | if (this->message().size() > 0) { 453 | ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( 454 | this->_internal_message().data(), static_cast(this->_internal_message().length()), 455 | ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::SERIALIZE, 456 | "grpc.example.ReplyGreeting.message"); 457 | target = stream->WriteStringMaybeAliased( 458 | 1, this->_internal_message(), target); 459 | } 460 | 461 | // uint64 current_nanosecond = 2; 462 | if (this->current_nanosecond() != 0) { 463 | target = stream->EnsureSpace(target); 464 | target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::WriteUInt64ToArray(2, this->_internal_current_nanosecond(), target); 465 | } 466 | 467 | if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { 468 | target = ::PROTOBUF_NAMESPACE_ID::internal::WireFormat::InternalSerializeUnknownFieldsToArray( 469 | _internal_metadata_.unknown_fields(), target, stream); 470 | } 471 | // @@protoc_insertion_point(serialize_to_array_end:grpc.example.ReplyGreeting) 472 | return target; 473 | } 474 | 475 | size_t ReplyGreeting::ByteSizeLong() const { 476 | // @@protoc_insertion_point(message_byte_size_start:grpc.example.ReplyGreeting) 477 | size_t total_size = 0; 478 | 479 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 480 | // Prevent compiler warnings about cached_has_bits being unused 481 | (void) cached_has_bits; 482 | 483 | // string message = 1; 484 | if (this->message().size() > 0) { 485 | total_size += 1 + 486 | ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( 487 | this->_internal_message()); 488 | } 489 | 490 | // uint64 current_nanosecond = 2; 491 | if (this->current_nanosecond() != 0) { 492 | total_size += 1 + 493 | ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::UInt64Size( 494 | this->_internal_current_nanosecond()); 495 | } 496 | 497 | if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { 498 | return ::PROTOBUF_NAMESPACE_ID::internal::ComputeUnknownFieldsSize( 499 | _internal_metadata_, total_size, &_cached_size_); 500 | } 501 | int cached_size = ::PROTOBUF_NAMESPACE_ID::internal::ToCachedSize(total_size); 502 | SetCachedSize(cached_size); 503 | return total_size; 504 | } 505 | 506 | void ReplyGreeting::MergeFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { 507 | // @@protoc_insertion_point(generalized_merge_from_start:grpc.example.ReplyGreeting) 508 | GOOGLE_DCHECK_NE(&from, this); 509 | const ReplyGreeting* source = 510 | ::PROTOBUF_NAMESPACE_ID::DynamicCastToGenerated( 511 | &from); 512 | if (source == nullptr) { 513 | // @@protoc_insertion_point(generalized_merge_from_cast_fail:grpc.example.ReplyGreeting) 514 | ::PROTOBUF_NAMESPACE_ID::internal::ReflectionOps::Merge(from, this); 515 | } else { 516 | // @@protoc_insertion_point(generalized_merge_from_cast_success:grpc.example.ReplyGreeting) 517 | MergeFrom(*source); 518 | } 519 | } 520 | 521 | void ReplyGreeting::MergeFrom(const ReplyGreeting& from) { 522 | // @@protoc_insertion_point(class_specific_merge_from_start:grpc.example.ReplyGreeting) 523 | GOOGLE_DCHECK_NE(&from, this); 524 | _internal_metadata_.MergeFrom(from._internal_metadata_); 525 | ::PROTOBUF_NAMESPACE_ID::uint32 cached_has_bits = 0; 526 | (void) cached_has_bits; 527 | 528 | if (from.message().size() > 0) { 529 | 530 | message_.AssignWithDefault(&::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), from.message_); 531 | } 532 | if (from.current_nanosecond() != 0) { 533 | _internal_set_current_nanosecond(from._internal_current_nanosecond()); 534 | } 535 | } 536 | 537 | void ReplyGreeting::CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message& from) { 538 | // @@protoc_insertion_point(generalized_copy_from_start:grpc.example.ReplyGreeting) 539 | if (&from == this) return; 540 | Clear(); 541 | MergeFrom(from); 542 | } 543 | 544 | void ReplyGreeting::CopyFrom(const ReplyGreeting& from) { 545 | // @@protoc_insertion_point(class_specific_copy_from_start:grpc.example.ReplyGreeting) 546 | if (&from == this) return; 547 | Clear(); 548 | MergeFrom(from); 549 | } 550 | 551 | bool ReplyGreeting::IsInitialized() const { 552 | return true; 553 | } 554 | 555 | void ReplyGreeting::InternalSwap(ReplyGreeting* other) { 556 | using std::swap; 557 | _internal_metadata_.Swap(&other->_internal_metadata_); 558 | message_.Swap(&other->message_, &::PROTOBUF_NAMESPACE_ID::internal::GetEmptyStringAlreadyInited(), 559 | GetArenaNoVirtual()); 560 | swap(current_nanosecond_, other->current_nanosecond_); 561 | } 562 | 563 | ::PROTOBUF_NAMESPACE_ID::Metadata ReplyGreeting::GetMetadata() const { 564 | return GetMetadataStatic(); 565 | } 566 | 567 | 568 | // @@protoc_insertion_point(namespace_scope) 569 | } // namespace example 570 | } // namespace grpc 571 | PROTOBUF_NAMESPACE_OPEN 572 | template<> PROTOBUF_NOINLINE ::grpc::example::RequestSubscribe* Arena::CreateMaybeMessage< ::grpc::example::RequestSubscribe >(Arena* arena) { 573 | return Arena::CreateInternal< ::grpc::example::RequestSubscribe >(arena); 574 | } 575 | template<> PROTOBUF_NOINLINE ::grpc::example::ReplyGreeting* Arena::CreateMaybeMessage< ::grpc::example::ReplyGreeting >(Arena* arena) { 576 | return Arena::CreateInternal< ::grpc::example::ReplyGreeting >(arena); 577 | } 578 | PROTOBUF_NAMESPACE_CLOSE 579 | 580 | // @@protoc_insertion_point(global_scope) 581 | #include 582 | --------------------------------------------------------------------------------