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) {
amount -= len; if(amount < len) {
iov[num].A2IOVEC_BASE =
reinterpret_cast<char*>(const_cast<unsigned char*>((*i)->getData()));
iov[num].A2IOVEC_LEN = len;
} else {
break; break;
} }
amount -= len;
iov[num].A2IOVEC_BASE =
reinterpret_cast<char*>(const_cast<unsigned char*>((*i)->getData()));
iov[num].A2IOVEC_LEN = len;
} }
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,25 +170,25 @@ 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;
bufq_.front()->progressUpdate(firstlen, true); slen -= firstlen;
bufq_.pop_front(); bufq_.front()->progressUpdate(firstlen, true);
offset_ = 0; bufq_.pop_front();
for(size_t i = 1; i < num; ++i) { offset_ = 0;
const std::unique_ptr<BufEntry>& buf = bufq_.front();
ssize_t len = buf->getLength(); for(size_t i = 1; i < num; ++i) {
if(len > slen) { auto& buf = bufq_.front();
offset_ = slen; ssize_t len = buf->getLength();
bufq_.front()->progressUpdate(slen, false); if(len > slen) {
goto fin; offset_ = slen;
break; bufq_.front()->progressUpdate(slen, false);
} else { goto fin;
slen -= len;
bufq_.front()->progressUpdate(len, true);
bufq_.pop_front();
}
} }
slen -= len;
bufq_.front()->progressUpdate(len, true);
bufq_.pop_front();
} }
} }
fin: fin:

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();