#ifndef CHROMECAST_CAST_CORE_GRPC_GRPC_STUB_H_
#define CHROMECAST_CAST_CORE_GRPC_GRPC_STUB_H_
#include <grpcpp/grpcpp.h>
#include "base/logging.h"
#include "chromecast/cast_core/grpc/grpc_server_streaming_call.h"
#include "chromecast/cast_core/grpc/grpc_unary_call.h"
namespace cast {
namespace utils {
template <typename TService>
class GrpcStub {
public:
using SyncInterface = typename TService::StubInterface;
using AsyncInterface = typename TService::StubInterface::async_interface;
explicit GrpcStub(const std::string& endpoint)
: GrpcStub(grpc::CreateChannel(endpoint,
grpc::InsecureChannelCredentials())) {}
explicit GrpcStub(std::shared_ptr<grpc::Channel> channel)
: channel_(std::move(channel)), stub_(TService::NewStub(channel_)) {
DCHECK(channel_);
}
GrpcStub(const GrpcStub& rhs) : GrpcStub(rhs.channel_) {}
GrpcStub& operator=(const GrpcStub& rhs) {
DCHECK(rhs.channel_);
channel_ = rhs.channel_;
stub_ = TService::NewStub(rhs.channel_);
return *this;
}
GrpcStub(GrpcStub&& rhs) = default;
GrpcStub& operator=(GrpcStub&& rhs) = default;
virtual ~GrpcStub() = default;
template <typename TGrpcCall>
TGrpcCall CreateCall() {
return TGrpcCall(stub_.get());
}
template <typename TGrpcCall>
TGrpcCall CreateCall(typename TGrpcCall::Request request) {
return TGrpcCall(stub_.get(), std::move(request));
}
private:
std::shared_ptr<grpc::Channel> channel_;
std::unique_ptr<typename TService::StubInterface> stub_;
};
}
}
#endif