Revert "Revive connection upon error."

This reverts commit 0c401294b7.
This commit is contained in:
Matt Low 2019-10-17 00:18:24 +04:00
parent 0be366e77a
commit 61c69691b4

View File

@ -41,21 +41,13 @@ scroll_text(const char *text_to_scroll, const size_t text_len, char *buffer,
} }
static int static int
handle_error(struct mpd_connection *conn, bool revive) handle_error(struct mpd_connection *c)
{ {
if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { assert(mpd_connection_get_error(c) != MPD_ERROR_SUCCESS);
fprintf(stderr, "%s\n", mpd_connection_get_error_message(conn));
if (revive) { fprintf(stderr, "%s\n", mpd_connection_get_error_message(c));
if (!mpd_connection_clear_error(conn)) { mpd_connection_free(c);
mpd_connection_free(conn); return EXIT_FAILURE;
conn = mpd_connection_new(NULL, 0, 0);
}
} else {
return EXIT_FAILURE;
}
}
return 0;
} }
static void static void
@ -75,7 +67,7 @@ print_status(struct mpd_connection *conn, struct worker_meta *meta)
struct mpd_status *status = mpd_run_status(conn); struct mpd_status *status = mpd_run_status(conn);
if (status == NULL) if (status == NULL)
return handle_error(conn, false); return handle_error(conn);
const enum mpd_state state = mpd_status_get_state(status); const enum mpd_state state = mpd_status_get_state(status);
@ -113,10 +105,8 @@ print_status(struct mpd_connection *conn, struct worker_meta *meta)
const unsigned remaining_secs = remaining % 60; const unsigned remaining_secs = remaining % 60;
mpd_status_free(status); mpd_status_free(status);
if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS)
if (handle_error(conn, false)) { return handle_error(conn);
return 1;
}
char artist[256]; char artist[256];
char album[256]; char album[256];
@ -128,9 +118,8 @@ print_status(struct mpd_connection *conn, struct worker_meta *meta)
get_tag(song, title, MPD_TAG_TITLE); get_tag(song, title, MPD_TAG_TITLE);
mpd_song_free(song); mpd_song_free(song);
if (handle_error(conn, false)) { if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS)
return 1; return handle_error(conn);
}
// the full song label. would be cool if this was customizable at runtime // the full song label. would be cool if this was customizable at runtime
char full_label[strlen(artist) + strlen(title) + 4]; char full_label[strlen(artist) + strlen(title) + 4];
@ -181,8 +170,10 @@ status_loop(void* worker_meta)
struct worker_meta *meta = (struct worker_meta*)worker_meta; struct worker_meta *meta = (struct worker_meta*)worker_meta;
struct mpd_connection *conn = mpd_connection_new(NULL, 0, 0); struct mpd_connection *conn = mpd_connection_new(NULL, 0, 0);
while (!meta->stop) { for (;;) {
handle_error(conn, true); if (meta->stop) {
break;
}
long long start = current_timestamp(); long long start = current_timestamp();
@ -202,9 +193,8 @@ enum click_command {
}; };
void void
mpd_run_command(struct worker_meta *meta, struct mpd_connection *conn, enum click_command command) { mpd_run_command(struct worker_meta *meta, enum click_command command) {
handle_error(conn, true); struct mpd_connection *conn = mpd_connection_new(NULL, 0, 0);
switch (command) { switch (command) {
case TOGGLE_PAUSE: case TOGGLE_PAUSE:
mpd_run_toggle_pause(conn); mpd_run_toggle_pause(conn);
@ -224,6 +214,7 @@ mpd_run_command(struct worker_meta *meta, struct mpd_connection *conn, enum clic
} }
print_status(conn, meta); print_status(conn, meta);
mpd_connection_free(conn);
} }
@ -236,28 +227,26 @@ int main(void) {
return 1; return 1;
} }
struct mpd_connection *conn = mpd_connection_new(NULL, 0, 0); for (;;) {
while (!meta.stop) {
char *line = NULL; char *line = NULL;
size_t size; size_t size;
if (getline(&line, &size, stdin) != -1) { if (getline(&line, &size, stdin) != -1) {
if (strcmp(line, "1\n") == 0) { if (strcmp(line, "1\n") == 0) {
mpd_run_command(&meta, conn, PREVIOUS); mpd_run_command(&meta, PREVIOUS);
} else if (strcmp(line, "2\n") == 0) { } else if (strcmp(line, "2\n") == 0) {
mpd_run_command(&meta, conn, TOGGLE_PAUSE); mpd_run_command(&meta, TOGGLE_PAUSE);
} else if (strcmp(line, "3\n") == 0) { } else if (strcmp(line, "3\n") == 0) {
mpd_run_command(&meta, conn, NEXT); mpd_run_command(&meta, NEXT);
} else if (strcmp(line, "4\n") == 0) { } else if (strcmp(line, "4\n") == 0) {
mpd_run_command(&meta, conn, SEEK_FWD); mpd_run_command(&meta, SEEK_FWD);
} else if (strcmp(line, "5\n") == 0) { } else if (strcmp(line, "5\n") == 0) {
mpd_run_command(&meta, conn, SEEK_BCKWD); mpd_run_command(&meta, SEEK_BCKWD);
} }
free(line); free(line);
} }
} }
mpd_connection_free(conn);
if(pthread_join(update_thread, NULL)) { if(pthread_join(update_thread, NULL)) {
fprintf(stderr, "Error joining thread\n"); fprintf(stderr, "Error joining thread\n");