SocketBuffer.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "SocketBuffer.h"
  36. #include <cassert>
  37. #include <algorithm>
  38. #include "SocketCore.h"
  39. #include "DlAbortEx.h"
  40. #include "message.h"
  41. #include "fmt.h"
  42. #include "LogFactory.h"
  43. #include "a2functional.h"
  44. namespace aria2 {
  45. SocketBuffer::ByteArrayBufEntry::ByteArrayBufEntry(
  46. unsigned char* bytes, size_t length,
  47. std::unique_ptr<ProgressUpdate> progressUpdate)
  48. : BufEntry(std::move(progressUpdate)), bytes_(bytes), length_(length)
  49. {
  50. }
  51. SocketBuffer::ByteArrayBufEntry::~ByteArrayBufEntry() { delete[] bytes_; }
  52. ssize_t
  53. SocketBuffer::ByteArrayBufEntry::send(const std::shared_ptr<SocketCore>& socket,
  54. size_t offset)
  55. {
  56. return socket->writeData(bytes_ + offset, length_ - offset);
  57. }
  58. bool SocketBuffer::ByteArrayBufEntry::final(size_t offset) const
  59. {
  60. return length_ <= offset;
  61. }
  62. size_t SocketBuffer::ByteArrayBufEntry::getLength() const { return length_; }
  63. const unsigned char* SocketBuffer::ByteArrayBufEntry::getData() const
  64. {
  65. return bytes_;
  66. }
  67. SocketBuffer::StringBufEntry::StringBufEntry(
  68. std::string s, std::unique_ptr<ProgressUpdate> progressUpdate)
  69. : BufEntry(std::move(progressUpdate)), str_(std::move(s))
  70. {
  71. }
  72. ssize_t
  73. SocketBuffer::StringBufEntry::send(const std::shared_ptr<SocketCore>& socket,
  74. size_t offset)
  75. {
  76. return socket->writeData(str_.data() + offset, str_.size() - offset);
  77. }
  78. bool SocketBuffer::StringBufEntry::final(size_t offset) const
  79. {
  80. return str_.size() <= offset;
  81. }
  82. size_t SocketBuffer::StringBufEntry::getLength() const { return str_.size(); }
  83. const unsigned char* SocketBuffer::StringBufEntry::getData() const
  84. {
  85. return reinterpret_cast<const unsigned char*>(str_.c_str());
  86. }
  87. SocketBuffer::SocketBuffer(std::shared_ptr<SocketCore> socket)
  88. : socket_(std::move(socket)), offset_(0)
  89. {
  90. }
  91. SocketBuffer::~SocketBuffer() {}
  92. void SocketBuffer::pushBytes(unsigned char* bytes, size_t len,
  93. std::unique_ptr<ProgressUpdate> progressUpdate)
  94. {
  95. if (len > 0) {
  96. bufq_.push_back(
  97. make_unique<ByteArrayBufEntry>(bytes, len, std::move(progressUpdate)));
  98. }
  99. }
  100. void SocketBuffer::pushStr(std::string data,
  101. std::unique_ptr<ProgressUpdate> progressUpdate)
  102. {
  103. if (!data.empty()) {
  104. bufq_.push_back(make_unique<StringBufEntry>(std::move(data),
  105. std::move(progressUpdate)));
  106. }
  107. }
  108. ssize_t SocketBuffer::send()
  109. {
  110. a2iovec iov[A2_IOV_MAX];
  111. size_t totalslen = 0;
  112. while (!bufq_.empty()) {
  113. size_t num;
  114. size_t bufqlen = bufq_.size();
  115. ssize_t amount = 24_k;
  116. ssize_t firstlen = bufq_.front()->getLength() - offset_;
  117. amount -= firstlen;
  118. iov[0].A2IOVEC_BASE = reinterpret_cast<char*>(
  119. const_cast<unsigned char*>(bufq_.front()->getData() + offset_));
  120. iov[0].A2IOVEC_LEN = firstlen;
  121. num = 1;
  122. for (auto i = std::begin(bufq_) + 1, eoi = std::end(bufq_);
  123. i != eoi && num < A2_IOV_MAX && num < bufqlen && amount > 0;
  124. ++i, ++num) {
  125. ssize_t len = (*i)->getLength();
  126. if (amount < len) {
  127. break;
  128. }
  129. amount -= len;
  130. iov[num].A2IOVEC_BASE =
  131. reinterpret_cast<char*>(const_cast<unsigned char*>((*i)->getData()));
  132. iov[num].A2IOVEC_LEN = len;
  133. }
  134. ssize_t slen = socket_->writeVector(iov, num);
  135. if (slen == 0 && !socket_->wantRead() && !socket_->wantWrite()) {
  136. throw DL_ABORT_EX(fmt(EX_SOCKET_SEND, "Connection closed."));
  137. }
  138. // A2_LOG_NOTICE(fmt("num=%zu, amount=%d, bufq.size()=%zu, SEND=%d",
  139. // num, amount, bufq_.size(), slen));
  140. totalslen += slen;
  141. if (firstlen > slen) {
  142. offset_ += slen;
  143. bufq_.front()->progressUpdate(slen, false);
  144. if (socket_->wantRead() || socket_->wantWrite()) {
  145. goto fin;
  146. }
  147. continue;
  148. }
  149. slen -= firstlen;
  150. bufq_.front()->progressUpdate(firstlen, true);
  151. bufq_.pop_front();
  152. offset_ = 0;
  153. for (size_t i = 1; i < num; ++i) {
  154. auto& buf = bufq_.front();
  155. ssize_t len = buf->getLength();
  156. if (len > slen) {
  157. offset_ = slen;
  158. bufq_.front()->progressUpdate(slen, false);
  159. goto fin;
  160. }
  161. slen -= len;
  162. bufq_.front()->progressUpdate(len, true);
  163. bufq_.pop_front();
  164. }
  165. }
  166. fin:
  167. return totalslen;
  168. }
  169. bool SocketBuffer::sendBufferIsEmpty() const { return bufq_.empty(); }
  170. } // namespace aria2