Tidy up socket options.

This commit is contained in:
chris_kohlhoff 2005-08-27 09:56:54 +00:00
parent 0f432816e7
commit 953a3fa68c
13 changed files with 282 additions and 161 deletions

View File

@ -79,6 +79,7 @@ nobase_include_HEADERS = \
asio/ipv4/host_resolver.hpp \
asio/ipv4/host_resolver_service.hpp \
asio/ipv4/multicast.hpp \
asio/ipv4/socket_option.hpp \
asio/ipv4/tcp.hpp \
asio/ipv4/udp.hpp \
asio/is_read_buffered.hpp \

View File

@ -44,6 +44,7 @@
#include "asio/ipv4/host_resolver.hpp"
#include "asio/ipv4/host_resolver_service.hpp"
#include "asio/ipv4/multicast.hpp"
#include "asio/ipv4/socket_option.hpp"
#include "asio/ipv4/tcp.hpp"
#include "asio/ipv4/udp.hpp"
#include "asio/is_read_buffered.hpp"

View File

@ -217,8 +217,8 @@ public:
*
* @throws asio::error Thrown on failure.
*/
template <typename Option>
void set_option(const Option& option)
template <typename Socket_Option>
void set_option(const Socket_Option& option)
{
service_.set_option(impl_, option, default_error_handler());
}
@ -236,8 +236,8 @@ public:
* const asio::error& error // Result of operation
* ); @endcode
*/
template <typename Option, typename Error_Handler>
void set_option(const Option& option, Error_Handler error_handler)
template <typename Socket_Option, typename Error_Handler>
void set_option(const Socket_Option& option, Error_Handler error_handler)
{
service_.set_option(impl_, option, error_handler);
}
@ -250,8 +250,8 @@ public:
*
* @throws asio::error Thrown on failure.
*/
template <typename Option>
void get_option(Option& option) const
template <typename Socket_Option>
void get_option(Socket_Option& option) const
{
service_.get_option(impl_, option, default_error_handler());
}
@ -269,8 +269,8 @@ public:
* const asio::error& error // Result of operation
* ); @endcode
*/
template <typename Option, typename Error_Handler>
void get_option(Option& option, Error_Handler error_handler) const
template <typename Socket_Option, typename Error_Handler>
void get_option(Socket_Option& option, Error_Handler error_handler) const
{
service_.get_option(impl_, option, error_handler);
}

View File

@ -149,8 +149,8 @@ public:
*
* @throws asio::error Thrown on failure.
*/
template <typename Option>
void set_option(const Option& option)
template <typename Socket_Option>
void set_option(const Socket_Option& option)
{
service_.set_option(impl_, option, default_error_handler());
}
@ -168,8 +168,8 @@ public:
* const asio::error& error // Result of operation
* ); @endcode
*/
template <typename Option, typename Error_Handler>
void set_option(const Option& option, Error_Handler error_handler)
template <typename Socket_Option, typename Error_Handler>
void set_option(const Socket_Option& option, Error_Handler error_handler)
{
service_.set_option(impl_, option, error_handler);
}
@ -182,8 +182,8 @@ public:
*
* @throws asio::error Thrown on failure.
*/
template <typename Option>
void get_option(Option& option) const
template <typename Socket_Option>
void get_option(Socket_Option& option) const
{
service_.get_option(impl_, option, default_error_handler());
}
@ -201,8 +201,8 @@ public:
* const asio::error& error // Result of operation
* ); @endcode
*/
template <typename Option, typename Error_Handler>
void get_option(Option& option, Error_Handler error_handler) const
template <typename Socket_Option, typename Error_Handler>
void get_option(Socket_Option& option, Error_Handler error_handler) const
{
service_.get_option(impl_, option, error_handler);
}

View File

