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:
Peter Johnson
2018-07-22 12:31:50 -07:00
committed by GitHub
parent 85118a023d
commit 739267d36d
2 changed files with 38 additions and 0 deletions

View File

@@ -56,6 +56,18 @@ class Tcp final : public NetworkStreamImpl<Tcp, uv_tcp_t> {
return Create(*loop, flags);
}
/**
* Reuse this handle. This closes the handle, and after the close completes,
* reinitializes it (identically to Create) and calls the provided callback.
* Unlike Close(), it does NOT emit the closed signal, however, IsClosing()
* will return true until the callback is called. This does nothing if
* IsClosing() is true (e.g. if Close() was called).
*
* @param flags Flags
* @param callback Callback
*/
void Reuse(std::function<void()> callback, unsigned int flags = AF_UNSPEC);
/**
* Accept incoming connection.
*
@@ -304,6 +316,12 @@ class Tcp final : public NetworkStreamImpl<Tcp, uv_tcp_t> {
private:
Tcp* DoAccept() override;
struct ReuseData {
std::function<void()> callback;
unsigned int flags;
};
std::unique_ptr<ReuseData> m_reuseData;
};
/**