git-svn-id: http://libopenmetaverse.googlecode.com/svn/trunk@18 52acb1d6-8a22-11de-b505-999d5b087335
227 lines
7.1 KiB
C++
227 lines
7.1 KiB
C++
//
|
|
// stream_socket_service.hpp
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
//
|
|
// Copyright (c) 2003-2005 Christopher M. Kohlhoff (chris at kohlhoff dot 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 BOOST_ASIO_STREAM_SOCKET_SERVICE_HPP
|
|
#define BOOST_ASIO_STREAM_SOCKET_SERVICE_HPP
|
|
|
|
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
|
# pragma once
|
|
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
|
|
|
#include <boost/asio/detail/push_options.hpp>
|
|
|
|
#include <boost/asio/detail/push_options.hpp>
|
|
#include <cstddef>
|
|
#include <memory>
|
|
#include <boost/config.hpp>
|
|
#include <boost/asio/detail/pop_options.hpp>
|
|
|
|
#include <boost/asio/basic_demuxer.hpp>
|
|
#include <boost/asio/demuxer_service.hpp>
|
|
#include <boost/asio/detail/epoll_reactor.hpp>
|
|
#include <boost/asio/detail/kqueue_reactor.hpp>
|
|
#include <boost/asio/detail/noncopyable.hpp>
|
|
#include <boost/asio/detail/select_reactor.hpp>
|
|
#include <boost/asio/detail/win_iocp_socket_service.hpp>
|
|
#include <boost/asio/detail/reactive_socket_service.hpp>
|
|
|
|
namespace boost {
|
|
namespace asio {
|
|
|
|
/// Default service implementation for a stream socket.
|
|
template <typename Allocator = std::allocator<void> >
|
|
class stream_socket_service
|
|
: private noncopyable
|
|
{
|
|
public:
|
|
/// The demuxer type.
|
|
typedef basic_demuxer<demuxer_service<Allocator> > demuxer_type;
|
|
|
|
private:
|
|
// The type of the platform-specific implementation.
|
|
#if defined(BOOST_ASIO_HAS_IOCP_DEMUXER)
|
|
typedef detail::win_iocp_socket_service<Allocator> service_impl_type;
|
|
#elif defined(BOOST_ASIO_HAS_EPOLL_REACTOR)
|
|
typedef detail::reactive_socket_service<
|
|
demuxer_type, detail::epoll_reactor<false> > service_impl_type;
|
|
#elif defined(BOOST_ASIO_HAS_KQUEUE_REACTOR)
|
|
typedef detail::reactive_socket_service<
|
|
demuxer_type, detail::kqueue_reactor<false> > service_impl_type;
|
|
#else
|
|
typedef detail::reactive_socket_service<
|
|
demuxer_type, detail::select_reactor<false> > service_impl_type;
|
|
#endif
|
|
|
|
public:
|
|
/// The type of a stream socket.
|
|
#if defined(GENERATING_DOCUMENTATION)
|
|
typedef implementation_defined impl_type;
|
|
#else
|
|
typedef typename service_impl_type::impl_type impl_type;
|
|
#endif
|
|
|
|
/// Construct a new stream socket service for the specified demuxer.
|
|
explicit stream_socket_service(demuxer_type& demuxer)
|
|
: service_impl_(demuxer.get_service(service_factory<service_impl_type>()))
|
|
{
|
|
}
|
|
|
|
/// Get the demuxer associated with the service.
|
|
demuxer_type& demuxer()
|
|
{
|
|
return service_impl_.demuxer();
|
|
}
|
|
|
|
/// Return a null stream socket implementation.
|
|
impl_type null() const
|
|
{
|
|
return service_impl_.null();
|
|
}
|
|
|
|
/// Open a new stream socket implementation.
|
|
template <typename Protocol, typename Error_Handler>
|
|
void open(impl_type& impl, const Protocol& protocol,
|
|
Error_Handler error_handler)
|
|
{
|
|
if (protocol.type() == SOCK_STREAM)
|
|
service_impl_.open(impl, protocol, error_handler);
|
|
else
|
|
error_handler(boost::asio::error(boost::asio::error::invalid_argument));
|
|
}
|
|
|
|
/// Assign a new stream socket implementation.
|
|
void assign(impl_type& impl, impl_type new_impl)
|
|
{
|
|
service_impl_.assign(impl, new_impl);
|
|
}
|
|
|
|
/// Close a stream socket implementation.
|
|
template <typename Error_Handler>
|
|
void close(impl_type& impl, Error_Handler error_handler)
|
|
{
|
|
service_impl_.close(impl, error_handler);
|
|
}
|
|
|
|
/// Bind the stream socket to the specified local endpoint.
|
|
template <typename Endpoint, typename Error_Handler>
|
|
void bind(impl_type& impl, const Endpoint& endpoint,
|
|
Error_Handler error_handler)
|
|
{
|
|
service_impl_.bind(impl, endpoint, error_handler);
|
|
}
|
|
|
|
/// Connect the stream socket to the specified endpoint.
|
|
template <typename Endpoint, typename Error_Handler>
|
|
void connect(impl_type& impl, const Endpoint& peer_endpoint,
|
|
Error_Handler error_handler)
|
|
{
|
|
service_impl_.connect(impl, peer_endpoint, error_handler);
|
|
}
|
|
|
|
/// Start an asynchronous connect.
|
|
template <typename Endpoint, typename Handler>
|
|
void async_connect(impl_type& impl, const Endpoint& peer_endpoint,
|
|
Handler handler)
|
|
{
|
|
service_impl_.async_connect(impl, peer_endpoint, handler);
|
|
}
|
|
|
|
/// Set a socket option.
|
|
template <typename Option, typename Error_Handler>
|
|
void set_option(impl_type& impl, const Option& option,
|
|
Error_Handler error_handler)
|
|
{
|
|
service_impl_.set_option(impl, option, error_handler);
|
|
}
|
|
|
|
/// Get a socket option.
|
|
template <typename Option, typename Error_Handler>
|
|
void get_option(const impl_type& impl, Option& option,
|
|
Error_Handler error_handler) const
|
|
{
|
|
service_impl_.get_option(impl, option, error_handler);
|
|
}
|
|
|
|
/// Perform an IO control command on the socket.
|
|
template <typename IO_Control_Command, typename Error_Handler>
|
|
void io_control(impl_type& impl, IO_Control_Command& command,
|
|
Error_Handler error_handler)
|
|
{
|
|
service_impl_.io_control(impl, command, error_handler);
|
|
}
|
|
|
|
/// Get the local endpoint.
|
|
template <typename Endpoint, typename Error_Handler>
|
|
void get_local_endpoint(const impl_type& impl, Endpoint& endpoint,
|
|
Error_Handler error_handler) const
|
|
{
|
|
service_impl_.get_local_endpoint(impl, endpoint, error_handler);
|
|
}
|
|
|
|
/// Get the remote endpoint.
|
|
template <typename Endpoint, typename Error_Handler>
|
|
void get_remote_endpoint(const impl_type& impl, Endpoint& endpoint,
|
|
Error_Handler error_handler) const
|
|
{
|
|
service_impl_.get_remote_endpoint(impl, endpoint, error_handler);
|
|
}
|
|
|
|
/// Disable sends or receives on the socket.
|
|
template <typename Error_Handler>
|
|
void shutdown(impl_type& impl, socket_base::shutdown_type what,
|
|
Error_Handler error_handler)
|
|
{
|
|
service_impl_.shutdown(impl, what, error_handler);
|
|
}
|
|
|
|
/// Send the given data to the peer.
|
|
template <typename Const_Buffers, typename Error_Handler>
|
|
std::size_t send(impl_type& impl, const Const_Buffers& buffers,
|
|
socket_base::message_flags flags, Error_Handler error_handler)
|
|
{
|
|
return service_impl_.send(impl, buffers, flags, error_handler);
|
|
}
|
|
|
|
/// Start an asynchronous send.
|
|
template <typename Const_Buffers, typename Handler>
|
|
void async_send(impl_type& impl, const Const_Buffers& buffers,
|
|
socket_base::message_flags flags, Handler handler)
|
|
{
|
|
service_impl_.async_send(impl, buffers, flags, handler);
|
|
}
|
|
|
|
/// Receive some data from the peer.
|
|
template <typename Mutable_Buffers, typename Error_Handler>
|
|
std::size_t receive(impl_type& impl, const Mutable_Buffers& buffers,
|
|
socket_base::message_flags flags, Error_Handler error_handler)
|
|
{
|
|
return service_impl_.receive(impl, buffers, flags, error_handler);
|
|
}
|
|
|
|
/// Start an asynchronous receive.
|
|
template <typename Mutable_Buffers, typename Handler>
|
|
void async_receive(impl_type& impl, const Mutable_Buffers& buffers,
|
|
socket_base::message_flags flags, Handler handler)
|
|
{
|
|
service_impl_.async_receive(impl, buffers, flags, handler);
|
|
}
|
|
|
|
private:
|
|
// The service that provides the platform-specific implementation.
|
|
service_impl_type& service_impl_;
|
|
};
|
|
|
|
} // namespace asio
|
|
} // namespace boost
|
|
|
|
#include <boost/asio/detail/pop_options.hpp>
|
|
|
|
#endif // BOOST_ASIO_STREAM_SOCKET_SERVICE_HPP
|