| 77 | |
| 78 | template <typename ReadCallback, typename FinishCallback> |
| 79 | std::unique_ptr<cmUVStreamReadHandle> cmUVStreamRead(uv_stream_t* stream, |
| 80 | ReadCallback onRead, |
| 81 | FinishCallback onFinish) |
| 82 | { |
| 83 | auto handle = cm::make_unique<cmUVStreamReadHandle>(); |
| 84 | handle->OnRead = std::move(onRead); |
| 85 | handle->OnFinish = std::move(onFinish); |
| 86 | |
| 87 | stream->data = handle.get(); |
| 88 | uv_read_start( |
| 89 | stream, |
| 90 | [](uv_handle_t* s, std::size_t suggestedSize, uv_buf_t* buffer) { |
| 91 | auto* data = static_cast<cmUVStreamReadHandle*>(s->data); |
| 92 | data->Buffer.resize(suggestedSize); |
| 93 | buffer->base = data->Buffer.data(); |
| 94 | buffer->len = suggestedSize; |
| 95 | }, |
| 96 | [](uv_stream_t* s, ssize_t nread, uv_buf_t const* buffer) { |
| 97 | auto* data = static_cast<cmUVStreamReadHandle*>(s->data); |
| 98 | if (nread > 0) { |
| 99 | (void)buffer; |
| 100 | assert(buffer->base == data->Buffer.data()); |
| 101 | data->Buffer.resize(nread); |
| 102 | data->OnRead(std::move(data->Buffer)); |
| 103 | } else if (nread < 0 /*|| nread == UV_EOF*/) { |
| 104 | data->OnFinish(); |
| 105 | uv_read_stop(s); |
| 106 | } |
| 107 | }); |
| 108 | |
| 109 | return handle; |
| 110 | } |
searching dependent graphs…