Пример #1
0
 void listen(endpoint_type const & endpoint, boost::system::error_code & ec, bool reuse_addr = true)
 {
     if (endpoint.protocol() == protocol_type::v4())
         v4_type::listen(endpoint, ec, reuse_addr);
     if (endpoint.protocol() == protocol_type::v6())
         v6_type::listen(endpoint, ec, reuse_addr);
 }
Пример #2
0
    explicit
    socket(endpoint_type endpoint) {
        typename endpoint_type::protocol_type protocol = endpoint.protocol();

        m_fd = ::socket(protocol.family(), protocol.type(), protocol.protocol());

        if(m_fd == -1) {
            throw std::system_error(errno, std::system_category(), "unable to create a socket");
        }

        medium_type::configure(m_fd);

        if(::connect(m_fd, endpoint.data(), endpoint.size()) != 0) {
            auto ec = std::error_code(errno, std::system_category());

            ::close(m_fd);

            throw std::system_error(
                ec,
                cocaine::format("unable to connect a socket to '%s'", endpoint)
            );
        }

        ::fcntl(m_fd, F_SETFD, FD_CLOEXEC);
        ::fcntl(m_fd, F_SETFL, O_NONBLOCK);
    }
Пример #3
0
 /**
  * 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
  * not returned to the closed state.
  *
  * @param peer_endpoint The remote endpoint to which the socket will be
  * connected.
  *
  * @throws boost::system::system_error Thrown on failure.
  *
  * @par Example
  * @code
  * boost::asio::ip::tcp::socket socket(io_service);
  * boost::asio::ip::tcp::endpoint endpoint(
  *     boost::asio::ip::address::from_string("1.2.3.4"), 12345);
  * socket.connect(endpoint);
  * @endcode
  */
 void connect(const endpoint_type& peer_endpoint)
 {
   boost::system::error_code ec;
   if (!is_open())
   {
     this->service.open(this->implementation, peer_endpoint.protocol(), ec);
     boost::asio::detail::throw_error(ec);
   }
   this->service.connect(this->implementation, peer_endpoint, ec);
   boost::asio::detail::throw_error(ec);
 }
Пример #4
0
  // Resolve an endpoint to a list of entries.
  iterator_type resolve(implementation_type&,
      const endpoint_type& endpoint, asio::error_code& ec)
  {
    char host_name[NI_MAXHOST];
    char service_name[NI_MAXSERV];
    socket_ops::sync_getnameinfo(endpoint.data(), endpoint.size(),
        host_name, NI_MAXHOST, service_name, NI_MAXSERV,
        endpoint.protocol().type(), ec);

    return ec ? iterator_type() : iterator_type::create(
        endpoint, host_name, service_name);
  }
Пример #5
0
 /**
  * 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
  * not 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->get_service().open(this->get_implementation(),
         peer_endpoint.protocol(), ec);
     asio::detail::throw_error(ec, "connect");
   }
   this->get_service().connect(this->get_implementation(), peer_endpoint, ec);
   asio::detail::throw_error(ec, "connect");
 }
Пример #6
0
  /**
   * 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
   * not 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
   * boost::asio::ip::tcp::socket socket(io_service);
   * boost::asio::ip::tcp::endpoint endpoint(
   *     boost::asio::ip::address::from_string("1.2.3.4"), 12345);
   * boost::system::error_code ec;
   * socket.connect(endpoint, ec);
   * if (ec)
   * {
   *   // An error occurred.
   * }
   * @endcode
   */
  boost::system::error_code connect(const endpoint_type& peer_endpoint,
      boost::system::error_code& ec)
  {
    if (!is_open())
    {
      if (this->service.open(this->implementation,
            peer_endpoint.protocol(), ec))
      {
        return ec;
      }
    }

    return this->service.connect(this->implementation, peer_endpoint, ec);
  }
