refactor: buffer snake case (#4682)

This commit is contained in:
Charles Kerr 2023-01-27 20:12:09 -06:00 committed by GitHub
parent c75f9a4a7a
commit 948f597d15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 215 additions and 214 deletions

View File

@ -85,8 +85,8 @@ struct tau_scrape_request
// build the payload
auto buf = libtransmission::Buffer{};
buf.addUint32(TAU_ACTION_SCRAPE);
buf.addUint32(transaction_id);
buf.add_uint32(TAU_ACTION_SCRAPE);
buf.add_uint32(transaction_id);
for (int i = 0; i < in.info_hash_count; ++i)
{
buf.add(in.info_hash[i]);
@ -130,16 +130,16 @@ struct tau_scrape_request
}
auto& row = response.rows[i];
row.seeders = buf.toUint32();
row.downloads = buf.toUint32();
row.leechers = buf.toUint32();
row.seeders = buf.to_uint32();
row.downloads = buf.to_uint32();
row.leechers = buf.to_uint32();
}
requestFinished();
}
else
{
std::string const errmsg = action == TAU_ACTION_ERROR && !std::empty(buf) ? buf.toString() : _("Unknown error");
std::string const errmsg = action == TAU_ACTION_ERROR && !std::empty(buf) ? buf.to_string() : _("Unknown error");
fail(true, false, errmsg);
}
}
@ -179,18 +179,18 @@ struct tau_announce_request
// build the payload
auto buf = libtransmission::Buffer{};
buf.addUint32(TAU_ACTION_ANNOUNCE);
buf.addUint32(transaction_id);
buf.add_uint32(TAU_ACTION_ANNOUNCE);
buf.add_uint32(transaction_id);
buf.add(in.info_hash);
buf.add(in.peer_id);
buf.addUint64(in.down);
buf.addUint64(in.leftUntilComplete);
buf.addUint64(in.up);
buf.addUint32(get_tau_announce_event(in.event));
buf.addUint32(announce_ip);
buf.addUint32(in.key);
buf.addUint32(in.numwant);
buf.addPort(in.port);
buf.add_uint64(in.down);
buf.add_uint64(in.leftUntilComplete);
buf.add_uint64(in.up);
buf.add_uint32(get_tau_announce_event(in.event));
buf.add_uint32(announce_ip);
buf.add_uint32(in.key);
buf.add_uint32(in.numwant);
buf.add_port(in.port);
payload.insert(std::end(payload), std::begin(buf), std::end(buf));
}
@ -224,9 +224,9 @@ struct tau_announce_request
if (action == TAU_ACTION_ANNOUNCE && buflen >= 3 * sizeof(uint32_t))
{
response.interval = buf.toUint32();
response.leechers = buf.toUint32();
response.seeders = buf.toUint32();
response.interval = buf.to_uint32();
response.leechers = buf.to_uint32();
response.seeders = buf.to_uint32();
auto const [bytes, n_bytes] = buf.pullup();
response.pex = tr_pex::from_compact_ipv4(bytes, n_bytes, nullptr, 0);
@ -234,7 +234,7 @@ struct tau_announce_request
}
else
{
std::string const errmsg = action == TAU_ACTION_ERROR && !std::empty(buf) ? buf.toString() : _("Unknown error");
std::string const errmsg = action == TAU_ACTION_ERROR && !std::empty(buf) ? buf.to_string() : _("Unknown error");
fail(true, false, errmsg);
}
}
@ -318,13 +318,13 @@ struct tau_tracker
if (action == TAU_ACTION_CONNECT)
{
this->connection_id = buf.toUint64();
this->connection_id = buf.to_uint64();
this->connection_expiration_time = tr_time() + TauConnectionTtlSecs;
logdbg(this->key, fmt::format("Got a new connection ID from tracker: {}", this->connection_id));
}
else if (action == TAU_ACTION_ERROR)
{
std::string const errmsg = !std::empty(buf) ? buf.toString() : _("Connection failed");
std::string const errmsg = !std::empty(buf) ? buf.to_string() : _("Connection failed");
logdbg(this->key, errmsg);
this->failAll(true, false, errmsg);
}
@ -375,9 +375,9 @@ struct tau_tracker
logtrace(this->key, fmt::format("Trying to connect. Transaction ID is {}", this->connection_transaction_id));
auto buf = libtransmission::Buffer{};
buf.addUint64(0x41727101980LL);
buf.addUint32(TAU_ACTION_CONNECT);
buf.addUint32(this->connection_transaction_id);
buf.add_uint64(0x41727101980LL);
buf.add_uint32(TAU_ACTION_CONNECT);
buf.add_uint32(this->connection_transaction_id);
auto const [bytes, n_bytes] = buf.pullup();
this->sendto(bytes, n_bytes);
@ -537,7 +537,7 @@ private:
logdbg(this->key, fmt::format("sending request w/connection id {}", this->connection_id));
auto buf = libtransmission::Buffer{};
buf.addUint64(this->connection_id);
buf.add_uint64(this->connection_id);
buf.add(payload, payload_len);
auto const [bytes, n_bytes] = buf.pullup();
@ -626,7 +626,7 @@ public:
// extract the action_id and see if it makes sense
auto buf = libtransmission::Buffer{};
buf.add(msg, msglen);
auto const action_id = static_cast<tau_action_t>(buf.toUint32());
auto const action_id = static_cast<tau_action_t>(buf.to_uint32());
if (!isResponseMessage(action_id, msglen))
{
@ -634,7 +634,7 @@ public:
}
/* extract the transaction_id and look for a match */
tau_transaction_t const transaction_id = buf.toUint32();
tau_transaction_t const transaction_id = buf.to_uint32();
for (auto& tracker : trackers_)
{

View File

@ -212,13 +212,13 @@ ReadState tr_handshake::read_yb(tr_peerIo* peer_io)
peer_io->write(outbuf, false);
peer_io->encrypt_init(peer_io->is_incoming(), dh_, info_hash);
outbuf.add(VC);
outbuf.addUint32(crypto_provide());
outbuf.addUint16(0);
outbuf.add_uint32(crypto_provide());
outbuf.add_uint16(0);
/* ENCRYPT len(IA)), ENCRYPT(IA) */
if (auto msg = std::array<uint8_t, HandshakeSize>{}; build_handshake_message(peer_io, std::data(msg)))
{
outbuf.addUint16(std::size(msg));
outbuf.add_uint16(std::size(msg));
outbuf.add(msg);
have_sent_bittorrent_handshake_ = true;
}
@ -606,7 +606,7 @@ ReadState tr_handshake::read_ia(tr_peerIo* peer_io)
if (crypto_select != 0)
{
tr_logAddTraceHand(this, fmt::format("selecting crypto mode '{}'", crypto_select));
outbuf.addUint32(crypto_select);
outbuf.add_uint32(crypto_select);
}
else
{
@ -619,7 +619,7 @@ ReadState tr_handshake::read_ia(tr_peerIo* peer_io)
/* ENCRYPT(VC, crypto_provide, len(PadD), PadD
* PadD is reserved for future extensions to the handshake...
* standard practice at this time is for it to be zero-length */
outbuf.addUint16(0);
outbuf.add_uint16(0);
/* maybe de-encrypt our connection */
if (crypto_select == CryptoProvidePlaintext)

View File

@ -11,6 +11,7 @@
#include <array>
#include <cstdint> // uint8_t, uint32_t, uint64_t
#include <string>
#include "transmission.h"

View File

@ -603,7 +603,7 @@ void tr_peerIo::read_bytes(void* bytes, size_t byte_count)
{
TR_ASSERT(read_buffer_size() >= byte_count);
inbuf_.toBuf(bytes, byte_count);
inbuf_.to_buf(bytes, byte_count);
if (is_encrypted())
{

View File

@ -103,7 +103,7 @@ public:
template<typename T>
[[nodiscard]] auto read_buffer_starts_with(T const& t) const noexcept
{
return inbuf_.startsWith(t);
return inbuf_.starts_with(t);
}
void read_buffer_drain(size_t byte_count);

View File

@ -579,11 +579,11 @@ private:
TR_ASSERT(isValidRequest(req));
auto& out = outMessages;
out.addUint32(sizeof(uint8_t) + 3 * sizeof(uint32_t));
out.addUint8(BtPeerMsgs::Request);
out.addUint32(req.index);
out.addUint32(req.offset);
out.addUint32(req.length);
out.add_uint32(sizeof(uint8_t) + 3 * sizeof(uint32_t));
out.add_uint8(BtPeerMsgs::Request);
out.add_uint32(req.index);
out.add_uint32(req.offset);
out.add_uint32(req.length);
logtrace(this, fmt::format(FMT_STRING("requesting {:d}:{:d}->{:d}..."), req.index, req.offset, req.length));
dbgOutMessageLen();
@ -714,11 +714,11 @@ void protocolSendReject(tr_peerMsgsImpl* msgs, struct peer_request const* req)
auto& out = msgs->outMessages;
out.addUint32(sizeof(uint8_t) + 3 * sizeof(uint32_t));
out.addUint8(BtPeerMsgs::FextReject);
out.addUint32(req->index);
out.addUint32(req->offset);
out.addUint32(req->length);
out.add_uint32(sizeof(uint8_t) + 3 * sizeof(uint32_t));
out.add_uint8(BtPeerMsgs::FextReject);
out.add_uint32(req->index);
out.add_uint32(req->offset);
out.add_uint32(req->length);
logtrace(msgs, fmt::format(FMT_STRING("rejecting {:d}:{:d}->{:d}..."), req->index, req->offset, req->length));
msgs->dbgOutMessageLen();
@ -728,11 +728,11 @@ void protocolSendCancel(tr_peerMsgsImpl* msgs, peer_request const& req)
{
auto& out = msgs->outMessages;
out.addUint32(sizeof(uint8_t) + 3 * sizeof(uint32_t));
out.addUint8(BtPeerMsgs::Cancel);
out.addUint32(req.index);
out.addUint32(req.offset);
out.addUint32(req.length);
out.add_uint32(sizeof(uint8_t) + 3 * sizeof(uint32_t));
out.add_uint8(BtPeerMsgs::Cancel);
out.add_uint32(req.index);
out.add_uint32(req.offset);
out.add_uint32(req.length);
logtrace(msgs, fmt::format(FMT_STRING("cancelling {:d}:{:d}->{:d}..."), req.index, req.offset, req.length));
msgs->dbgOutMessageLen();
@ -744,18 +744,18 @@ void protocolSendPort(tr_peerMsgsImpl* msgs, tr_port port)
auto& out = msgs->outMessages;
logtrace(msgs, fmt::format(FMT_STRING("sending Port {:d}"), port.host()));
out.addUint32(3);
out.addUint8(BtPeerMsgs::Port);
out.addPort(port);
out.add_uint32(3);
out.add_uint8(BtPeerMsgs::Port);
out.add_port(port);
}
void protocolSendHave(tr_peerMsgsImpl* msgs, tr_piece_index_t index)
{
auto& out = msgs->outMessages;
out.addUint32(sizeof(uint8_t) + sizeof(uint32_t));
out.addUint8(BtPeerMsgs::Have);
out.addUint32(index);
out.add_uint32(sizeof(uint8_t) + sizeof(uint32_t));
out.add_uint8(BtPeerMsgs::Have);
out.add_uint32(index);
logtrace(msgs, fmt::format(FMT_STRING("sending Have {:d}"), index));
msgs->dbgOutMessageLen();
@ -766,8 +766,8 @@ void protocolSendChoke(tr_peerMsgsImpl* msgs, bool choke)
{
auto& out = msgs->outMessages;
out.addUint32(sizeof(uint8_t));
out.addUint8(choke ? BtPeerMsgs::Choke : BtPeerMsgs::Unchoke);
out.add_uint32(sizeof(uint8_t));
out.add_uint8(choke ? BtPeerMsgs::Choke : BtPeerMsgs::Unchoke);
logtrace(msgs, choke ? "sending choke" : "sending unchoked");
msgs->dbgOutMessageLen();
@ -780,8 +780,8 @@ void protocolSendHaveAll(tr_peerMsgsImpl* msgs)
auto& out = msgs->outMessages;
out.addUint32(sizeof(uint8_t));
out.addUint8(BtPeerMsgs::FextHaveAll);
out.add_uint32(sizeof(uint8_t));
out.add_uint8(BtPeerMsgs::FextHaveAll);
logtrace(msgs, "sending HAVE_ALL...");
msgs->dbgOutMessageLen();
@ -794,8 +794,8 @@ void protocolSendHaveNone(tr_peerMsgsImpl* msgs)
auto& out = msgs->outMessages;
out.addUint32(sizeof(uint8_t));
out.addUint8(BtPeerMsgs::FextHaveNone);
out.add_uint32(sizeof(uint8_t));
out.add_uint8(BtPeerMsgs::FextHaveNone);
logtrace(msgs, "sending HAVE_NONE...");
msgs->dbgOutMessageLen();
@ -811,8 +811,8 @@ void sendInterest(tr_peerMsgsImpl* msgs, bool b)
auto& out = msgs->outMessages;
logtrace(msgs, b ? "Sending Interested" : "Sending Not Interested");
out.addUint32(sizeof(uint8_t));
out.addUint8(b ? BtPeerMsgs::Interested : BtPeerMsgs::NotInterested);
out.add_uint32(sizeof(uint8_t));
out.add_uint8(b ? BtPeerMsgs::Interested : BtPeerMsgs::NotInterested);
msgs->pokeBatchPeriod(HighPriorityIntervalSecs);
msgs->dbgOutMessageLen();
@ -958,9 +958,9 @@ void sendLtepHandshake(tr_peerMsgsImpl* msgs)
auto payload = tr_variantToStr(&val, TR_VARIANT_FMT_BENC);
out.addUint32(2 * sizeof(uint8_t) + std::size(payload));
out.addUint8(BtPeerMsgs::Ltep);
out.addUint8(LtepMessages::Handshake);
out.add_uint32(2 * sizeof(uint8_t) + std::size(payload));
out.add_uint8(BtPeerMsgs::Ltep);
out.add_uint8(LtepMessages::Handshake);
out.add(payload);
msgs->pokeBatchPeriod(ImmediatePriorityIntervalSecs);
msgs->dbgOutMessageLen();
@ -1131,9 +1131,9 @@ void parseUtMetadata(tr_peerMsgsImpl* msgs, uint32_t msglen)
auto const payload = tr_variantToStr(&v, TR_VARIANT_FMT_BENC);
/* write it out as a LTEP message to our outMessages buffer */
out.addUint32(2 * sizeof(uint8_t) + std::size(payload));
out.addUint8(BtPeerMsgs::Ltep);
out.addUint8(msgs->ut_metadata_id);
out.add_uint32(2 * sizeof(uint8_t) + std::size(payload));
out.add_uint8(BtPeerMsgs::Ltep);
out.add_uint8(msgs->ut_metadata_id);
out.add(payload);
msgs->pokeBatchPeriod(HighPriorityIntervalSecs);
msgs->dbgOutMessageLen();
@ -1882,9 +1882,9 @@ void updateMetadataRequests(tr_peerMsgsImpl* msgs, time_t now)
logtrace(msgs, fmt::format(FMT_STRING("requesting metadata piece #{:d}"), *piece));
/* write it out as a LTEP message to our outMessages buffer */
out.addUint32(2 * sizeof(uint8_t) + std::size(payload));
out.addUint8(BtPeerMsgs::Ltep);
out.addUint8(msgs->ut_metadata_id);
out.add_uint32(2 * sizeof(uint8_t) + std::size(payload));
out.add_uint8(BtPeerMsgs::Ltep);
out.add_uint8(msgs->ut_metadata_id);
out.add(payload);
msgs->pokeBatchPeriod(HighPriorityIntervalSecs);
msgs->dbgOutMessageLen();
@ -1970,9 +1970,9 @@ size_t fillOutputBuffer(tr_peerMsgsImpl* msgs, time_t now)
auto const payload = tr_variantToStr(&tmp, TR_VARIANT_FMT_BENC);
/* write it out as a LTEP message to our outMessages buffer */
out.addUint32(2 * sizeof(uint8_t) + std::size(payload) + std::size(*piece_data));
out.addUint8(BtPeerMsgs::Ltep);
out.addUint8(msgs->ut_metadata_id);
out.add_uint32(2 * sizeof(uint8_t) + std::size(payload) + std::size(*piece_data));
out.add_uint8(BtPeerMsgs::Ltep);
out.add_uint8(msgs->ut_metadata_id);
out.add(payload);
out.add(*piece_data);
msgs->pokeBatchPeriod(HighPriorityIntervalSecs);
@ -1995,9 +1995,9 @@ size_t fillOutputBuffer(tr_peerMsgsImpl* msgs, time_t now)
auto payload = tr_variantToStr(&tmp, TR_VARIANT_FMT_BENC);
/* write it out as a LTEP message to our outMessages buffer */
out.addUint32(2 * sizeof(uint8_t) + std::size(payload));
out.addUint8(BtPeerMsgs::Ltep);
out.addUint8(msgs->ut_metadata_id);
out.add_uint32(2 * sizeof(uint8_t) + std::size(payload));
out.add_uint8(BtPeerMsgs::Ltep);
out.add_uint8(msgs->ut_metadata_id);
out.add(payload);
msgs->pokeBatchPeriod(HighPriorityIntervalSecs);
msgs->dbgOutMessageLen();
@ -2020,10 +2020,10 @@ size_t fillOutputBuffer(tr_peerMsgsImpl* msgs, time_t now)
auto out = libtransmission::Buffer{};
out.reserve(msglen);
out.addUint32(sizeof(uint8_t) + 2 * sizeof(uint32_t) + req.length);
out.addUint8(BtPeerMsgs::Piece);
out.addUint32(req.index);
out.addUint32(req.offset);
out.add_uint32(sizeof(uint8_t) + 2 * sizeof(uint32_t) + req.length);
out.add_uint8(BtPeerMsgs::Piece);
out.add_uint32(req.index);
out.add_uint32(req.offset);
auto buf = std::array<uint8_t, tr_block_info::BlockSize>{};
bool err = msgs->session->cache->readBlock(
msgs->torrent,
@ -2083,7 +2083,7 @@ size_t fillOutputBuffer(tr_peerMsgsImpl* msgs, time_t now)
if (msgs != nullptr && msgs->clientSentAnythingAt != 0 && now - msgs->clientSentAnythingAt > KeepaliveIntervalSecs)
{
logtrace(msgs, "sending a keepalive message");
msgs->outMessages.addUint32(0);
msgs->outMessages.add_uint32(0);
msgs->pokeBatchPeriod(ImmediatePriorityIntervalSecs);
}
@ -2120,8 +2120,8 @@ void sendBitfield(tr_peerMsgsImpl* msgs)
auto& out = msgs->outMessages;
auto bytes = msgs->torrent->createPieceBitfield();
out.addUint32(sizeof(uint8_t) + bytes.size());
out.addUint8(BtPeerMsgs::Bitfield);
out.add_uint32(sizeof(uint8_t) + bytes.size());
out.add_uint8(BtPeerMsgs::Bitfield);
out.add(bytes);
logtrace(msgs, fmt::format(FMT_STRING("sending bitfield... outMessage size is now {:d}"), std::size(out)));
msgs->pokeBatchPeriod(ImmediatePriorityIntervalSecs);
@ -2276,9 +2276,9 @@ void tr_peerMsgsImpl::sendPex()
/* write the pex message */
auto payload = tr_variantToStr(&val, TR_VARIANT_FMT_BENC);
out.addUint32(2 * sizeof(uint8_t) + std::size(payload));
out.addUint8(BtPeerMsgs::Ltep);
out.addUint8(this->ut_pex_id);
out.add_uint32(2 * sizeof(uint8_t) + std::size(payload));
out.add_uint8(BtPeerMsgs::Ltep);
out.add_uint8(this->ut_pex_id);
out.add(payload);
this->pokeBatchPeriod(HighPriorityIntervalSecs);
logtrace(this, fmt::format(FMT_STRING("sending a pex message; outMessage size is now {:d}"), std::size(out)));

View File

@ -79,7 +79,7 @@ size_t tr_peer_socket::try_write(Buffer& buf, size_t max, tr_error** error) cons
if (is_tcp())
{
return buf.toSocket(handle.tcp, max, error);
return buf.to_socket(handle.tcp, max, error);
}
#ifdef WITH_UTP
@ -116,7 +116,7 @@ size_t tr_peer_socket::try_read(Buffer& buf, size_t max, tr_error** error) const
if (is_tcp())
{
return buf.addSocket(handle.tcp, max, error);
return buf.add_socket(handle.tcp, max, error);
}
#ifdef WITH_UTP

View File

@ -10,7 +10,6 @@
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <event2/buffer.h>
@ -37,7 +36,7 @@ public:
Iterator(evbuffer* buf, size_t offset)
: buf_{ buf }
{
setOffset(offset);
set_offset(offset);
}
[[nodiscard]] constexpr value_type& operator*() noexcept
@ -79,7 +78,7 @@ public:
}
else
{
incOffset(n_bytes);
inc_offset(n_bytes);
}
return *this;
@ -93,7 +92,7 @@ public:
}
else
{
setOffset(offset() - 1);
set_offset(offset() - 1);
}
return *this;
}
@ -114,14 +113,14 @@ public:
return ptr_.pos + iov_offset_;
}
void incOffset(size_t increment)
void inc_offset(size_t increment)
{
evbuffer_ptr_set(buf_, &ptr_, iov_offset_ + increment, EVBUFFER_PTR_ADD);
evbuffer_peek(buf_, std::numeric_limits<ev_ssize_t>::max(), &ptr_, &iov_, 1);
iov_offset_ = 0;
}
void setOffset(size_t offset)
void set_offset(size_t offset)
{
evbuffer_ptr_set(buf_, &ptr_, offset, EVBUFFER_PTR_SET);
evbuffer_peek(buf_, std::numeric_limits<ev_ssize_t>::max(), &ptr_, &iov_, 1);
@ -176,18 +175,8 @@ public:
return Iterator{ buf_.get(), size() };
}
[[nodiscard]] auto cbegin() const noexcept
{
return Iterator{ buf_.get(), 0U };
}
[[nodiscard]] auto cend() const noexcept
{
return Iterator{ buf_.get(), size() };
}
template<typename T>
[[nodiscard]] TR_CONSTEXPR20 bool startsWith(T const& needle) const
[[nodiscard]] TR_CONSTEXPR20 bool starts_with(T const& needle) const
{
auto const n_bytes = std::size(needle);
auto const needle_begin = reinterpret_cast<std::byte const*>(std::data(needle));
@ -195,29 +184,37 @@ public:
return n_bytes <= size() && std::equal(needle_begin, needle_end, cbegin());
}
auto toBuf(void* tgt, size_t n_bytes)
[[nodiscard]] std::string to_string() const
{
auto str = std::string{};
str.resize(size());
evbuffer_copyout(buf_.get(), std::data(str), std::size(str));
return str;
}
auto to_buf(void* tgt, size_t n_bytes)
{
return evbuffer_remove(buf_.get(), tgt, n_bytes);
}
[[nodiscard]] uint16_t toUint16()
[[nodiscard]] uint16_t to_uint16()
{
auto tmp = uint16_t{};
toBuf(&tmp, sizeof(tmp));
to_buf(&tmp, sizeof(tmp));
return ntohs(tmp);
}
[[nodiscard]] uint32_t toUint32()
[[nodiscard]] uint32_t to_uint32()
{
auto tmp = uint32_t{};
toBuf(&tmp, sizeof(tmp));
to_buf(&tmp, sizeof(tmp));
return ntohl(tmp);
}
[[nodiscard]] uint64_t toUint64()
[[nodiscard]] uint64_t to_uint64()
{
auto tmp = uint64_t{};
toBuf(&tmp, sizeof(tmp));
to_buf(&tmp, sizeof(tmp));
return tr_ntohll(tmp);
}
@ -232,7 +229,7 @@ public:
}
// Returns the number of bytes written. Check `error` for error.
size_t toSocket(tr_socket_t sockfd, size_t n_bytes, tr_error** error = nullptr)
size_t to_socket(tr_socket_t sockfd, size_t n_bytes, tr_error** error = nullptr)
{
EVUTIL_SET_SOCKET_ERROR(0);
auto const res = evbuffer_write_atmost(buf_.get(), sockfd, n_bytes);
@ -255,7 +252,7 @@ public:
evbuffer_expand(buf_.get(), n_bytes - size());
}
size_t addSocket(tr_socket_t sockfd, size_t n_bytes, tr_error** error = nullptr)
size_t add_socket(tr_socket_t sockfd, size_t n_bytes, tr_error** error = nullptr)
{
EVUTIL_SET_SOCKET_ERROR(0);
auto const res = evbuffer_read(buf_.get(), sockfd, static_cast<int>(n_bytes));
@ -310,60 +307,62 @@ public:
add(&ch, 1);
}
void addPort(tr_port const& port)
void add_port(tr_port const& port)
{
auto nport = port.network();
add(&nport, sizeof(nport));
}
void addUint8(uint8_t uch)
void add_uint8(uint8_t uch)
{
add(&uch, 1);
}
void addUint16(uint16_t hs)
void add_uint16(uint16_t hs)
{
uint16_t const ns = htons(hs);
add(&ns, sizeof(ns));
}
void addHton16(uint16_t hs)
void add_hton16(uint16_t hs)
{
addUint16(hs);
add_uint16(hs);
}
void addUint32(uint32_t hl)
void add_uint32(uint32_t hl)
{
uint32_t const nl = htonl(hl);
add(&nl, sizeof(nl));
}
void addHton32(uint32_t hl)
void eadd_hton32(uint32_t hl)
{
addUint32(hl);
add_uint32(hl);
}
void addUint64(uint64_t hll)
void add_uint64(uint64_t hll)
{
uint64_t const nll = tr_htonll(hll);
add(&nll, sizeof(nll));
}
void addHton64(uint64_t hll)
void add_hton64(uint64_t hll)
{
addUint64(hll);
}
[[nodiscard]] std::string toString() const
{
auto str = std::string{};
str.resize(size());
evbuffer_copyout(buf_.get(), std::data(str), std::size(str));
return str;
add_uint64(hll);
}
private:
evhelpers::evbuffer_unique_ptr buf_{ evbuffer_new() };
[[nodiscard]] Iterator cbegin() const noexcept
{
return Iterator{ buf_.get(), 0U };
}
[[nodiscard]] Iterator cend() const noexcept
{
return Iterator{ buf_.get(), size() };
}
};
} // namespace libtransmission

