libmpdclient 2.22
Enumerations | Functions
async.h File Reference

Asynchronous MPD connections. More...

#include "error.h"
#include "compiler.h"
#include <stdbool.h>
#include <stdarg.h>
#include <stddef.h>
Include dependency graph for async.h:

Go to the source code of this file.

Enumerations

enum  mpd_async_event { MPD_ASYNC_EVENT_READ = 1 , MPD_ASYNC_EVENT_WRITE = 2 , MPD_ASYNC_EVENT_HUP = 4 , MPD_ASYNC_EVENT_ERROR = 8 }
 

Functions

struct mpd_asyncmpd_async_new (int fd)
 
void mpd_async_free (struct mpd_async *async)
 
enum mpd_error mpd_async_get_error (const struct mpd_async *async)
 
const char * mpd_async_get_error_message (const struct mpd_async *async)
 
int mpd_async_get_system_error (const struct mpd_async *async)
 
int mpd_async_get_fd (const struct mpd_async *async)
 
bool mpd_async_set_keepalive (struct mpd_async *async, bool keepalive)
 
enum mpd_async_event mpd_async_events (const struct mpd_async *async)
 
bool mpd_async_io (struct mpd_async *async, enum mpd_async_event events)
 
bool mpd_async_send_command_v (struct mpd_async *async, const char *command, va_list args)
 
mpd_sentinel bool mpd_async_send_command (struct mpd_async *async, const char *command,...)
 
char * mpd_async_recv_line (struct mpd_async *async)
 
size_t mpd_async_recv_raw (struct mpd_async *async, void *dest, size_t length)
 

Detailed Description

Asynchronous MPD connections.

This class provides a very basic interface to MPD connections. It does not know much about the MPD protocol, it does not know any specific MPD command.

The constructor expects a socket descriptor which is already connected to MPD. The first thing it does is read the server's handshake code ("OK MPD 0.15.0").

Definition in file async.h.

Enumeration Type Documentation

◆ mpd_async_event

Event bit mask for polling.

Enumerator
MPD_ASYNC_EVENT_READ 

ready to read from the file descriptor

MPD_ASYNC_EVENT_WRITE 

ready to write to the file descriptor

MPD_ASYNC_EVENT_HUP 

hangup detected

MPD_ASYNC_EVENT_ERROR 

I/O error

Definition at line 54 of file async.h.

Function Documentation

◆ mpd_async_new()

struct mpd_async * mpd_async_new ( int  fd)

Creates a new asynchronous MPD connection, based on a stream socket connected with MPD.

Parameters
fdthe socket file descriptor of the stream connection to MPD
Returns
a mpd_async object, or NULL on out of memory

◆ mpd_async_free()

void mpd_async_free ( struct mpd_async async)

Closes the socket and frees memory.

◆ mpd_async_get_error()

enum mpd_error mpd_async_get_error ( const struct mpd_async async)

After an error has occurred, this function returns the error code. If no error has occurred, it returns MPD_ERROR_SUCCESS.

◆ mpd_async_get_error_message()

const char * mpd_async_get_error_message ( const struct mpd_async async)

If mpd_async_get_error() returns an error code other than MPD_ERROR_SUCCESS, this function returns the human readable error message which caused this. This message is optional, and may be NULL. The pointer is invalidated by mpd_async_free().

For MPD_ERROR_SERVER, the error message is encoded in UTF-8. MPD_ERROR_SYSTEM obtains its error message from the operating system, and thus the locale's character set (and probably language) is used. Keep that in mind when you print error messages.

◆ mpd_async_get_system_error()

int mpd_async_get_system_error ( const struct mpd_async async)

Returns the error code from the operating system; on most operating systems, this is the errno value. Calling this function is only valid if mpd_async_get_error() returned MPD_ERROR_SYSTEM.

May be 0 if the operating system did not specify an error code.

◆ mpd_async_get_fd()

int mpd_async_get_fd ( const struct mpd_async async)

Returns the file descriptor which should be polled by the caller. Do not use the file descriptor for anything except polling! The file descriptor never changes during the lifetime of this mpd_async object.

◆ mpd_async_set_keepalive()

bool mpd_async_set_keepalive ( struct mpd_async async,
bool  keepalive 
)

Enables (or disables) TCP keepalives.

Keepalives are enabled using the SO_KEEPALIVE socket option. They may be required for long-idled connections to persist on some networks that would otherwise terminate inactive TCP sessions.

The default value is false.

Parameters
asyncthe mpd_async object
keepalivewhether TCP keepalives should be enabled
Returns
true on success, false if setsockopt failed
Since
libmpdclient 2.10

◆ mpd_async_events()

enum mpd_async_event mpd_async_events ( const struct mpd_async async)

Returns a bit mask of events which should be polled for.

◆ mpd_async_io()

bool mpd_async_io ( struct mpd_async async,
enum mpd_async_event  events 
)

Call this function when poll() has returned events for this object's file descriptor. libmpdclient will attempt to perform I/O operations.

Returns
false if the connection was closed due to an error

◆ mpd_async_send_command_v()

bool mpd_async_send_command_v ( struct mpd_async async,
const char *  command,
va_list  args 
)

Appends a command to the output buffer.

Parameters
asyncthe connection
commandthe command name, followed by arguments, terminated by NULL
argsthe list of 'const char *' arguments
Returns
true on success, false if the buffer is full or an error has previously occurred

◆ mpd_async_send_command()

mpd_sentinel bool mpd_async_send_command ( struct mpd_async async,
const char *  command,
  ... 
)

Appends a command to the output buffer.

Parameters
asyncthe connection
commandthe command name, followed by arguments, terminated by NULL. The arguments should be of type 'const char *'
Returns
true on success, false if the buffer is full or an error has previously occurred

◆ mpd_async_recv_line()

char * mpd_async_recv_line ( struct mpd_async async)

Receives a line from the input buffer. The result will be null-terminated, without the newline character. The pointer is only valid until the next async function is called.

You can use mpd_parser_new() and mpd_parser_feed() for parsing the line.

Parameters
asyncthe connection
Returns
a line on success, NULL otherwise

◆ mpd_async_recv_raw()

size_t mpd_async_recv_raw ( struct mpd_async async,
void *  dest,
size_t  length 
)

Copy raw data from the input buffer. This can be used to receive binary data from MPD, such as album art.

Parameters
asyncthe connection
desta buffer where this function will copy the data
lengthof bytes to consume
Returns
the number of bytes copied to the destination buffer (may be 0 if the input buffer was empty)
Since
libmpdclient 2.17