[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua Socket
- From: gary ng <garyng2000@...>
- Date: Fri, 20 Jul 2007 23:41:30 -0700 (PDT)
I am writing a binding for xyssl which has its own
send/receive routine, thus need to redo these
buffering. And I am thinking, shouldn't this magical
"*a" and "*l"(which obviously requires buffering) be
either implemented in lua or as a seperate module(then
wrap around the underlying object that does the raw
read/write) ? That way, the underlying C module don't
need to change and it would be much easier to
customize and reuse ?
--- Duck <duck@roaming.ath.cx> wrote:
> The change I had in mind is _very_ simple. For
> example, if you use "=x" to
> mean "read a line of data terminated by x", then
> this should do the trick.
> There are three lines of executable code changed and
> just one added:
>
> --- buffer.old 2007-07-21 15:45:41.000000000 +1000
> +++ buffer.c 2007-07-21 15:53:55.000000000 +1000
> @@ -15,3 +15,3 @@
> static int recvraw(p_buffer buf, size_t wanted,
> luaL_Buffer *b);
> -static int recvline(p_buffer buf, luaL_Buffer *b);
> +static int recvline(p_buffer buf, luaL_Buffer *b,
> char eol, int eatcr);
> static int recvall(p_buffer buf, luaL_Buffer *b);
> @@ -121,3 +121,4 @@
> const char *p= luaL_optstring(L, 2, "*l");
> - if (p[0] == '*' && p[1] == 'l') err =
> recvline(buf, &b);
> + if (p[0] == '*' && p[1] == 'l') err =
> recvline(buf, &b, '\n', 1);
> + else if (p[0] == '=') err = recvline(buf,
> &b, p[1], 0);
> else if (p[0] == '*' && p[1] == 'a') err =
> recvall(buf, &b);
> @@ -216,6 +217,6 @@
>
>
/*-------------------------------------------------------------------------*\
> -* Reads a line terminated by a CR LF pair or just
> by a LF. The CR and LF
> -* are not returned by the function and are
> discarded from the buffer
> +* Reads a line terminated by an eol char,
> optionally "eating" CRs. The
> +* eol char is not returned by the function and is
> discarded from the buffer.
>
>
\*-------------------------------------------------------------------------*/
> -static int recvline(p_buffer buf, luaL_Buffer *b) {
> +static int recvline(p_buffer buf, luaL_Buffer *b,
> char eol, int eatcr) {
> int err = IO_DONE;
> @@ -225,5 +226,5 @@
> pos = 0;
> - while (pos < count && data[pos] != '\n') {
> + while (pos < count && data[pos] != eol) {
> /* we ignore all \r's */
> - if (data[pos] != '\r') luaL_putchar(b,
> data[pos]);
> + if (eatcr == 0 || data[pos] != '\r')
> luaL_putchar(b, data[pos]);
> pos++;
>
____________________________________________________________________________________
Looking for a deal? Find great prices on flights and hotels with Yahoo! FareChase.
http://farechase.yahoo.com/