2003-06-26 20:47:49 +02:00
|
|
|
/*=========================================================================*\
|
|
|
|
* Input/Output abstraction
|
|
|
|
* LuaSocket toolkit
|
|
|
|
*
|
|
|
|
* RCS ID: $Id$
|
|
|
|
\*=========================================================================*/
|
2003-05-25 03:54:13 +02:00
|
|
|
#include "io.h"
|
|
|
|
|
2003-06-26 20:47:49 +02:00
|
|
|
/*=========================================================================*\
|
|
|
|
* Exported functions
|
|
|
|
\*=========================================================================*/
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Initializes C structure
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-01 05:32:09 +02:00
|
|
|
void io_init(p_io io, p_send send, p_recv recv, p_geterr geterr, void *ctx) {
|
2003-05-25 03:54:13 +02:00
|
|
|
io->send = send;
|
|
|
|
io->recv = recv;
|
2004-07-01 05:32:09 +02:00
|
|
|
io->geterr = geterr;
|
2003-05-25 03:54:13 +02:00
|
|
|
io->ctx = ctx;
|
|
|
|
}
|
2003-06-26 20:47:49 +02:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Translate error codes to Lua
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-01 05:32:09 +02:00
|
|
|
const char *io_strerror(int code) {
|
2003-06-26 20:47:49 +02:00
|
|
|
switch (code) {
|
2004-01-18 01:04:20 +01:00
|
|
|
case IO_DONE: return NULL;
|
|
|
|
case IO_CLOSED: return "closed";
|
2004-07-01 05:32:09 +02:00
|
|
|
case IO_TIMEOUT: return "timeout";
|
|
|
|
case IO_CLIPPED: return "clipped";
|
2004-01-18 01:04:20 +01:00
|
|
|
default: return "unknown error";
|
2003-06-26 20:47:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-18 01:04:20 +01:00
|
|
|
/*-------------------------------------------------------------------------*\
|
2004-07-01 05:32:09 +02:00
|
|
|
* Push error message from code or from driver
|
2004-01-18 01:04:20 +01:00
|
|
|
\*-------------------------------------------------------------------------*/
|
2004-07-01 05:32:09 +02:00
|
|
|
void io_pusherror(lua_State *L, p_io io, int code)
|
2004-01-18 01:04:20 +01:00
|
|
|
{
|
2004-07-01 05:32:09 +02:00
|
|
|
const char *err = NULL;
|
|
|
|
if (code < IO_USER) err = io_strerror(code);
|
|
|
|
else err = io->geterr(io->ctx, code);
|
2004-01-18 01:04:20 +01:00
|
|
|
if (err) lua_pushstring(L, err);
|
|
|
|
else lua_pushnil(L);
|
|
|
|
}
|