SocketService

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 "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 "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 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.

Constructors

this
this(GSocketService* gSocketService)

Sets our main struct and passes it to the parent class

this
this()

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(). Since 2.22

Members

Functions

addOnIncoming
void addOnIncoming(bool delegate(GSocketConnection*, GObject*, SocketService) dlg, ConnectFlags connectFlags)

The ::incoming signal is emitted when a new incoming connection to service needs to be handled. The handler must initiate the handling of connection, but may not block; in essence, asynchronous operations must be used. connection will be unreffed once the signal handler returns, so you need to ref it yourself if you are planning to use it. TRUE to stop other handlers from being called Since 2.22 See Also GThreadedSocketService, GSocketListener.

getSocketServiceStruct
GSocketService* getSocketServiceStruct()

Get the main Gtk struct

getStruct
void* getStruct()

the main Gtk struct as a void*

isActive
int isActive()

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. Since 2.22 Signal Details The "incoming" signal gboolean user_function (GSocketService *service, GSocketConnection *connection, GObject *source_object, gpointer user_data) : Run Last The ::incoming signal is emitted when a new incoming connection to service needs to be handled. The handler must initiate the handling of connection, but may not block; in essence, asynchronous operations must be used. connection will be unreffed once the signal handler returns, so you need to ref it yourself if you are planning to use it. Since 2.22

setStruct
void setStruct(GObject* obj)
Undocumented in source. Be warned that the author may not have intended to support it.
start
void start()

Starts the service, i.e. start 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. Since 2.22

stop
void stop()

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. Since 2.22

Static functions

callBackIncoming
gboolean callBackIncoming(GSocketService* serviceStruct, GSocketConnection* connection, GObject* sourceObject, SocketService _socketService)
Undocumented in source. Be warned that the author may not have intended to support it.

Variables

connectedSignals
int[string] connectedSignals;
gSocketService
GSocketService* gSocketService;

the main Gtk struct

onIncomingListeners
bool delegate(GSocketConnection*, GObject*, SocketService)[] onIncomingListeners;
Undocumented in source.

Inherited Members

From SocketListener

gSocketListener
GSocketListener* gSocketListener;

the main Gtk struct

getSocketListenerStruct
GSocketListener* getSocketListenerStruct()

Get the main Gtk struct

getStruct
void* getStruct()

the main Gtk struct as a void*

setStruct
void setStruct(GObject* obj)
Undocumented in source. Be warned that the author may not have intended to support it.
addSocket
int addSocket(Socket socket, ObjectG sourceObject)

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. Since 2.22

addAddress
int addAddress(SocketAddress address, GSocketType type, GSocketProtocol protocol, ObjectG sourceObject, SocketAddress effectiveAddress)

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-NULL 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. Since 2.22

addInetPort
int addInetPort(ushort port, ObjectG sourceObject)

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. Since 2.22

addAnyInetPort
ushort addAnyInetPort(ObjectG sourceObject)

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. Since 2.24

accept
SocketConnection accept(ObjectG sourceObject, Cancellable cancellable)

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 NULL 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 NULL, 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. Since 2.22

acceptAsync
void acceptAsync(Cancellable cancellable, GAsyncReadyCallback callback, void* userData)

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_socket() to get the result of the operation. Since 2.22

acceptFinish
SocketConnection acceptFinish(AsyncResultIF result, ObjectG sourceObject)

Finishes an async accept operation. See g_socket_listener_accept_async() Since 2.22

acceptSocket
Socket acceptSocket(ObjectG sourceObject, Cancellable cancellable)

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 NULL 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 NULL, 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. Since 2.22

acceptSocketAsync
void acceptSocketAsync(Cancellable cancellable, GAsyncReadyCallback callback, void* userData)

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. Since 2.22

acceptSocketFinish
Socket acceptSocketFinish(AsyncResultIF result, ObjectG sourceObject)

Finishes an async accept operation. See g_socket_listener_accept_socket_async() Since 2.22

close
void close()

Closes all the sockets in the listener. Since 2.22

setBacklog
void setBacklog(int listenBacklog)

Sets the listen backlog on the sockets in the listener. See g_socket_set_listen_backlog() for details Since 2.22

Meta