io.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. #include "timeout.h"
  18. /* IO error codes */
  19. enum {
  20. IO_DONE = 0, /* operation completed successfully */
  21. IO_TIMEOUT = -1, /* operation timed out */
  22. IO_CLOSED = -2, /* the connection has been closed */
  23. IO_UNKNOWN = -3
  24. };
  25. /* interface to error message function */
  26. typedef const char *(*p_error) (
  27. void *ctx, /* context needed by send */
  28. int err /* error code */
  29. );
  30. /* interface to send function */
  31. typedef int (*p_send) (
  32. void *ctx, /* context needed by send */
  33. const char *data, /* pointer to buffer with data to send */
  34. size_t count, /* number of bytes to send from buffer */
  35. size_t *sent, /* number of bytes sent uppon return */
  36. p_timeout tm /* timeout control */
  37. );
  38. /* interface to recv function */
  39. typedef int (*p_recv) (
  40. void *ctx, /* context needed by recv */
  41. char *data, /* pointer to buffer where data will be writen */
  42. size_t count, /* number of bytes to receive into buffer */
  43. size_t *got, /* number of bytes received uppon return */
  44. p_timeout tm /* timeout control */
  45. );
  46. /* IO driver definition */
  47. typedef struct t_io_ {
  48. void *ctx; /* context needed by send/recv */
  49. p_send send; /* send function pointer */
  50. p_recv recv; /* receive function pointer */
  51. p_error error; /* strerror function */
  52. } t_io;
  53. typedef t_io *p_io;
  54. void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx);
  55. const char *io_strerror(int err);
  56. #endif /* IO_H */