Moving on to beta2.

This commit is contained in:
Diego Nehab
2004-07-01 03:32:09 +00:00
parent 7aaba59909
commit 7115c12fbc
16 changed files with 391 additions and 443 deletions

View File

@ -33,8 +33,7 @@ static int sendraw(p_buf buf, const char *data, size_t count, size_t *sent);
/*-------------------------------------------------------------------------*\
* Initializes module
\*-------------------------------------------------------------------------*/
int buf_open(lua_State *L)
{
int buf_open(lua_State *L) {
(void) L;
return 0;
}
@ -42,8 +41,7 @@ int buf_open(lua_State *L)
/*-------------------------------------------------------------------------*\
* Initializes C structure
\*-------------------------------------------------------------------------*/
void buf_init(p_buf buf, p_io io, p_tm tm)
{
void buf_init(p_buf buf, p_io io, p_tm tm) {
buf->first = buf->last = 0;
buf->io = io;
buf->tm = tm;
@ -52,13 +50,11 @@ void buf_init(p_buf buf, p_io io, p_tm tm)
/*-------------------------------------------------------------------------*\
* object:send() interface
\*-------------------------------------------------------------------------*/
int buf_meth_send(lua_State *L, p_buf buf)
{
int buf_meth_send(lua_State *L, p_buf buf) {
int top = lua_gettop(L);
size_t total = 0;
int arg, err = IO_DONE;
p_tm tm = buf->tm;
tm_markstart(tm);
p_tm tm = tm_markstart(buf->tm);
for (arg = 2; arg <= top; arg++) { /* first arg is socket object */
size_t sent, count;
const char *data = luaL_optlstring(L, arg, NULL, &count);
@ -69,7 +65,7 @@ int buf_meth_send(lua_State *L, p_buf buf)
/* check if there was an error */
if (err != IO_DONE) {
lua_pushnil(L);
io_pusherror(L, err);
io_pusherror(L, buf->io, err);
lua_pushnumber(L, total);
} else {
lua_pushnumber(L, total);
@ -78,7 +74,7 @@ int buf_meth_send(lua_State *L, p_buf buf)
}
#ifdef LUASOCKET_DEBUG
/* push time elapsed during operation as the last return value */
lua_pushnumber(L, (tm_gettime() - tm_getstart(tm))/1000.0);
lua_pushnumber(L, tm_gettime() - tm_getstart(tm));
#endif
return lua_gettop(L) - top;
}
@ -86,13 +82,11 @@ int buf_meth_send(lua_State *L, p_buf buf)
/*-------------------------------------------------------------------------*\
* object:receive() interface
\*-------------------------------------------------------------------------*/
int buf_meth_receive(lua_State *L, p_buf buf)
{
int buf_meth_receive(lua_State *L, p_buf buf) {
int err = IO_DONE, top = lua_gettop(L);
p_tm tm = buf->tm;
p_tm tm = tm_markstart(buf->tm);
luaL_Buffer b;
luaL_buffinit(L, &b);
tm_markstart(tm);
/* receive all patterns */
if (!lua_isnumber(L, 2)) {
static const char *patternnames[] = {"*l", "*a", NULL};
@ -107,7 +101,7 @@ int buf_meth_receive(lua_State *L, p_buf buf)
/* check if there was an error */
if (err != IO_DONE) {
luaL_pushresult(&b);
io_pusherror(L, err);
io_pusherror(L, buf->io, err);
lua_pushvalue(L, -2);
lua_pushnil(L);
lua_replace(L, -4);
@ -118,7 +112,7 @@ int buf_meth_receive(lua_State *L, p_buf buf)
}
#ifdef LUASOCKET_DEBUG
/* push time elapsed during operation as the last return value */
lua_pushnumber(L, (tm_gettime() - tm_getstart(tm))/1000.0);
lua_pushnumber(L, tm_gettime() - tm_getstart(tm));
#endif
return lua_gettop(L) - top;
}
@ -126,8 +120,7 @@ int buf_meth_receive(lua_State *L, p_buf buf)
/*-------------------------------------------------------------------------*\
* Determines if there is any data in the read buffer
\*-------------------------------------------------------------------------*/
int buf_isempty(p_buf buf)
{
int buf_isempty(p_buf buf) {
return buf->first >= buf->last;
}
@ -137,16 +130,14 @@ int buf_isempty(p_buf buf)
/*-------------------------------------------------------------------------*\
* Sends a block of data (unbuffered)
\*-------------------------------------------------------------------------*/
static
int sendraw(p_buf buf, const char *data, size_t count, size_t *sent)
{
static int sendraw(p_buf buf, const char *data, size_t count, size_t *sent) {
p_io io = buf->io;
p_tm tm = buf->tm;
size_t total = 0;
int err = IO_DONE;
while (total < count && (err == IO_DONE || err == IO_RETRY)) {
while (total < count && err == IO_DONE) {
size_t done;
err = io->send(io->ctx, data+total, count-total, &done, tm_get(tm));
err = io->send(io->ctx, data+total, count-total, &done, tm);
total += done;
}
*sent = total;
@ -156,12 +147,10 @@ int sendraw(p_buf buf, const char *data, size_t count, size_t *sent)
/*-------------------------------------------------------------------------*\
* Reads a fixed number of bytes (buffered)
\*-------------------------------------------------------------------------*/
static
int recvraw(p_buf buf, size_t wanted, luaL_Buffer *b)
{
int err = IO_DONE;
static int recvraw(p_buf buf, size_t wanted, luaL_Buffer *b) {
int err = IO_DONE;
size_t total = 0;
while (total < wanted && (err == IO_DONE || err == IO_RETRY)) {
while (total < wanted && err == IO_DONE) {
size_t count; const char *data;
err = buf_get(buf, &data, &count);
count = MIN(count, wanted - total);
@ -175,11 +164,9 @@ int recvraw(p_buf buf, size_t wanted, luaL_Buffer *b)
/*-------------------------------------------------------------------------*\
* Reads everything until the connection is closed (buffered)
\*-------------------------------------------------------------------------*/
static
int recvall(p_buf buf, luaL_Buffer *b)
{
static int recvall(p_buf buf, luaL_Buffer *b) {
int err = IO_DONE;
while (err == IO_DONE || err == IO_RETRY) {
while (err == IO_DONE) {
const char *data; size_t count;
err = buf_get(buf, &data, &count);
luaL_addlstring(b, data, count);
@ -193,11 +180,9 @@ int recvall(p_buf buf, luaL_Buffer *b)
* 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
\*-------------------------------------------------------------------------*/
static
int recvline(p_buf buf, luaL_Buffer *b)
{
static int recvline(p_buf buf, luaL_Buffer *b) {
int err = IO_DONE;
while (err == IO_DONE || err == IO_RETRY) {
while (err == IO_DONE) {
size_t count, pos; const char *data;
err = buf_get(buf, &data, &count);
pos = 0;
@ -219,9 +204,7 @@ int recvline(p_buf buf, luaL_Buffer *b)
* Skips a given number of bytes from read buffer. No data is read from the
* transport layer
\*-------------------------------------------------------------------------*/
static
void buf_skip(p_buf buf, size_t count)
{
static void buf_skip(p_buf buf, size_t count) {
buf->first += count;
if (buf_isempty(buf))
buf->first = buf->last = 0;
@ -231,15 +214,13 @@ void buf_skip(p_buf buf, size_t count)
* Return any data available in buffer, or get more data from transport layer
* if buffer is empty
\*-------------------------------------------------------------------------*/
static
int buf_get(p_buf buf, const char **data, size_t *count)
{
static int buf_get(p_buf buf, const char **data, size_t *count) {
int err = IO_DONE;
p_io io = buf->io;
p_tm tm = buf->tm;
if (buf_isempty(buf)) {
size_t got;
err = io->recv(io->ctx, buf->data, BUF_SIZE, &got, tm_get(tm));
err = io->recv(io->ctx, buf->data, BUF_SIZE, &got, tm);
buf->first = 0;
buf->last = got;
}
@ -247,4 +228,3 @@ int buf_get(p_buf buf, const char **data, size_t *count)
*data = buf->data + buf->first;
return err;
}