mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-29 02:21:44 +00:00
Update uv Udp wrapper for latest features
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
@@ -69,6 +69,33 @@ void Udp::Bind6(const Twine& ip, unsigned int port, unsigned int flags) {
|
||||
Bind(reinterpret_cast<const sockaddr&>(addr), flags);
|
||||
}
|
||||
|
||||
void Udp::Connect(const Twine& ip, unsigned int port) {
|
||||
sockaddr_in addr;
|
||||
int err = NameToAddr(ip, port, &addr);
|
||||
if (err < 0)
|
||||
ReportError(err);
|
||||
else
|
||||
Connect(reinterpret_cast<const sockaddr&>(addr));
|
||||
}
|
||||
|
||||
void Udp::Connect6(const Twine& ip, unsigned int port) {
|
||||
sockaddr_in6 addr;
|
||||
int err = NameToAddr(ip, port, &addr);
|
||||
if (err < 0)
|
||||
ReportError(err);
|
||||
else
|
||||
Connect(reinterpret_cast<const sockaddr&>(addr));
|
||||
}
|
||||
|
||||
sockaddr_storage Udp::GetPeer() {
|
||||
sockaddr_storage name;
|
||||
int len = sizeof(name);
|
||||
if (!Invoke(&uv_udp_getpeername, GetRaw(), reinterpret_cast<sockaddr*>(&name),
|
||||
&len))
|
||||
std::memset(&name, 0, sizeof(name));
|
||||
return name;
|
||||
}
|
||||
|
||||
sockaddr_storage Udp::GetSock() {
|
||||
sockaddr_storage name;
|
||||
int len = sizeof(name);
|
||||
@@ -111,6 +138,22 @@ void Udp::Send(const sockaddr& addr, ArrayRef<Buffer> bufs,
|
||||
Send(addr, bufs, std::make_shared<CallbackUdpSendReq>(bufs, callback));
|
||||
}
|
||||
|
||||
void Udp::Send(ArrayRef<Buffer> bufs, const std::shared_ptr<UdpSendReq>& req) {
|
||||
if (Invoke(&uv_udp_send, req->GetRaw(), GetRaw(), bufs.data(), bufs.size(),
|
||||
nullptr, [](uv_udp_send_t* r, int status) {
|
||||
auto& h = *static_cast<UdpSendReq*>(r->data);
|
||||
if (status < 0) h.ReportError(status);
|
||||
h.complete(Error(status));
|
||||
h.Release(); // this is always a one-shot
|
||||
}))
|
||||
req->Keep();
|
||||
}
|
||||
|
||||
void Udp::Send(ArrayRef<Buffer> bufs,
|
||||
std::function<void(MutableArrayRef<Buffer>, Error)> callback) {
|
||||
Send(bufs, std::make_shared<CallbackUdpSendReq>(bufs, callback));
|
||||
}
|
||||
|
||||
void Udp::StartRecv() {
|
||||
Invoke(&uv_udp_recv_start, GetRaw(), &AllocBuf,
|
||||
[](uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf,
|
||||
|
||||
@@ -115,6 +115,49 @@ class Udp final : public HandleImpl<Udp, uv_udp_t> {
|
||||
*/
|
||||
void Bind6(const Twine& ip, unsigned int port, unsigned int flags = 0);
|
||||
|
||||
/**
|
||||
* Associate the handle to a remote address and port, so every message sent
|
||||
* by this handle is automatically sent to that destination.
|
||||
*
|
||||
* @param addr Initialized `sockaddr_in` or `sockaddr_in6` data structure.
|
||||
*/
|
||||
void Connect(const sockaddr& addr) {
|
||||
Invoke(&uv_udp_connect, GetRaw(), &addr);
|
||||
}
|
||||
|
||||
void Connect(const sockaddr_in& addr) {
|
||||
Connect(reinterpret_cast<const sockaddr&>(addr));
|
||||
}
|
||||
|
||||
void Connect(const sockaddr_in6& addr) {
|
||||
Connect(reinterpret_cast<const sockaddr&>(addr));
|
||||
}
|
||||
|
||||
/**
|
||||
* Associate the handle to an IPv4 address and port, so every message sent
|
||||
* by this handle is automatically sent to that destination.
|
||||
*
|
||||
* @param ip The address to which to bind.
|
||||
* @param port The port to which to bind.
|
||||
*/
|
||||
void Connect(const Twine& ip, unsigned int port);
|
||||
|
||||
/**
|
||||
* Associate the handle to an IPv6 address and port, so every message sent
|
||||
* by this handle is automatically sent to that destination.
|
||||
*
|
||||
* @param ip The address to which to bind.
|
||||
* @param port The port to which to bind.
|
||||
* @param flags Optional additional flags.
|
||||
*/
|
||||
void Connect6(const Twine& ip, unsigned int port);
|
||||
|
||||
/**
|
||||
* Get the remote IP and port on connected UDP handles.
|
||||
* @return The address (will be zeroed if an error occurred).
|
||||
*/
|
||||
sockaddr_storage GetPeer();
|
||||
|
||||
/**
|
||||
* Get the current address to which the handle is bound.
|
||||
* @return The address (will be zeroed if an error occurred).
|
||||
@@ -204,6 +247,15 @@ class Udp final : public HandleImpl<Udp, uv_udp_t> {
|
||||
Send(reinterpret_cast<const sockaddr&>(addr), bufs, req);
|
||||
}
|
||||
|
||||
/**
|
||||
* Variant of Send() for connected sockets. Cannot be used with
|
||||
* connectionless sockets.
|
||||
*
|
||||
* @param bufs The buffers to be written to the stream.
|
||||
* @param req write request
|
||||
*/
|
||||
void Send(ArrayRef<Buffer> bufs, const std::shared_ptr<UdpSendReq>& 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)
|
||||
@@ -234,6 +286,16 @@ class Udp final : public HandleImpl<Udp, uv_udp_t> {
|
||||
Send(reinterpret_cast<const sockaddr&>(addr), bufs, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Variant of Send() for connected sockets. Cannot be used with
|
||||
* connectionless sockets.
|
||||
*
|
||||
* @param bufs The buffers to be written to the stream.
|
||||
* @param callback Callback function to call when the data has been sent.
|
||||
*/
|
||||
void Send(ArrayRef<Buffer> bufs,
|
||||
std::function<void(MutableArrayRef<Buffer>, Error)> callback);
|
||||
|
||||
/**
|
||||
* Same as Send(), but won't queue a send request if it can't be completed
|
||||
* immediately.
|
||||
@@ -261,6 +323,23 @@ class Udp final : public HandleImpl<Udp, uv_udp_t> {
|
||||
return TrySend(reinterpret_cast<const sockaddr&>(addr), bufs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Variant of TrySend() for connected sockets. Cannot be used with
|
||||
* connectionless sockets.
|
||||
*
|
||||
* @param bufs The buffers to be written to the stream.
|
||||
* @return Number of bytes sent.
|
||||
*/
|
||||
int TrySend(ArrayRef<Buffer> bufs) {
|
||||
int val = uv_udp_try_send(GetRaw(), bufs.data(),
|
||||
static_cast<unsigned>(bufs.size()), nullptr);
|
||||
if (val < 0) {
|
||||
this->ReportError(val);
|
||||
return 0;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
Reference in New Issue
Block a user