From 38436c3f6e3eabe9899593b71a244fd7fc3e346b Mon Sep 17 00:00:00 2001 From: Philipp Schafft Date: Tue, 17 Apr 2018 07:29:49 +0000 Subject: [PATCH] Update: Abstract body read with client_body_read() and client_body_eof() --- src/client.c | 20 ++++++++++++++++++++ src/client.h | 2 ++ src/format_ebml.c | 2 +- src/format_mp3.c | 7 ++++--- src/format_ogg.c | 4 ++-- src/source.c | 5 +---- 6 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/client.c b/src/client.c index 44ae457e..279ac1ff 100644 --- a/src/client.c +++ b/src/client.c @@ -32,6 +32,7 @@ #include "refobject.h" #include "cfgfile.h" #include "connection.h" +#include "tls.h" #include "refbuf.h" #include "format.h" #include "stats.h" @@ -435,3 +436,22 @@ void client_set_queue(client_t *client, refbuf_t *refbuf) if (to_release) refbuf_release(to_release); } + +ssize_t client_body_read(client_t *client, void *buf, size_t len) +{ + return client_read_bytes(client, buf, len); +} + +int client_body_eof(client_t *client) +{ + if (!client->con) + return 0; + + if (client->con->tls && tls_got_shutdown(client->con->tls) > 1) + client->con->error = 1; + + if (client->con->error) + return 1; + + return 0; +} diff --git a/src/client.h b/src/client.h index d87a2465..81e78565 100644 --- a/src/client.h +++ b/src/client.h @@ -122,5 +122,7 @@ admin_format_t client_get_admin_format_by_content_negotiation(client_t *client); int client_send_bytes (client_t *client, const void *buf, unsigned len); int client_read_bytes (client_t *client, void *buf, unsigned len); void client_set_queue (client_t *client, refbuf_t *refbuf); +ssize_t client_body_read(client_t *client, void *buf, size_t len); +int client_body_eof(client_t *client); #endif /* __CLIENT_H__ */ diff --git a/src/format_ebml.c b/src/format_ebml.c index 5ac45d6b..0a9fe9bc 100644 --- a/src/format_ebml.c +++ b/src/format_ebml.c @@ -317,7 +317,7 @@ static refbuf_t *ebml_get_buffer(source_t *source) } else if(read_bytes == 0) { /* Feed more bytes into the parser */ write_buffer = ebml_get_write_buffer(ebml_source_state->ebml, &write_bytes); - read_bytes = client_read_bytes (source->client, write_buffer, write_bytes); + read_bytes = client_body_read(source->client, write_buffer, write_bytes); if (read_bytes <= 0) { ebml_wrote (ebml_source_state->ebml, 0); return NULL; diff --git a/src/format_mp3.c b/src/format_mp3.c index dae620c6..f652868d 100644 --- a/src/format_mp3.c +++ b/src/format_mp3.c @@ -465,7 +465,7 @@ static void format_mp3_free_plugin(format_plugin_t *self) */ static int complete_read(source_t *source) { - int bytes; + ssize_t bytes; format_plugin_t *format = source->format; mp3_state *source_mp3 = format->_state; char *buf; @@ -480,10 +480,11 @@ static int complete_read(source_t *source) } buf = source_mp3->read_data->data + source_mp3->read_count; - bytes = client_read_bytes (source->client, buf, REFBUF_SIZE-source_mp3->read_count); + bytes = client_body_read(source->client, buf, REFBUF_SIZE-source_mp3->read_count); if (bytes < 0) { - if (source->client->con->error) + /* Why do we do this here (not source.c)? -- ph3-der-loewe, 2018-04-17 */ + if (client_body_eof(source->client)) { refbuf_release (source_mp3->read_data); source_mp3->read_data = NULL; diff --git a/src/format_ogg.c b/src/format_ogg.c index 605279f8..c2795adb 100644 --- a/src/format_ogg.c +++ b/src/format_ogg.c @@ -402,7 +402,7 @@ static refbuf_t *ogg_get_buffer(source_t *source) ogg_state_t *ogg_info = source->format->_state; format_plugin_t *format = source->format; char *data = NULL; - int bytes = 0; + ssize_t bytes = 0; while (1) { @@ -449,7 +449,7 @@ static refbuf_t *ogg_get_buffer(source_t *source) /* we need more data to continue getting pages */ data = ogg_sync_buffer (&ogg_info->oy, 4096); - bytes = client_read_bytes (source->client, data, 4096); + bytes = client_body_read(source->client, data, 4096); if (bytes <= 0) { ogg_sync_wrote (&ogg_info->oy, 0); diff --git a/src/source.c b/src/source.c index 73120c0e..69d0f7f6 100644 --- a/src/source.c +++ b/src/source.c @@ -516,10 +516,7 @@ static refbuf_t *get_next_buffer (source_t *source) } source->last_read = current; refbuf = source->format->get_buffer (source); - if (source->client->con && source->client->con->tls && tls_got_shutdown(source->client->con->tls) > 1) - source->client->con->error = 1; - if (source->client->con && source->client->con->error) - { + if (client_body_eof(source->client)) { ICECAST_LOG_INFO("End of Stream %s", source->mount); source->running = 0; continue;