--- /dev/null
+/*
+ * Copyright (C) 2007 Andre Noll <maan@systemlinux.org>
+ *
+ * Licensed under the GPL v2. For licencing details see COPYING.
+ */
+
+/** \file chunk_queue.c Queuing functions for paraslash senders. */
+
+#include "para.h"
+#include "list.h"
+#include "vss.h"
+#include "string.h"
+#include "error.h"
+
+/**
+ * Senders may use the chunk queue facility to deal with laggy connections. It
+ * allows them to enqueue chunks if they can not be sent out immediately.
+ *
+ * Chunk queues are "cheap" in the sense that only reference to the audio file
+ * data is stored, but not the data itsself.
+ */
+struct chunk_queue {
+ /** The list of pending chunks for this client. */
+ struct list_head q;
+ /** The number of pending bytes for this client. */
+ unsigned long num_pending;
+ /** Enqueueing more than that many bytes is an error. */
+ unsigned long max_pending;
+};
+
+/** Describes one queued chunk in a chunk queue. */
+struct queued_chunk {
+ /** The number of the queued chunk, -1U means header. */
+ unsigned chunk_num;
+ /** The number of bytes already sent. */
+ unsigned sent;
+ /** Position of the chunk in the chunk queue. */
+ struct list_head node;
+};
+
+/**
+ * Add a chunk to the given queue.
+ *
+ * \param cq the queue to add the chunk to.
+ * \param chunk_num The number of the chunk to be queued.
+ * \param sent The number of bytes of this chunk that the sender was able to
+ * send.
+ *
+ * \return Positive on success, negative on errors.
+ */
+int cq_enqueue(struct chunk_queue *cq, long unsigned chunk_num,
+ size_t sent)
+{
+ struct queued_chunk *qc;
+ char *buf;
+ size_t len;
+ int ret;
+
+ if (chunk_num != -1U) {
+ ret = vss_get_chunk(chunk_num, &buf, &len);
+ if (ret < 0)
+ return ret;
+ } else
+ buf = vss_get_header(&len);
+ if (cq->num_pending + len > cq->max_pending)
+ return -E_QUEUE;
+ qc = para_malloc(sizeof(struct queued_chunk));
+ cq->num_pending += len;
+ qc->chunk_num = chunk_num;
+ qc->sent = sent;
+ list_add_tail(&qc->node, &cq->q);
+ PARA_DEBUG_LOG("%lu bytes queued for %p\n", cq->num_pending, &cq->q);
+ return 1;
+}
+
+/**
+ * Lookup the next chunk in the queue.
+ *
+ * \param cq The chunk queue.
+ *
+ * \return The next queued chunk, or \p NULL if there is no chunk awailable.
+ */
+struct queued_chunk *cq_peek(struct chunk_queue *cq)
+{
+ if (list_empty(&cq->q))
+ return NULL;
+ return list_entry(cq->q.next, struct queued_chunk, node);
+}
+
+/**
+ * Remove the current chunk from the queue.
+ *
+ * \param cq The chunk to remove.
+ */
+void cq_dequeue(struct chunk_queue *cq)
+{
+ struct queued_chunk *qc = cq_peek(cq);
+ assert(qc);
+ list_del(&qc->node);
+ free(qc);
+}
+
+/**
+ * Change the number of bytes send for the current queued chunk.
+ *
+ * \param cq The chunk queue.
+ * \param sent Number of bytes successfully sent.
+ */
+void cq_update(struct chunk_queue *cq, size_t sent)
+{
+ struct queued_chunk *qc = cq_peek(cq);
+ assert(qc);
+ qc->sent += sent;
+ cq->num_pending -= sent;
+}
+
+/**
+ * Get a pointer to the given queued chunk.
+ *
+ * \param qc The queued chunk.
+ * \param buf Result pointer.
+ * \param len Number of bytes of \a buf.
+ *
+ * \return Positive on success, negative on errors.
+ */
+int cq_get(struct queued_chunk *qc, char **buf, size_t *len)
+{
+ int ret;
+
+ if (qc->chunk_num != -1U) {
+ ret = vss_get_chunk(qc->chunk_num, buf, len);
+ if (ret < 0)
+ return ret;
+ } else
+ *buf = vss_get_header(len);
+ assert(*len > qc->sent);
+ *buf += qc->sent;
+ *len -= qc->sent;
+ return 1;
+}
+
+/**
+ * Allocate and initialize a chunk queue.
+ *
+ * \param max_pending Maximal number of bytes that will be queued.
+ *
+ * \return A pointer to the new queue.
+ */
+struct chunk_queue *cq_new(size_t max_pending)
+{
+ struct chunk_queue *cq = para_malloc(sizeof(*cq));
+ INIT_LIST_HEAD(&cq->q);
+ cq->max_pending = max_pending;
+ cq->num_pending = 0;
+ return cq;
+}
+
+/**
+ * Deallocate all resources of this queue.
+ *
+ * \param cq The chunk queue.
+ */
+void cq_destroy(struct chunk_queue *cq)
+{
+ struct queued_chunk *qc, *tmp;
+ list_for_each_entry_safe(qc, tmp, &cq->q, node) {
+ list_del(&qc->node);
+ free(qc);
+ }
+ free(cq);
+}
--- /dev/null
+/*
+ * Copyright (C) 2007 Andre Noll <maan@systemlinux.org>
+ *
+ * Licensed under the GPL v2. For licencing details see COPYING.
+ */
+
+/** \file chunk_queue.h Exported symbols from chunk_queue.c. */
+
+struct chunk_queue;
+struct queued_chunk;
+
+int cq_enqueue(struct chunk_queue *cq, long unsigned chunk_num, size_t sent);
+struct queued_chunk *cq_peek(struct chunk_queue *cq);
+void cq_dequeue(struct chunk_queue *cq);
+void cq_update(struct chunk_queue *cq, size_t sent);
+int cq_get(struct queued_chunk *qc, char **buf, size_t *len);
+struct chunk_queue *cq_new(size_t max_pending);
+void cq_destroy(struct chunk_queue *cq);
playlist_selector_command_list"
server_errlist_objs="server mp3_afh vss command net string signal random_selector
time daemon stat crypt http_send afs close_on_fork playlist_selector
- ipc dccp dccp_send fd user_list"
+ ipc dccp dccp_send fd user_list chunk_queue"
server_ldflags=""
server_audio_formats=" mp3"
SS_FILE_WRITE,
SS_OSX_WRITE,
SS_USER_LIST,
+ SS_CHUNK_QUEUE,
NUM_SS
};
#define HTTP_SEND_ERRORS \
- PARA_ERROR(QUEUE, "packet queue overrun"), \
PARA_ERROR(WRITE_OK, "can not check whether fd is writable"), \
+ PARA_ERROR(SEND_QUEUED_CHUNK, "failed to send queued chunk"), \
#define RANDOM_SELECTOR_ERRORS \
PARA_ERROR(AAC_OVERRUN, "aac output buffer overrun"), \
+#define CHUNK_QUEUE_ERRORS \
+ PARA_ERROR(QUEUE, "packet queue overrun"), \
+
+
/**
* the subsystem shift
*
SS_ENUM(CLIENT_COMMON);
SS_ENUM(AUDIOC);
SS_ENUM(USER_LIST);
+SS_ENUM(CHUNK_QUEUE);
/** \endcond */
#undef PARA_ERROR
/* rest of the world only sees the error text */
#include "net.h"
#include "string.h"
#include "fd.h"
+#include "chunk_queue.h"
/** \cond convert sock_addr_in to ascii */
#define CLIENT_ADDR(hc) inet_ntoa((hc)->addr.sin_addr)
/** The whitelist/blacklist. */
static struct list_head access_perm_list;
-struct chunk_queue{
- /** The list of pending chunks for this client. */
- struct list_head q;
- /** The number of pending bytes for this client. */
- unsigned long num_pending;
- unsigned long max_pending;
-};
-
/** Describes one client that connected the tcp port of the http sender. */
struct http_client {
/** The file descriptor of the client. */
struct chunk_queue *cq;
};
-/**
- * Describes one queued chunk of the chunk queue.
- *
- * The send function of the http sender checks each client fd for writing. If a
- * client fd is not ready, it tries to queue that chunk for this client until
- * the number of queued bytes exceeds \p MAX_BACKLOG.
- */
-struct queued_chunk {
- /** The number of the queued chunk, -1U means header. */
- unsigned chunk_num;
- /** The number of bytes already sent. */
- unsigned sent;
- /** Position of the chunk in the chunk queue. */
- struct list_head node;
-};
-
/**
* Describes one entry in the blacklist/whitelist of the http sender.
*/
static struct sender *self;
-static int cq_enqueue(struct chunk_queue *cq, long unsigned chunk_num,
- size_t sent)
-{
- struct queued_chunk *qc;
- char *buf;
- size_t len;
- int ret;
-
- if (chunk_num != -1U) {
- ret = vss_get_chunk(chunk_num, &buf, &len);
- if (ret < 0)
- return ret;
- } else
- buf = vss_get_header(&len);
- if (cq->num_pending + len > cq->max_pending)
- return -E_QUEUE;
- qc = para_malloc(sizeof(struct queued_chunk));
- cq->num_pending += len;
- qc->chunk_num = chunk_num;
- qc->sent = sent;
- list_add_tail(&qc->node, &cq->q);
- PARA_DEBUG_LOG("%lu bytes queued for %p\n", cq->num_pending, &cq->q);
- return 1;
-}
-
-static struct queued_chunk *cq_peek(struct chunk_queue *cq)
-{
- if (list_empty(&cq->q))
- return NULL;
- return list_entry(cq->q.next, struct queued_chunk, node);
-}
-
-int cq_dequeue(struct chunk_queue *cq)
-{
- struct queued_chunk *qc = cq_peek(cq);
- assert(qc);
- list_del(&qc->node);
- free(qc);
- return 1;
-}
-
-void cq_update(struct chunk_queue *cq, size_t sent)
-{
- struct queued_chunk *qc = cq_peek(cq);
- assert(qc);
- qc->sent += sent;
- cq->num_pending -= sent;
-}
-
-int cq_get(struct queued_chunk *qc, char **buf, size_t *len)
-{
- int ret;
-
- if (qc->chunk_num != -1U) {
- ret = vss_get_chunk(qc->chunk_num, buf, len);
- if (ret < 0)
- return ret;
- } else
- *buf = vss_get_header(len);
- assert(*len > qc->sent);
- *buf += qc->sent;
- *len -= qc->sent;
- return 1;
-}
-
-struct chunk_queue *cq_init(size_t max_pending)
-{
- struct chunk_queue *cq = para_malloc(sizeof(*cq));
- INIT_LIST_HEAD(&cq->q);
- cq->max_pending = max_pending;
- cq->num_pending = 0;
- return cq;
-}
-
-void cq_destroy(struct chunk_queue *cq)
-{
- struct queued_chunk *qc, *tmp;
- list_for_each_entry_safe(qc, tmp, &cq->q, node) {
- list_del(&qc->node);
- free(qc);
- }
- free(cq);
-}
-
static void http_shutdown_client(struct http_client *hc, const char *msg)
{
PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", CLIENT_ADDR(hc),
return http_send_msg(hc, HTTP_ERR_MSG);
}
-
static int send_queued_chunks(struct http_client *hc)
{
struct queued_chunk *qc;
cq_get(qc, &buf, &len);
ret = write(hc->fd, buf, len);
if (ret < 0)
- return -1; /* FIXME */
+ return -E_SEND_QUEUED_CHUNK;
cq_update(hc->cq, ret);
if (ret != len)
return 1;
goto err_out;
}
hc->status = HTTP_CONNECTED;
- hc->cq = cq_init(MAX_BACKLOG);
+ hc->cq = cq_new(MAX_BACKLOG);
PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
CLIENT_ADDR(hc), hc->fd);
numclients++;