View File

@ -350,5 +350,5 @@ std::string tr_variantToStrBenc(tr_variant const* top)
auto buf = libtransmission::Buffer{};
tr_variantWalk(top, &walk_funcs, &buf, true);
return buf.toString();
return buf.to_string();
}

View File

@ -11,6 +11,7 @@
#include <cstdint> // int64_t
#include <optional>
#include <string>
#include <string_view>
#include "transmission.h"

View File

@ -689,5 +689,5 @@ std::string tr_variantToStrJson(tr_variant const* top, bool lean)
{
buf.push_back('\n');
}
return buf.toString();
return buf.to_string();
}

View File

@ -113,9 +113,9 @@ protected:
[[nodiscard]] static uint32_t parseConnectionRequest(libtransmission::Buffer& buf)
{
EXPECT_EQ(ProtocolId, buf.toUint64());
EXPECT_EQ(ConnectAction, buf.toUint32());
return buf.toUint32();
EXPECT_EQ(ProtocolId, buf.to_uint64());
EXPECT_EQ(ConnectAction, buf.to_uint32());
return buf.to_uint32();
}
[[nodiscard]] static auto buildScrapeRequestFromResponse(tr_scrape_response const& response)
@ -149,14 +149,14 @@ protected:
[[nodiscard]] static auto parseScrapeRequest(libtransmission::Buffer& buf, uint64_t expected_connection_id)
{
EXPECT_EQ(expected_connection_id, buf.toUint64());
EXPECT_EQ(ScrapeAction, buf.toUint32());
auto const transaction_id = buf.toUint32();
EXPECT_EQ(expected_connection_id, buf.to_uint64());
EXPECT_EQ(ScrapeAction, buf.to_uint32());
auto const transaction_id = buf.to_uint32();
auto info_hashes = std::vector<tr_sha1_digest_t>{};
while (!std::empty(buf))
{
auto tmp = tr_sha1_digest_t{};
buf.toBuf(std::data(tmp), std::size(tmp));
buf.to_buf(std::data(tmp), std::size(tmp));
info_hashes.emplace_back(tmp);
}
return std::make_pair(transaction_id, info_hashes);
@ -173,13 +173,13 @@ protected:
[[nodiscard]] static bool sendError(tr_announcer_udp& announcer, uint32_t transaction_id, std::string_view errmsg)
{
auto buf = libtransmission::Buffer{};
buf.addUint32(ErrorAction);
buf.addUint32(transaction_id);
buf.add_uint32(ErrorAction);
buf.add_uint32(transaction_id);
buf.add(errmsg);
auto const response_size = std::size(buf);
auto arr = std::array<uint8_t, 256>{};
buf.toBuf(std::data(arr), response_size);
buf.to_buf(std::data(arr), response_size);
return announcer.handleMessage(std::data(arr), response_size);
}
@ -188,13 +188,13 @@ protected:
{
auto const connection_id = tr_rand_obj<uint64_t>();
auto buf = libtransmission::Buffer{};
buf.addUint32(ConnectAction);
buf.addUint32(transaction_id);
buf.addUint64(connection_id);
buf.add_uint32(ConnectAction);
buf.add_uint32(transaction_id);
buf.add_uint64(connection_id);
auto arr = std::array<uint8_t, 128>{};
auto response_size = std::size(buf);
buf.toBuf(std::data(arr), response_size);
buf.to_buf(std::data(arr), response_size);
EXPECT_TRUE(announcer.handleMessage(std::data(arr), response_size));
return connection_id;
@ -253,19 +253,19 @@ protected:
[[nodiscard]] static auto parseAnnounceRequest(libtransmission::Buffer& buf, uint64_t connection_id)
{
auto req = UdpAnnounceReq{};
req.connection_id = buf.toUint64();
req.action = buf.toUint32();
req.transaction_id = buf.toUint32();
buf.toBuf(std::data(req.info_hash), std::size(req.info_hash));
buf.toBuf(std::data(req.peer_id), std::size(req.peer_id));
req.downloaded = buf.toUint64();
req.left = buf.toUint64();
req.uploaded = buf.toUint64();
req.event = buf.toUint32();
req.ip_address = buf.toUint32();
req.key = buf.toUint32();
req.num_want = buf.toUint32();
req.port = buf.toUint16();
req.connection_id = buf.to_uint64();
req.action = buf.to_uint32();
req.transaction_id = buf.to_uint32();
buf.to_buf(std::data(req.info_hash), std::size(req.info_hash));
buf.to_buf(std::data(req.peer_id), std::size(req.peer_id));
req.downloaded = buf.to_uint64();
req.left = buf.to_uint64();
req.uploaded = buf.to_uint64();
req.event = buf.to_uint32();
req.ip_address = buf.to_uint32();
req.key = buf.to_uint32();
req.num_want = buf.to_uint32();
req.port = buf.to_uint16();
EXPECT_EQ(AnnounceAction, req.action);
EXPECT_EQ(connection_id, req.connection_id);
@ -327,14 +327,14 @@ TEST_F(AnnouncerUdpTest, canScrape)
// Have the tracker respond to the request
auto buf = libtransmission::Buffer{};
buf.addUint32(ScrapeAction);
buf.addUint32(scrape_transaction_id);
buf.addUint32(expected_response.rows[0].seeders);
buf.addUint32(expected_response.rows[0].downloads);
buf.addUint32(expected_response.rows[0].leechers);
buf.add_uint32(ScrapeAction);
buf.add_uint32(scrape_transaction_id);
buf.add_uint32(expected_response.rows[0].seeders);
buf.add_uint32(expected_response.rows[0].downloads);
buf.add_uint32(expected_response.rows[0].leechers);
auto response_size = std::size(buf);
auto arr = std::array<uint8_t, 256>{};
buf.toBuf(std::data(arr), response_size);
buf.to_buf(std::data(arr), response_size);
EXPECT_TRUE(announcer->handleMessage(std::data(arr), response_size));
// confirm that announcer processed the response
@ -408,17 +408,17 @@ TEST_F(AnnouncerUdpTest, canMultiScrape)
// Have the tracker respond to the request
auto buf = libtransmission::Buffer{};
buf.addUint32(ScrapeAction);
buf.addUint32(scrape_transaction_id);
buf.add_uint32(ScrapeAction);
buf.add_uint32(scrape_transaction_id);
for (int i = 0; i < expected_response.row_count; ++i)
{
buf.addUint32(expected_response.rows[i].seeders);
buf.addUint32(expected_response.rows[i].downloads);
buf.addUint32(expected_response.rows[i].leechers);
buf.add_uint32(expected_response.rows[i].seeders);
buf.add_uint32(expected_response.rows[i].downloads);
buf.add_uint32(expected_response.rows[i].leechers);
}
auto response_size = std::size(buf);
auto arr = std::array<uint8_t, 256>{};
buf.toBuf(std::data(arr), response_size);
buf.to_buf(std::data(arr), response_size);
EXPECT_TRUE(announcer->handleMessage(std::data(arr), response_size));
// Confirm that announcer processed the response
@ -542,21 +542,21 @@ TEST_F(AnnouncerUdpTest, handleMessageReturnsFalseOnInvalidMessage)
// send a connection response but with an *invalid* transaction id
auto buf = libtransmission::Buffer{};
buf.addUint32(ConnectAction);
buf.addUint32(transaction_id + 1);
buf.addUint64(tr_rand_obj<uint64_t>());
buf.add_uint32(ConnectAction);
buf.add_uint32(transaction_id + 1);
buf.add_uint64(tr_rand_obj<uint64_t>());
auto response_size = std::size(buf);
auto arr = std::array<uint8_t, 256>{};
buf.toBuf(std::data(arr), response_size);
buf.to_buf(std::data(arr), response_size);
EXPECT_FALSE(announcer->handleMessage(std::data(arr), response_size));
// send a connection response but with an *invalid* action
buf.clear();
buf.addUint32(ScrapeAction);
buf.addUint32(transaction_id);
buf.addUint64(tr_rand_obj<uint64_t>());
buf.add_uint32(ScrapeAction);
buf.add_uint32(transaction_id);
buf.add_uint64(tr_rand_obj<uint64_t>());
response_size = std::size(buf);
buf.toBuf(std::data(arr), response_size);
buf.to_buf(std::data(arr), response_size);
EXPECT_FALSE(announcer->handleMessage(std::data(arr), response_size));
// but after discarding invalid messages,
@ -629,21 +629,21 @@ TEST_F(AnnouncerUdpTest, canAnnounce)
// Have the tracker respond to the request
auto buf = libtransmission::Buffer{};
buf.addUint32(AnnounceAction);
buf.addUint32(udp_ann_req.transaction_id);
buf.addUint32(expected_response.interval);
buf.addUint32(expected_response.leechers);
buf.addUint32(expected_response.seeders);
buf.add_uint32(AnnounceAction);
buf.add_uint32(udp_ann_req.transaction_id);
buf.add_uint32(expected_response.interval);
buf.add_uint32(expected_response.leechers);
buf.add_uint32(expected_response.seeders);
for (auto const& [addr, port] : addresses)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
buf.add(&addr.addr.addr4.s_addr, sizeof(addr.addr.addr4.s_addr));
buf.addUint16(port.host());
buf.add_uint16(port.host());
}
auto response_size = std::size(buf);
auto arr = std::array<uint8_t, 512>{};
buf.toBuf(std::data(arr), response_size);
buf.to_buf(std::data(arr), response_size);
EXPECT_TRUE(announcer->handleMessage(std::data(arr), response_size));
// Confirm that announcer processed the response

View File

@ -21,21 +21,21 @@ TEST_F(BufferTest, startsWithInSingleSegment)
auto buf = Buffer{};
buf.add(Hello);
EXPECT_TRUE(buf.startsWith(Hello));
EXPECT_TRUE(buf.starts_with(Hello));
buf.add(World);
EXPECT_TRUE(buf.startsWith(Hello));
EXPECT_TRUE(buf.startsWith("Hello, Worl"sv));
EXPECT_TRUE(buf.startsWith("Hello, World"sv));
EXPECT_FALSE(buf.startsWith("Hello, World!"sv));
EXPECT_FALSE(buf.startsWith("Hello!"sv));
EXPECT_TRUE(buf.starts_with(Hello));
EXPECT_TRUE(buf.starts_with("Hello, Worl"sv));
EXPECT_TRUE(buf.starts_with("Hello, World"sv));
EXPECT_FALSE(buf.starts_with("Hello, World!"sv));
EXPECT_FALSE(buf.starts_with("Hello!"sv));
buf.add(Bang);
EXPECT_FALSE(buf.startsWith("Hello!"));
EXPECT_TRUE(buf.startsWith(Hello));
EXPECT_TRUE(buf.startsWith("Hello, Worl"sv));
EXPECT_TRUE(buf.startsWith("Hello, World"sv));
EXPECT_TRUE(buf.startsWith("Hello, World!"sv));
EXPECT_FALSE(buf.starts_with("Hello!"));
EXPECT_TRUE(buf.starts_with(Hello));
EXPECT_TRUE(buf.starts_with("Hello, Worl"sv));
EXPECT_TRUE(buf.starts_with("Hello, World"sv));
EXPECT_TRUE(buf.starts_with("Hello, World!"sv));
}
TEST_F(BufferTest, startsWithInMultiSegment)
{
@ -45,19 +45,19 @@ TEST_F(BufferTest, startsWithInMultiSegment)
auto buf = std::make_unique<Buffer>();
buf->add(Buffer{ Hello });
EXPECT_TRUE(buf->startsWith(Hello));
EXPECT_TRUE(buf->starts_with(Hello));
buf->add(Buffer{ World });
EXPECT_TRUE(buf->startsWith(Hello));
EXPECT_TRUE(buf->startsWith("Hello, Worl"sv));
EXPECT_TRUE(buf->startsWith("Hello, World"sv));
EXPECT_FALSE(buf->startsWith("Hello, World!"sv));
EXPECT_FALSE(buf->startsWith("Hello!"sv));
EXPECT_TRUE(buf->starts_with(Hello));
EXPECT_TRUE(buf->starts_with("Hello, Worl"sv));
EXPECT_TRUE(buf->starts_with("Hello, World"sv));
EXPECT_FALSE(buf->starts_with("Hello, World!"sv));
EXPECT_FALSE(buf->starts_with("Hello!"sv));
buf->add(Buffer{ Bang });
EXPECT_FALSE(buf->startsWith("Hello!"));
EXPECT_TRUE(buf->startsWith(Hello));
EXPECT_TRUE(buf->startsWith("Hello, Worl"sv));
EXPECT_TRUE(buf->startsWith("Hello, World"sv));
EXPECT_TRUE(buf->startsWith("Hello, World!"sv));
EXPECT_FALSE(buf->starts_with("Hello!"));
EXPECT_TRUE(buf->starts_with(Hello));
EXPECT_TRUE(buf->starts_with("Hello, Worl"sv));
EXPECT_TRUE(buf->starts_with("Hello, World"sv));
EXPECT_TRUE(buf->starts_with("Hello, World!"sv));
}