network.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #ifndef NETWORK_H_
  2. #define NETWORK_H_
  3. #include <sys/select.h>
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #include <unistd.h>
  7. /* Opaque address structure. */
  8. struct sock_addr;
  9. /**
  10. * network_accept(fd, callback, cookie):
  11. * Asynchronously accept a connection on the socket ${fd}, which must be
  12. * already marked as listening and non-blocking. When a connection has been
  13. * accepted or an error occurs, invoke ${callback}(${cookie}, s) where s is
  14. * the accepted connection or -1 on error. Return a cookie which can be
  15. * passed to network_accept_cancel() in order to cancel the accept.
  16. */
  17. void * network_accept(int, int (*)(void *, int), void *);
  18. /**
  19. * network_accept_cancel(cookie):
  20. * Cancel the connection accept for which the cookie ${cookie} was returned
  21. * by network_accept(). Do not invoke the callback associated with the accept.
  22. */
  23. void network_accept_cancel(void *);
  24. /**
  25. * network_connect(sas, callback, cookie):
  26. * Iterate through the addresses in ${sas}, attempting to create and connect
  27. * a non-blocking socket. Once connected, invoke ${callback}(${cookie}, s)
  28. * where s is the connected socket; upon fatal error or if there are no
  29. * addresses remaining to attempt, invoke ${callback}(${cookie}, -1). Return
  30. * a cookie which can be passed to network_connect_cancel() in order to cancel
  31. * the connection attempt.
  32. */
  33. void * network_connect(struct sock_addr * const *,
  34. int (*)(void *, int), void *);
  35. /**
  36. * network_connect_bind(sas, sa_b, callback, cookie):
  37. * Iterate through the addresses in ${sas}, attempting to create and connect
  38. * a non-blocking socket. If ${sa_b} is not NULL, bind the socket to ${sa_b}
  39. * immediately after creating it. Once connected, invoke
  40. * ${callback}(${cookie}, s) where s is the connected socket; upon fatal error
  41. * or if there are no addresses remaining to attempt, invoke
  42. * ${callback}(${cookie}, -1). Return a cookie which can be passed to
  43. * network_connect_cancel() in order to cancel the connection attempt.
  44. */
  45. void * network_connect_bind(struct sock_addr * const *,
  46. const struct sock_addr *, int (*)(void *, int), void *);
  47. /**
  48. * network_connect_timeo(sas, timeo, callback, cookie):
  49. * Behave as network_connect(), but wait a duration of at most ${timeo} for
  50. * each address which is being attempted.
  51. */
  52. void * network_connect_timeo(struct sock_addr * const *, const struct timeval *,
  53. int (*)(void *, int), void *);
  54. /**
  55. * network_connect_cancel(cookie):
  56. * Cancel the connection attempt for which ${cookie} was returned by
  57. * network_connect(). Do not invoke the associated callback.
  58. */
  59. void network_connect_cancel(void *);
  60. /**
  61. * network_read(fd, buf, buflen, minread, callback, cookie):
  62. * Asynchronously read up to ${buflen} bytes of data from ${fd} into ${buf}.
  63. * When at least ${minread} bytes have been read or on error, invoke
  64. * ${callback}(${cookie}, lenread), where lenread is 0 on EOF or -1 on error,
  65. * and the number of bytes read (between ${minread} and ${buflen} inclusive)
  66. * otherwise. Return a cookie which can be passed to network_read_cancel() in
  67. * order to cancel the read.
  68. */
  69. void * network_read(int, uint8_t *, size_t, size_t,
  70. int (*)(void *, ssize_t), void *);
  71. /**
  72. * network_read_cancel(cookie):
  73. * Cancel the buffer read for which the cookie ${cookie} was returned by
  74. * network_read(). Do not invoke the callback associated with the read.
  75. */
  76. void network_read_cancel(void *);
  77. /**
  78. * network_write(fd, buf, buflen, minwrite, callback, cookie):
  79. * Asynchronously write up to ${buflen} bytes of data from ${buf} to ${fd}.
  80. * When at least ${minwrite} bytes have been written or on error, invoke
  81. * ${callback}(${cookie}, lenwrit), where lenwrit is -1 on error and the
  82. * number of bytes written (between ${minwrite} and ${buflen} inclusive)
  83. * otherwise. Return a cookie which can be passed to network_write_cancel() in
  84. * order to cancel the write.
  85. */
  86. void * network_write(int, const uint8_t *, size_t, size_t,
  87. int (*)(void *, ssize_t), void *);
  88. /**
  89. * network_write_cancel(cookie):
  90. * Cancel the buffer write for which the cookie ${cookie} was returned by
  91. * network_write(). Do not invoke the callback associated with the write.
  92. */
  93. void network_write_cancel(void *);
  94. #endif /* !NETWORK_H_ */