⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basic_socket.hpp

📁 这是国外的resip协议栈
💻 HPP
📖 第 1 页 / 共 2 页
字号:
//// basic_socket.hpp// ~~~~~~~~~~~~~~~~//// Copyright (c) 2003-2007 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 ASIO_BASIC_SOCKET_HPP#define ASIO_BASIC_SOCKET_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/basic_io_object.hpp"#include "asio/error.hpp"#include "asio/socket_base.hpp"#include "asio/detail/throw_error.hpp"namespace asio {/// Provides socket functionality./** * The basic_socket class template provides functionality that is common to both * stream-oriented and datagram-oriented sockets. * * @par Thread Safety * @e Distinct @e objects: Safe.@n * @e Shared @e objects: Unsafe. */template <typename Protocol, typename SocketService>class basic_socket  : public basic_io_object<SocketService>,    public socket_base{public:  /// The native representation of a socket.  typedef typename SocketService::native_type native_type;  /// The protocol type.  typedef Protocol protocol_type;  /// The endpoint type.  typedef typename Protocol::endpoint endpoint_type;  /// A basic_socket is always the lowest layer.  typedef basic_socket<Protocol, SocketService> lowest_layer_type;  /// Construct a basic_socket without opening it.  /**   * This constructor creates a socket without opening it.   *   * @param io_service The io_service object that the socket will use to   * dispatch handlers for any asynchronous operations performed on the socket.   */  explicit basic_socket(asio::io_service& io_service)    : basic_io_object<SocketService>(io_service)  {  }  /// Construct and open a basic_socket.  /**   * This constructor creates and opens a socket.   *   * @param io_service The io_service object that the socket will use to   * dispatch handlers for any asynchronous operations performed on the socket.   *   * @param protocol An object specifying protocol parameters to be used.   *   * @throws asio::system_error Thrown on failure.   */  basic_socket(asio::io_service& io_service,      const protocol_type& protocol)    : basic_io_object<SocketService>(io_service)  {    asio::error_code ec;    this->service.open(this->implementation, protocol, ec);    asio::detail::throw_error(ec);  }  /// Construct a basic_socket, opening it and binding it to the given local  /// endpoint.  /**   * This constructor creates a socket and automatically opens it bound to the   * specified endpoint on the local machine. The protocol used is the protocol   * associated with the given endpoint.   *   * @param io_service The io_service object that the socket will use to   * dispatch handlers for any asynchronous operations performed on the socket.   *   * @param endpoint An endpoint on the local machine to which the socket will   * be bound.   *   * @throws asio::system_error Thrown on failure.   */  basic_socket(asio::io_service& io_service,      const endpoint_type& endpoint)    : basic_io_object<SocketService>(io_service)  {    asio::error_code ec;    this->service.open(this->implementation, endpoint.protocol(), ec);    asio::detail::throw_error(ec);    this->service.bind(this->implementation, endpoint, ec);    asio::detail::throw_error(ec);  }  /// Construct a basic_socket on an existing native socket.  /**   * This constructor creates a socket object to hold an existing native socket.   *   * @param io_service The io_service object that the socket will use to   * dispatch handlers for any asynchronous operations performed on the socket.   *   * @param protocol An object specifying protocol parameters to be used.   *   * @param native_socket A native socket.   *   * @throws asio::system_error Thrown on failure.   */  basic_socket(asio::io_service& io_service,      const protocol_type& protocol, const native_type& native_socket)    : basic_io_object<SocketService>(io_service)  {    asio::error_code ec;    this->service.assign(this->implementation, protocol, native_socket, ec);    asio::detail::throw_error(ec);  }  /// Get a reference to the lowest layer.  /**   * This function returns a reference to the lowest layer in a stack of   * layers. Since a basic_socket cannot contain any further layers, it simply   * returns a reference to itself.   *   * @return A reference to the lowest layer in the stack of layers. Ownership   * is not transferred to the caller.   */  lowest_layer_type& lowest_layer()  {    return *this;  }  /// Open the socket using the specified protocol.  /**   * This function opens the socket so that it will use the specified protocol.   *   * @param protocol An object specifying protocol parameters to be used.   *   * @throws asio::system_error Thrown on failure.   *   * @par Example   * @code   * asio::ip::tcp::socket socket(io_service);   * socket.open(asio::ip::tcp::v4());   * @endcode   */  void open(const protocol_type& protocol = protocol_type())  {    asio::error_code ec;    this->service.open(this->implementation, protocol, ec);    asio::detail::throw_error(ec);  }  /// Open the socket using the specified protocol.  /**   * This function opens the socket so that it will use the specified protocol.   *   * @param protocol An object specifying which protocol is to be used.   *   * @param ec Set to indicate what error occurred, if any.   *   * @par Example   * @code   * asio::ip::tcp::socket socket(io_service);   * asio::error_code ec;   * socket.open(asio::ip::tcp::v4(), ec);   * if (ec)   * {   *   // An error occurred.   * }   * @endcode   */  asio::error_code open(const protocol_type& protocol,      asio::error_code& ec)  {    return this->service.open(this->implementation, protocol, ec);  }  /// Assign an existing native socket to the socket.  /*   * This function opens the socket to hold an existing native socket.   *   * @param protocol An object specifying which protocol is to be used.   *   * @param native_socket A native socket.   *   * @throws asio::system_error Thrown on failure.   */  void assign(const protocol_type& protocol, const native_type& native_socket)  {    asio::error_code ec;    this->service.assign(this->implementation, protocol, native_socket, ec);    asio::detail::throw_error(ec);  }  /// Assign an existing native socket to the socket.  /*   * This function opens the socket to hold an existing native socket.   *   * @param protocol An object specifying which protocol is to be used.   *   * @param native_socket A native socket.   *   * @param ec Set to indicate what error occurred, if any.   */  asio::error_code assign(const protocol_type& protocol,      const native_type& native_socket, asio::error_code& ec)  {    return this->service.assign(this->implementation,        protocol, native_socket, ec);  }  /// Determine whether the socket is open.  bool is_open() const  {    return this->service.is_open(this->implementation);  }  /// Close the socket.  /**   * This function is used to close the socket. Any asynchronous send, receive   * or connect operations will be cancelled immediately, and will complete   * with the asio::error::operation_aborted error.   *   * @throws asio::system_error Thrown on failure.   *   * @note For portable behaviour with respect to graceful closure of a   * connected socket, call shutdown() before closing the socket.   */  void close()  {    asio::error_code ec;    this->service.close(this->implementation, ec);    asio::detail::throw_error(ec);  }  /// Close the socket.  /**   * This function is used to close the socket. Any asynchronous send, receive   * or connect operations will be cancelled immediately, and will complete   * with the asio::error::operation_aborted error.   *   * @param ec Set to indicate what error occurred, if any.   *   * @par Example   * @code   * asio::ip::tcp::socket socket(io_service);   * ...   * asio::error_code ec;   * socket.close(ec);   * if (ec)   * {   *   // An error occurred.   * }   * @endcode   *   * @note For portable behaviour with respect to graceful closure of a   * connected socket, call shutdown() before closing the socket.   */  asio::error_code close(asio::error_code& ec)  {    return this->service.close(this->implementation, ec);  }  /// Get the native socket representation.  /**   * This function may be used to obtain the underlying representation of the   * socket. This is intended to allow access to native socket functionality   * that is not otherwise provided.   */  native_type native()  {    return this->service.native(this->implementation);  }  /// Cancel all asynchronous operations associated with the socket.  /**   * This function causes all outstanding asynchronous connect, send and receive   * operations to finish immediately, and the handlers for cancelled operations   * will be passed the asio::error::operation_aborted error.   *   * @throws asio::system_error Thrown on failure.   */  void cancel()  {    asio::error_code ec;    this->service.cancel(this->implementation, ec);    asio::detail::throw_error(ec);  }  /// Cancel all asynchronous operations associated with the socket.  /**   * This function causes all outstanding asynchronous connect, send and receive   * operations to finish immediately, and the handlers for cancelled operations   * will be passed the asio::error::operation_aborted error.   *   * @param ec Set to indicate what error occurred, if any.   */  asio::error_code cancel(asio::error_code& ec)  {    return this->service.cancel(this->implementation, ec);  }  /// Determine whether the socket is at the out-of-band data mark.  /**   * This function is used to check whether the socket input is currently   * positioned at the out-of-band data mark.   *   * @return A bool indicating whether the socket is at the out-of-band data   * mark.   *   * @throws asio::system_error Thrown on failure.   */  bool at_mark() const  {    asio::error_code ec;    bool b = this->service.at_mark(this->implementation, ec);    asio::detail::throw_error(ec);    return b;  }  /// Determine whether the socket is at the out-of-band data mark.  /**   * This function is used to check whether the socket input is currently   * positioned at the out-of-band data mark.   *   * @param ec Set to indicate what error occurred, if any.   *   * @return A bool indicating whether the socket is at the out-of-band data   * mark.   */  bool at_mark(asio::error_code& ec) const  {    return this->service.at_mark(this->implementation, ec);  }  /// Determine the number of bytes available for reading.  /**   * This function is used to determine the number of bytes that may be read   * without blocking.   *   * @return The number of bytes that may be read without blocking, or 0 if an   * error occurs.   *   * @throws asio::system_error Thrown on failure.   */  std::size_t available() const  {    asio::error_code ec;    std::size_t s = this->service.available(this->implementation, ec);    asio::detail::throw_error(ec);    return s;  }  /// Determine the number of bytes available for reading.  /**   * This function is used to determine the number of bytes that may be read   * without blocking.   *   * @param ec Set to indicate what error occurred, if any.   *   * @return The number of bytes that may be read without blocking, or 0 if an   * error occurs.   */  std::size_t available(asio::error_code& ec) const  {    return this->service.available(this->implementation, ec);  }  /// Bind the socket to the given local endpoint.  /**   * This function binds the socket to the specified endpoint on the local   * machine.   *   * @param endpoint An endpoint on the local machine to which the socket will   * be bound.   *   * @throws asio::system_error Thrown on failure.   *   * @par Example   * @code   * asio::ip::tcp::socket socket(io_service);   * socket.open(asio::ip::tcp::v4());   * socket.bind(asio::ip::tcp::endpoint(   *       asio::ip::tcp::v4(), 12345));   * @endcode   */  void bind(const endpoint_type& endpoint)  {    asio::error_code ec;    this->service.bind(this->implementation, endpoint, ec);    asio::detail::throw_error(ec);  }  /// Bind the socket to the given local endpoint.  /**   * This function binds the socket to the specified endpoint on the local   * machine.   *   * @param endpoint An endpoint on the local machine to which the socket will   * be bound.   *   * @param ec Set to indicate what error occurred, if any.   *   * @par Example   * @code   * asio::ip::tcp::socket socket(io_service);   * socket.open(asio::ip::tcp::v4());   * asio::error_code ec;   * socket.bind(asio::ip::tcp::endpoint(   *       asio::ip::tcp::v4(), 12345), ec);   * if (ec)   * {   *   // An error occurred.   * }   * @endcode   */  asio::error_code bind(const endpoint_type& endpoint,      asio::error_code& ec)  {    return this->service.bind(this->implementation, endpoint, ec);  }  /// Connect the socket to the specified endpoint.  /**   * This function is used to connect a socket to the specified remote endpoint.   * The function call will block until the connection is successfully made or   * an error occurs.   *   * The socket is automatically opened if it is not already open. If the   * connect fails, and the socket was automatically opened, the socket is   * returned to the closed state.   *   * @param peer_endpoint The remote endpoint to which the socket will be   * connected.   *   * @throws asio::system_error Thrown on failure.   *   * @par Example   * @code   * asio::ip::tcp::socket socket(io_service);   * asio::ip::tcp::endpoint endpoint(   *     asio::ip::address::from_string("1.2.3.4"), 12345);   * socket.connect(endpoint);   * @endcode   */  void connect(const endpoint_type& peer_endpoint)  {    asio::error_code ec;    if (!is_open())    {      this->service.open(this->implementation, peer_endpoint.protocol(), ec);      asio::detail::throw_error(ec);    }    this->service.connect(this->implementation, peer_endpoint, ec);    asio::detail::throw_error(ec);  }  /// Connect the socket to the specified endpoint.  /**   * This function is used to connect a socket to the specified remote endpoint.   * The function call will block until the connection is successfully made or   * an error occurs.   *   * The socket is automatically opened if it is not already open. If the   * connect fails, and the socket was automatically opened, the socket is   * returned to the closed state.   *   * @param peer_endpoint The remote endpoint to which the socket will be   * connected.   *   * @param ec Set to indicate what error occurred, if any.   *   * @par Example   * @code

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -