class Gio::SocketService

Overview

A #GSocketService is an object that represents a service that is provided to the network or over local sockets. When a new connection is made to the service the #GSocketService::incoming signal is emitted.

A #GSocketService is a subclass of #GSocketListener and you need to add the addresses you want to accept connections on with the #GSocketListener APIs.

There are two options for implementing a network service based on #GSocketService. The first is to create the service using g_socket_service_new() and to connect to the #GSocketService::incoming signal. The second is to subclass #GSocketService and override the default signal handler implementation.

In either case, the handler must immediately return, or else it will block additional incoming connections from being serviced. If you are interested in writing connection handlers that contain blocking code then see #GThreadedSocketService.

The socket service runs on the main loop of the [thread-default context][g-main-context-push-thread-default-context] of the thread it is created in, and is not threadsafe in general. However, the calls to start and stop the service are thread-safe so these can be used from threads that handle incoming clients.

Direct Known Subclasses

Defined in:

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

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class Gio::SocketListener

accept(cancellable : Gio::Cancellable?) : Gio::SocketConnection accept, accept_async(cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil accept_async, accept_finish(result : Gio::AsyncResult) : Gio::SocketConnection accept_finish, accept_socket(cancellable : Gio::Cancellable?) : Gio::Socket accept_socket, accept_socket_async(cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil accept_socket_async, accept_socket_finish(result : Gio::AsyncResult) : Gio::Socket accept_socket_finish, add_address(address : Gio::SocketAddress, type : Gio::SocketType, protocol : Gio::SocketProtocol, source_object : GObject::Object?) : Bool add_address, add_any_inet_port(source_object : GObject::Object?) : UInt16 add_any_inet_port, add_inet_port(port : UInt16, source_object : GObject::Object?) : Bool add_inet_port, add_socket(socket : Gio::Socket, source_object : GObject::Object?) : Bool add_socket, backlog=(listen_backlog : Int32) : Nil backlog=, close : Nil close, event_signal event_signal, listen_backlog : Int32 listen_backlog, listen_backlog=(value : Int32) : Int32 listen_backlog=

Constructor methods inherited from class Gio::SocketListener

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

Class methods inherited from class Gio::SocketListener

g_type : UInt64 g_type

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 #GSocketService 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().

New services are created active, there is no need to call g_socket_service_start(), unless g_socket_service_stop() has been called before.


def self.new(*, active : Bool? = nil, 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 active=(value : Bool) : Bool #

def active? : Bool #

def incoming_signal #

def is_active : Bool #

Check whether the service is active or not. An active service will accept new clients that connect, while a non-active service will let connecting clients queue up until the service is started.


def start : Nil #

Restarts the service, i.e. start accepting connections from the added sockets when the mainloop runs. This only needs to be called after the service has been stopped from g_socket_service_stop().

This call is thread-safe, so it may be called from a thread handling an incoming client request.


def stop : Nil #

Stops the service, i.e. stops accepting connections from the added sockets when the mainloop runs.

This call is thread-safe, so it may be called from a thread handling an incoming client request.

Note that this only stops accepting new connections; it does not close the listening sockets, and you can call g_socket_service_start() again later to begin listening again. To close the listening sockets, call g_socket_listener_close(). (This will happen automatically when the #GSocketService is finalized.)

This must be called before calling g_socket_listener_close() as the socket service will start accepting connections immediately when a new socket is added.