Commit c7862b5e authored by Dominik Charousset's avatar Dominik Charousset

Split check_socket_io_res into multiple functions

parent fc119f23
......@@ -62,5 +62,11 @@ variant<size_t, sec> write(pipe_socket x, const void* buf, size_t buf_size);
/// @relates pipe_socket
variant<size_t, sec> read(pipe_socket x, void* buf, size_t buf_size);
/// Converts the result from I/O operation on a ::pipe_socket to either an
/// error code or a non-zero positive integer.
/// @relates pipe_socket
variant<size_t, sec>
check_pipe_socket_io_res(std::make_signed<size_t>::type res);
} // namespace net
} // namespace caf
......@@ -68,10 +68,5 @@ error child_process_inherit(socket x, bool new_value);
/// @relates socket
error nonblocking(socket x, bool new_value);
/// Converts the result from `recv` or `send` to either an error code or a
/// non-zero positive integer.
/// @relates socket
variant<size_t, sec> check_socket_io_res(std::make_signed<size_t>::type res);
} // namespace net
} // namespace caf
......@@ -71,5 +71,11 @@ variant<size_t, sec> read(stream_socket x, void* buf, size_t buf_size);
/// @post either the result is a `sec` or a positive (non-zero) integer
variant<size_t, sec> write(stream_socket x, const void* buf, size_t buf_size);
/// Converts the result from I/O operation on a ::stream_socket to either an
/// error code or a non-zero positive integer.
/// @relates stream_socket
variant<size_t, sec>
check_stream_socket_io_res(std::make_signed<size_t>::type res);
} // namespace net
} // namespace caf
......@@ -82,15 +82,20 @@ expected<std::pair<pipe_socket, pipe_socket>> make_pipe() {
variant<size_t, sec> write(pipe_socket x, const void* buf, size_t buf_size) {
auto res = ::write(x.id, buf, buf_size);
return check_socket_io_res(res);
return check_pipe_socket_io_res(res);
}
variant<size_t, sec> read(pipe_socket x, void* buf, size_t buf_size) {
auto res = ::read(x.id, buf, buf_size);
return check_socket_io_res(res);
return check_pipe_socket_io_res(res);
}
#endif // CAF_WINDOWS
variant<size_t, sec>
check_pipe_socket_io_res(std::make_signed<size_t>::type res) {
return check_stream_socket_io_res(res);
}
} // namespace net
} // namespace caf
......@@ -180,18 +180,5 @@ error nonblocking(socket x, bool new_value) {
#endif // CAF_WINDOWS
variant<size_t, sec> check_socket_io_res(std::make_signed<size_t>::type res) {
if (res == 0)
return sec::socket_disconnected;
if (res < 0) {
auto code = last_socket_error();
if (code == std::errc::operation_would_block
|| code == std::errc::resource_unavailable_try_again)
return sec::unavailable_or_would_block;
return sec::socket_operation_failed;
}
return static_cast<size_t>(res);
}
} // namespace net
} // namespace caf
......@@ -164,13 +164,27 @@ error nodelay(stream_socket x, bool new_value) {
variant<size_t, sec> read(stream_socket x, void* buf, size_t buf_size) {
auto res = ::recv(x.id, reinterpret_cast<socket_recv_ptr>(buf), buf_size,
no_sigpipe_io_flag);
return check_socket_io_res(res);
return check_stream_socket_io_res(res);
}
variant<size_t, sec> write(stream_socket x, const void* buf, size_t buf_size) {
auto res = ::send(x.id, reinterpret_cast<socket_send_ptr>(buf), buf_size,
no_sigpipe_io_flag);
return check_socket_io_res(res);
return check_stream_socket_io_res(res);
}
variant<size_t, sec>
check_stream_socket_io_res(std::make_signed<size_t>::type res) {
if (res == 0)
return sec::socket_disconnected;
if (res < 0) {
auto code = last_socket_error();
if (code == std::errc::operation_would_block
|| code == std::errc::resource_unavailable_try_again)
return sec::unavailable_or_would_block;
return sec::socket_operation_failed;
}
return static_cast<size_t>(res);
}
} // namespace net
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment