Пример #1
0
//------------------------------------------------------------------------------
//!
RCP<ConnectedSocketDevice>
Socket::connect( const Endpoint& ep )
{
#if BASE_SOCKET_USE_BSD || BASE_SOCKET_USE_WINSOCK
   state( STATE_CONNECTING );

   addrinfo* candidates = getAddrInfo( *this, ep.hostname().cstr(), ep.port() );
   if( candidates == NULL )
   {
      return NULL;
   }

   Family   family   = fromAPI_family  ( candidates->ai_family   );
   Type     type     = fromAPI_type    ( candidates->ai_socktype );
   Protocol protocol = fromAPI_protocol( candidates->ai_protocol );

   RCP<Socket>  socket = new Socket( family, type, protocol );

   int err = ::connect( socket->_impl->_sock, candidates->ai_addr, SocketSSizeT(candidates->ai_addrlen) );
   if( err != 0 )
   {
      printAPIError( "ERROR - Socket::connect() - connect failed" );
      freeaddrinfo( candidates );
      return NULL;
   }

   RCP<ConnectedSocketDevice>  device = new ConnectedSocketDevice( socket.ptr() );
   device->_remoteEndpoint = ep;

   freeaddrinfo( candidates );

   state( STATE_CONNECTED );

   return device;
#else
   return NULL;
#endif
}