| 19 | #include "transfer.h" |
| 20 | |
| 21 | int64_t |
| 22 | transfer_content_bytes(Data *const data) |
| 23 | { |
| 24 | // nothing to transfer if there's no source. |
| 25 | if (nullptr == data->m_upstream.m_read.m_reader) { |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | TSIOBufferReader const reader = data->m_upstream.m_read.m_reader; |
| 30 | TSIOBuffer const output_buf = data->m_dnstream.m_write.m_iobuf; |
| 31 | TSVIO const output_vio = data->m_dnstream.m_write.m_vio; |
| 32 | |
| 33 | int64_t consumed = 0; // input vio bytes visited |
| 34 | int64_t copied = 0; // output bytes transferred |
| 35 | |
| 36 | int64_t avail = TSIOBufferReaderAvail(reader); |
| 37 | if (0 < avail) { |
| 38 | int64_t const toskip = std::min(data->m_blockskip, avail); |
| 39 | if (0 < toskip) { |
| 40 | TSIOBufferReaderConsume(reader, toskip); |
| 41 | data->m_blockskip -= toskip; |
| 42 | avail -= toskip; |
| 43 | consumed += toskip; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // bool const canWrite = data->m_dnstream.m_write.isOpen(); |
| 48 | |
| 49 | if (0 < avail) { |
| 50 | int64_t const bytesleft = data->m_bytestosend - data->m_bytessent; |
| 51 | int64_t const tocopy = std::min(avail, bytesleft); |
| 52 | if (0 < tocopy) { |
| 53 | copied = TSIOBufferCopy(output_buf, reader, tocopy, 0); |
| 54 | |
| 55 | data->m_bytessent += copied; |
| 56 | TSIOBufferReaderConsume(reader, copied); |
| 57 | |
| 58 | avail -= copied; |
| 59 | consumed += copied; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // if hit fulfillment start bulk consuming |
| 64 | if (0 < avail && data->m_bytestosend <= data->m_bytessent) { |
| 65 | TSIOBufferReaderConsume(reader, avail); |
| 66 | consumed += avail; |
| 67 | } |
| 68 | |
| 69 | if (0 < copied && nullptr != output_vio) { |
| 70 | TSVIOReenable(output_vio); |
| 71 | } |
| 72 | |
| 73 | if (0 < consumed) { |
| 74 | data->m_blockconsumed += consumed; |
| 75 | |
| 76 | TSVIO const input_vio = data->m_upstream.m_read.m_vio; |
| 77 | if (nullptr != input_vio) { |
| 78 | TSVIONDoneSet(input_vio, TSVIONDoneGet(input_vio) + consumed); |
no test coverage detected