Skip to content

Instantly share code, notes, and snippets.

@ppLorins
Created July 18, 2019 07:23
Show Gist options
  • Save ppLorins/d75e60dbbbbd84e218928d9fe6781869 to your computer and use it in GitHub Desktop.
Save ppLorins/d75e60dbbbbd84e218928d9fe6781869 to your computer and use it in GitHub Desktop.
an example for an unary async grpc c++ server.
/*
*
* Copyright 2015 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <iostream>
#include <memory>
#include <string>
#include <chrono>
#include <grpc++/grpc++.h>
#ifdef BAZEL_BUILD
#include "examples/protos/helloworld.grpc.pb.h"
#else
#include "helloworld.grpc.pb.h"
#endif
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using helloworld::HelloRequest;
using helloworld::HelloReply;
using helloworld::Greeter;
int g_sleep = 3;
// Logic and data behind the server's behavior.
class GreeterServiceImpl final : public Greeter::Service {
Status SayHello(ServerContext* context, const HelloRequest* request,
HelloReply* reply) override {
//std::cout << "client peer addr:" << context->peer() << std::endl;
//std::cout << "receive.." << request->DebugString() << std::endl;
std::string prefix("Hello ");
reply->set_message(prefix + request->name());
/*
std::chrono::seconds _sec(g_sleep);
std::cout << "begin sleep.." << std::endl;
std::this_thread::sleep_for(_sec);
std::cout << "end sleep.." << std::endl;
*/
return Status::OK;
}
::grpc::Status SayHelloEx(::grpc::ServerContext* context, ::grpc::ServerReaderWriter< ::helloworld::HelloReply, ::helloworld::HelloRequest>* stream) {
HelloRequest _req;
HelloReply reply;
int _counter = 0;
while (stream->Read(&_req)) {
reply.set_message("stream rsp : this is the " + std::to_string(++_counter));
stream->Write(reply);
}
return ::grpc::Status::OK;
}
};
void RunServer(int port) {
#define ADDR_BUFF_SIZE (50)
char sz_addr[ADDR_BUFF_SIZE] = { 0 };
std::memset(sz_addr, 0, ADDR_BUFF_SIZE);
std::snprintf(sz_addr,ADDR_BUFF_SIZE,"0.0.0.0:%d",port);
std::string server_address(sz_addr);
GreeterServiceImpl service;
ServerBuilder builder;
// Listen on the given address without any authentication mechanism.
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
// Register "service" as the instance through which we'll communicate with
// clients. In this case it corresponds to an *synchronous* service.
builder.RegisterService(&service);
// Finally assemble the server.
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on " << server_address << std::endl;
// Wait for the server to shutdown. Note that some other thread must be
// responsible for shutting down the server for this call to ever return.
server->Wait();
}
int main(int argc, char** argv) {
int port = 50051;
if (argc <= 1) {
std::cout << "Usage:./program --port=xx --sleep=xx";
return 0;
}
const char * target_str = "--port=";
auto p_target = std::strstr(argv[1],target_str);
if (p_target == nullptr) {
printf("para error argv[1] should be --port=xx...\n");
return 0;
}
p_target += std::strlen(target_str);
port = std::atoi(p_target);
if (argc == 3) {
target_str = "--sleep=";
p_target = std::strstr(argv[2],target_str);
if (p_target != nullptr) {
p_target += std::strlen(target_str);
g_sleep = std::atoi(p_target);
}
}
RunServer(port);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment