Switch to enum instead of string cast to void*

This commit is contained in:
Eric Musgrave 2020-12-11 10:27:56 -05:00
parent 7ba7a17e1e
commit 4b62b2913d
2 changed files with 7 additions and 11 deletions

View File

@ -16,6 +16,7 @@ using grpc::ClientAsyncResponseReader;
namespace etcdv3 {
class AsyncLeaseKeepAliveAction : public etcdv3::Action {
enum class Type { READ = 1, WRITE = 2, CONNECT = 3, WRITES_DONE = 4, FINISH = 5 };
public:
AsyncLeaseKeepAliveAction(etcdv3::ActionParameters param);
AsyncLeaseKeepAliveResponse ParseResponse();
@ -24,8 +25,6 @@ class AsyncLeaseKeepAliveAction : public etcdv3::Action {
private:
LeaseKeepAliveResponse reply;
std::unique_ptr<ClientAsyncReaderWriter<LeaseKeepAliveRequest, LeaseKeepAliveResponse>> stream;
char* doneTag = "writes done";
};
} // namespace etcdv3

View File

@ -8,10 +8,7 @@ using etcdserverpb::RangeRequest;
using etcdserverpb::RangeResponse;
etcdv3::AsyncLeaseKeepAliveAction::AsyncLeaseKeepAliveAction(etcdv3::ActionParameters param) : etcdv3::Action(param) {
char* createTag = "create";
char* writeTag = "write";
stream = parameters.lease_stub->AsyncLeaseKeepAlive(&context, &cq_, (void*)createTag);
stream = parameters.lease_stub->AsyncLeaseKeepAlive(&context, &cq_, reinterpret_cast<void*>(Type::CONNECT));
LeaseKeepAliveRequest request;
request.set_id(parameters.lease_id);
@ -19,15 +16,15 @@ etcdv3::AsyncLeaseKeepAliveAction::AsyncLeaseKeepAliveAction(etcdv3::ActionParam
// wait "create" success (the stream becomes ready)
void* got_tag;
bool ok = false;
if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void*)createTag) {
stream->Write(request, (void*)writeTag);
if (cq_.Next(&got_tag, &ok) && ok && got_tag == reinterpret_cast<void*>(Type::CONNECT)) {
stream->Write(request, reinterpret_cast<void*>(Type::WRITE));
} else {
throw std::runtime_error("failed to create a keepalive connection");
}
// wait "write" (LeaseKeepAliveRequest) success, and start to read the first
// reply
if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void*)writeTag) {
if (cq_.Next(&got_tag, &ok) && ok && got_tag == reinterpret_cast<void*>(Type::WRITE)) {
stream->Read(&reply, (void*)this);
} else {
throw std::runtime_error("failed to write LeaseKeepAliveRequest to server");
@ -42,14 +39,14 @@ void etcdv3::AsyncLeaseKeepAliveAction::waitForResponse() {
if (ok == false) {
break;
}
if (got_tag == (void*)doneTag) {
if (got_tag == reinterpret_cast<void*>(Type::WRITES_DONE)) {
cq_.Shutdown();
break;
}
if (got_tag == (void*)this) // read tag
{
if (reply.ByteSize()) {
stream->WritesDone((void*)doneTag);
stream->WritesDone(reinterpret_cast<void*>(Type::WRITES_DONE));
} else {
stream->Read(&reply, (void*)this);
}