added Stream::tryWrite
This commit is contained in:
parent
bc1e5919a6
commit
b2f992f5e4
@ -1,6 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
|
||||||
|
#include <iterator>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <memory>
|
||||||
|
#include <array>
|
||||||
#include <uv.h>
|
#include <uv.h>
|
||||||
#include "handle.hpp"
|
#include "handle.hpp"
|
||||||
#include "loop.hpp"
|
#include "loop.hpp"
|
||||||
@ -9,6 +13,37 @@
|
|||||||
namespace uvw {
|
namespace uvw {
|
||||||
|
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class Stream;
|
||||||
|
|
||||||
|
|
||||||
|
class Buffer final {
|
||||||
|
template<typename>
|
||||||
|
friend class Stream;
|
||||||
|
|
||||||
|
uv_buf_t uvBuf() const noexcept {
|
||||||
|
return uv_buf_init(data.get(), size);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
Buffer(std::unique_ptr<char[]> dt, std::size_t s)
|
||||||
|
: data{std::move(dt)}, size{s}
|
||||||
|
{ }
|
||||||
|
|
||||||
|
Buffer(Buffer &&) = default;
|
||||||
|
Buffer& operator=(Buffer &&) = default;
|
||||||
|
|
||||||
|
void reset(std::unique_ptr<char[]> dt, std::size_t s) noexcept {
|
||||||
|
data.swap(dt);
|
||||||
|
size = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unique_ptr<char[]> data;
|
||||||
|
std::size_t size;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class Stream: public Handle<T> {
|
class Stream: public Handle<T> {
|
||||||
static constexpr unsigned int DEFAULT_BACKLOG = 128;
|
static constexpr unsigned int DEFAULT_BACKLOG = 128;
|
||||||
@ -50,7 +85,20 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO write
|
// TODO write
|
||||||
// TODO tryWrite
|
|
||||||
|
UVWOptionalData<int> tryWrite(Buffer buf) noexcept {
|
||||||
|
Buffer data[] = { std::move(buf) };
|
||||||
|
return tryWrite(std::begin(data), std::end(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename It>
|
||||||
|
UVWOptionalData<int> tryWrite(It first, It last) noexcept {
|
||||||
|
uv_buf_t data[last - first];
|
||||||
|
std::size_t pos = 0;
|
||||||
|
while(first != last) { data[pos++] = (first++)->uvBuf(); }
|
||||||
|
auto bw = uv_try_write(this->template get<uv_stream_t>(), data, pos);
|
||||||
|
return (bw >= 0 ? bw : UVWError{bw});
|
||||||
|
}
|
||||||
|
|
||||||
bool readable() const noexcept {
|
bool readable() const noexcept {
|
||||||
return (uv_is_readable(this->template get<uv_stream_t>()) == 1);
|
return (uv_is_readable(this->template get<uv_stream_t>()) == 1);
|
||||||
|
|||||||
@ -62,7 +62,18 @@ void conn(uvw::Loop &loop) {
|
|||||||
auto cb = [](uvw::UVWError err, uvw::Tcp &tcp) mutable {
|
auto cb = [](uvw::UVWError err, uvw::Tcp &tcp) mutable {
|
||||||
std::cout << "connect: " << ((bool)err) << std::endl;
|
std::cout << "connect: " << ((bool)err) << std::endl;
|
||||||
|
|
||||||
auto cb = [](uvw::UVWError err, uvw::Tcp &tcp) mutable {
|
auto data = std::unique_ptr<char[]>(new char[1]);
|
||||||
|
data[0] = 42;
|
||||||
|
uvw::Buffer buf{std::move(data), 1};
|
||||||
|
uvw::UVWOptionalData<int> bw = tcp.tryWrite(std::move(buf));
|
||||||
|
|
||||||
|
if(bw) {
|
||||||
|
std::cout << "written: " << ((int)bw) << std::endl;
|
||||||
|
} else {
|
||||||
|
std::cout << "written err: " << ((int)bw) << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto cb = [](uvw::UVWError err, uvw::Tcp &) mutable {
|
||||||
std::cout << "close: " << ((bool)err) << std::endl;
|
std::cout << "close: " << ((bool)err) << std::endl;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user