From 9e61a3498d2a90d57c3ffec68b4df5f2e229bb58 Mon Sep 17 00:00:00 2001 From: Vlad Zolotarov Date: Tue, 24 May 2016 18:58:49 +0300 Subject: [PATCH] cql_server::response: rework make_frame() Use a template function to avoid code duplication. Signed-off-by: Vlad Zolotarov --- transport/server.cc | 57 +++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/transport/server.cc b/transport/server.cc index 338236de30..3a6a72c245 100644 --- a/transport/server.cc +++ b/transport/server.cc @@ -39,6 +39,7 @@ #include "database.hh" #include "net/byteorder.hh" #include +#include #include "enum_set.hh" #include "service/query_state.hh" @@ -215,8 +216,32 @@ public: return _opcode; } private: - sstring make_frame(uint8_t version, uint8_t flags, size_t length); std::vector compress(const std::vector& body); + + template + sstring make_frame_one(uint8_t version, uint8_t flags, size_t length) { + sstring frame_buf(sstring::initialized_later(), sizeof(CqlFrameHeaderType)); + auto* frame = reinterpret_cast(frame_buf.begin()); + frame->version = version | 0x80; + frame->flags = flags; + frame->opcode = static_cast(_opcode); + frame->length = htonl(length); + frame->stream = net::hton((decltype(frame->stream))_stream); + + return frame_buf; + } + + sstring make_frame(uint8_t version, uint8_t flags, size_t length) { + if (version > 0x04) { + throw exceptions::protocol_exception(sprint("Invalid or unsupported protocol version: %d", version)); + } + + if (version > 0x02) { + return make_frame_one(version, flags, length); + } else { + return make_frame_one(version, flags, length); + } + } }; cql_server::cql_server(distributed& proxy, distributed& qp, cql_load_balance lb) @@ -1369,36 +1394,6 @@ void cql_server::response::serialize(const event::schema_change& event, uint8_t } } -sstring cql_server::response::make_frame(uint8_t version, uint8_t flags, size_t length) -{ - switch (version) { - case 0x01: - case 0x02: { - sstring frame_buf(sstring::initialized_later(), sizeof(cql_binary_frame_v1)); - auto* frame = reinterpret_cast(frame_buf.begin()); - frame->version = version | 0x80; - frame->flags = flags; - frame->stream = _stream; - frame->opcode = static_cast(_opcode); - frame->length = htonl(length); - return frame_buf; - } - case 0x03: - case 0x04: { - sstring frame_buf(sstring::initialized_later(), sizeof(cql_binary_frame_v3)); - auto* frame = reinterpret_cast(frame_buf.begin()); - frame->version = version | 0x80; - frame->flags = flags; - frame->stream = htons(_stream); - frame->opcode = static_cast(_opcode); - frame->length = htonl(length); - return frame_buf; - } - default: - throw exceptions::protocol_exception(sprint("Invalid or unsupported protocol version: %d", version)); - } -} - void cql_server::response::write_byte(uint8_t b) { _body.insert(_body.end(), b);