Skip to content

Commit

Permalink
Merge pull request #1 from yandaren/develop
Browse files Browse the repository at this point in the history
fix the node data value got by zk_cpp::get_node may not be completed
  • Loading branch information
yandaren authored Jan 8, 2020
2 parents 81837c0 + 957a349 commit 5f46ca7
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions zk_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,34 @@ zoo_rc zk_cpp::get_node(const char* path, std::string& out_value, zoo_state

char buf[zoo_value_buf_len] = { 0 };
int buf_size = sizeof(buf);
zoo_rc rt = (zoo_rc)zoo_get((zhandle_t*)m_zh, path, watch, buf, &buf_size, &s);
if (rt == z_ok) {
out_value = std::move(std::string(buf, buf_size));
}
if (info) {
details::state_to_zoo_state_t(s, info);
char *data_buffer = buf;
zoo_rc rt = (zoo_rc)zoo_get((zhandle_t*)m_zh, path, watch, data_buffer, &buf_size, &s);
do {
if (rt != z_ok) {
break;
}

// check data complete
if (s.dataLength > buf_size) {
data_buffer = (char*)malloc(s.dataLength + 1);
buf_size = s.dataLength;
rt = (zoo_rc)zoo_get((zhandle_t*)m_zh, path, watch, data_buffer, &buf_size, &s);
if (rt != z_ok) {
break;
}
}

out_value = std::move(std::string(data_buffer, buf_size));
if (info) {
details::state_to_zoo_state_t(s, info);
}
} while (0);

// try free buffer
if (data_buffer != buf) {
free(data_buffer);
}

return rt;
}

Expand Down

0 comments on commit 5f46ca7

Please sign in to comment.