BUG/MEDIUM: lua: many errors when we try to send data with the channel API

First we allow to use the reserved size to write the data that
will be sent. The reserved size remain guaranty because the
writed data will be sent quickly and the reserved room we be
again avalaible.

This permits to guaranty that the function send always have
avalaible space to send data (except if it cannot connect to
the server).

The function buffer_replace2 works only on contiguous buffer.
This patch also detects if the required size is contiguous.
If it not the case we realign the buffer.
diff --git a/src/hlua.c b/src/hlua.c
index 7f937f7..40eedb5 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -2209,11 +2209,33 @@
 		}
 	}
 
-	max = channel_recv_limit(chn->chn) - buffer_len(chn->chn->buf);
+	/* the writed data will be immediatly sent, so we can check
+	 * the avalaible space without taking in account the reserve.
+	 * The reserve is guaranted for the processing of incoming
+	 * data, because the buffer will be flushed.
+	 */
+	max = chn->chn->buf->size - buffer_len(chn->chn->buf);
+
+	/* If there are no space avalaible, and the output buffer is empty.
+	 * in this case, we cannot add more data, so we cannot yield,
+	 * we return the amount of copyied data.
+	 */
+	if (max == 0 && chn->chn->buf->o == 0)
+		return 1;
+
+	/* Adjust the real required length. */
 	if (max > len - l)
 		max = len - l;
 
+	/* The buffer avalaible size may be not contiguous. This test
+	 * detects a non contiguous buffer and realign it.
+	 */
+	if (buffer_contig_space(chn->chn->buf) < max)
+		buffer_slow_realign(chn->chn->buf);
+
+	/* Copy input data in the buffer. */
 	max = buffer_replace2(chn->chn->buf, chn->chn->buf->p, chn->chn->buf->p, str+l, max);
+
 	/* buffer replace considers that the input part is filled.
 	 * so, I must forward these new data in the output part.
 	 */
@@ -2223,14 +2245,14 @@
 	lua_pop(L, 1);
 	lua_pushinteger(L, l);
 
-	max = channel_recv_limit(chn->chn) - buffer_len(chn->chn->buf);
-	if (max == 0 && chn->chn->buf->o == 0) {
-		/* There are no space avalaible, and the output buffer is empty.
-		 * in this case, we cannot add more data, so we cannot yield,
-		 * we return the amount of copyied data.
-		 */
+	/* If there are no space avalaible, and the output buffer is empty.
+	 * in this case, we cannot add more data, so we cannot yield,
+	 * we return the amount of copyied data.
+	 */
+	max = chn->chn->buf->size - buffer_len(chn->chn->buf);
+	if (max == 0 && chn->chn->buf->o == 0)
 		return 1;
-	}
+
 	if (l < len) {
 		/* If we are waiting for space in the response buffer, we
 		 * must set the flag WAKERESWR. This flag required the task