Cancellable
Object Hierarchy:
GLib.Cancellable
GLib.Cancellable
GLib.Cancellable
GLib.Object
GLib.Object
GLib.Object->GLib.Cancellable
Description:
[ CCode ( type_id = "g_cancellable_get_type ()" ) ]public class Cancellable : Object
GCancellable is a thread-safe operation cancellation stack used throughout GIO to allow for cancellation of synchronous and asynchronous
operations.
Example: Socket listener, async:
Note:
Use telnet localhost 1024
or telnet localhost 1025
to create connections
Note:
Send "shutdown" to close the server.
public class Server : Object { private Cancellable cancellable; private class Source : Object { public uint16 port { private set ; get ; } public Source (uint16 port) { this .port = port; } } private async void worker_func (SocketConnection connection, Source source) { try { DataInputStream istream = new DataInputStream (connection.input_stream); DataOutputStream ostream = new DataOutputStream (connection.output_stream); // Get the received message: string message = yield istream.read_line_async (Priority.DEFAULT, this .cancellable); message._strip (); print ("Received: %s \n " , message); // Response: ostream.put_string (message, this .cancellable); ostream.put_byte (' \n ' , this .cancellable); if (message == "shutdown" ) { this .cancellable.cancel (); } } catch (Error e) { print ("Error: %s \n " , e.message); } } public async void listen (Cancellable cancellable) { this .cancellable = cancellable; try { // Listen on port 1024 and 1025. // Source is used as source-identifier. SocketListener listener = new SocketListener (); listener.add_inet_port (1024 , new Source (1024 )); listener.add_inet_port (1025 , new Source (1025 )); // Wait for connections: while (true ) { // Get the connection: Object source_obj; SocketConnection connection = yield listener.accept_async (this .cancellable, out source_obj); // Identify the source: Source source = source_obj as Source; assert (source != null ); print ("Accepted! (Source: %d ) \n " , source.port); // Register a worker: worker_func.begin (connection, source); } } catch (Error e) { print ("Error: %s \n " , e.message); } } public static int main (string [] args) { MainLoop loop = new MainLoop (); // Used to shutdown the program: Cancellable cancellable = new Cancellable (); cancellable.cancelled.connect (() => { loop.quit (); }); new Server ().listen.begin (cancellable); loop.run (); return 0 ; } }
valac --pkg gio-2.0 GLib.SocketListener.accept_async.vala
Content:
Static methods:
Creation methods:
Methods:
public void cancel ()
Will set this to cancelled, and will
emit the cancelled signal.
public ulong connect (owned Func <Cancellable > callback)
Convenience function to connect to the
cancelled signal.
public void disconnect (ulong handler_id)
Disconnects a handler from a cancellable instance similar to
disconnect .
public int get_fd ()
Gets the file descriptor for a cancellable job.
public bool is_cancelled ()
Checks if a cancellable job has been cancelled.
public bool make_pollfd (PollFD pollfd)
Creates a PollFD corresponding to
this ; this can be passed to poll and used to poll for cancellation.
public void pop_current ()
Pops this off the cancellable stack
(verifying that this is on the top of the stack).
public void push_current ()
Pushes this onto the cancellable
stack.
public void release_fd ()
public void reset ()
Resets this to its uncancelled
state.
public bool set_error_if_cancelled () throws IOError
If the this is cancelled, sets the
error to notify that the operation was cancelled.
public CancellableSource source_new ()
Signals:
Inherited Members:
All known members inherited from class GLib.Object
@get
@new
@ref
@set
add_toggle_ref
add_weak_pointer
bind_property
connect
constructed
disconnect
dispose
dup_data
dup_qdata
force_floating
freeze_notify
get_class
get_data
get_property
get_qdata
get_type
getv
interface_find_property
interface_install_property
interface_list_properties
is_floating
new_valist
new_with_properties
newv
notify
notify_property
ref_count
ref_sink
remove_toggle_ref
remove_weak_pointer
replace_data
replace_qdata
set_data
set_data_full
set_property
set_qdata
set_qdata_full
set_valist
setv
steal_data
steal_qdata
thaw_notify
unref
watch_closure
weak_ref
weak_unref