Logo Search packages:      
Sourcecode: libmpdclient version File versions  Download package

mpd_malloc struct mpd_connection* mpd_connection_new ( const char *  host,
unsigned  port,
unsigned  timeout_ms 
) [read]

Opens a new connection to a MPD server. Both the name server lookup and the connect() call are done synchronously. After this function has returned, you should check if the connection was successful with mpd_connection_get_error().

Parameters:
host the server's host name, IP address or Unix socket path. If the resolver returns more than one IP address for a host name, this functions tries all of them until one accepts the connection. NULL is allowed here, which will connect to the default host.
port the TCP port to connect to, 0 for default port. If "host" is a Unix socket path, this parameter is ignored.
timeout_ms the timeout in milliseconds, 0 for the default timeout; you may modify it later with mpd_connection_set_timeout()
Returns:
a mpd_connection object (which may have failed to connect), or NULL on out-of-memory

Definition at line 192 of file connection.c.

References mpd_connection::async, mpd_connection::error, mpd_async_new(), mpd_connection_new(), mpd_connection_set_timeout(), MPD_ERROR_OOM, mpd_run_password(), mpd_connection::pair_state, mpd_connection::PAIR_STATE_NONE, mpd_connection::parser, mpd_connection::receiving, mpd_connection::request, mpd_connection::sending_command_list, and mpd_connection::timeout.

Referenced by mpd_connection_new().

{
      struct mpd_connection *connection = malloc(sizeof(*connection));
      bool success;
      int fd;
      const char *line;
      char *password = NULL;


      if (connection == NULL)
            return NULL;

      mpd_error_init(&connection->error);
      connection->async = NULL;
      connection->parser = NULL;
      connection->receiving = false;
      connection->sending_command_list = false;
      connection->pair_state = PAIR_STATE_NONE;
      connection->request = NULL;

      if (!mpd_socket_global_init(&connection->error))
            return connection;

      if (timeout_ms == 0)
            /* 30s is the default */
            timeout_ms = 30000;

      mpd_connection_set_timeout(connection, timeout_ms);

      host = mpd_check_host(host, &password);
      port = mpd_check_port(port);

      fd = mpd_connect(host, port, &connection->timeout, &connection->error);
      if (fd < 0) {
            free(password);
            return connection;
      }

      connection->async = mpd_async_new(fd);
      if (connection->async == NULL) {
            free(password);
            mpd_socket_close(fd);
            mpd_error_code(&connection->error, MPD_ERROR_OOM);
            return connection;
      }

      connection->parser = mpd_parser_new();
      if (connection->parser == NULL) {
            free(password);
            mpd_error_code(&connection->error, MPD_ERROR_OOM);
            return connection;
      }

      line = mpd_sync_recv_line(connection->async, &connection->timeout);
      if (line == NULL) {
            free(password);
            mpd_connection_sync_error(connection);
            return connection;
      }

      success = mpd_parse_welcome(connection, line);

      if (password != NULL) {
            if (success)
                  mpd_run_password(connection, password);
            free(password);
      }

      return connection;
}


Generated by  Doxygen 1.6.0   Back to index