SocketBuffer.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. namespace aria2 {
  44. SocketBuffer::ByteArrayBufEntry::ByteArrayBufEntry
  45. (unsigned char* bytes, size_t length, ProgressUpdate* progressUpdate)
  46. : BufEntry(progressUpdate), bytes_(bytes), length_(length)
  47. {}
  48. SocketBuffer::ByteArrayBufEntry::~ByteArrayBufEntry()
  49. {
  50. delete [] bytes_;
  51. }
  52. ssize_t SocketBuffer::ByteArrayBufEntry::send
  53. (const std::shared_ptr<SocketCore>& socket, size_t offset)
  54. {
  55. return socket->writeData(bytes_+offset, length_-offset);
  56. }
  57. bool SocketBuffer::ByteArrayBufEntry::final(size_t offset) const
  58. {
  59. return length_ <= offset;
  60. }
  61. size_t SocketBuffer::ByteArrayBufEntry::getLength() const
  62. {
  63. return length_;
  64. }
  65. const unsigned char* SocketBuffer::ByteArrayBufEntry::getData() const
  66. {
  67. return bytes_;
  68. }
  69. SocketBuffer::StringBufEntry::StringBufEntry(const std::string& s,
  70. ProgressUpdate* progressUpdate)
  71. : BufEntry(progressUpdate), str_(s)
  72. {}
  73. // SocketBuffer::StringBufEntry::StringBufEntry() {}
  74. ssize_t SocketBuffer::StringBufEntry::send
  75. (const std::shared_ptr<SocketCore>& socket, size_t offset)
  76. {
  77. return socket->writeData(str_.data()+offset, str_.size()-offset);
  78. }
  79. bool SocketBuffer::StringBufEntry::final(size_t offset) const
  80. {
  81. return str_.size() <= offset;
  82. }
  83. size_t SocketBuffer::StringBufEntry::getLength() const
  84. {
  85. return str_.size();
  86. }
  87. const unsigned char* SocketBuffer::StringBufEntry::getData() const
  88. {
  89. return reinterpret_cast<const unsigned char*>(str_.c_str());
  90. }
  91. void SocketBuffer::StringBufEntry::swap(std::string& s)
  92. {
  93. str_.swap(s);
  94. }
  95. SocketBuffer::SocketBuffer(const std::shared_ptr<SocketCore>& socket):
  96. socket_(socket), offset_(0) {}
  97. SocketBuffer::~SocketBuffer() {}
  98. void SocketBuffer::pushBytes(unsigned char* bytes, size_t len,
  99. ProgressUpdate* progressUpdate)
  100. {
  101. if(len > 0) {
  102. bufq_.push_back(std::shared_ptr<BufEntry>
  103. (new ByteArrayBufEntry(bytes, len, progressUpdate)));
  104. }
  105. }
  106. void SocketBuffer::pushStr(const std::string& data,
  107. ProgressUpdate* progressUpdate)
  108. {
  109. if(data.size() > 0) {
  110. bufq_.push_back(std::shared_ptr<BufEntry>
  111. (new StringBufEntry(data, progressUpdate)));
  112. }
  113. }
  114. ssize_t SocketBuffer::send()
  115. {
  116. a2iovec iov[A2_IOV_MAX];
  117. size_t totalslen = 0;
  118. while(!bufq_.empty()) {
  119. size_t num;
  120. ssize_t amount = 24*1024;
  121. ssize_t firstlen = bufq_[0]->getLength() - offset_;
  122. amount -= firstlen;
  123. iov[0].A2IOVEC_BASE =
  124. reinterpret_cast<char*>(const_cast<unsigned char*>
  125. (bufq_[0]->getData() + offset_));
  126. iov[0].A2IOVEC_LEN = firstlen;
  127. for(num = 1; num < A2_IOV_MAX && num < bufq_.size() && amount > 0; ++num) {
  128. const std::shared_ptr<BufEntry>& buf = bufq_[num];
  129. ssize_t len = buf->getLength();
  130. if(amount >= len) {
  131. amount -= len;
  132. iov[num].A2IOVEC_BASE =
  133. reinterpret_cast<char*>(const_cast<unsigned char*>(buf->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_[0]->progressUpdate(slen, false);
  149. goto fin;
  150. } else {
  151. slen -= firstlen;
  152. bufq_[0]->progressUpdate(firstlen, true);
  153. bufq_.pop_front();
  154. offset_ = 0;
  155. for(size_t i = 1; i < num; ++i) {
  156. const std::shared_ptr<BufEntry>& buf = bufq_[0];
  157. ssize_t len = buf->getLength();
  158. if(len > slen) {
  159. offset_ = slen;
  160. bufq_[0]->progressUpdate(slen, false);
  161. goto fin;
  162. break;
  163. } else {
  164. slen -= len;
  165. bufq_[0]->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