2 * Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file chunk_queue.c Queuing functions for paraslash senders. */
16 #include "chunk_queue.h"
19 * Senders may use the chunk queue facility to deal with laggy connections. It
20 * allows them to enqueue chunks if they can not be sent out immediately.
22 * Chunk queues are "cheap" in the sense that only reference to the audio file
23 * data is stored, but not the data itself.
26 /** The list of pending chunks for this client. */
28 /** The number of pending bytes for this client. */
29 unsigned long num_pending;
30 /** More than that many bytes in the queue is considered an error. */
31 unsigned long max_pending;
34 /** Describes one queued chunk in a chunk queue. */
36 /** Pointer to the data to be queued. */
38 /** The number of bytes of this chunk. */
40 /** Position of the chunk in the chunk queue. */
41 struct list_head node;
45 * Add a chunk to the given queue.
47 * \param cq the queue to add the chunk to.
48 * \param buf Pointer to the data to be queued.
49 * \param num_bytes The size of \a buf.
53 int cq_enqueue(struct chunk_queue *cq, const char *buf, size_t num_bytes)
55 struct queued_chunk *qc;
57 if (cq->num_pending + num_bytes > cq->max_pending)
59 qc = para_malloc(sizeof(struct queued_chunk));
60 cq->num_pending += num_bytes;
62 qc->num_bytes = num_bytes;
63 list_add_tail(&qc->node, &cq->q);
64 PARA_DEBUG_LOG("%lu bytes queued for %p\n", cq->num_pending, &cq->q);
69 * Lookup the next chunk in the queue.
71 * \param cq The chunk queue.
73 * \return The next queued chunk, or \p NULL if there is no chunk available.
75 struct queued_chunk *cq_peek(struct chunk_queue *cq)
77 if (list_empty(&cq->q))
79 return list_entry(cq->q.next, struct queued_chunk, node);
83 * Remove the current chunk from the queue.
85 * \param cq The queue to remove from.
87 void cq_dequeue(struct chunk_queue *cq)
89 struct queued_chunk *qc = cq_peek(cq);
91 assert(cq->num_pending >= qc->num_bytes);
92 cq->num_pending -= qc->num_bytes;
98 * Change the number of bytes sent for the current queued chunk.
100 * \param cq The chunk queue.
101 * \param sent Number of bytes successfully sent.
103 void cq_update(struct chunk_queue *cq, size_t sent)
105 struct queued_chunk *qc = cq_peek(cq);
107 qc->num_bytes -= sent;
109 cq->num_pending -= sent;
113 * Get a pointer to the given queued chunk.
115 * \param qc The queued chunk.
116 * \param buf Result pointer.
117 * \param num_bytes Number of bytes of \a buf.
119 * \return Positive on success, negative on errors.
121 int cq_get(struct queued_chunk *qc, const char **buf, size_t *num_bytes)
124 *num_bytes = qc->num_bytes;
129 * Allocate and initialize a chunk queue.
131 * \param max_pending Maximal number of bytes that will be queued.
133 * \return A pointer to the new queue.
135 struct chunk_queue *cq_new(size_t max_pending)
137 struct chunk_queue *cq = para_malloc(sizeof(*cq));
138 INIT_LIST_HEAD(&cq->q);
139 cq->max_pending = max_pending;
145 * Deallocate all resources of this queue.
147 * \param cq The chunk queue.
149 void cq_destroy(struct chunk_queue *cq)
151 struct queued_chunk *qc, *tmp;
152 list_for_each_entry_safe(qc, tmp, &cq->q, node) {