posix/socket: add wrappers for sendto and recvfrom

This commit is contained in:
Danny Robson 2018-07-27 18:29:43 +10:00
parent f0eb3f0775
commit d99b257268
2 changed files with 49 additions and 0 deletions

View File

@ -170,3 +170,46 @@ socket::connect (util::view<const char*> host, int port)
throw std::runtime_error ("unable to reconnect"); throw std::runtime_error ("unable to reconnect");
} }
///////////////////////////////////////////////////////////////////////////////
template <typename ByteT>
ssize_t
socket::sendto (util::view<const ByteT *> buffer, int flags, ::sockaddr_in const &dst)
{
return util::posix::error::try_value (
::sendto (
native (),
buffer.data (),
buffer.size (),
flags,
reinterpret_cast<sockaddr const*> (&dst),
sizeof (dst)
)
);
}
template ssize_t socket::sendto (util::view<const char*>, int, sockaddr_in const&);
template ssize_t socket::sendto (util::view<const unsigned char*>, int, sockaddr_in const&);
template ssize_t socket::sendto (util::view<const std::byte*>, int, sockaddr_in const&);
//-----------------------------------------------------------------------------
template <typename ByteT>
util::view<ByteT*>
socket::recvfrom (util::view<ByteT*> buffer, int flags)
{
auto res = util::posix::error::try_value (
::recvfrom (native (), buffer.data (), buffer.size (), flags, nullptr, nullptr)
);
return { buffer.begin (), res };
}
template util::view<char*> socket::recvfrom (util::view<char*> buffer, int flags);
template util::view<unsigned char*> socket::recvfrom (util::view<unsigned char*> buffer, int flags);
template util::view<std::byte*> socket::recvfrom (util::view<std::byte*> buffer, int flags);

View File

@ -54,6 +54,12 @@ namespace util::posix {
void connect (util::view<const char*> host, int port); void connect (util::view<const char*> host, int port);
void shutdown (); void shutdown ();
template <typename ByteT>
ssize_t sendto (util::view<ByteT const*> data, int flags, ::sockaddr_in const &dst);
template <typename ByteT>
util::view<ByteT*> recvfrom (util::view<ByteT*> buffer, int flags);
template <typename ValueT> template <typename ValueT>
void setoption (int _level, int _name, const ValueT &_value) void setoption (int _level, int _name, const ValueT &_value)
{ {