Add ability to get the local address of an acceptor, and both the local
and remote addresses of a stream socket.
This commit is contained in:
parent
6b7b9f77fb
commit
91bf6544cf
@ -268,6 +268,41 @@ public:
|
||||
service_.get_option(impl_, option, error_handler);
|
||||
}
|
||||
|
||||
/// Get the local address of the acceptor.
|
||||
/**
|
||||
* This function is used to obtain the locally bound address of the acceptor.
|
||||
*
|
||||
* @param address An address object that receives the local address of the
|
||||
* acceptor.
|
||||
*
|
||||
* @throws socket_error Thrown on failure.
|
||||
*/
|
||||
template <typename Address>
|
||||
void get_local_address(Address& address)
|
||||
{
|
||||
service_.get_local_address(impl_, address, default_error_handler());
|
||||
}
|
||||
|
||||
/// Get the local address of the acceptor.
|
||||
/**
|
||||
* This function is used to obtain the locally bound address of the acceptor.
|
||||
*
|
||||
* @param address An address object that receives the local address of the
|
||||
* acceptor.
|
||||
*
|
||||
* @param error_handler The handler to be called when an error occurs. Copies
|
||||
* will be made of the handler as required. The equivalent function signature
|
||||
* of the handler must be:
|
||||
* @code void error_handler(
|
||||
* const asio::socket_error& error // Result of operation
|
||||
* ); @endcode
|
||||
*/
|
||||
template <typename Address, typename Error_Handler>
|
||||
void get_local_address(Address& address, Error_Handler error_handler)
|
||||
{
|
||||
service_.get_local_address(impl_, address, error_handler);
|
||||
}
|
||||
|
||||
/// Accept a new connection.
|
||||
/**
|
||||
* This function is used to accept a new connection from a peer into the
|
||||
|
@ -195,6 +195,76 @@ public:
|
||||
service_.get_option(impl_, option, error_handler);
|
||||
}
|
||||
|
||||
/// Get the local address of the socket.
|
||||
/**
|
||||
* This function is used to obtain the locally bound address of the socket.
|
||||
*
|
||||
* @param address An address object that receives the local address of the
|
||||
* socket.
|
||||
*
|
||||
* @throws socket_error Thrown on failure.
|
||||
*/
|
||||
template <typename Address>
|
||||
void get_local_address(Address& address)
|
||||
{
|
||||
service_.get_local_address(impl_, address, default_error_handler());
|
||||
}
|
||||
|
||||
/// Get the local address of the socket.
|
||||
/**
|
||||
* This function is used to obtain the locally bound address of the socket.
|
||||
*
|
||||
* @param address An address object that receives the local address of the
|
||||
* socket.
|
||||
*
|
||||
* @param error_handler The handler to be called when an error occurs. Copies
|
||||
* will be made of the handler as required. The equivalent function signature
|
||||
* of the handler must be:
|
||||
* @code void error_handler(
|
||||
* const asio::socket_error& error // Result of operation
|
||||
* ); @endcode
|
||||
*/
|
||||
template <typename Address, typename Error_Handler>
|
||||
void get_local_address(Address& address, Error_Handler error_handler)
|
||||
{
|
||||
service_.get_local_address(impl_, address, error_handler);
|
||||
}
|
||||
|
||||
/// Get the remote address of the socket.
|
||||
/**
|
||||
* This function is used to obtain the remote address of the socket.
|
||||
*
|
||||
* @param address An address object that receives the remote address of the
|
||||
* socket.
|
||||
*
|
||||
* @throws socket_error Thrown on failure.
|
||||
*/
|
||||
template <typename Address>
|
||||
void get_remote_address(Address& address)
|
||||
{
|
||||
service_.get_remote_address(impl_, address, default_error_handler());
|
||||
}
|
||||
|
||||
/// Get the remote address of the socket.
|
||||
/**
|
||||
* This function is used to obtain the remote address of the socket.
|
||||
*
|
||||
* @param address An address object that receives the remote address of the
|
||||
* socket.
|
||||
*
|
||||
* @param error_handler The handler to be called when an error occurs. Copies
|
||||
* will be made of the handler as required. The equivalent function signature
|
||||
* of the handler must be:
|
||||
* @code void error_handler(
|
||||
* const asio::socket_error& error // Result of operation
|
||||
* ); @endcode
|
||||
*/
|
||||
template <typename Address, typename Error_Handler>
|
||||
void get_remote_address(Address& address, Error_Handler error_handler)
|
||||
{
|
||||
service_.get_remote_address(impl_, address, error_handler);
|
||||
}
|
||||
|
||||
/// Send the given data to the peer.
|
||||
/**
|
||||
* This function is used to send data to the stream socket's peer. The
|
||||
|
@ -124,6 +124,17 @@ public:
|
||||
error_handler(socket_error(socket_ops::get_error()));
|
||||
}
|
||||
|
||||
// Get the local socket address.
|
||||
template <typename Address, typename Error_Handler>
|
||||
void get_local_address(impl_type& impl, Address& address,
|
||||
Error_Handler error_handler)
|
||||
{
|
||||
socket_addr_len_type addr_len = address.native_size();
|
||||
if (socket_ops::getsockname(impl, address.native_address(), &addr_len))
|
||||
error_handler(socket_error(socket_ops::get_error()));
|
||||
address.native_size(addr_len);
|
||||
}
|
||||
|
||||
// Accept a new connection.
|
||||
template <typename Stream_Socket_Service, typename Error_Handler>
|
||||
void accept(impl_type& impl,
|
||||
|
@ -92,6 +92,28 @@ public:
|
||||
error_handler(socket_error(socket_ops::get_error()));
|
||||
}
|
||||
|
||||
// Get the local socket address.
|
||||
template <typename Address, typename Error_Handler>
|
||||
void get_local_address(impl_type& impl, Address& address,
|
||||
Error_Handler error_handler)
|
||||
{
|
||||
socket_addr_len_type addr_len = address.native_size();
|
||||
if (socket_ops::getsockname(impl, address.native_address(), &addr_len))
|
||||
error_handler(socket_error(socket_ops::get_error()));
|
||||
address.native_size(addr_len);
|
||||
}
|
||||
|
||||
// Get the remote socket address.
|
||||
template <typename Address, typename Error_Handler>
|
||||
void get_remote_address(impl_type& impl, Address& address,
|
||||
Error_Handler error_handler)
|
||||
{
|
||||
socket_addr_len_type addr_len = address.native_size();
|
||||
if (socket_ops::getpeername(impl, address.native_address(), &addr_len))
|
||||
error_handler(socket_error(socket_ops::get_error()));
|
||||
address.native_size(addr_len);
|
||||
}
|
||||
|
||||
// Send the given data to the peer. Returns the number of bytes sent or
|
||||
// 0 if the connection was closed cleanly.
|
||||
template <typename Error_Handler>
|
||||
|
@ -173,6 +173,13 @@ inline int getsockopt(socket_type s, int level, int optname, void* optval,
|
||||
#endif // defined(_WIN32)
|
||||
}
|
||||
|
||||
inline int getpeername(socket_type s, socket_addr_type* addr,
|
||||
socket_addr_len_type* addrlen)
|
||||
{
|
||||
set_error(0);
|
||||
return error_wrapper(::getpeername(s, addr, addrlen));
|
||||
}
|
||||
|
||||
inline int getsockname(socket_type s, socket_addr_type* addr,
|
||||
socket_addr_len_type* addrlen)
|
||||
{
|
||||
|
@ -98,6 +98,28 @@ public:
|
||||
error_handler(socket_error(socket_ops::get_error()));
|
||||
}
|
||||
|
||||
// Get the local socket address.
|
||||
template <typename Address, typename Error_Handler>
|
||||
void get_local_address(impl_type& impl, Address& address,
|
||||
Error_Handler error_handler)
|
||||
{
|
||||
socket_addr_len_type addr_len = address.native_size();
|
||||
if (socket_ops::getsockname(impl, address.native_address(), &addr_len))
|
||||
error_handler(socket_error(socket_ops::get_error()));
|
||||
address.native_size(addr_len);
|
||||
}
|
||||
|
||||
// Get the remote socket address.
|
||||
template <typename Address, typename Error_Handler>
|
||||
void get_remote_address(impl_type& impl, Address& address,
|
||||
Error_Handler error_handler)
|
||||
{
|
||||
socket_addr_len_type addr_len = address.native_size();
|
||||
if (socket_ops::getpeername(impl, address.native_address(), &addr_len))
|
||||
error_handler(socket_error(socket_ops::get_error()));
|
||||
address.native_size(addr_len);
|
||||
}
|
||||
|
||||
// Send the given data to the peer. Returns the number of bytes sent or
|
||||
// 0 if the connection was closed cleanly.
|
||||
template <typename Error_Handler>
|
||||
|
Loading…
Reference in New Issue
Block a user