nghttpx: Convert MemcachedStatusCode to enum class

This commit is contained in:
Tatsuhiro Tsujikawa 2018-10-17 14:11:36 +09:00
parent 0c4e9fef29
commit d68edf56c0
5 changed files with 27 additions and 20 deletions

View File

@ -120,7 +120,8 @@ namespace {
void clear_request(std::deque<std::unique_ptr<MemcachedRequest>> &q) {
for (auto &req : q) {
if (req->cb) {
req->cb(req.get(), MemcachedResult(MEMCACHED_ERR_EXT_NETWORK_ERROR));
req->cb(req.get(),
MemcachedResult(MemcachedStatusCode::EXT_NETWORK_ERROR));
}
}
q.clear();
@ -451,7 +452,8 @@ int MemcachedConnection::parse_packet() {
parse_state_.extralen = *in++;
// skip 1 byte reserved data type
++in;
parse_state_.status_code = util::get_uint16(in);
parse_state_.status_code =
static_cast<MemcachedStatusCode>(util::get_uint16(in));
in += 2;
parse_state_.totalbody = util::get_uint32(in);
in += 4;
@ -481,7 +483,8 @@ int MemcachedConnection::parse_packet() {
}
if (parse_state_.op == MemcachedOp::GET &&
parse_state_.status_code == 0 && parse_state_.extralen == 0) {
parse_state_.status_code == MemcachedStatusCode::NO_ERROR &&
parse_state_.extralen == 0) {
MCLOG(WARN, this) << "response for GET does not have extra";
return -1;
}
@ -538,9 +541,9 @@ int MemcachedConnection::parse_packet() {
}
if (LOG_ENABLED(INFO)) {
if (parse_state_.status_code) {
MCLOG(INFO, this)
<< "response returned error status: " << parse_state_.status_code;
if (parse_state_.status_code != MemcachedStatusCode::NO_ERROR) {
MCLOG(INFO, this) << "response returned error status: "
<< static_cast<uint16_t>(parse_state_.status_code);
}
}

View File

@ -44,6 +44,7 @@ namespace shrpx {
struct MemcachedRequest;
enum class MemcachedOp : uint8_t;
enum class MemcachedStatusCode : uint16_t;
enum class MemcachedParseState {
HEADER24,
@ -69,7 +70,7 @@ struct MemcachedParseContext {
// Parser state; see enum above
MemcachedParseState state;
// status_code in response
int status_code;
MemcachedStatusCode status_code;
// op in response
MemcachedOp op;
};

View File

@ -31,18 +31,18 @@
namespace shrpx {
enum MemcachedStatusCode {
MEMCACHED_ERR_NO_ERROR,
MEMCACHED_ERR_EXT_NETWORK_ERROR = 0x1001,
enum class MemcachedStatusCode : uint16_t {
NO_ERROR,
EXT_NETWORK_ERROR = 0x1001,
};
struct MemcachedResult {
MemcachedResult(int status_code) : status_code(status_code) {}
MemcachedResult(int status_code, std::vector<uint8_t> value)
MemcachedResult(MemcachedStatusCode status_code) : status_code(status_code) {}
MemcachedResult(MemcachedStatusCode status_code, std::vector<uint8_t> value)
: value(std::move(value)), status_code(status_code) {}
std::vector<uint8_t> value;
int status_code;
MemcachedStatusCode status_code;
};
} // namespace shrpx

View File

@ -343,12 +343,14 @@ int tls_session_new_cb(SSL *ssl, SSL_SESSION *session) {
req->cb = [](MemcachedRequest *req, MemcachedResult res) {
if (LOG_ENABLED(INFO)) {
LOG(INFO) << "Memcached: session cache done. key=" << req->key
<< ", status_code=" << res.status_code << ", value="
<< ", status_code=" << static_cast<uint16_t>(res.status_code)
<< ", value="
<< std::string(std::begin(res.value), std::end(res.value));
}
if (res.status_code != 0) {
if (res.status_code != MemcachedStatusCode::NO_ERROR) {
LOG(WARN) << "Memcached: failed to cache session key=" << req->key
<< ", status_code=" << res.status_code << ", value="
<< ", status_code=" << static_cast<uint16_t>(res.status_code)
<< ", value="
<< std::string(std::begin(res.value), std::end(res.value));
}
};
@ -404,7 +406,8 @@ SSL_SESSION *tls_session_get_cb(SSL *ssl,
util::format_hex(balloc, StringRef{id, static_cast<size_t>(idlen)});
req->cb = [conn](MemcachedRequest *, MemcachedResult res) {
if (LOG_ENABLED(INFO)) {
LOG(INFO) << "Memcached: returned status code " << res.status_code;
LOG(INFO) << "Memcached: returned status code "
<< static_cast<uint16_t>(res.status_code);
}
// We might stop reading, so start it again
@ -415,7 +418,7 @@ SSL_SESSION *tls_session_get_cb(SSL *ssl,
ev_timer_again(conn->loop, &conn->wt);
conn->tls.cached_session_lookup_req = nullptr;
if (res.status_code != 0) {
if (res.status_code != MemcachedStatusCode::NO_ERROR) {
conn->tls.handshake_state = TLSHandshakeState::CANCEL_SESSION_CACHE;
return;
}

View File

@ -275,9 +275,9 @@ void memcached_get_ticket_key_cb(struct ev_loop *loop, ev_timer *w,
req->op = MemcachedOp::GET;
req->cb = [conn_handler, w](MemcachedRequest *req, MemcachedResult res) {
switch (res.status_code) {
case MEMCACHED_ERR_NO_ERROR:
case MemcachedStatusCode::NO_ERROR:
break;
case MEMCACHED_ERR_EXT_NETWORK_ERROR:
case MemcachedStatusCode::EXT_NETWORK_ERROR:
conn_handler->on_tls_ticket_key_network_error(w);
return;
default: