SocketBuffer.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. std::vector<unsigned char> bytes,
  47. std::unique_ptr<ProgressUpdate> progressUpdate)
  48. : BufEntry(std::move(progressUpdate)), bytes_(std::move(bytes))
  49. {
  50. }
  51. SocketBuffer::ByteArrayBufEntry::~ByteArrayBufEntry() {}
  52. ssize_t
  53. SocketBuffer::ByteArrayBufEntry::send(const std::shared_ptr<SocketCore>& socket,
  54. size_t offset)
  55. {
  56. return socket->writeData(bytes_.data() + offset, bytes_.size() - offset);
  57. }
  58. bool SocketBuffer::ByteArrayBufEntry::final(size_t offset) const
  59. {
  60. return bytes_.size() <= offset;
  61. }
  62. size_t SocketBuffer::ByteArrayBufEntry::getLength() const
  63. {
  64. return bytes_.size();
  65. }
  66. const unsigned char* SocketBuffer::ByteArrayBufEntry::getData() const
  67. {
  68. return bytes_.data();
  69. }
  70. SocketBuffer::StringBufEntry::StringBufEntry(
  71. std::string s, std::unique_ptr<ProgressUpdate> progressUpdate)
  72. : BufEntry(std::move(progressUpdate)), str_(std::move(s))
  73. {
  74. }
  75. ssize_t
  76. SocketBuffer::StringBufEntry::send(const std::shared_ptr<SocketCore>& socket,
  77. size_t offset)
  78. {
  79. return socket->writeData(str_.data() + offset, str_.size() - offset);
  80. }
  81. bool SocketBuffer::StringBufEntry::final(size_t offset) const
  82. {
  83. return str_.size() <= offset;
  84. }
  85. size_t SocketBuffer::StringBufEntry::getLength() const { return str_.size(); }
  86. const unsigned char* SocketBuffer::StringBufEntry::getData() const
  87. {
  88. return reinterpret_cast<const unsigned char*>(str_.c_str());
  89. }
  90. SocketBuffer::SocketBuffer(std::shared_ptr<SocketCore> socket)
  91. : socket_(std::move(socket)), offset_(0)
  92. {
  93. }
  94. SocketBuffer::~SocketBuffer() {}
  95. void SocketBuffer::pushBytes(std::vector<unsigned char> bytes,
  96. std::unique_ptr<ProgressUpdate> progressUpdate)
  97. {
  98. if (!bytes.empty()) {
  99. bufq_.push_back(make_unique<ByteArrayBufEntry>(std::move(bytes),
  100. std::move(progressUpdate)));
  101. }
  102. }
  103. void SocketBuffer::pushStr(std::string data,
  104. std::unique_ptr<ProgressUpdate> progressUpdate)
  105. {
  106. if (!data.empty()) {
  107. bufq_.push_back(make_unique<StringBufEntry>(std::move(data),
  108. std::move(progressUpdate)));
  109. }
  110. }
  111. ssize_t SocketBuffer::send()
  112. {
  113. a2iovec iov[A2_IOV_MAX];
  114. size_t totalslen = 0;
  115. while (!bufq_.empty()) {
  116. size_t num;
  117. size_t bufqlen = bufq_.size();
  118. ssize_t amount = 24_k;
  119. ssize_t firstlen = bufq_.front()->getLength() - offset_;
  120. amount -= firstlen;
  121. iov[0].A2IOVEC_BASE = reinterpret_cast<char*>(
  122. const_cast<unsigned char*>(bufq_.front()->getData() + offset_));
  123. iov[0].A2IOVEC_LEN = firstlen;
  124. num = 1;
  125. for (auto i = std::begin(bufq_) + 1, eoi = std::end(bufq_);
  126. i != eoi && num < A2_IOV_MAX && num < bufqlen && amount > 0;
  127. ++i, ++num) {
  128. ssize_t len = (*i)->getLength();
  129. if (amount < len) {
  130. break;
  131. }
  132. amount -= len;
  133. iov[num].A2IOVEC_BASE =
  134. reinterpret_cast<char*>(const_cast<unsigned char*>((*i)->getData()));
  135. iov[num].A2IOVEC_LEN = len;
  136. }
  137. ssize_t slen = socket_->writeVector(iov, num);
  138. if (slen == 0 && !socket_->wantRead() && !socket_->wantWrite()) {
  139. throw DL_ABORT_EX(fmt(EX_SOCKET_SEND, "Connection closed."));
  140. }
  141. // A2_LOG_NOTICE(fmt("num=%zu, amount=%d, bufq.size()=%zu, SEND=%d",
  142. // num, amount, bufq_.size(), slen));
  143. totalslen += slen;
  144. if (firstlen > slen) {
  145. offset_ += slen;
  146. bufq_.front()->progressUpdate(slen, false);
  147. if (socket_->wantRead() || socket_->wantWrite()) {
  148. goto fin;
  149. }
  150. continue;
  151. }
  152. slen -= firstlen;
  153. bufq_.front()->progressUpdate(firstlen, true);
  154. bufq_.pop_front();
  155. offset_ = 0;
  156. for (size_t i = 1; i < num; ++i) {
  157. auto& buf = bufq_.front();
  158. ssize_t len = buf->getLength();
  159. if (len > slen) {
  160. offset_ = slen;
  161. bufq_.front()->progressUpdate(slen, false);
  162. goto fin;
  163. }
  164. slen -= len;
  165. bufq_.front()->progressUpdate(len, true);
  166. bufq_.pop_front();
  167. }
  168. }
  169. fin:
  170. return totalslen;
  171. }
  172. bool SocketBuffer::sendBufferIsEmpty() const { return bufq_.empty(); }
  173. } // namespace aria2