2 #ifndef UVCC_REQUEST_STREAM__HPP 3 #define UVCC_REQUEST_STREAM__HPP 5 #include "uvcc/utility.hpp" 6 #include "uvcc/request-base.hpp" 7 #include "uvcc/handle-stream.hpp" 8 #include "uvcc/buffer.hpp" 13 #include <type_traits> 28 friend class request::instance< connect >;
32 using uv_t = ::uv_connect_t;
33 using on_request_t = std::function<
void(
connect _request) >;
42 explicit connect(uv_t *_uv_req) :
request(
reinterpret_cast<
request::uv_t* >(_uv_req)) {}
49 uv_req = instance::create();
50 static_cast< uv_t* >(uv_req)->type = UV_CONNECT;
53 connect(
const connect&) =
default;
56 connect(
connect&&)
noexcept =
default;
60 template<
typename =
void >
static void connect_cb(::uv_connect_t*,
int);
63 on_request_t& on_request()
const noexcept {
return instance::from(uv_req)->request_cb_storage.value(); }
74 int run(
tcp &_tcp,
const _T_ &_sockaddr)
76 tcp::instance::from(_tcp.uv_handle)->ref();
77 instance::from(uv_req)->ref();
80 auto uv_ret = ::uv_tcp_connect(
81 static_cast< uv_t* >(uv_req),
static_cast< tcp::uv_t* >(_tcp),
82 reinterpret_cast<
const ::sockaddr* >(&_sockaddr),
88 tcp::instance::from(_tcp.uv_handle)->unref();
89 instance::from(uv_req)->unref();
98 pipe::instance::from(_pipe.uv_handle)->ref();
99 instance::from(uv_req)->ref();
100 ::uv_pipe_connect(
static_cast< uv_t* >(uv_req),
static_cast< pipe::uv_t* >(_pipe), _name, connect_cb);
104 explicit operator
const uv_t*()
const noexcept {
return static_cast<
const uv_t* >(uv_req); }
105 explicit operator uv_t*()
noexcept {
return static_cast< uv_t* >(uv_req); }
109 void connect::connect_cb(::uv_connect_t *_uv_req,
int _status)
111 auto instance_ptr = instance::from(_uv_req);
112 instance_ptr->uv_error = _status;
114 ref_guard<
stream::instance > unref_handle(*stream::instance::from(_uv_req->handle), adopt_ref);
115 ref_guard< instance > unref_req(*instance_ptr, adopt_ref);
117 auto &connect_cb = instance_ptr->request_cb_storage.value();
118 if (connect_cb) connect_cb(connect(_uv_req));
129 friend class request::instance< write >;
134 using uv_t = ::uv_write_t;
135 using on_request_t = std::function<
void(
write _request,
buffer _buffer) >;
143 struct properties :
request::properties
145 buffer::uv_t *uv_buf =
nullptr;
155 explicit write(uv_t *_uv_req) :
request(
reinterpret_cast<
request::uv_t* >(_uv_req)) {}
162 uv_req = instance::create();
163 static_cast< uv_t* >(uv_req)->type = UV_WRITE;
166 write(
const write&) =
default;
169 write(
write&&)
noexcept =
default;
170 write& operator =(
write&&)
noexcept =
default;
173 template<
typename =
void >
static void write_cb(::uv_write_t*,
int);
174 template<
typename =
void >
static void write2_cb(::uv_write_t*,
int);
177 on_request_t& on_request()
const noexcept {
return instance::from(uv_req)->request_cb_storage.value(); }
188 auto instance_ptr = instance::from(uv_req);
190 stream::instance::from(_stream.uv_handle)->ref();
191 buffer::instance::from(_buf.uv_buf)->ref();
196 auto &properties = instance_ptr->properties();
197 properties.uv_buf = _buf.uv_buf;
201 auto uv_ret = ::uv_write(
202 static_cast< uv_t* >(uv_req),
static_cast< stream::uv_t* >(_stream),
203 static_cast<
const buffer::uv_t* >(_buf), _buf.count(),
209 stream::instance::from(_stream.uv_handle)->unref();
210 buffer::instance::from(_buf.uv_buf)->unref();
211 instance_ptr->unref();
220 auto instance_ptr = instance::from(uv_req);
222 pipe::instance::from(_pipe.uv_handle)->ref();
223 buffer::instance::from(_buf.uv_buf)->ref();
224 stream::instance::from(_send_handle.uv_handle)->ref();
229 auto &properties = instance_ptr->properties();
230 properties.uv_buf = _buf.uv_buf;
234 auto uv_ret = ::uv_write2(
235 static_cast< uv_t* >(uv_req),
static_cast< stream::uv_t* >(_pipe),
236 static_cast<
const buffer::uv_t* >(_buf), _buf.count(),
237 static_cast< stream::uv_t* >(_send_handle),
243 pipe::instance::from(_pipe.uv_handle)->unref();
244 buffer::instance::from(_buf.uv_buf)->unref();
245 stream::instance::from(_send_handle.uv_handle)->unref();
246 instance_ptr->unref();
258 ::uv_try_write(
static_cast< stream::uv_t* >(_stream),
static_cast<
const buffer::uv_t* >(_buf), _buf.count())
263 explicit operator
const uv_t*()
const noexcept {
return static_cast<
const uv_t* >(uv_req); }
264 explicit operator uv_t*()
noexcept {
return static_cast< uv_t* >(uv_req); }
268 void write::write_cb(::uv_write_t *_uv_req,
int _status)
270 auto instance_ptr = instance::from(_uv_req);
271 instance_ptr->uv_error = _status;
273 ref_guard<
stream::instance > unref_handle(*stream::instance::from(_uv_req->handle), adopt_ref);
274 ref_guard< instance > unref_req(*instance_ptr, adopt_ref);
276 auto &write_cb = instance_ptr->request_cb_storage.value();
278 write_cb(write(_uv_req), buffer(instance_ptr->properties().uv_buf, adopt_ref));
280 buffer::instance::from(instance_ptr->properties().uv_buf)->unref();
283 void write::write2_cb(::uv_write_t *_uv_req,
int _status)
285 ref_guard<
stream::instance > unref_send_handle(*stream::instance::from(_uv_req->send_handle), adopt_ref);
286 write_cb(_uv_req, _status);
297 friend class request::instance< shutdown >;
301 using uv_t = ::uv_shutdown_t;
302 using on_request_t = std::function<
void(
shutdown _request) >;
311 explicit shutdown(uv_t *_uv_req) :
request(
reinterpret_cast<
request::uv_t* >(_uv_req)) {}
315 ~shutdown() =
default;
318 uv_req = instance::create();
319 static_cast< uv_t* >(uv_req)->type = UV_SHUTDOWN;
322 shutdown(
const shutdown&) =
default;
325 shutdown(
shutdown&&)
noexcept =
default;
329 template<
typename =
void >
static void shutdown_cb(::uv_shutdown_t*,
int);
332 on_request_t& on_request()
const noexcept {
return instance::from(uv_req)->request_cb_storage.value(); }
335 stream handle()
const noexcept {
return stream(
static_cast< uv_t* >(uv_req)->handle); }
340 stream::instance::from(_stream.uv_handle)->ref();
341 instance::from(uv_req)->ref();
344 auto uv_ret = ::uv_shutdown(
static_cast< uv_t* >(uv_req),
static_cast< stream::uv_t* >(_stream), shutdown_cb);
348 stream::instance::from(_stream.uv_handle)->unref();
349 instance::from(uv_req)->unref();
356 explicit operator
const uv_t*()
const noexcept {
return static_cast<
const uv_t* >(uv_req); }
357 explicit operator uv_t*()
noexcept {
return static_cast< uv_t* >(uv_req); }
361 void shutdown::shutdown_cb(::uv_shutdown_t *_uv_req,
int _status)
363 auto instance_ptr = instance::from(_uv_req);
364 instance_ptr->uv_error = _status;
366 ref_guard<
stream::instance > unref_handle(*stream::instance::from(_uv_req->handle), adopt_ref);
367 ref_guard< instance > unref_req(*instance_ptr, adopt_ref);
369 auto &shutdown_cb = instance_ptr->request_cb_storage.value();
370 if (shutdown_cb) shutdown_cb(shutdown(_uv_req));
Namespace for all uvcc definitions.
operator bool() const noexcept
Equivalent to (base() != nullptr).
The base class for the libuv requests.
Stream connect request type.
int run(stream &_stream, const buffer &_buf)
Run the request.
int run(stream &_stream)
Run the request.
int run(pipe &_pipe, const buffer &_buf, stream &_send_handle)
The overload for sending handles over a pipe.
Stream shutdown request type.
int try_write(stream &_stream, const buffer &_buf)
void run(pipe &_pipe, const char *_name)
Run the request for uv::pipe stream.
int run(tcp &_tcp, const _T_ &_sockaddr)
Run the request for uv::tcp stream.
stream handle() const noexcept
The stream which this shutdown request has been running on.
Stream write request type.
A scoped reference counting guard.
stream send_handle() const noexcept
The handle of the stream to be sent over a pipe using this write request.
stream handle() const noexcept
The stream which this connect request has been running on.
stream handle() const noexcept
The stream which this write request has been running on.