Small code cleanup for SocketBuffer

pull/235/merge
Tatsuhiro Tsujikawa 2014-06-05 23:33:58 +09:00
parent c0e4381780
commit 0ee8104953
2 changed files with 34 additions and 33 deletions

View File

@ -104,8 +104,9 @@ const unsigned char* SocketBuffer::StringBufEntry::getData() const
return reinterpret_cast<const unsigned char*>(str_.c_str()); return reinterpret_cast<const unsigned char*>(str_.c_str());
} }
SocketBuffer::SocketBuffer(const std::shared_ptr<SocketCore>& socket): SocketBuffer::SocketBuffer(std::shared_ptr<SocketCore> socket)
socket_(socket), offset_(0) {} : socket_(std::move(socket)), offset_(0)
{}
SocketBuffer::~SocketBuffer() {} SocketBuffer::~SocketBuffer() {}
@ -145,15 +146,17 @@ ssize_t SocketBuffer::send()
for(auto i = std::begin(bufq_)+1, eoi = std::end(bufq_); for(auto i = std::begin(bufq_)+1, eoi = std::end(bufq_);
i != eoi && num < A2_IOV_MAX && num < bufqlen && amount > 0; i != eoi && num < A2_IOV_MAX && num < bufqlen && amount > 0;
++i, ++num) { ++i, ++num) {
ssize_t len = (*i)->getLength(); ssize_t len = (*i)->getLength();
if(amount >= len) {
if(amount < len) {
break;
}
amount -= len; amount -= len;
iov[num].A2IOVEC_BASE = iov[num].A2IOVEC_BASE =
reinterpret_cast<char*>(const_cast<unsigned char*>((*i)->getData())); reinterpret_cast<char*>(const_cast<unsigned char*>((*i)->getData()));
iov[num].A2IOVEC_LEN = len; iov[num].A2IOVEC_LEN = len;
} else {
break;
}
} }
ssize_t slen = socket_->writeVector(iov, num); ssize_t slen = socket_->writeVector(iov, num);
if(slen == 0 && !socket_->wantRead() && !socket_->wantWrite()) { if(slen == 0 && !socket_->wantRead() && !socket_->wantWrite()) {
@ -167,27 +170,27 @@ ssize_t SocketBuffer::send()
offset_ += slen; offset_ += slen;
bufq_.front()->progressUpdate(slen, false); bufq_.front()->progressUpdate(slen, false);
goto fin; goto fin;
} else { }
slen -= firstlen; slen -= firstlen;
bufq_.front()->progressUpdate(firstlen, true); bufq_.front()->progressUpdate(firstlen, true);
bufq_.pop_front(); bufq_.pop_front();
offset_ = 0; offset_ = 0;
for(size_t i = 1; i < num; ++i) { for(size_t i = 1; i < num; ++i) {
const std::unique_ptr<BufEntry>& buf = bufq_.front(); auto& buf = bufq_.front();
ssize_t len = buf->getLength(); ssize_t len = buf->getLength();
if(len > slen) { if(len > slen) {
offset_ = slen; offset_ = slen;
bufq_.front()->progressUpdate(slen, false); bufq_.front()->progressUpdate(slen, false);
goto fin; goto fin;
break; }
} else {
slen -= len; slen -= len;
bufq_.front()->progressUpdate(len, true); bufq_.front()->progressUpdate(len, true);
bufq_.pop_front(); bufq_.pop_front();
} }
} }
}
}
fin: fin:
return totalslen; return totalslen;
} }

View File

@ -109,13 +109,13 @@ private:
// to the data to be sent in the next send() call. // to the data to be sent in the next send() call.
size_t offset_; size_t offset_;
public: public:
SocketBuffer(const std::shared_ptr<SocketCore>& socket); SocketBuffer(std::shared_ptr<SocketCore> socket);
~SocketBuffer(); ~SocketBuffer();
// Don't allow copying // Don't allow copying
SocketBuffer(const SocketBuffer&); SocketBuffer(const SocketBuffer&) = delete;
SocketBuffer& operator=(const SocketBuffer&); SocketBuffer& operator=(const SocketBuffer&) = delete;
// Feeds data pointered by bytes with length len into queue. This // Feeds data pointered by bytes with length len into queue. This
// object gets ownership of bytes, so caller must not delete or // 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 // each time the data is sent. It will be deleted by this object. It
// can be null. // can be null.
void pushBytes(unsigned char* bytes, size_t len, void pushBytes(unsigned char* bytes, size_t len,
std::unique_ptr<ProgressUpdate> progressUpdate = std::unique_ptr<ProgressUpdate> progressUpdate = nullptr);
std::unique_ptr<ProgressUpdate>{});
// Feeds data into queue. This function doesn't send data. If // Feeds data into queue. This function doesn't send data. If
// progressUpdate is not null, its update() function will be called // progressUpdate is not null, its update() function will be called
// each time the data is sent. It will be deleted by this object. It // each time the data is sent. It will be deleted by this object. It
// can be null. // can be null.
void pushStr(std::string data, void pushStr(std::string data,
std::unique_ptr<ProgressUpdate> progressUpdate = std::unique_ptr<ProgressUpdate> progressUpdate = nullptr);
std::unique_ptr<ProgressUpdate>{});
// Sends data in queue. Returns the number of bytes sent. // Sends data in queue. Returns the number of bytes sent.
ssize_t send(); ssize_t send();