diff --git a/src/SocketBuffer.cc b/src/SocketBuffer.cc index 715681bd..0a09f2ae 100644 --- a/src/SocketBuffer.cc +++ b/src/SocketBuffer.cc @@ -104,8 +104,9 @@ const unsigned char* SocketBuffer::StringBufEntry::getData() const return reinterpret_cast(str_.c_str()); } -SocketBuffer::SocketBuffer(const std::shared_ptr& socket): - socket_(socket), offset_(0) {} +SocketBuffer::SocketBuffer(std::shared_ptr socket) + : socket_(std::move(socket)), offset_(0) +{} SocketBuffer::~SocketBuffer() {} @@ -145,15 +146,17 @@ ssize_t SocketBuffer::send() for(auto i = std::begin(bufq_)+1, eoi = std::end(bufq_); i != eoi && num < A2_IOV_MAX && num < bufqlen && amount > 0; ++i, ++num) { + ssize_t len = (*i)->getLength(); - if(amount >= len) { - amount -= len; - iov[num].A2IOVEC_BASE = - reinterpret_cast(const_cast((*i)->getData())); - iov[num].A2IOVEC_LEN = len; - } else { + + if(amount < len) { break; } + + amount -= len; + iov[num].A2IOVEC_BASE = + reinterpret_cast(const_cast((*i)->getData())); + iov[num].A2IOVEC_LEN = len; } ssize_t slen = socket_->writeVector(iov, num); if(slen == 0 && !socket_->wantRead() && !socket_->wantWrite()) { @@ -167,25 +170,25 @@ ssize_t SocketBuffer::send() offset_ += slen; bufq_.front()->progressUpdate(slen, false); goto fin; - } else { - slen -= firstlen; - bufq_.front()->progressUpdate(firstlen, true); - bufq_.pop_front(); - offset_ = 0; - for(size_t i = 1; i < num; ++i) { - const std::unique_ptr& buf = bufq_.front(); - ssize_t len = buf->getLength(); - if(len > slen) { - offset_ = slen; - bufq_.front()->progressUpdate(slen, false); - goto fin; - break; - } else { - slen -= len; - bufq_.front()->progressUpdate(len, true); - bufq_.pop_front(); - } + } + + slen -= firstlen; + bufq_.front()->progressUpdate(firstlen, true); + bufq_.pop_front(); + offset_ = 0; + + for(size_t i = 1; i < num; ++i) { + auto& buf = bufq_.front(); + ssize_t len = buf->getLength(); + if(len > slen) { + offset_ = slen; + bufq_.front()->progressUpdate(slen, false); + goto fin; } + + slen -= len; + bufq_.front()->progressUpdate(len, true); + bufq_.pop_front(); } } fin: diff --git a/src/SocketBuffer.h b/src/SocketBuffer.h index 32d78287..256e3ce1 100644 --- a/src/SocketBuffer.h +++ b/src/SocketBuffer.h @@ -109,13 +109,13 @@ private: // to the data to be sent in the next send() call. size_t offset_; public: - SocketBuffer(const std::shared_ptr& socket); + SocketBuffer(std::shared_ptr socket); ~SocketBuffer(); // Don't allow copying - SocketBuffer(const SocketBuffer&); - SocketBuffer& operator=(const SocketBuffer&); + SocketBuffer(const SocketBuffer&) = delete; + SocketBuffer& operator=(const SocketBuffer&) = delete; // Feeds data pointered by bytes with length len into queue. This // object gets ownership of bytes, so caller must not delete or @@ -124,16 +124,14 @@ public: // each time the data is sent. It will be deleted by this object. It // can be null. void pushBytes(unsigned char* bytes, size_t len, - std::unique_ptr progressUpdate = - std::unique_ptr{}); + std::unique_ptr progressUpdate = nullptr); // Feeds data into queue. This function doesn't send data. If // progressUpdate is not null, its update() function will be called // each time the data is sent. It will be deleted by this object. It // can be null. void pushStr(std::string data, - std::unique_ptr progressUpdate = - std::unique_ptr{}); + std::unique_ptr progressUpdate = nullptr); // Sends data in queue. Returns the number of bytes sent. ssize_t send();