2 #ifndef UVCC_REQUEST_IO__HPP 3 #define UVCC_REQUEST_IO__HPP 5 #include "uvcc/utility.hpp" 6 #include "uvcc/request-base.hpp" 7 #include "uvcc/handle-io.hpp" 8 #include "uvcc/request-fs.hpp" 9 #include "uvcc/request-stream.hpp" 10 #include "uvcc/request-udp.hpp" 11 #include "uvcc/buffer.hpp" 34 friend class request::instance< output >;
39 fs::
write::uv_t uv_file_write_req;
40 write::uv_t uv_stream_write_req;
43 using on_request_t = std::function<
void(
output _request,
buffer _buffer) >;
53 union request_properties
55 fs::
write::properties file_write_properties;
56 write::properties stream_write_properties;
57 udp_send::properties udp_send_properties;
59 ~request_properties() {}
60 request_properties() { std::memset(
this, 0,
sizeof(*
this)); }
63 request_properties property_storage;
64 ::uv_req_t *uv_req =
nullptr;
69 if (uv_req)
switch (uv_req->type)
72 property_storage.stream_write_properties.~properties();
75 property_storage.udp_send_properties.~properties();
78 property_storage.file_write_properties.~properties();
93 explicit output(uv_t *_uv_req) :
request(
reinterpret_cast<
request::uv_t* >(_uv_req)) {}
100 uv_req = instance::create();
101 static_cast< ::uv_req_t* >(uv_req)->type = UV_REQ;
102 instance::from(uv_req)->properties().uv_req =
static_cast< ::uv_req_t* >(uv_req);
105 output(
const output&) =
default;
108 output(
output&&)
noexcept =
default;
112 on_request_t& on_request()
const noexcept {
return instance::from(uv_req)->request_cb_storage.value(); }
117 switch (
static_cast< ::uv_req_t* >(uv_req)->type)
127 ret.uv_status(UV_EBADF);
133 int64_t
offset()
const noexcept {
return instance::from(uv_req)->properties().offset; }
152 int run(
io &_io,
const buffer &_buf, int64_t _offset = -1,
void *_info =
nullptr)
159 static_cast<
write::uv_t* >(uv_req)->type = UV_WRITE;
160 instance::from(uv_req)->properties().offset = _offset;
163 static_cast<
udp_send::uv_t* >(uv_req)->type = UV_UDP_SEND;
164 instance::from(uv_req)->properties().offset = _offset;
166 reinterpret_cast< udp_send* >(
this)->run(
167 static_cast< udp& >(_io), _buf,
168 *
static_cast<
const udp::io_info* >(_info)->peer
171 uv_status(UV_EINVAL);
173 static_cast<
fs::uv_t* >(uv_req)->type = UV_FS;
174 static_cast< fs::uv_t* >(uv_req)->fs_type = UV_FS_WRITE;
175 instance::from(uv_req)->properties().offset = _offset;
178 return uv_status(UV_EBADF);
196 static_cast<
write::uv_t* >(uv_req)->type = UV_WRITE;
199 static_cast<
udp_send::uv_t* >(uv_req)->type = UV_UDP_SEND;
201 reinterpret_cast< udp_send* >(
this)->try_send(
202 static_cast< udp& >(_io), _buf,
203 *
static_cast<
const udp::io_info* >(_info)->peer
206 uv_status(UV_EINVAL);
208 static_cast<
fs::uv_t* >(uv_req)->type = UV_FS;
209 static_cast< fs::uv_t* >(uv_req)->fs_type = UV_FS_WRITE;
212 return uv_status(UV_EBADF);
217 explicit operator
const fs::
write&()
const noexcept {
return *
reinterpret_cast<
const fs::
write* >(
this); }
218 explicit operator
fs::
write&()
noexcept {
return *
reinterpret_cast<
fs::
write* >(
this); }
220 explicit operator
const write&()
const noexcept {
return *
reinterpret_cast<
const write* >(
this); }
221 explicit operator
write&()
noexcept {
return *
reinterpret_cast<
write* >(
this); }
223 explicit operator
const udp_send&()
const noexcept {
return *
reinterpret_cast<
const udp_send* >(
this); }
224 explicit operator
udp_send&()
noexcept {
return *
reinterpret_cast<
udp_send* >(
this); }
Namespace for all uvcc definitions.
operator bool() const noexcept
Equivalent to (base() != nullptr).
The base class for the libuv requests.
file handle() const noexcept
The file which this write request has been running on.
int run(stream &_stream, const buffer &_buf)
Run the request.
::uv_handle_type type() const noexcept
The tag indicating the libuv type of the handle.
udp handle() const noexcept
The UDP handle where this send request has been taking place.
The base calss for filesystem requests.
int try_write(stream &_stream, const buffer &_buf)
int try_output(io &_io, const buffer &_buf, int64_t _offset=-1, void *_info=nullptr)
Same as run(), but won’t queue an output request if it can’t be completed immediately.
Stream write request type.
int run(io &_io, const buffer &_buf, int64_t _offset=-1, void *_info=nullptr)
Run the request with interpreting arguments as additional parameters for actual write/send request pe...
int try_write(file &_file, const buffer &_buf, int64_t _offset)
Try to execute the request synchronously if it can be completed immediately...
The base class for handles representing I/O endpoints: a file, TCP/UDP socket, pipe, TTY.
int run(file &_file, const buffer &_buf, int64_t _offset)
Run the request. Write data to the _file from the buffers described by _buf object.
io handle() const noexcept
The I/O endpoint handle where this output request has been taking place.
int64_t offset() const noexcept
The offset value specified for run() call by which this output request has been set going...
Generic write/send request type for I/O endpoints (files, TCP/UDP sockets, pipes, TTYs)...
stream handle() const noexcept
The stream which this write request has been running on.