SocketBuffer.cc 5.8 KB

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