class Gio::SocketListener

Overview

A #GSocketListener is an object that keeps track of a set of server sockets and helps you accept sockets from any of the socket, either sync or async.

Add addresses and ports to listen on using g_socket_listener_add_address() and g_socket_listener_add_inet_port(). These will be listened on until g_socket_listener_close() is called. Dropping your final reference to the #GSocketListener will not cause g_socket_listener_close() to be called implicitly, as some references to the #GSocketListener may be held internally.

If you want to implement a network server, also look at #GSocketService and #GThreadedSocketService which are subclasses of #GSocketListener that make this even easier.

Direct Known Subclasses

Defined in:

lib/gi-crystal/src/auto/gio-2.0/socket_listener.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class GObject::Object

bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding bind_property, bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding bind_property_full, data(key : String) : Pointer(Void)? data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, notify(property_name : String) : Nil notify, notify_by_pspec(pspec : GObject::ParamSpec) : Nil notify_by_pspec, notify_signal notify_signal, property(property_name : String, value : _) : Nil property, qdata(quark : UInt32) : Pointer(Void)? qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void)?) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void)? steal_data, steal_qdata(quark : UInt32) : Pointer(Void)? steal_qdata, thaw_notify : Nil thaw_notify, to_unsafe : Pointer(Void) to_unsafe, watch_closure(closure : GObject::Closure) : Nil watch_closure

Constructor methods inherited from class GObject::Object

cast(obj : GObject::Object) : self cast, cast?(obj : GObject::Object) : self? cast?, new(pointer : Pointer(Void), transfer : GICrystal::Transfer)
new
new
, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv

Class methods inherited from class GObject::Object

compat_control(what : UInt64, data : Pointer(Void)?) : UInt64 compat_control, g_type : UInt64 g_type, interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec interface_find_property, interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec) interface_list_properties

Constructor Detail

def self.new : self #

Creates a new #GSocketListener with no sockets to listen for. New listeners can be added with e.g. g_socket_listener_add_address() or g_socket_listener_add_inet_port().


def self.new(*, listen_backlog : Int32? = nil) #

Class Method Detail

def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.


Instance Method Detail

def accept(cancellable : Gio::Cancellable?) : Gio::SocketConnection #

Blocks waiting for a client to connect to any of the sockets added to the listener. Returns a #GSocketConnection for the socket that was accepted.

If @source_object is not nil it will be filled out with the source object specified when the corresponding socket or address was added to the listener.

If @cancellable is not nil, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.


def accept_async(cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

This is the asynchronous version of g_socket_listener_accept().

When the operation is finished @callback will be called. You can then call g_socket_listener_accept_finish() to get the result of the operation.


def accept_finish(result : Gio::AsyncResult) : Gio::SocketConnection #

Finishes an async accept operation. See g_socket_listener_accept_async()


def accept_socket(cancellable : Gio::Cancellable?) : Gio::Socket #

Blocks waiting for a client to connect to any of the sockets added to the listener. Returns the #GSocket that was accepted.

If you want to accept the high-level #GSocketConnection, not a #GSocket, which is often the case, then you should use g_socket_listener_accept() instead.

If @source_object is not nil it will be filled out with the source object specified when the corresponding socket or address was added to the listener.

If @cancellable is not nil, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.


def accept_socket_async(cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

This is the asynchronous version of g_socket_listener_accept_socket().

When the operation is finished @callback will be called. You can then call g_socket_listener_accept_socket_finish() to get the result of the operation.


def accept_socket_finish(result : Gio::AsyncResult) : Gio::Socket #

Finishes an async accept operation. See g_socket_listener_accept_socket_async()


def add_address(address : Gio::SocketAddress, type : Gio::SocketType, protocol : Gio::SocketProtocol, source_object : GObject::Object?) : Bool #

Creates a socket of type @type and protocol @protocol, binds it to @address and adds it to the set of sockets we're accepting sockets from.

Note that adding an IPv6 address, depending on the platform, may or may not result in a listener that also accepts IPv4 connections. For more deterministic behavior, see g_socket_listener_add_inet_port().

@source_object will be passed out in the various calls to accept to identify this particular source, which is useful if you're listening on multiple addresses and do different things depending on what address is connected to.

If successful and @effective_address is non-nil then it will be set to the address that the binding actually occurred at. This is helpful for determining the port number that was used for when requesting a binding to port 0 (ie: "any port"). This address, if requested, belongs to the caller and must be freed.

Call g_socket_listener_close() to stop listening on @address; this will not be done automatically when you drop your final reference to @listener, as references may be held internally.


def add_any_inet_port(source_object : GObject::Object?) : UInt16 #

Listens for TCP connections on any available port number for both IPv6 and IPv4 (if each is available).

This is useful if you need to have a socket for incoming connections but don't care about the specific port number.

@source_object will be passed out in the various calls to accept to identify this particular source, which is useful if you're listening on multiple addresses and do different things depending on what address is connected to.


def add_inet_port(port : UInt16, source_object : GObject::Object?) : Bool #

Helper function for g_socket_listener_add_address() that creates a TCP/IP socket listening on IPv4 and IPv6 (if supported) on the specified port on all interfaces.

@source_object will be passed out in the various calls to accept to identify this particular source, which is useful if you're listening on multiple addresses and do different things depending on what address is connected to.

Call g_socket_listener_close() to stop listening on @port; this will not be done automatically when you drop your final reference to @listener, as references may be held internally.


def add_socket(socket : Gio::Socket, source_object : GObject::Object?) : Bool #

Adds @socket to the set of sockets that we try to accept new clients from. The socket must be bound to a local address and listened to.

@source_object will be passed out in the various calls to accept to identify this particular source, which is useful if you're listening on multiple addresses and do different things depending on what address is connected to.

The @socket will not be automatically closed when the @listener is finalized unless the listener held the final reference to the socket. Before GLib 2.42, the @socket was automatically closed on finalization of the @listener, even if references to it were held elsewhere.


def backlog=(listen_backlog : Int32) : Nil #

Sets the listen backlog on the sockets in the listener. This must be called before adding any sockets, addresses or ports to the #GSocketListener (for example, by calling g_socket_listener_add_inet_port()) to be effective.

See g_socket_set_listen_backlog() for details


def close : Nil #

Closes all the sockets in the listener.


def event_signal #

def listen_backlog : Int32 #

def listen_backlog=(value : Int32) : Int32 #