Skip to content

Commit

Permalink
bugfix for vector sender; path methods
Browse files Browse the repository at this point in the history
- When passing an r-value reference to ::send_datagram(...), we must make the shared_ptr keep-alive prior to constructing the string_view pointing into the data
- Path spaceship/==/!= methods added plus hash for indexing using paths
  • Loading branch information
dr7ana committed May 17, 2024
1 parent f4b75ec commit fb8be22
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
19 changes: 19 additions & 0 deletions include/oxen/quic/address.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ namespace oxen::quic
return r <=> 0;
return oxenc::big_to_host(a.sin_port) <=> oxenc::big_to_host(b.sin_port);
}

if (is_ipv6() && other.is_ipv6())
{
auto& a = in6();
Expand All @@ -258,6 +259,7 @@ namespace oxen::quic
}

bool operator==(const Address& other) const { return (*this <=> other) == 0; }

bool operator!=(const Address& other) const { return !(*this == other); }

// Returns the size of the sockaddr
Expand Down Expand Up @@ -340,6 +342,12 @@ namespace oxen::quic
return *this;
}

auto operator<=>(const Path& other) const { return std::tie(local, remote) <=> std::tie(other.local, other.remote); }

bool operator==(const Path& other) const { return (*this <=> other) == 0; }

bool operator!=(const Path& other) const { return !(*this == other); }

// template code to pass Path as ngtcp2_path into ngtcp2 functions
template <typename T>
requires std::same_as<T, ngtcp2_path>
Expand Down Expand Up @@ -390,4 +398,15 @@ namespace std
return h;
}
};

template <>
struct hash<oxen::quic::Path>
{
size_t operator()(const oxen::quic::Path& addr) const
{
auto h = hash<oxen::quic::Address>{}(addr.local);
h ^= hash<oxen::quic::Address>{}(addr.remote);
return h;
}
};
} // namespace std
6 changes: 3 additions & 3 deletions include/oxen/quic/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ namespace oxen::quic
template <oxenc::basic_char Char>
void send_datagram(std::vector<Char>&& buf)
{
send_datagram(
std::basic_string_view<Char>{buf.data(), buf.size()},
std::make_shared<std::vector<Char>>(std::move(buf)));
auto keep_alive = std::make_shared<std::vector<Char>>(std::move(buf));
std::basic_string_view<Char> view{keep_alive->data(), keep_alive->size()};
send_datagram(view, std::move(keep_alive));
}

template <oxenc::basic_char CharType>
Expand Down

0 comments on commit fb8be22

Please sign in to comment.