1 #include "stg/blockio.h"
6 void* adjust(void* base, size_t shift)
8 char* ptr = static_cast<char*>(base);
12 } // namspace anonymous
14 using STG::BlockReader;
15 using STG::BlockWriter;
17 BlockReader::BlockReader(const IOVec& ioVec)
21 for (size_t i = 0; i < m_dest.size(); ++i)
22 m_remainder += m_dest[i].iov_len;
25 bool BlockReader::read(int socket)
30 size_t offset = m_dest.size() - 1;
31 size_t toRead = m_remainder;
33 if (toRead < m_dest[offset].iov_len)
35 toRead -= m_dest[offset].iov_len;
39 IOVec dest(m_dest.size() - offset);
40 for (size_t i = 0; i < dest.size(); ++i) {
42 dest[0].iov_len = toRead;
43 dest[0].iov_base = adjust(m_dest[offset].iov_base, m_dest[offset].iov_len - toRead);
45 dest[i] = m_dest[offset + i];
49 ssize_t res = readv(socket, dest.data(), dest.size());
53 return m_remainder == 0;
54 if (res < static_cast<ssize_t>(m_remainder))
61 BlockWriter::BlockWriter(const IOVec& ioVec)
65 for (size_t i = 0; i < m_source.size(); ++i)
66 m_remainder += m_source[i].iov_len;
69 bool BlockWriter::write(int socket)
74 size_t offset = m_source.size() - 1;
75 size_t toWrite = m_remainder;
77 if (toWrite < m_source[offset].iov_len)
79 toWrite -= m_source[offset].iov_len;
83 IOVec source(m_source.size() - offset);
84 for (size_t i = 0; i < source.size(); ++i) {
86 source[0].iov_len = toWrite;
87 source[0].iov_base = adjust(m_source[offset].iov_base, m_source[offset].iov_len - toWrite);
89 source[i] = m_source[offset + i];
92 ssize_t res = writev(socket, source.data(), source.size());
96 return m_remainder == 0;
97 if (res < static_cast<ssize_t>(m_remainder))