Пример #7
0
 http_sync_server(endpoint_type const& ep,
         std::string const& root)
     : sock_(ios_)
     , acceptor_(ios_)
     , root_(root)
 {
     acceptor_.open(ep.protocol());
     acceptor_.bind(ep);
     acceptor_.listen(
         boost::asio::socket_base::max_connections);
     acceptor_.async_accept(sock_,
         std::bind(&http_sync_server::on_accept, this,
             beast::asio::placeholders::error));
     thread_ = std::thread{[&]{ ios_.run(); }};
 }
Пример #8
0
  /**
   * 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
   * not 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
   * asio::ip::tcp::socket socket(io_service);
   * asio::ip::tcp::endpoint endpoint(
   *     asio::ip::address::from_string("1.2.3.4"), 12345);
   * asio::error_code ec;
   * socket.connect(endpoint, ec);
   * if (ec)
   * {
   *   // An error occurred.
   * }
   * @endcode
   */
  asio::error_code connect(const endpoint_type& peer_endpoint,
      asio::error_code& ec)
  {
    if (!is_open())
    {
      if (this->get_service().open(this->get_implementation(),
            peer_endpoint.protocol(), ec))
      {
        return ec;
      }
    }

    return this->get_service().connect(
        this->get_implementation(), peer_endpoint, ec);
  }
Пример #9
0
  void async_connect(const endpoint_type& peer_endpoint, ConnectHandler handler)
  {
    if (!is_open())
    {
      boost::system::error_code ec;
      if (this->service.open(this->implementation,
            peer_endpoint.protocol(), ec))
      {
        this->get_io_service().post(
            boost::asio::detail::bind_handler(handler, ec));
        return;
      }
    }

    this->service.async_connect(this->implementation, peer_endpoint, handler);
  }
Пример #10
0
 /**
  * This constructor creates an acceptor and automatically opens it to listen
  * for new connections on the specified endpoint.
  *
  * @param io_service The io_service object that the acceptor will use to
  * dispatch handlers for any asynchronous operations performed on the
  * acceptor.
  *
  * @param endpoint An endpoint on the local machine on which the acceptor
  * will listen for new connections.
  *
  * @param reuse_addr Whether the constructor should set the socket option
  * socket_base::reuse_address.
  *
  * @throws asio::system_error Thrown on failure.
  *
  * @note This constructor is equivalent to the following code:
  * @code
  * basic_socket_acceptor<Protocol> acceptor(io_service);
  * acceptor.open(endpoint.protocol());
  * if (reuse_addr)
  *   acceptor.set_option(socket_base::reuse_address(true));
  * acceptor.bind(endpoint);
  * acceptor.listen(listen_backlog);
  * @endcode
  */
 basic_socket_acceptor(asio::io_service& io_service,
     const endpoint_type& endpoint, bool reuse_addr = true)
   : basic_io_object<SocketAcceptorService>(io_service)
 {
   asio::error_code ec;
   this->service.open(this->implementation, endpoint.protocol(), ec);
   asio::detail::throw_error(ec);
   if (reuse_addr)
   {
     this->service.set_option(this->implementation,
         socket_base::reuse_address(true), ec);
     asio::detail::throw_error(ec);
   }
   this->service.bind(this->implementation, endpoint, ec);
   asio::detail::throw_error(ec);
   this->service.listen(this->implementation,
       socket_base::max_connections, ec);
   asio::detail::throw_error(ec);
 }
Пример #11
0
 sync_echo_server(bool server, endpoint_type ep)
     : sock_(ios_)
     , acceptor_(ios_)
 {
     error_code ec;
     acceptor_.open(ep.protocol(), ec);
     maybe_throw(ec, "open");
     acceptor_.set_option(
         boost::asio::socket_base::reuse_address{true});
     acceptor_.bind(ep, ec);
     maybe_throw(ec, "bind");
     acceptor_.listen(
         boost::asio::socket_base::max_connections, ec);
     maybe_throw(ec, "listen");
     acceptor_.async_accept(sock_,
                            std::bind(&sync_echo_server::on_accept, this,
                                      beast::asio::placeholders::error));
     thread_ = std::thread{[&]{ ios_.run(); }};
 }