| 1 | #ifndef IO_H |
| 2 | #define IO_H |
| 3 | /*=========================================================================*\ |
| 4 | * Input/Output abstraction |
| 5 | * LuaSocket toolkit |
| 6 | * |
| 7 | * This module defines the interface that LuaSocket expects from the |
| 8 | * transport layer for streamed input/output. The idea is that if any |
| 9 | * transport implements this interface, then the buffer.c functions |
| 10 | * automatically work on it. |
| 11 | * |
| 12 | * The module socket.h implements this interface, and thus the module tcp.h |
| 13 | * is very simple. |
| 14 | \*=========================================================================*/ |
| 15 | #include <stdio.h> |
| 16 | #include "lua.h" |
| 17 | |
| 18 | #include "timeout.h" |
| 19 | |
| 20 | /* IO error codes */ |
| 21 | enum { |
| 22 | IO_DONE = 0, /* operation completed successfully */ |
| 23 | IO_TIMEOUT = -1, /* operation timed out */ |
| 24 | IO_CLOSED = -2, /* the connection has been closed */ |
| 25 | IO_UNKNOWN = -3 |
| 26 | }; |
| 27 | |
| 28 | /* interface to error message function */ |
| 29 | typedef const char *(*p_error) ( |
| 30 | void *ctx, /* context needed by send */ |
| 31 | int err /* error code */ |
| 32 | ); |
| 33 | |
| 34 | /* interface to send function */ |
| 35 | typedef int (*p_send) ( |
| 36 | void *ctx, /* context needed by send */ |
| 37 | const char *data, /* pointer to buffer with data to send */ |
| 38 | size_t count, /* number of bytes to send from buffer */ |
| 39 | size_t *sent, /* number of bytes sent uppon return */ |
| 40 | p_timeout tm /* timeout control */ |
| 41 | ); |
| 42 | |
| 43 | /* interface to recv function */ |
| 44 | typedef int (*p_recv) ( |
| 45 | void *ctx, /* context needed by recv */ |
| 46 | char *data, /* pointer to buffer where data will be writen */ |
| 47 | size_t count, /* number of bytes to receive into buffer */ |
| 48 | size_t *got, /* number of bytes received uppon return */ |
| 49 | p_timeout tm /* timeout control */ |
| 50 | ); |
| 51 | |
| 52 | /* IO driver definition */ |
| 53 | typedef struct t_io_ { |
| 54 | void *ctx; /* context needed by send/recv */ |
| 55 | p_send send; /* send function pointer */ |
| 56 | p_recv recv; /* receive function pointer */ |
| 57 | p_error error; /* strerror function */ |
| 58 | } t_io; |
| 59 | typedef t_io *p_io; |
| 60 | |
| 61 | void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx); |
| 62 | const char *io_strerror(int err); |
| 63 | |
| 64 | #endif /* IO_H */ |
| 65 | |
| 66 | |