From 53f4c5137da249e5e955809ffe32afa3cf5c3522 Mon Sep 17 00:00:00 2001 From: Determinant Date: Thu, 12 Jul 2018 18:04:52 -0400 Subject: ... --- include/salticidae/conn.h | 42 +++++++++++++++++++++++++++++++++--------- include/salticidae/msg.h | 31 +++++++++++++++++++------------ include/salticidae/stream.h | 19 +++++++++++++++---- include/salticidae/util.h | 42 +++++++++++++++++++++++++++++------------- src/conn.cpp | 2 +- 5 files changed, 97 insertions(+), 39 deletions(-) diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h index 40facc9..589bf16 100644 --- a/include/salticidae/conn.h +++ b/include/salticidae/conn.h @@ -75,21 +75,45 @@ class RingBuffer { size_t length() const { return data.end() - offset; } }; + std::list ring; size_t _size; public: RingBuffer(): _size(0) {} ~RingBuffer() { clear(); } - RingBuffer &operator=(const RingBuffer &other) = delete; - RingBuffer(const RingBuffer &other) = delete; - RingBuffer &operator=(RingBuffer &&other) { - ring = std::move(other.ring); - _size = other._size; + + void swap(RingBuffer &other) { + std::swap(ring, other.ring); + std::swap(_size, other._size); + } + + RingBuffer(const RingBuffer &other): + ring(other.ring), _size(other._size) {} + + RingBuffer(RingBuffer &&other): + ring(std::move(other.ring)), _size(other._size) { other._size = 0; + } + + RingBuffer &operator=(RingBuffer &&other) { + if (this != &other) + { + RingBuffer tmp(std::move(other)); + tmp.swap(*this); + } return *this; } - + + RingBuffer &operator=(const RingBuffer &other) { + if (this != &other) + { + RingBuffer tmp(other); + tmp.swap(*this); + } + return *this; + } + void push(bytearray_t &&data) { _size += data.size(); ring.push_back(buffer_entry_t(std::move(data))); @@ -188,9 +212,9 @@ class ConnPool { protected: /** close the connection and free all on-going or planned events. */ virtual void close() { - ev_read.clear(); - ev_write.clear(); - ev_connect.clear(); + ev_read.del(); + ev_write.del(); + ev_connect.del(); ::close(fd); fd = -1; } diff --git a/include/salticidae/msg.h b/include/salticidae/msg.h index b9db1b8..798062a 100644 --- a/include/salticidae/msg.h +++ b/include/salticidae/msg.h @@ -93,23 +93,30 @@ class MsgBase { checksum = letoh(_checksum); } + void swap(MsgBase &other) { + std::swap(magic, other.magic); + std::swap(opcode, other.opcode); + std::swap(length, other.length); + std::swap(checksum, other.checksum); + std::swap(payload, other.payload); + std::swap(no_payload, other.no_payload); + } + MsgBase &operator=(const MsgBase &other) { - magic = other.magic; - opcode = other.opcode; - length = other.length; - checksum = other.checksum; - payload = other.payload; - no_payload = other.no_payload; + if (this != &other) + { + MsgBase tmp(other); + tmp.swap(*this); + } return *this; } MsgBase &operator=(MsgBase &&other) { - magic = other.magic; - opcode = std::move(other.opcode); - length = other.length; - checksum = other.checksum; - payload = std::move(other.payload); - no_payload = other.no_payload; + if (this != &other) + { + MsgBase tmp(std::move(other)); + tmp.swap(*this); + } return *this; } diff --git a/include/salticidae/stream.h b/include/salticidae/stream.h index 9e4791c..b5e0757 100644 --- a/include/salticidae/stream.h +++ b/include/salticidae/stream.h @@ -52,15 +52,26 @@ class DataStream { buffer(other.buffer), offset(other.offset) {} + void swap(DataStream &other) { + std::swap(buffer, other.buffer); + std::swap(offset, other.offset); + } + DataStream &operator=(const DataStream &other) { - buffer = other.buffer; - offset = other.offset; + if (this != &other) + { + DataStream tmp(other); + tmp.swap(*this); + } return *this; } DataStream &operator=(DataStream &&other) { - buffer = std::move(other.buffer); - offset = other.offset; + if (this != &other) + { + DataStream tmp(std::move(other)); + tmp.swap(*this); + } return *this; } diff --git a/include/salticidae/util.h b/include/salticidae/util.h index 05e7b66..0324215 100644 --- a/include/salticidae/util.h +++ b/include/salticidae/util.h @@ -271,31 +271,47 @@ class Event { eb(eb), fd(fd), events(events), ev(event_new(eb, fd, events, Event::_then, this)), callback(callback) {} + Event(const Event &other): + eb(other.eb), fd(other.fd), events(other.events), + ev(event_new(eb, fd, events, Event::_then, this)), + callback(other.callback) {} + Event(Event &&other): eb(other.eb), fd(other.fd), events(other.events), - callback(std::move(other.callback)) { - other.clear(); - ev = event_new(eb, fd, events, Event::_then, this); + ev(event_new(eb, fd, events, Event::_then, this)), + callback(std::move(other.callback)) {} + + void swap(Event &other) { + std::swap(eb, other.eb); + std::swap(fd, other.fd); + std::swap(events, other.events); + std::swap(ev, other.ev); + std::swap(callback, other.callback); } + Event &operator=(Event &&other) { - clear(); - other.clear(); - eb = other.eb; - fd = other.fd; - events = other.events; - ev = event_new(eb, fd, events, Event::_then, this); - callback = std::move(other.callback); + if (this != &other) + { + Event tmp(std::move(other)); + tmp.swap(*this); + } return *this; } - ~Event() { clear(); } + Event &operator=(const Event &other) { + if (this != &other) + { + Event tmp(other); + tmp.swap(*this); + } + return *this; + } - void clear() { + ~Event() { if (ev != nullptr) { event_del(ev); event_free(ev); - ev = nullptr; } } diff --git a/src/conn.cpp b/src/conn.cpp index b323340..2bcd0ae 100644 --- a/src/conn.cpp +++ b/src/conn.cpp @@ -165,7 +165,7 @@ void ConnPool::Conn::conn_server(evutil_socket_t fd, short events) { std::bind(&Conn::send_data, this, _1, _2)); ev_read.add(); ev_write.add(); - ev_connect.clear(); + ev_connect.del(); ready_send = false; SALTICIDAE_LOG_INFO("connected to peer %s", std::string(*this).c_str()); on_setup(); -- cgit v1.2.3-70-g09d2