You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stream.h 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #ifndef _GPXE_STREAM_H
  2. #define _GPXE_STREAM_H
  3. /** @file
  4. *
  5. * Stream API
  6. */
  7. #include <stdint.h>
  8. #include <gpxe/socket.h>
  9. struct stream_application;
  10. struct stream_connection;
  11. /** Stream applicatin-layer operations */
  12. struct stream_application_operations {
  13. /**
  14. * Connection established
  15. *
  16. * @v app Stream application
  17. */
  18. void ( * connected ) ( struct stream_application *app );
  19. /**
  20. * Connection closed
  21. *
  22. * @v app Stream application
  23. * @v rc Error code, if any
  24. *
  25. * This is called when the connection is closed for any
  26. * reason, including timeouts or aborts. The error code
  27. * contains the negative error number, if the closure is due
  28. * to an error, or zero for a normal close.
  29. *
  30. * When closed() is called, the application no longer has a
  31. * valid stream connection. Note that connected() may not
  32. * have been called before closed(), if the close is due to an
  33. * error during connection setup.
  34. */
  35. void ( * closed ) ( struct stream_application *app, int rc );
  36. /**
  37. * Transmit data
  38. *
  39. * @v app Stream application
  40. * @v data Temporary data buffer
  41. * @v len Length of temporary data buffer
  42. *
  43. * The application should transmit whatever it currently wants
  44. * to send using stream_send(). If retransmissions are
  45. * required, senddata() will be called again and the
  46. * application must regenerate the data. The easiest way to
  47. * implement this is to ensure that senddata() never changes
  48. * the application's state.
  49. *
  50. * The application may use the temporary data buffer to
  51. * construct the data to be sent. Note that merely filling
  52. * the buffer will do nothing; the application must call
  53. * stream_send() in order to actually transmit the data. Use
  54. * of the buffer is not compulsory; the application may call
  55. * stream_send() on any block of data.
  56. */
  57. void ( * senddata ) ( struct stream_application *app,
  58. void *data, size_t len );
  59. /**
  60. * Transmitted data acknowledged
  61. *
  62. * @v app Stream application
  63. * @v len Length of acknowledged data
  64. *
  65. * @c len is guaranteed to not exceed the outstanding amount
  66. * of unacknowledged data.
  67. */
  68. void ( * acked ) ( struct stream_application *app, size_t len );
  69. /**
  70. * Receive new data
  71. *
  72. * @v app Stream application
  73. * @v data Data
  74. * @v len Length of data
  75. */
  76. void ( * newdata ) ( struct stream_application *app,
  77. void *data, size_t len );
  78. };
  79. /** Stream connection-layer operations */
  80. struct stream_connection_operations {
  81. /**
  82. * Bind to local address
  83. *
  84. * @v conn Stream connection
  85. * @v local Local address
  86. * @ret rc Return status code
  87. */
  88. int ( * bind ) ( struct stream_connection *conn,
  89. struct sockaddr *local );
  90. /**
  91. * Connect to remote address
  92. *
  93. * @v conn Stream connection
  94. * @v peer Remote address
  95. * @ret rc Return status code
  96. *
  97. * This initiates the connection. If the connection succeeds,
  98. * the application's connected() method will be called. If
  99. * the connection fails (e.g. due to a timeout), the
  100. * application's closed() method will be called with an
  101. * appropriate error code.
  102. */
  103. int ( * connect ) ( struct stream_connection *conn,
  104. struct sockaddr *peer );
  105. /**
  106. * Close connection
  107. *
  108. * @v conn Stream connection
  109. */
  110. void ( * close ) ( struct stream_connection *conn );
  111. /**
  112. * Send data via connection
  113. *
  114. * @v conn Stream connection
  115. * @v data Data to send
  116. * @v len Length of data
  117. * @ret rc Return status code
  118. *
  119. * This method should be called only in the context of an
  120. * application's senddata() method.
  121. */
  122. int ( * send ) ( struct stream_connection *conn,
  123. const void *data, size_t len );
  124. /**
  125. * Notify connection that data is available to send
  126. *
  127. * @v conn Stream connection
  128. * @ret rc Return status code
  129. *
  130. * This will cause the connection to call the application's
  131. * senddata() method. It should be called when the
  132. * application acquires new data to send as a result of
  133. * something external to the data stream (e.g. when iSCSI is
  134. * asked to issue a new command on an otherwise-idle
  135. * connection). Most applications will not need to call this
  136. * method.
  137. */
  138. int ( * kick ) ( struct stream_connection *conn );
  139. };
  140. /** A stream application */
  141. struct stream_application {
  142. /** Stream connection, if any
  143. *
  144. * This will be NULL if the stream does not currently have a
  145. * valid connection.
  146. */
  147. struct stream_connection *conn;
  148. /** Stream application-layer operations */
  149. struct stream_application_operations *op;
  150. };
  151. /** A stream connection */
  152. struct stream_connection {
  153. /** Stream application, if any
  154. *
  155. * This will be NULL if the stream does not currently have a
  156. * valid application.
  157. */
  158. struct stream_application *app;
  159. /** Stream connection-layer operations */
  160. struct stream_connection_operations *op;
  161. };
  162. extern void stream_associate ( struct stream_application *app,
  163. struct stream_connection *conn );
  164. extern void stream_connected ( struct stream_connection *conn );
  165. extern void stream_closed ( struct stream_connection *conn, int rc );
  166. extern void stream_senddata ( struct stream_connection *conn,
  167. void *data, size_t len );
  168. extern void stream_acked ( struct stream_connection *conn, size_t len );
  169. extern void stream_newdata ( struct stream_connection *conn,
  170. void *data, size_t len );
  171. extern int stream_bind ( struct stream_application *app,
  172. struct sockaddr *local );
  173. extern int stream_connect ( struct stream_application *app,
  174. struct sockaddr *peer );
  175. extern void stream_close ( struct stream_application *app );
  176. extern int stream_send ( struct stream_application *app,
  177. const void *data, size_t len );
  178. extern int stream_kick ( struct stream_application *app );
  179. #endif /* _GPXE_STREAM_H */