Пример #1
0
inline Error initLocalStreamAcceptor(
   SocketAcceptorService<boost::asio::local::stream_protocol>& acceptorService,
   const core::FilePath& localStreamPath)
{
   // initialize endpoint
   using boost::asio::local::stream_protocol;
   stream_protocol::endpoint endpoint(localStreamPath.absolutePath());
   
   // get acceptor
   stream_protocol::acceptor& acceptor = acceptorService.acceptor();
   
   // open
   boost::system::error_code ec;
   acceptor.open(endpoint.protocol(), ec) ;
   if (ec)
      return Error(ec, ERROR_LOCATION) ;
   
   // bind
   acceptor.bind(endpoint, ec) ;
   if (ec)
      return Error(ec, ERROR_LOCATION) ;
   
   // chmod on the stream file
   Error error = changeFileMode(localStreamPath,
                                system::EveryoneReadWriteMode);
   if (error)
      return error;
   
   // listen
   acceptor.listen(boost::asio::socket_base::max_connections, ec) ;
   if (ec)
      return Error(ec, ERROR_LOCATION) ;
   
   return Success() ;
}
Пример #2
0
inline Error initTcpIpAcceptor(
    SocketAcceptorService<boost::asio::ip::tcp>& acceptorService,
    const std::string& address,
    const std::string& port)
{
    using boost::asio::ip::tcp;

    tcp::resolver resolver(acceptorService.ioService()) ;
    tcp::resolver::query query(address, port) ;

    boost::system::error_code ec ;
    tcp::resolver::iterator entries = resolver.resolve(query,ec) ;
    if (ec)
        return Error(ec, ERROR_LOCATION) ;

    tcp::acceptor& acceptor = acceptorService.acceptor();
    const tcp::endpoint& endpoint = *entries ;
    acceptor.open(endpoint.protocol(), ec) ;
    if (ec)
        return Error(ec, ERROR_LOCATION) ;

    acceptor.set_option(tcp::acceptor::reuse_address(true), ec) ;
    if (ec)
        return Error(ec, ERROR_LOCATION) ;

    acceptor.set_option(tcp::no_delay(true), ec) ;
    if (ec)
        return Error(ec, ERROR_LOCATION) ;

    acceptor.bind(endpoint, ec) ;
    if (ec)
        return Error(ec, ERROR_LOCATION) ;

    acceptor.listen(boost::asio::socket_base::max_connections, ec) ;
    if (ec)
        return Error(ec, ERROR_LOCATION) ;

    return Success() ;
}