@ -18,20 +18,21 @@
#include "asio/detail/push_options.hpp"
#include "asio/socket_option.hpp"
#include "asio/detail/socket_ops.hpp"
#include "asio/detail/socket_types.hpp"
#include "asio/ipv4/socket_option.hpp"
namespace asio {
namespace ipv4 {
namespace multicast {
namespace socket_option {
/// Helper template for implementing ip_mreq-based options.
template <int Level, int Name>
class ip_mreq_option
class request
{
public:
/// Default constructor.
ip_mreq_option()
request()
{
value_.imr_multiaddr.s_addr =
asio::detail::socket_ops::host_to_network_long(
@ -42,7 +43,7 @@ public:
}
/// Construct with multicast address only.
ip_mreq_option(const asio::ipv4::address& multicast_address)
request(const asio::ipv4::address& multicast_address)
{
value_.imr_multiaddr.s_addr =
asio::detail::socket_ops::host_to_network_long(
@ -53,7 +54,7 @@ public:
}
/// Construct with multicast address and address of local interface to use.
ip_mreq_option(const asio::ipv4::address& multicast_address,
request(const asio::ipv4::address& multicast_address,
const asio::ipv4::address& local_address)
{
value_.imr_multiaddr.s_addr =
@ -76,19 +77,19 @@ public:
return Name;
}
/// Get the address of the flag data.
/// Get the address of the option data.
void* data()
{
return &value_;
}
/// Get the address of the flag data.
/// Get the address of the option data.
const void* data() const
{
return &value_;
}
/// Get the size of the flag data.
/// Get the size of the option data.
size_t size() const
{
return sizeof(value_);
@ -98,88 +99,28 @@ private:
ip_mreq value_;
};
/// Helper template for implementing address-based options.
template <int Level, int Name>
class address_option
{
public:
/// Default constructor.
address_option()
{
value_.s_addr = asio::detail::socket_ops::host_to_network_long(
asio::ipv4::address::any().to_ulong());
}
} // namespace socket_option
/// Construct with address.
address_option(const asio::ipv4::address& value)
{
value_.s_addr =
asio::detail::socket_ops::host_to_network_long(value.to_ulong());
}
/// Socket option to join a multicast group on a specified interface.
typedef asio::ipv4::multicast::socket_option::request<
IPPROTO_IP, IP_ADD_MEMBERSHIP> add_membership;
/// Get the level of the socket option.
int level() const
{
return Level;
}
/// Socket option to leave a multicast group on a specified interface.
typedef asio::ipv4::multicast::socket_option::request<
IPPROTO_IP, IP_DROP_MEMBERSHIP> drop_membership;
/// Get the name of the socket option.
int name() const
{
return Name;
}
/// Socket option for local interface to use for outgoing multicast packets.
typedef asio::ipv4::socket_option::address<
IPPROTO_IP, IP_MULTICAST_IF> outbound_interface;
/// Set the value of the socket option.
void set(const asio::ipv4::address& value)
{
value_.s_addr =
asio::detail::socket_ops::host_to_network_long(value.to_ulong());
}
/// Socket option for time-to-live associated with outgoing multicast packets.
typedef asio::socket_option::integer<
IPPROTO_IP, IP_MULTICAST_TTL> time_to_live;
/// Get the current value of the socket option.
asio::ipv4::address get() const
{
return asio::ipv4::address(
asio::detail::socket_ops::network_to_host_long(value_.s_addr));
}
/// Get the address of the flag data.
void* data()
{
return &value_;
}
/// Get the address of the flag data.
const void* data() const
{
return &value_;
}
/// Get the size of the flag data.
size_t size() const
{
return sizeof(value_);
}
private:
in_addr value_;
};
/// Join a multicast group on a specified interface.
typedef ip_mreq_option<IPPROTO_IP, IP_ADD_MEMBERSHIP> add_membership;
/// Leave a multicast group on a specified interface.
typedef ip_mreq_option<IPPROTO_IP, IP_DROP_MEMBERSHIP> drop_membership;
/// Local interface to use for outgoing multicast packets.
typedef address_option<IPPROTO_IP, IP_MULTICAST_IF> outbound_interface;
/// Time-to-live associated with outgoing multicast packets.
typedef socket_option::integer<IPPROTO_IP, IP_MULTICAST_TTL> time_to_live;
/// Whether outgoing multicast packets will be received on the same socket if
/// it is a member of the multicast group.
typedef socket_option::flag<IPPROTO_IP, IP_MULTICAST_LOOP> enable_loopback;
/// Socket option determining whether outgoing multicast packets will be
/// received on the same socket if it is a member of the multicast group.
typedef asio::socket_option::boolean<
IPPROTO_IP, IP_MULTICAST_LOOP> enable_loopback;
} // namespace multicast
} // namespace ipv4

View File

@ -0,0 +1,101 @@
//
// socket_option.hpp
// ~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2005 Christopher M. Kohlhoff (chris@kohlhoff.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef ASIO_IPV4_SOCKET_OPTION_HPP
#define ASIO_IPV4_SOCKET_OPTION_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/push_options.hpp"
#include "asio/ipv4/address.hpp"
#include "asio/detail/socket_ops.hpp"
#include "asio/detail/socket_types.hpp"
namespace asio {
namespace ipv4 {
namespace socket_option {
/// Helper template for implementing address-based options.
template <int Level, int Name>
class address
{
public:
/// Default constructor.
address()
{
value_.s_addr = asio::detail::socket_ops::host_to_network_long(
asio::ipv4::address::any().to_ulong());
}
/// Construct with address.
address(const asio::ipv4::address& value)
{
value_.s_addr =
asio::detail::socket_ops::host_to_network_long(value.to_ulong());
}
/// Get the level of the socket option.
int level() const
{
return Level;
}
/// Get the name of the socket option.
int name() const
{
return Name;
}
/// Set the value of the socket option.
void set(const asio::ipv4::address& value)
{
value_.s_addr =
asio::detail::socket_ops::host_to_network_long(value.to_ulong());
}
/// Get the current value of the socket option.
asio::ipv4::address get() const
{
return asio::ipv4::address(
asio::detail::socket_ops::network_to_host_long(value_.s_addr));
}
/// Get the address of the flag data.
void* data()
{
return &value_;
}
/// Get the address of the flag data.
const void* data() const
{
return &value_;
}
/// Get the size of the flag data.
size_t size() const
{
return sizeof(value_);
}
private:
in_addr value_;
};
} // namespace socket_option
} // namespace ipv4
} // namespace asio
#include "asio/detail/pop_options.hpp"
#endif // ASIO_IPV4_SOCKET_OPTION_HPP

View File

@ -61,7 +61,7 @@ public:
}
/// Socket option for disabling the Nagle algorithm.
typedef socket_option::flag<IPPROTO_TCP, TCP_NODELAY> no_delay;
typedef asio::socket_option::boolean<IPPROTO_TCP, TCP_NODELAY> no_delay;
};
/// Describes an endpoint for a TCP socket.

View File

@ -17,6 +17,7 @@
#include "asio/detail/push_options.hpp"
#include "asio/socket_option.hpp"
#include "asio/detail/socket_types.hpp"
namespace asio {
@ -65,6 +66,47 @@ public:
#endif
};
/// Socket option to permit sending of broadcast messages.
typedef asio::socket_option::boolean<
SOL_SOCKET, SO_BROADCAST> broadcast;
/// Socket option to prevent routing, use local interfaces only.
typedef asio::socket_option::boolean<
SOL_SOCKET, SO_DONTROUTE> do_not_route;
/// Socket option to send keep-alives.
typedef asio::socket_option::boolean<
SOL_SOCKET, SO_KEEPALIVE> keep_alive;
/// Socket option for the send buffer size of a socket.
typedef asio::socket_option::integer<
SOL_SOCKET, SO_SNDBUF> send_buffer_size;
/// Socket option for the send low watermark.
typedef asio::socket_option::integer<
SOL_SOCKET, SO_SNDLOWAT> send_low_watermark;
/// Socket option for the send timeout.
typedef asio::socket_option::integer<
SOL_SOCKET, SO_SNDTIMEO> send_timeout;
/// Socket option for the receive buffer size of a socket.
typedef asio::socket_option::integer<
SOL_SOCKET, SO_RCVBUF> receive_buffer_size;
/// Socket option for the receive low watermark.
typedef asio::socket_option::integer<
SOL_SOCKET, SO_RCVLOWAT> receive_low_watermark;
/// Socket option for the receive timeout.
typedef asio::socket_option::integer<
SOL_SOCKET, SO_RCVTIMEO> receive_timeout;
/// Socket option to allow the socket to be bound to an address that is
/// already in use.
typedef asio::socket_option::boolean<
SOL_SOCKET, SO_REUSEADDR> reuse_address;
protected:
/// Protected destructor to prevent deletion through this type.
~socket_base()

View File

@ -22,20 +22,20 @@
namespace asio {
namespace socket_option {
/// Helper template for implementing flag-based options.
/// Helper template for implementing boolean-based options.
template <int Level, int Name>
class flag
class boolean
{
public:
/// Default constructor.
flag()
boolean()
: value_(0)
{
}
/// Construct to be either enabled or disabled.
flag(bool enabled)
: value_(enabled ? 1 : 0)
/// Construct with a specific option value.
boolean(bool value)
: value_(value ? 1 : 0)
{
}
@ -51,38 +51,38 @@ public:
return Name;
}
/// Set the value of the flag.
void set(bool enabled)
/// Set the value of the boolean.
void set(bool value)
{
value_ = enabled ? 1 : 0;
value_ = value ? 1 : 0;
}
/// Get the current value of the flag.
/// Get the current value of the boolean.
bool get() const
{
return value_;
}
/// Get the address of the flag data.
/// Get the address of the boolean data.
void* data()
{
return &value_;
}
/// Get the address of the flag data.
/// Get the address of the boolean data.
const void* data() const
{
return &value_;
}
/// Get the size of the flag data.
/// Get the size of the boolean data.
size_t size() const
{
return sizeof(value_);
}
private:
/// The underlying value of the flag.
/// The underlying value of the boolean option.
int value_;
};
@ -150,36 +150,6 @@ private:
int value_;
};
/// Permit sending of broadcast messages.
typedef flag<SOL_SOCKET, SO_BROADCAST> broadcast;
/// Prevent routing, use local interfaces only.
typedef flag<SOL_SOCKET, SO_DONTROUTE> dont_route;
/// Send keep-alives.
typedef flag<SOL_SOCKET, SO_KEEPALIVE> keep_alive;
/// The receive buffer size for a socket.
typedef integer<SOL_SOCKET, SO_SNDBUF> send_buffer_size;
/// Send low watermark.
typedef integer<SOL_SOCKET, SO_SNDLOWAT> send_low_watermark;
/// Send timeout.
typedef integer<SOL_SOCKET, SO_SNDTIMEO> send_timeout;
/// The send buffer size for a socket.
typedef integer<SOL_SOCKET, SO_RCVBUF> recv_buffer_size;
/// Receive low watermark.
typedef integer<SOL_SOCKET, SO_RCVLOWAT> recv_low_watermark;
/// Receive timeout.
typedef integer<SOL_SOCKET, SO_RCVTIMEO> recv_timeout;
/// Allow the socket to be bound to an address that is already in use.
typedef flag<SOL_SOCKET, SO_REUSEADDR> reuse_address;
} // namespace socket_option
} // namespace asio

View File

@ -8,23 +8,36 @@
\brief The asio namespace defines the classes associated with IP version 4.
*/
/**
\namespace asio::socket_option
\brief The asio::socket_option namespace defines the supported socket options.
*/
/**
\namespace asio::arg
\brief The asio::arg namespace defines argument placeholders for use with
\ref boost_bind.
*/
/**
\namespace asio::ipv4::multicast
\brief The asio::ipv4::multicast namespace defines the socket options used for
multicast.
*/
/**
\namespace asio::ipv4::multicast::socket_option
\brief The asio::ipv4::multicast::socket_option namespace defines socket
options helper templates.
*/
/**
\namespace asio::ipv4::socket_option
\brief The asio::ipv4::socket_option namespace defines socket options helper
templates.
*/
/**
\namespace asio::placeholders
\brief The asio::placeholders namespace defines argument placeholders for use
with \ref boost_bind.
*/
/**
\namespace asio::socket_option
\brief The asio::socket_option namespace defines socket options helper
templates.
*/
/**
\page boost_bind boost::bind
See the <a href="http://www.boost.org/libs/bind/bind.html">Boost: bind.hpp
@ -41,9 +54,11 @@ documentation</a> for more information on how to use <tt>boost::bind</tt>.
<H2>Namespaces</H2>
\li ::asio
\li asio::arg
\li asio::ipv4
\li asio::ipv4::multicast
\li asio::ipv4::multicast::socket_option
\li asio::ipv4::socket_option
\li asio::placeholders
\li asio::socket_option
<H2>Classes</H2>
@ -61,6 +76,7 @@ documentation</a> for more information on how to use <tt>boost::bind</tt>.
\li asio::ipv4::udp::endpoint
\li asio::locking_dispatcher
\li asio::socket_acceptor
\li asio::socket_base
\li asio::socket_connector
\li asio::stream_socket
\li asio::thread
@ -121,6 +137,7 @@ documentation</a> for more information on how to use <tt>boost::bind</tt>.
\li Dispatcher
\li Endpoint
\li Protocol
\li Socket_Option
\li Stream
\li Sync_Read_Stream
\li Sync_Write_Stream

View File

@ -0,0 +1,48 @@
//
// Socket_Option.hpp
// ~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2005 Christopher M. Kohlhoff (chris@kohlhoff.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
/// Socket_Option concept.
/**
* @par Implemented By:
* asio::socket_base::broadcast @n
* asio::socket_base::do_not_route @n
* asio::socket_base::keep_alive @n
* asio::socket_base::send_buffer_size @n
* asio::socket_base::send_low_watermark @n
* asio::socket_base::send_timeout @n
* asio::socket_base::receive_buffer_size @n
* asio::socket_base::receive_low_watermark @n
* asio::socket_base::receive_timeout @n
* asio::socket_base::reuse_address @n
* asio::ipv4::tcp::no_delay @n
* asio::ipv4::multicast::add_membership @n
* asio::ipv4::multicast::drop_membership @n
* asio::ipv4::multicast::outbound_interface @n
* asio::ipv4::multicast::time_to_live @n
* asio::ipv4::multicast::enable_loopback
*/
class Socket_Option
{
public:
/// Get the level of the socket option.
int level() const;
/// Get the name of the socket option.
int name() const;
/// Get the address of the socket option data.
void* data();
/// Get the address of the socket option data.
const void* data() const;
/// Get the size of the socket option data.
size_t size() const;
};

View File

@ -14,7 +14,7 @@ public:
{
// Create the socket so that multiple may be bound to the same address.
socket_.open(asio::ipv4::udp());
socket_.set_option(asio::socket_option::reuse_address(true));
socket_.set_option(asio::datagram_socket::reuse_address(true));
socket_.bind(asio::ipv4::udp::endpoint(multicast_port));
// Join the multicast group.

View File

@ -131,7 +131,7 @@ public:
block_size_(block_size)
{
acceptor_.open(ipv4::tcp());
acceptor_.set_option(socket_option::reuse_address(1));
acceptor_.set_option(stream_socket::reuse_address(1));
acceptor_.bind(endpoint);
acceptor_.listen();