From 5876a2b11dd7f0b3b9e2a5ca8991f11bc8ffb88e Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Wed, 28 Jul 2021 11:29:10 +0200 Subject: [PATCH] Add support for uv_try_write2 --- src/uvw/stream.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/uvw/stream.h b/src/uvw/stream.h index 88d6dc11..c83704c0 100644 --- a/src/uvw/stream.h +++ b/src/uvw/stream.h @@ -372,6 +372,30 @@ public: return bw; } + /** + * @brief Queues a write request if it can be completed immediately. + * + * Same as `tryWrite` for sending handles over a pipe.
+ * An ErrorEvent event will be emitted in case of errors. + * + * @param data The data to be written to the stream. + * @param len The lenght of the submitted data. + * @param send A valid handle suitable for the purpose. + * @return Number of bytes written. + */ + template + int tryWrite(std::unique_ptr data, unsigned int len, StreamHandle &send) { + uv_buf_t bufs[] = { uv_buf_init(data.get(), len) }; + auto bw = uv_try_write2(this->template get(), bufs, 1, send.raw()); + + if(bw < 0) { + this->publish(ErrorEvent{bw}); + bw = 0; + } + + return bw; + } + /** * @brief Queues a write request if it can be completed immediately. * @@ -395,6 +419,30 @@ public: return bw; } + /** + * @brief Queues a write request if it can be completed immediately. + * + * Same as `tryWrite` for sending handles over a pipe.
+ * An ErrorEvent event will be emitted in case of errors. + * + * @param data The data to be written to the stream. + * @param len The lenght of the submitted data. + * @param send A valid handle suitable for the purpose. + * @return Number of bytes written. + */ + template + int tryWrite(char *data, unsigned int len, StreamHandle &send) { + uv_buf_t bufs[] = { uv_buf_init(data, len) }; + auto bw = uv_try_write2(this->template get(), bufs, 1, send.raw()); + + if(bw < 0) { + this->publish(ErrorEvent{bw}); + bw = 0; + } + + return bw; + } + /** * @brief Checks if the stream is readable. * @return True if the stream is readable, false otherwise.