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

View File

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