nghttpx: Convert memcached op to enum class

This commit is contained in:
Tatsuhiro Tsujikawa 2018-10-17 14:02:57 +09:00
parent 571404c6e8
commit 0c4e9fef29
5 changed files with 18 additions and 16 deletions

View File

@ -445,7 +445,7 @@ int MemcachedConnection::parse_packet() {
} }
++in; ++in;
parse_state_.op = *in++; parse_state_.op = static_cast<MemcachedOp>(*in++);
parse_state_.keylen = util::get_uint16(in); parse_state_.keylen = util::get_uint16(in);
in += 2; in += 2;
parse_state_.extralen = *in++; parse_state_.extralen = *in++;
@ -463,7 +463,8 @@ int MemcachedConnection::parse_packet() {
if (req->op != parse_state_.op) { if (req->op != parse_state_.op) {
MCLOG(WARN, this) MCLOG(WARN, this)
<< "opcode in response does not match to the request: want " << "opcode in response does not match to the request: want "
<< static_cast<uint32_t>(req->op) << ", got " << parse_state_.op; << static_cast<uint32_t>(req->op) << ", got "
<< static_cast<uint32_t>(parse_state_.op);
return -1; return -1;
} }
@ -479,7 +480,7 @@ int MemcachedConnection::parse_packet() {
return -1; return -1;
} }
if (parse_state_.op == MEMCACHED_OP_GET && if (parse_state_.op == MemcachedOp::GET &&
parse_state_.status_code == 0 && parse_state_.extralen == 0) { parse_state_.status_code == 0 && parse_state_.extralen == 0) {
MCLOG(WARN, this) << "response for GET does not have extra"; MCLOG(WARN, this) << "response for GET does not have extra";
return -1; return -1;
@ -661,9 +662,9 @@ void MemcachedConnection::drain_send_queue(size_t nwrite) {
size_t MemcachedConnection::serialized_size(MemcachedRequest *req) { size_t MemcachedConnection::serialized_size(MemcachedRequest *req) {
switch (req->op) { switch (req->op) {
case MEMCACHED_OP_GET: case MemcachedOp::GET:
return 24 + req->key.size(); return 24 + req->key.size();
case MEMCACHED_OP_ADD: case MemcachedOp::ADD:
default: default:
return 24 + 8 + req->key.size() + req->value.size(); return 24 + 8 + req->key.size() + req->value.size();
} }
@ -676,14 +677,14 @@ void MemcachedConnection::make_request(MemcachedSendbuf *sendbuf,
std::fill(std::begin(headbuf.buf), std::end(headbuf.buf), 0); std::fill(std::begin(headbuf.buf), std::end(headbuf.buf), 0);
headbuf[0] = MEMCACHED_REQ_MAGIC; headbuf[0] = MEMCACHED_REQ_MAGIC;
headbuf[1] = req->op; headbuf[1] = static_cast<uint8_t>(req->op);
switch (req->op) { switch (req->op) {
case MEMCACHED_OP_GET: case MemcachedOp::GET:
util::put_uint16be(&headbuf[2], req->key.size()); util::put_uint16be(&headbuf[2], req->key.size());
util::put_uint32be(&headbuf[8], req->key.size()); util::put_uint32be(&headbuf[8], req->key.size());
headbuf.write(24); headbuf.write(24);
break; break;
case MEMCACHED_OP_ADD: case MemcachedOp::ADD:
util::put_uint16be(&headbuf[2], req->key.size()); util::put_uint16be(&headbuf[2], req->key.size());
headbuf[4] = 8; headbuf[4] = 8;
util::put_uint32be(&headbuf[8], 8 + req->key.size() + req->value.size()); util::put_uint32be(&headbuf[8], 8 + req->key.size() + req->value.size());

View File

@ -43,6 +43,7 @@ using namespace nghttp2;
namespace shrpx { namespace shrpx {
struct MemcachedRequest; struct MemcachedRequest;
enum class MemcachedOp : uint8_t;
enum class MemcachedParseState { enum class MemcachedParseState {
HEADER24, HEADER24,
@ -70,7 +71,7 @@ struct MemcachedParseContext {
// status_code in response // status_code in response
int status_code; int status_code;
// op in response // op in response
int op; MemcachedOp op;
}; };
struct MemcachedSendbuf { struct MemcachedSendbuf {

View File

@ -35,9 +35,9 @@
namespace shrpx { namespace shrpx {
enum { enum class MemcachedOp : uint8_t {
MEMCACHED_OP_GET = 0x00, GET = 0x00,
MEMCACHED_OP_ADD = 0x02, ADD = 0x02,
}; };
struct MemcachedRequest; struct MemcachedRequest;
@ -50,7 +50,7 @@ struct MemcachedRequest {
std::vector<uint8_t> value; std::vector<uint8_t> value;
MemcachedResultCallback cb; MemcachedResultCallback cb;
uint32_t expiry; uint32_t expiry;
int op; MemcachedOp op;
bool canceled; bool canceled;
}; };

View File

@ -330,7 +330,7 @@ int tls_session_new_cb(SSL *ssl, SSL_SESSION *session) {
} }
auto req = std::make_unique<MemcachedRequest>(); auto req = std::make_unique<MemcachedRequest>();
req->op = MEMCACHED_OP_ADD; req->op = MemcachedOp::ADD;
req->key = MEMCACHED_SESSION_CACHE_KEY_PREFIX.str(); req->key = MEMCACHED_SESSION_CACHE_KEY_PREFIX.str();
req->key += req->key +=
util::format_hex(balloc, StringRef{id, static_cast<size_t>(idlen)}); util::format_hex(balloc, StringRef{id, static_cast<size_t>(idlen)});
@ -398,7 +398,7 @@ SSL_SESSION *tls_session_get_cb(SSL *ssl,
} }
auto req = std::make_unique<MemcachedRequest>(); auto req = std::make_unique<MemcachedRequest>();
req->op = MEMCACHED_OP_GET; req->op = MemcachedOp::GET;
req->key = MEMCACHED_SESSION_CACHE_KEY_PREFIX.str(); req->key = MEMCACHED_SESSION_CACHE_KEY_PREFIX.str();
req->key += req->key +=
util::format_hex(balloc, StringRef{id, static_cast<size_t>(idlen)}); util::format_hex(balloc, StringRef{id, static_cast<size_t>(idlen)});

View File

@ -272,7 +272,7 @@ void memcached_get_ticket_key_cb(struct ev_loop *loop, ev_timer *w,
auto req = std::make_unique<MemcachedRequest>(); auto req = std::make_unique<MemcachedRequest>();
req->key = "nghttpx:tls-ticket-key"; req->key = "nghttpx:tls-ticket-key";
req->op = MEMCACHED_OP_GET; req->op = MemcachedOp::GET;
req->cb = [conn_handler, w](MemcachedRequest *req, MemcachedResult res) { req->cb = [conn_handler, w](MemcachedRequest *req, MemcachedResult res) {
switch (res.status_code) { switch (res.status_code) {
case MEMCACHED_ERR_NO_ERROR: case MEMCACHED_ERR_NO_ERROR: