From eed28a5852371158c4466cdf299a149ec1bf96fd Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 22 Jul 2018 13:01:00 -0700 Subject: [PATCH] Add sockaddr_in overloads for uv::Tcp and uv::Udp (#1206) These help avoid the need for reinterpret_casts in common use cases. --- wpiutil/src/main/native/include/wpi/uv/Tcp.h | 26 ++++++++++++++ wpiutil/src/main/native/include/wpi/uv/Udp.h | 36 ++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/wpiutil/src/main/native/include/wpi/uv/Tcp.h b/wpiutil/src/main/native/include/wpi/uv/Tcp.h index 791c80d1f2..08e6a7135a 100644 --- a/wpiutil/src/main/native/include/wpi/uv/Tcp.h +++ b/wpiutil/src/main/native/include/wpi/uv/Tcp.h @@ -166,6 +166,14 @@ class Tcp final : public NetworkStreamImpl { Invoke(&uv_tcp_bind, GetRaw(), &addr, flags); } + void Bind(const sockaddr_in& addr, unsigned int flags = 0) { + Bind(reinterpret_cast(addr), flags); + } + + void Bind(const sockaddr_in6& addr, unsigned int flags = 0) { + Bind(reinterpret_cast(addr), flags); + } + /** * Bind the handle to an IPv4 address and port. * @@ -227,6 +235,16 @@ class Tcp final : public NetworkStreamImpl { */ void Connect(const sockaddr& addr, const std::shared_ptr& req); + void Connect(const sockaddr_in& addr, + const std::shared_ptr& req) { + Connect(reinterpret_cast(addr), req); + } + + void Connect(const sockaddr_in6& addr, + const std::shared_ptr& req) { + Connect(reinterpret_cast(addr), req); + } + /** * Establish an IPv4 or IPv6 TCP connection. * @@ -242,6 +260,14 @@ class Tcp final : public NetworkStreamImpl { */ void Connect(const sockaddr& addr, std::function callback); + void Connect(const sockaddr_in& addr, std::function callback) { + Connect(reinterpret_cast(addr), callback); + } + + void Connect(const sockaddr_in6& addr, std::function callback) { + Connect(reinterpret_cast(addr), callback); + } + /** * Establish an IPv4 TCP connection. * diff --git a/wpiutil/src/main/native/include/wpi/uv/Udp.h b/wpiutil/src/main/native/include/wpi/uv/Udp.h index 9c9ade72b8..7ceb0bbb92 100644 --- a/wpiutil/src/main/native/include/wpi/uv/Udp.h +++ b/wpiutil/src/main/native/include/wpi/uv/Udp.h @@ -89,6 +89,14 @@ class Udp final : public HandleImpl { Invoke(&uv_udp_bind, GetRaw(), &addr, flags); } + void Bind(const sockaddr_in& addr, unsigned int flags = 0) { + Bind(reinterpret_cast(addr), flags); + } + + void Bind(const sockaddr_in6& addr, unsigned int flags = 0) { + Bind(reinterpret_cast(addr), flags); + } + /** * Bind the handle to an IPv4 address and port. * @@ -186,6 +194,16 @@ class Udp final : public HandleImpl { void Send(const sockaddr& addr, ArrayRef bufs, const std::shared_ptr& req); + void Send(const sockaddr_in& addr, ArrayRef bufs, + const std::shared_ptr& req) { + Send(reinterpret_cast(addr), bufs, req); + } + + void Send(const sockaddr_in6& addr, ArrayRef bufs, + const std::shared_ptr& req) { + Send(reinterpret_cast(addr), bufs, req); + } + /** * Send data over the UDP socket. If the socket has not previously been bound * with Bind() it will be bound to 0.0.0.0 (the "all interfaces" IPv4 address) @@ -206,6 +224,16 @@ class Udp final : public HandleImpl { void Send(const sockaddr& addr, ArrayRef bufs, std::function, Error)> callback); + void Send(const sockaddr_in& addr, ArrayRef bufs, + std::function, Error)> callback) { + Send(reinterpret_cast(addr), bufs, callback); + } + + void Send(const sockaddr_in6& addr, ArrayRef bufs, + std::function, Error)> callback) { + Send(reinterpret_cast(addr), bufs, callback); + } + /** * Same as Send(), but won't queue a send request if it can't be completed * immediately. @@ -224,6 +252,14 @@ class Udp final : public HandleImpl { return val; } + int TrySend(const sockaddr_in& addr, ArrayRef bufs) { + return TrySend(reinterpret_cast(addr), bufs); + } + + int TrySend(const sockaddr_in6& addr, ArrayRef bufs) { + return TrySend(reinterpret_cast(addr), bufs); + } + /** * Prepare for receiving data. If the socket has not previously been bound * with Bind() it is bound to 0.0.0.0 (the "all interfaces" IPv4 address) and