27 #include <system_error>
85 void close() noexcept;
95 internal_::socket::native_handle_t
native_handle()
const noexcept;
118 template<
typename Option>
119 auto option(
const Option& ) const noexcept {
120 return Option::get(*
this);
140 template<
typename Option,
typename... Args>
142 Option::set(*
this, std::forward<Args>(args)...);
193 template<typename Buffer = std::vector<uint8_t>>
194 Buffer&
read(
size_t bytes_count, Buffer&, std::error_code&,
endpoint* source =
nullptr) noexcept;
200 template<typename Buffer = std::vector<uint8_t>>
201 Buffer
read(
size_t bytes_count, std::error_code&,
endpoint* source =
nullptr) noexcept;
216 template<typename Buffer = std::vector<uint8_t>>
219 #ifdef __cpp_exceptions
236 template<
typename Buffer = std::vector<u
int8_t>>
237 Buffer&
read(
size_t bytes_count, Buffer&,
endpoint* src =
nullptr);
239 template<
typename Buffer = std::vector<u
int8_t>>
240 Buffer
read(
size_t bytes_count,
endpoint* src =
nullptr);
246 template<
typename Buffer = std::vector<u
int8_t>>
248 #endif // ifdef __cpp_exceptions
252 internal_::socket impl;
255 template<
typename Buffer>
257 static_assert(
sizeof(std::remove_pointer_t<decltype(output.data())>) ==
sizeof(uint8_t),
258 "socket::read can't be used with container with non-byte elements");
260 output.resize(bytes_count);
261 size_t bytes_received;
262 if (source ==
nullptr) {
263 bytes_received = impl.read(output.data(), bytes_count, ec);
265 bytes_received = impl.recvfrom(output.data(), bytes_count, *source, ec);
267 if (ec)
return output;
268 output.resize(bytes_received);
273 template<
typename Buffer>
276 return read(bytes_count, buffer, ec, source);
282 extern template std::vector<uint8_t>&
socket::read(
size_t, std::vector<uint8_t>&, std::error_code&,
286 template<
typename Buffer>
288 static_assert(
sizeof(std::remove_pointer_t<decltype(input.data())>) ==
sizeof(uint8_t),
289 "socket::write can't be used with container with non-byte elements");
291 if (dest.is_invalid()) {
292 return impl.write(input.data(), input.size(), ec);
294 return impl.sendto(input.data(), input.size(), dest, ec);
298 extern template size_t socket::write(
const std::vector<uint8_t>&, std::error_code&,
const endpoint&);
301 #ifdef __cpp_exceptions
302 template<
typename Buffer>
305 auto res = read<Buffer>(bytes_count, output, ec, source);
306 if (ec)
throw std::system_error(ec);
310 template<
typename Buffer>
313 return read(bytes_count, buffer, source);
316 extern template std::vector<uint8_t>&
socket::read(
size_t, std::vector<uint8_t>&, endpoint* source);
317 extern template std::string&
socket::read(
size_t, std::string&, endpoint* source);
319 extern template std::vector<uint8_t>
socket::read(
size_t, endpoint* source);
320 extern template std::string
socket::read(
size_t, endpoint* source);
322 template<
typename Buffer>
323 size_t socket::write(
const Buffer& input,
const endpoint& dest) {
325 size_t res = write<Buffer>(input, ec, dest);
326 if (ec)
throw std::system_error(ec);
330 extern template size_t socket::write(
const std::vector<uint8_t>&,
const endpoint&);
331 extern template size_t socket::write(
const std::string&,
const endpoint&);
332 #endif // ifdef __cpp_exceptions
size_t write(const Buffer &, std::error_code &, const endpoint &dest=endpoint::invalid) noexcept
Write contents of buffer to socket.
void set_option(const Option &, Args &&...args) noexcept
Set socket option value specified by type tag Option to value value.
void associate(endpoint target, std::error_code &ec) noexcept
Associate UDP socket with remote endpoint.
auto option(const Option &) const noexcept
Query socket option value specified by type tag Option.
internal_::socket & implementation() noexcept
void listen(endpoint target, std::error_code &ec) noexcept
Accept datagrams coming on specified endpoint.
socket(ip ipver) noexcept
Create new socket object.
static const endpoint invalid
internal_::socket::native_handle_t native_handle() const noexcept
Get native handle/descriptor for socket.
~socket()
Deallocate socket.
Buffer & read(size_t bytes_count, Buffer &, std::error_code &, endpoint *source=nullptr) noexcept
Read pending datagram into buffer.
This header defines set of enumerations for platform-independent error code handling.
Descriptor wrapper for UDP socket.
void disassociate() noexcept
Undo previous call to associate().
void close() noexcept
Deallocate socket.
socket & operator=(const socket &)=delete