1 #ifndef IOSTREAM_PUMP_H
2 #define IOSTREAM_PUMP_H
3 
4 /* iostream-pump
5    =============
6 
7    This construct pumps data from istream to ostream asynchronously.
8 
9    The pump requires you to provide completion callback. The completion
10    callback is called with success parameter to indicate whether it ended
11    with error.
12 
13    The istream and ostream are reffed on creation and unreffed
14    on unref.
15  */
16 
17 struct istream;
18 struct ostream;
19 struct ioloop;
20 struct iostream_pump;
21 
22 enum iostream_pump_status {
23 	/* pump succeeded - EOF received from istream and all output was
24 	   written successfully to ostream. */
25 	IOSTREAM_PUMP_STATUS_INPUT_EOF,
26 	/* pump failed - istream returned an error */
27 	IOSTREAM_PUMP_STATUS_INPUT_ERROR,
28 	/* pump failed - ostream returned an error */
29 	IOSTREAM_PUMP_STATUS_OUTPUT_ERROR,
30 };
31 
32 /* The callback is called once when the pump succeeds or fails due to
33    iostreams. (It's not called if pump is destroyed.) */
34 typedef void iostream_pump_callback_t(enum iostream_pump_status status,
35 				      void *context);
36 
37 struct iostream_pump *
38 iostream_pump_create(struct istream *input, struct ostream *output);
39 
40 struct istream *iostream_pump_get_input(struct iostream_pump *pump);
41 struct ostream *iostream_pump_get_output(struct iostream_pump *pump);
42 
43 void iostream_pump_start(struct iostream_pump *pump);
44 void iostream_pump_stop(struct iostream_pump *pump);
45 
46 void iostream_pump_ref(struct iostream_pump *pump);
47 void iostream_pump_unref(struct iostream_pump **_pump);
48 void iostream_pump_destroy(struct iostream_pump **_pump);
49 
50 void iostream_pump_set_completion_callback(struct iostream_pump *pump,
51 					   iostream_pump_callback_t *callback,
52 					   void *context);
53 #define iostream_pump_set_completion_callback(pump, callback, context) \
54 	iostream_pump_set_completion_callback(pump, \
55 		(iostream_pump_callback_t *)callback, TRUE ? context : \
56 		CALLBACK_TYPECHECK(callback, \
57 			void (*)(enum iostream_pump_status, typeof(context))))
58 
59 /* Returns TRUE if the pump is currently only writing to the ostream. The input
60    listener has been removed either because the ostream buffer is full or
61    because the istream already returned EOF. This function can also be called
62    from the completion callback in error conditions. */
63 bool iostream_pump_is_waiting_output(struct iostream_pump *pump);
64 
65 void iostream_pump_switch_ioloop_to(struct iostream_pump *pump,
66 				    struct ioloop *ioloop);
67 void iostream_pump_switch_ioloop(struct iostream_pump *pump);
68 
69 #endif
70