MEDIUM: channel: adapt to the new buffer API
Also, ci_swpbuf() was removed (unused).
diff --git a/include/proto/channel.h b/include/proto/channel.h
index 26bc481..8ca6695 100644
--- a/include/proto/channel.h
+++ b/include/proto/channel.h
@@ -46,7 +46,6 @@
/* SI-to-channel functions working with buffers */
int ci_putblk(struct channel *chn, const char *str, int len);
-struct buffer *ci_swpbuf(struct channel *chn, struct buffer *buf);
int ci_putchr(struct channel *chn, char c);
int ci_getline_nc(const struct channel *chn, char **blk1, size_t *len1, char **blk2, size_t *len2);
int ci_getblk_nc(const struct channel *chn, char **blk1, size_t *len1, char **blk2, size_t *len2);
diff --git a/src/channel.c b/src/channel.c
index 2a75042..fed0cb2 100644
--- a/src/channel.c
+++ b/src/channel.c
@@ -38,7 +38,7 @@
* regular code paths.
*/
if (unlikely(chn->to_forward == CHN_INFINITE_FORWARD)) {
- c_adv(chn, chn->buf->i);
+ c_adv(chn, ci_data(chn));
return bytes;
}
@@ -48,7 +48,7 @@
budget = MIN(bytes, CHN_INFINITE_FORWARD - 1);
/* transfer as much as we can of buf->i */
- forwarded = MIN(chn->buf->i, budget);
+ forwarded = MIN(ci_data(chn), budget);
c_adv(chn, forwarded);
budget -= forwarded;
@@ -82,7 +82,7 @@
if (len == 0)
return -1;
- if (len < 0 || len > chn->buf->size) {
+ if (len < 0 || len > c_size(chn)) {
/* we can't write this chunk and will never be able to, because
* it is larger than the buffer. This must be reported as an
* error. Then we return -2 so that writers that don't care can
@@ -96,7 +96,7 @@
if (len > max)
return max;
- memcpy(b_tail(chn->buf), msg, len);
+ memcpy(ci_tail(chn), msg, len);
b_add(chn->buf, len);
c_adv(chn, len);
chn->total += len;
@@ -119,7 +119,7 @@
*ci_tail(chn) = c;
- chn->buf->i++;
+ b_add(chn->buf, 1);
chn->flags |= CF_READ_PARTIAL;
if (chn->to_forward >= 1) {
@@ -151,7 +151,7 @@
return -3;
max = channel_recv_limit(chn);
- if (unlikely(len > max - b_data(chn->buf))) {
+ if (unlikely(len > max - c_data(chn))) {
/* we can't write this chunk right now because the buffer is
* almost full or because the block is too large. Return the
* available space or -2 if impossible.
@@ -169,7 +169,7 @@
max = b_contig_space(chn->buf);
memcpy(ci_tail(chn), blk, MIN(len, max));
if (len > max)
- memcpy(b_orig(chn->buf), blk + max, len - max);
+ memcpy(c_orig(chn), blk + max, len - max);
b_add(chn->buf, len);
chn->total += len;
@@ -188,49 +188,6 @@
return len;
}
-/* Tries to copy the whole buffer <buf> into the channel's buffer after length
- * controls. It will only succeed if the target buffer is empty, in which case
- * it will simply swap the buffers. The buffer not attached to the channel is
- * returned so that the caller can store it locally. The chn->buf->o and
- * to_forward pointers are updated. If the output buffer is a dummy buffer or
- * if it still contains data <buf> is returned, indicating that nothing could
- * be done. Channel flag READ_PARTIAL is updated if some data can be transferred.
- * The chunk's length is updated with the number of bytes sent. On errors, NULL
- * is returned. Note that only buf->i is considered.
- */
-struct buffer *ci_swpbuf(struct channel *chn, struct buffer *buf)
-{
- struct buffer *old;
-
- if (unlikely(channel_input_closed(chn)))
- return NULL;
-
- if (!c_size(chn) || !c_empty(chn))
- return buf;
-
- old = chn->buf;
- chn->buf = buf;
-
- if (!buf->i)
- return old;
-
- chn->total += buf->i;
-
- if (chn->to_forward) {
- unsigned long fwd = buf->i;
- if (chn->to_forward != CHN_INFINITE_FORWARD) {
- if (fwd > chn->to_forward)
- fwd = chn->to_forward;
- chn->to_forward -= fwd;
- }
- c_adv(chn, fwd);
- }
-
- /* notify that some data was read from the SI into the buffer */
- chn->flags |= CF_READ_PARTIAL;
- return old;
-}
-
/* Gets one text line out of a channel's buffer from a stream interface.
* Return values :
* >0 : number of bytes read. Includes the \n if present before len or end.
@@ -256,10 +213,10 @@
goto out;
}
- p = b_head(chn->buf);
+ p = co_head(chn);
- if (max > chn->buf->o) {
- max = chn->buf->o;
+ if (max > co_data(chn)) {
+ max = co_data(chn);
str[max-1] = 0;
}
while (max) {
@@ -269,10 +226,10 @@
if (*p == '\n')
break;
- p = buffer_wrap_add(chn->buf, p + 1);
+ p = b_next(chn->buf, p);
}
if (ret > 0 && ret < len &&
- (ret < chn->buf->o || channel_may_recv(chn)) &&
+ (ret < co_data(chn) || channel_may_recv(chn)) &&
*(str-1) != '\n' &&
!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
ret = 0;
@@ -314,13 +271,13 @@
*/
int co_getblk_nc(const struct channel *chn, const char **blk1, size_t *len1, const char **blk2, size_t *len2)
{
- if (unlikely(chn->buf->o == 0)) {
+ if (unlikely(co_data(chn) == 0)) {
if (chn->flags & CF_SHUTW)
return -1;
return 0;
}
- return b_getblk_nc(chn->buf, blk1, len1, blk2, len2, 0, chn->buf->o);
+ return b_getblk_nc(chn->buf, blk1, len1, blk2, len2, 0, co_data(chn));
}
/* Gets one text line out of a channel's output buffer from a stream interface.
@@ -380,22 +337,22 @@
char **blk1, size_t *len1,
char **blk2, size_t *len2)
{
- if (unlikely(chn->buf->i == 0)) {
+ if (unlikely(ci_data(chn) == 0)) {
if (chn->flags & CF_SHUTR)
return -1;
return 0;
}
- if (unlikely(chn->buf->p + chn->buf->i > b_wrap(chn->buf))) {
- *blk1 = chn->buf->p;
- *len1 = b_wrap(chn->buf) - chn->buf->p;
- *blk2 = b_orig(chn->buf);
- *len2 = chn->buf->i - *len1;
+ if (unlikely(ci_head(chn) + ci_data(chn) > c_wrap(chn))) {
+ *blk1 = ci_head(chn);
+ *len1 = c_wrap(chn) - ci_head(chn);
+ *blk2 = c_orig(chn);
+ *len2 = ci_data(chn) - *len1;
return 2;
}
- *blk1 = chn->buf->p;
- *len1 = chn->buf->i;
+ *blk1 = ci_head(chn);
+ *len1 = ci_data(chn);
return 1;
}