1 #ifndef __GSK_IO_H_ 2 #define __GSK_IO_H_ 3 4 #include <glib-object.h> 5 #include "gskhook.h" 6 #include "gskerror.h" 7 8 G_BEGIN_DECLS 9 10 /* --- typedefs --- */ 11 typedef struct _GskIO GskIO; 12 typedef struct _GskIOClass GskIOClass; 13 14 typedef gboolean (*GskIOHookFunc) (GskIO *io, 15 gpointer data); 16 /* --- type macros --- */ 17 GType gsk_io_get_type(void) G_GNUC_CONST; 18 #define GSK_TYPE_IO (gsk_io_get_type ()) 19 #define GSK_IO(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GSK_TYPE_IO, GskIO)) 20 #define GSK_IO_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GSK_TYPE_IO, GskIOClass)) 21 #define GSK_IO_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GSK_TYPE_IO, GskIOClass)) 22 #define GSK_IS_IO(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GSK_TYPE_IO)) 23 #define GSK_IS_IO_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GSK_TYPE_IO)) 24 25 typedef enum 26 { 27 GSK_IO_ERROR_NONE, 28 GSK_IO_ERROR_INIT, 29 GSK_IO_ERROR_CONNECT, 30 GSK_IO_ERROR_OPEN, 31 GSK_IO_ERROR_READ, 32 GSK_IO_ERROR_WRITE, 33 GSK_IO_ERROR_POLL_READ, 34 GSK_IO_ERROR_POLL_WRITE, 35 GSK_IO_ERROR_SHUTDOWN_READ, 36 GSK_IO_ERROR_SHUTDOWN_WRITE, 37 GSK_IO_ERROR_CLOSE, 38 GSK_IO_ERROR_SYNC, 39 GSK_IO_ERROR_POLL 40 } GskIOErrorCause; 41 const char * gsk_io_error_cause_to_string (GskIOErrorCause cause); 42 43 /* --- structures --- */ 44 struct _GskIOClass 45 { 46 GObjectClass object_class; 47 /* --- signals (do not override, usually) --- */ 48 /* Emitted after the connection is made. */ 49 void (*on_connect) (GskIO *io); 50 void (*on_error) (GskIO *io); 51 52 /* --- virtuals --- */ 53 gboolean (*open) (GskIO *io, 54 GError **error); 55 void (*set_poll_read) (GskIO *io, 56 gboolean do_poll); 57 void (*set_poll_write) (GskIO *io, 58 gboolean do_poll); 59 gboolean (*shutdown_read) (GskIO *io, 60 GError **error); 61 gboolean (*shutdown_write) (GskIO *io, 62 GError **error); 63 void (*close) (GskIO *io); 64 }; 65 struct _GskIO 66 { 67 GObject object; 68 69 guint is_connecting : 1; 70 guint is_open : 1; 71 guint open_failed : 1; 72 guint shutdown_on_error : 1; 73 74 /*< private >*/ 75 guint in_idle_ready_thread : 1; 76 77 /*< public read-only >*/ 78 guint error_cause : 4; 79 80 /*< public read-write >*/ 81 guint print_errors : 1; 82 83 /*< public read-only >*/ 84 GError *error; 85 86 /*< private >*/ 87 /* hooks */ 88 GskHook read_hook; 89 GskHook write_hook; 90 }; 91 /* --- prototypes --- */ 92 93 /* public */ 94 #define GSK_IO_READ_HOOK(io) ((GskHook*) &GSK_IO (io)->read_hook) 95 #define GSK_IO_WRITE_HOOK(io) ((GskHook*) &GSK_IO (io)->write_hook) 96 #define gsk_io_block_read(io) gsk_hook_block (GSK_IO_READ_HOOK (io)) 97 #define gsk_io_block_write(io) gsk_hook_block (GSK_IO_WRITE_HOOK (io)) 98 #define gsk_io_unblock_read(io) gsk_hook_unblock (GSK_IO_READ_HOOK (io)) 99 #define gsk_io_unblock_write(io) gsk_hook_unblock (GSK_IO_WRITE_HOOK (io)) 100 #define gsk_io_has_read_hook(io) gsk_hook_is_trapped (GSK_IO_READ_HOOK (io)) 101 #define gsk_io_has_write_hook(io) gsk_hook_is_trapped (GSK_IO_WRITE_HOOK (io)) 102 #define gsk_io_trap_readable(io, func, shutdown_func, data, destroy) \ 103 gsk_hook_trap (GSK_IO_READ_HOOK (io), \ 104 (GskHookFunc) func, (GskHookFunc) shutdown_func, data, destroy) 105 #define gsk_io_trap_writable(io, func, shutdown_func, data, destroy) \ 106 gsk_hook_trap (GSK_IO_WRITE_HOOK (io), \ 107 (GskHookFunc) func, (GskHookFunc) shutdown_func, data, destroy) 108 #define gsk_io_untrap_readable(io) gsk_hook_untrap (GSK_IO_READ_HOOK (io)) 109 #define gsk_io_untrap_writable(io) gsk_hook_untrap (GSK_IO_WRITE_HOOK (io)) 110 void gsk_io_shutdown (GskIO *io, 111 GError **error); 112 113 /* shutdown the io in various ways */ 114 #define gsk_io_read_shutdown(io, error) gsk_hook_shutdown (GSK_IO_READ_HOOK (io), error) 115 #define gsk_io_write_shutdown(io, error) gsk_hook_shutdown (GSK_IO_WRITE_HOOK (io), error) 116 void gsk_io_close (GskIO *io); 117 118 119 /* protected: do notifications of the usual events */ 120 #define gsk_io_notify_ready_to_read(io) gsk_hook_notify (GSK_IO_READ_HOOK (io)) 121 #define gsk_io_notify_ready_to_write(io) gsk_hook_notify (GSK_IO_WRITE_HOOK (io)) 122 #define gsk_io_notify_read_shutdown(io) gsk_hook_notify_shutdown (GSK_IO_READ_HOOK (io)) 123 #define gsk_io_notify_write_shutdown(io) gsk_hook_notify_shutdown (GSK_IO_WRITE_HOOK (io)) 124 void gsk_io_notify_shutdown (GskIO *io); 125 void gsk_io_notify_connected (GskIO *io); 126 127 /* flags */ 128 #define gsk_io_get_is_connecting(io) _GSK_IO_TEST_FIELD (io, is_connecting) 129 #define gsk_io_get_is_readable(io) _GSK_IO_TEST_READ_FLAG (io, IS_AVAILABLE) 130 #define gsk_io_get_is_writable(io) _GSK_IO_TEST_WRITE_FLAG (io, IS_AVAILABLE) 131 #define gsk_io_get_is_read_shutting_down(io) GSK_HOOK_TEST_SHUTTING_DOWN (GSK_IO_READ_HOOK (io)) 132 #define gsk_io_get_is_write_shutting_down(io) GSK_HOOK_TEST_SHUTTING_DOWN (GSK_IO_WRITE_HOOK (io)) 133 #define gsk_io_get_never_partial_reads(io) _GSK_IO_TEST_FIELD (io, never_partial_reads) 134 #define gsk_io_get_never_partial_writes(io) _GSK_IO_TEST_FIELD (io, never_partial_writes) 135 #define gsk_io_get_never_blocks_write(io) GSK_HOOK_TEST_NEVER_BLOCKS (GSK_IO_WRITE_HOOK (io)) 136 #define gsk_io_get_never_blocks_read(io) GSK_HOOK_TEST_NEVER_BLOCKS (GSK_IO_READ_HOOK (io)) 137 #define gsk_io_get_idle_notify_write(io) GSK_HOOK_TEST_IDLE_NOTIFY (GSK_IO_WRITE_HOOK (io)) 138 #define gsk_io_get_idle_notify_read(io) GSK_HOOK_TEST_IDLE_NOTIFY (GSK_IO_READ_HOOK (io)) 139 #define gsk_io_get_is_open(io) _GSK_IO_TEST_FIELD (io, is_open) 140 #define gsk_io_get_shutdown_on_error(io) _GSK_IO_TEST_FIELD (io, shutdown_on_error) 141 142 /* protected */ 143 #define gsk_io_set_idle_notify_read(io,v) gsk_hook_set_idle_notify (GSK_IO_READ_HOOK (io),v) 144 #define gsk_io_set_idle_notify_write(io,v) gsk_hook_set_idle_notify (GSK_IO_WRITE_HOOK (io),v) 145 #define gsk_io_mark_is_connecting(io) _GSK_IO_MARK_FIELD (io, is_connecting) 146 #define gsk_io_mark_is_readable(io) _GSK_IO_SET_READ_FLAG (io, IS_AVAILABLE) 147 #define gsk_io_mark_is_writable(io) _GSK_IO_SET_WRITE_FLAG (io, IS_AVAILABLE) 148 #define gsk_io_mark_never_partial_reads(io) _GSK_IO_MARK_FIELD (io, never_partial_reads) 149 #define gsk_io_mark_never_partial_writes(io) _GSK_IO_MARK_FIELD (io, never_partial_writes) 150 #define gsk_io_mark_shutdown_on_error(io) _GSK_IO_MARK_FIELD (io, shutdown_on_error) 151 #define gsk_io_mark_never_blocks_write(io) gsk_hook_mark_never_blocks (GSK_IO_WRITE_HOOK (io)) 152 #define gsk_io_mark_never_blocks_read(io) gsk_hook_mark_never_blocks (GSK_IO_READ_HOOK (io)) 153 #define gsk_io_mark_idle_notify_write(io) gsk_hook_mark_idle_notify (GSK_IO_WRITE_HOOK (io)) 154 #define gsk_io_mark_idle_notify_read(io) gsk_hook_mark_idle_notify (GSK_IO_READ_HOOK (io)) 155 #define gsk_io_mark_is_open(io) _GSK_IO_MARK_FIELD (io, is_open) 156 #define gsk_io_clear_is_readable(io) _GSK_IO_CLEAR_READ_FLAG (io, IS_AVAILABLE) 157 #define gsk_io_clear_is_writable(io) _GSK_IO_CLEAR_WRITE_FLAG (io, IS_AVAILABLE) 158 #define gsk_io_clear_never_partial_reads(io) _GSK_IO_CLEAR_FIELD (io, never_partial_reads) 159 #define gsk_io_clear_never_partial_writes(io) _GSK_IO_CLEAR_FIELD (io, never_partial_writes) 160 #define gsk_io_clear_idle_notify_write(io) gsk_hook_clear_idle_notify (GSK_IO_WRITE_HOOK (io)) 161 #define gsk_io_clear_idle_notify_read(io) gsk_hook_clear_idle_notify (GSK_IO_READ_HOOK (io)) 162 #define gsk_io_clear_is_open(io) _GSK_IO_CLEAR_FIELD (io, is_open) 163 #define gsk_io_clear_shutdown_on_error(io) _GSK_IO_CLEAR_FIELD (io, shutdown_on_error) 164 #define gsk_io_is_polling_for_read(io) gsk_hook_get_last_poll_state (GSK_IO_READ_HOOK (io)) 165 #define gsk_io_is_polling_for_write(io) gsk_hook_get_last_poll_state (GSK_IO_WRITE_HOOK (io)) 166 167 168 169 170 /* --- error handling --- */ 171 172 /* these functions are for use by derived classes only. */ 173 void gsk_io_set_error (GskIO *io, 174 GskIOErrorCause cause, 175 GskErrorCode error_code, 176 const char *format, 177 ...) G_GNUC_PRINTF(4,5); 178 void gsk_io_set_gerror (GskIO *io, 179 GskIOErrorCause cause, 180 GError *error); 181 182 void gsk_io_set_default_print_errors (gboolean print_errors); 183 184 /*< private >*/ 185 void _gsk_io_stop_idle_ready (GskIO *io); 186 void _gsk_io_make_idle_ready (GskIO *io); 187 void _gsk_io_nonblocking_init (); 188 189 /* implementation bits */ 190 #define _GSK_IO_TEST_FIELD(io, field) (GSK_IO (io)->field != 0) 191 #define _GSK_IO_TEST_READ_FLAG(io, flag) GSK_HOOK_TEST_FLAG(GSK_IO_READ_HOOK(io), flag) 192 #define _GSK_IO_TEST_WRITE_FLAG(io, flag) GSK_HOOK_TEST_FLAG(GSK_IO_WRITE_HOOK(io), flag) 193 #define _GSK_IO_MARK_FIELD(io, field) G_STMT_START{ GSK_IO (io)->field = 1; }G_STMT_END 194 #define _GSK_IO_MARK_READ_FLAG(io, flag) GSK_HOOK_MARK_FLAG(GSK_IO_READ_HOOK(io), flag) 195 #define _GSK_IO_MARK_WRITE_FLAG(io, flag) GSK_HOOK_MARK_FLAG(GSK_IO_WRITE_HOOK(io), flag) 196 #define _GSK_IO_CLEAR_FIELD(io, field) G_STMT_START{ GSK_IO (io)->field = 0; }G_STMT_END 197 #define _GSK_IO_CLEAR_READ_FLAG(io, flag) GSK_HOOK_CLEAR_FLAG(GSK_IO_READ_HOOK(io), flag) 198 #define _GSK_IO_CLEAR_WRITE_FLAG(io, flag) GSK_HOOK_CLEAR_FLAG(GSK_IO_WRITE_HOOK(io), flag) 199 200 #ifndef GSK_DISABLE_DEPRECATED 201 #define _GSK_IO_SET_READ_FLAG _GSK_IO_MARK_READ_FLAG 202 #define _GSK_IO_SET_WRITE_FLAG _GSK_IO_MARK_WRITE_FLAG 203 #endif 204 205 G_END_DECLS 206 207 #endif 208