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());
}
SocketBuffer::SocketBuffer(const std::shared_ptr<SocketCore>& socket):
socket_(socket), offset_(0) {}
SocketBuffer::SocketBuffer(std::shared_ptr<SocketCore> 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<char*>(const_cast<unsigned char*>((*i)->getData()));
iov[num].A2IOVEC_LEN = len;
} else {
if(amount < len) {
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);
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<BufEntry>& 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:

View File

@ -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<SocketCore>& socket);
SocketBuffer(std::shared_ptr<SocketCore> 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> progressUpdate =
std::unique_ptr<ProgressUpdate>{});
std::unique_ptr<ProgressUpdate> 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> progressUpdate =
std::unique_ptr<ProgressUpdate>{});
std::unique_ptr<ProgressUpdate> progressUpdate = nullptr);
// Sends data in queue. Returns the number of bytes sent.
ssize_t send();