mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
Add Reuse function to uv::Tcp (#1208)
This allows reusing the Tcp object in cases when the connection errors out.
This commit is contained in:
@@ -25,6 +25,26 @@ std::shared_ptr<Tcp> Tcp::Create(Loop& loop, unsigned int flags) {
|
||||
return h;
|
||||
}
|
||||
|
||||
void Tcp::Reuse(std::function<void()> callback, unsigned int flags) {
|
||||
if (!IsClosing()) {
|
||||
m_reuseData = std::make_unique<ReuseData>();
|
||||
m_reuseData->callback = callback;
|
||||
m_reuseData->flags = flags;
|
||||
uv_close(GetRawHandle(), [](uv_handle_t* handle) {
|
||||
Tcp& h = *static_cast<Tcp*>(handle->data);
|
||||
if (!h.m_reuseData) return; // just in case
|
||||
auto data = std::move(h.m_reuseData);
|
||||
int err =
|
||||
uv_tcp_init_ex(h.GetLoopRef().GetRaw(), h.GetRaw(), data->flags);
|
||||
if (err < 0) {
|
||||
h.ReportError(err);
|
||||
return;
|
||||
}
|
||||
data->callback();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<Tcp> Tcp::Accept() {
|
||||
auto client = Create(GetLoopRef());
|
||||
if (!client) return nullptr;
|
||||
|
||||
Reference in New Issue
Block a user