diff --git a/src/protocol/HttpMessage.cc b/src/protocol/HttpMessage.cc index 11c2c646..e142c00b 100644 --- a/src/protocol/HttpMessage.cc +++ b/src/protocol/HttpMessage.cc @@ -19,6 +19,7 @@ #include #include #include +#include #include "HttpMessage.h" namespace protocol @@ -222,11 +223,9 @@ inline int HttpMessage::append(const void *buf, size_t *size) return ret; } -HttpMessage::HttpMessage(HttpMessage&& msg) +HttpMessage::HttpMessage(HttpMessage&& msg) : + ProtocolMessage(std::move(msg)) { - this->size_limit = msg.size_limit; - msg.size_limit = (size_t)-1; - this->parser = msg.parser; msg.parser = NULL; @@ -243,8 +242,7 @@ HttpMessage& HttpMessage::operator = (HttpMessage&& msg) { if (&msg != this) { - this->size_limit = msg.size_limit; - msg.size_limit = (size_t)-1; + *(ProtocolMessage *)this = std::move(msg); http_parser_deinit(this->parser); delete this->parser; diff --git a/src/protocol/HttpMessage.h b/src/protocol/HttpMessage.h index 8cd372ba..bb2b7e82 100644 --- a/src/protocol/HttpMessage.h +++ b/src/protocol/HttpMessage.h @@ -278,12 +278,8 @@ public: /* for std::move() */ public: - HttpRequest(HttpRequest&& req) : HttpMessage(std::move(req)) { } - HttpRequest& operator = (HttpRequest&& req) - { - *(HttpMessage *)this = std::move(req); - return *this; - } + HttpRequest(HttpRequest&& req) = default; + HttpRequest& operator = (HttpRequest&& req) = default; }; class HttpResponse : public HttpMessage @@ -361,12 +357,8 @@ public: /* for std::move() */ public: - HttpResponse(HttpResponse&& resp) : HttpMessage(std::move(resp)) { } - HttpResponse& operator = (HttpResponse&& resp) - { - *(HttpMessage *)this = std::move(resp); - return *this; - } + HttpResponse(HttpResponse&& resp) = default; + HttpResponse& operator = (HttpResponse&& resp) = default; }; } diff --git a/src/protocol/KafkaMessage.cc b/src/protocol/KafkaMessage.cc index 3beb1ad5..5f121d02 100644 --- a/src/protocol/KafkaMessage.cc +++ b/src/protocol/KafkaMessage.cc @@ -1620,11 +1620,9 @@ KafkaMessage::~KafkaMessage() } } -KafkaMessage::KafkaMessage(KafkaMessage&& msg) +KafkaMessage::KafkaMessage(KafkaMessage&& msg) : + ProtocolMessage(std::move(msg)) { - this->size_limit = msg.size_limit; - msg.size_limit = (size_t)-1; - this->parser = msg.parser; this->stream = msg.stream; msg.parser = NULL; @@ -1651,8 +1649,7 @@ KafkaMessage& KafkaMessage::operator= (KafkaMessage &&msg) { if (this != &msg) { - this->size_limit = msg.size_limit; - msg.size_limit = (size_t)-1; + *(ProtocolMessage *)this = std::move(msg); kafka_parser_deinit(this->parser); delete this->parser; diff --git a/src/protocol/MySQLMessage.cc b/src/protocol/MySQLMessage.cc index 7ca861b5..b3aedb29 100644 --- a/src/protocol/MySQLMessage.cc +++ b/src/protocol/MySQLMessage.cc @@ -22,6 +22,7 @@ #include #include #include +#include #include "MySQLMessage.h" #include "mysql_types.h" @@ -41,11 +42,9 @@ MySQLMessage::~MySQLMessage() } } -MySQLMessage::MySQLMessage(MySQLMessage&& move) +MySQLMessage::MySQLMessage(MySQLMessage&& move) : + ProtocolMessage(std::move(move)) { - this->size_limit = move.size_limit; - move.size_limit = (size_t)-1; - parser_ = move.parser_; stream_ = move.stream_; seqid_ = move.seqid_; @@ -61,8 +60,7 @@ MySQLMessage& MySQLMessage::operator= (MySQLMessage&& move) { if (this != &move) { - this->size_limit = move.size_limit; - move.size_limit = (size_t)-1; + *(ProtocolMessage *)this = std::move(move); mysql_parser_deinit(parser_); mysql_stream_deinit(stream_); diff --git a/src/protocol/ProtocolMessage.h b/src/protocol/ProtocolMessage.h index a51609f3..d0ca9431 100644 --- a/src/protocol/ProtocolMessage.h +++ b/src/protocol/ProtocolMessage.h @@ -61,11 +61,52 @@ public: void set_size_limit(size_t limit) { this->size_limit = limit; } size_t get_size_limit() const { return this->size_limit; } +public: + class Attachment + { + public: + virtual ~Attachment() { } + }; + + void set_attachment(Attachment *att) { this->attachment = att; } + Attachment *get_attachment() const { return this->attachment; } + protected: size_t size_limit; +private: + Attachment *attachment; + public: - ProtocolMessage() { this->size_limit = (size_t)-1; } + ProtocolMessage() + { + this->size_limit = (size_t)-1; + this->attachment = NULL; + } + + virtual ~ProtocolMessage() { delete this->attachment; } + +public: + ProtocolMessage(ProtocolMessage&& msg) + { + this->size_limit = msg.size_limit; + msg.size_limit = (size_t)-1; + this->attachment = msg.attachment; + msg.attachment = NULL; + } + + ProtocolMessage& operator = (ProtocolMessage&& msg) + { + if (&msg != this) + { + this->size_limit = msg.size_limit; + msg.size_limit = (size_t)-1; + this->attachment = msg.attachment; + msg.attachment = NULL; + } + + return *this; + } }; } diff --git a/src/protocol/RedisMessage.cc b/src/protocol/RedisMessage.cc index 20e1f839..254d69a0 100644 --- a/src/protocol/RedisMessage.cc +++ b/src/protocol/RedisMessage.cc @@ -485,11 +485,9 @@ std::string RedisValue::debug_string() const return ret; } -RedisMessage::RedisMessage(RedisMessage&& move) +RedisMessage::RedisMessage(RedisMessage&& move) : + ProtocolMessage(std::move(move)) { - this->size_limit = move.size_limit; - move.size_limit = (size_t)-1; - parser_ = move.parser_; stream_ = move.stream_; cur_size_ = move.cur_size_; @@ -503,8 +501,7 @@ RedisMessage& RedisMessage::operator= (RedisMessage &&move) { if (this != &move) { - this->size_limit = move.size_limit; - move.size_limit = (size_t)-1; + *(ProtocolMessage *)this = std::move(move); redis_parser_deinit(parser_); delete parser_; diff --git a/tutorial/tutorial-10-user_defined_protocol/message.cc b/tutorial/tutorial-10-user_defined_protocol/message.cc index 644ec22f..c99fca22 100644 --- a/tutorial/tutorial-10-user_defined_protocol/message.cc +++ b/tutorial/tutorial-10-user_defined_protocol/message.cc @@ -21,6 +21,7 @@ #include #include #include +#include #include "message.h" namespace protocol @@ -106,11 +107,9 @@ int TutorialMessage::set_message_body(const void *body, size_t size) return 0; } -TutorialMessage::TutorialMessage(TutorialMessage&& msg) +TutorialMessage::TutorialMessage(TutorialMessage&& msg) : + ProtocolMessage(std::move(msg)) { - this->size_limit = msg.size_limit; - msg.size_limit = (size_t)-1; - memcpy(this->head, msg.head, 4); this->head_received = msg.head_received; this->body = msg.body; @@ -126,8 +125,7 @@ TutorialMessage& TutorialMessage::operator = (TutorialMessage&& msg) { if (&msg != this) { - this->size_limit = msg.size_limit; - msg.size_limit = (size_t)-1; + *(ProtocolMessage *)this = std::move(msg); memcpy(this->head, msg.head, 4); this->head_received = msg.head_received;