diff --git a/etcd/v3/action_constants.hpp b/etcd/v3/action_constants.hpp index 5c3fdc5..faa77c2 100644 --- a/etcd/v3/action_constants.hpp +++ b/etcd/v3/action_constants.hpp @@ -13,6 +13,17 @@ namespace etcdv3 extern char const * LOCK_ACTION; extern char const * UNLOCK_ACTION; extern char const * TXN_ACTION; + + extern char const * NUL; + + extern char const * KEEPALIVE_CREATE; + extern char const * KEEPALIVE_WRITE; + extern char const * KEEPALIVE_READ; + extern char const * KEEPALIVE_DONE; + + extern char const * WATCH_CREATE; + extern char const * WATCH_WRITE; + extern char const * WATCH_WRITES_DONE; } #endif diff --git a/src/v3/Action.cpp b/src/v3/Action.cpp index 558e9fa..43d2ef2 100644 --- a/src/v3/Action.cpp +++ b/src/v3/Action.cpp @@ -1,4 +1,5 @@ #include +#include "etcd/v3/action_constants.hpp" #include "etcd/v3/Action.hpp" etcdv3::Action::Action(etcdv3::ActionParameters params) @@ -50,7 +51,5 @@ std::string etcdv3::detail::string_plus_one(std::string const &value) { return ret; } } - // see: noPrefixEnd in etcd, however c++ doesn't allows '\0' inside a string, thus we use - // the UTF-8 char U+0000 (i.e., "\xC0\x80"). - return {"\xC0\x80"}; + return {etcdv3::NUL}; } diff --git a/src/v3/AsyncDeleteAction.cpp b/src/v3/AsyncDeleteAction.cpp index 8bf0933..4e93e62 100644 --- a/src/v3/AsyncDeleteAction.cpp +++ b/src/v3/AsyncDeleteAction.cpp @@ -12,7 +12,7 @@ etcdv3::AsyncDeleteAction::AsyncDeleteAction(ActionParameters param) if(parameters.withPrefix) { if (parameters.key.empty()) { - del_request.set_range_end(detail::string_plus_one("\0")); + del_request.set_range_end(detail::string_plus_one(etcdv3::NUL)); } else { del_request.set_range_end(detail::string_plus_one(parameters.key)); } diff --git a/src/v3/AsyncGetAction.cpp b/src/v3/AsyncGetAction.cpp index bed840e..0d3ba7e 100644 --- a/src/v3/AsyncGetAction.cpp +++ b/src/v3/AsyncGetAction.cpp @@ -11,7 +11,7 @@ etcdv3::AsyncGetAction::AsyncGetAction(etcdv3::ActionParameters param) { RangeRequest get_request; if (parameters.key.empty()) { - get_request.set_key("\0"); + get_request.set_key(etcdv3::NUL); } else { get_request.set_key(parameters.key); } @@ -19,7 +19,7 @@ etcdv3::AsyncGetAction::AsyncGetAction(etcdv3::ActionParameters param) if(parameters.withPrefix) { if (parameters.key.empty()) { - get_request.set_range_end(detail::string_plus_one("\0")); + get_request.set_range_end(detail::string_plus_one(etcdv3::NUL)); } else { get_request.set_range_end(detail::string_plus_one(parameters.key)); } diff --git a/src/v3/AsyncLeaseAction.cpp b/src/v3/AsyncLeaseAction.cpp index 2ef12aa..62e57ba 100644 --- a/src/v3/AsyncLeaseAction.cpp +++ b/src/v3/AsyncLeaseAction.cpp @@ -59,11 +59,11 @@ etcdv3::AsyncLeaseKeepAliveAction::AsyncLeaseKeepAliveAction(etcdv3::ActionParam : etcdv3::Action(param) { isCancelled = false; - stream = parameters.lease_stub->AsyncLeaseKeepAlive(&context, &cq_, (void*)"keepalive create"); + stream = parameters.lease_stub->AsyncLeaseKeepAlive(&context, &cq_, (void*)etcdv3::KEEPALIVE_CREATE); void *got_tag = nullptr; bool ok = false; - if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)"keepalive create") { + if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)etcdv3::KEEPALIVE_CREATE) { // ok } else { throw std::runtime_error("Failed to create a lease keep-alive connection"); @@ -100,12 +100,12 @@ etcd::Response etcdv3::AsyncLeaseKeepAliveAction::Refresh() void *got_tag = nullptr; bool ok = false; - stream->Write(leasekeepalive_request, (void *)"keepalive write"); + stream->Write(leasekeepalive_request, (void *)etcdv3::KEEPALIVE_WRITE); // wait write finish - if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)"keepalive write") { - stream->Read(&reply, (void*)"keepalive read"); + if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)etcdv3::KEEPALIVE_WRITE) { + stream->Read(&reply, (void*)etcdv3::KEEPALIVE_READ); // wait read finish - if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)"keepalive read") { + if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)etcdv3::KEEPALIVE_READ) { auto resp = ParseResponse(); auto duration = std::chrono::duration_cast( std::chrono::high_resolution_clock::now() - start_timepoint); @@ -121,7 +121,7 @@ void etcdv3::AsyncLeaseKeepAliveAction::CancelKeepAlive() if(isCancelled == false) { isCancelled = true; - stream->WritesDone((void*)"keepalive done"); + stream->WritesDone((void*)etcdv3::KEEPALIVE_DONE); grpc::Status status; stream->Finish(&status, (void *)this); cq_.Shutdown(); diff --git a/src/v3/AsyncWatchAction.cpp b/src/v3/AsyncWatchAction.cpp index 0c4a77e..6454993 100644 --- a/src/v3/AsyncWatchAction.cpp +++ b/src/v3/AsyncWatchAction.cpp @@ -10,7 +10,7 @@ etcdv3::AsyncWatchAction::AsyncWatchAction(etcdv3::ActionParameters param) : etcdv3::Action(param) { isCancelled.store(false); - stream = parameters.watch_stub->AsyncWatch(&context,&cq_,(void*)"create"); + stream = parameters.watch_stub->AsyncWatch(&context,&cq_,(void*)etcdv3::WATCH_CREATE); WatchRequest watch_req; WatchCreateRequest watch_create_req; @@ -21,7 +21,7 @@ etcdv3::AsyncWatchAction::AsyncWatchAction(etcdv3::ActionParameters param) if(parameters.withPrefix) { if (parameters.key.empty()) { - watch_create_req.set_range_end(detail::string_plus_one("\0")); + watch_create_req.set_range_end(detail::string_plus_one(etcdv3::NUL)); } else { watch_create_req.set_range_end(detail::string_plus_one(parameters.key)); } @@ -35,14 +35,14 @@ etcdv3::AsyncWatchAction::AsyncWatchAction(etcdv3::ActionParameters param) // wait "create" success (the stream becomes ready) void *got_tag; bool ok = false; - if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)"create") { - stream->Write(watch_req, (void *)"write"); + if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)etcdv3::WATCH_CREATE) { + stream->Write(watch_req, (void *)etcdv3::WATCH_WRITE); } else { throw std::runtime_error("failed to create a watch connection"); } // wait "write" (WatchCreateRequest) success, and start to read the first reply - if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)"write") { + if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)etcdv3::WATCH_WRITE) { stream->Read(&reply, (void*)this); } else { throw std::runtime_error("failed to write WatchCreateRequest to server"); @@ -60,7 +60,7 @@ void etcdv3::AsyncWatchAction::waitForResponse() { break; } - if(got_tag == (void*)"writes done") { + if(got_tag == (void*)etcdv3::WATCH_WRITES_DONE) { isCancelled.store(true); cq_.Shutdown(); break; @@ -78,7 +78,7 @@ void etcdv3::AsyncWatchAction::waitForResponse() // 1. watch for a future revision, return immediately with empty events set // 2. receive any effective events. isCancelled.store(true); - stream->WritesDone((void*)"writes done"); + stream->WritesDone((void*)etcdv3::WATCH_WRITES_DONE); grpc::Status status; stream->Finish(&status, (void *)this); cq_.Shutdown(); @@ -101,7 +101,7 @@ void etcdv3::AsyncWatchAction::CancelWatch() { std::lock_guard scope_lock(this->protect_is_cancalled); if (!isCancelled.exchange(true)) { - stream->WritesDone((void*)"writes done"); + stream->WritesDone((void*)etcdv3::WATCH_WRITES_DONE); grpc::Status status; stream->Finish(&status, (void *)this); cq_.Shutdown(); @@ -123,7 +123,7 @@ void etcdv3::AsyncWatchAction::waitForResponse(std::function