ThreadedSocketService
Object Hierarchy:
Description:
[ Version ( since = "2.22" ) ]
public class ThreadedSocketService : SocketService
A `GThreadedSocketService` is a simple subclass of [class@Gio.
SocketService] that handles incoming connections by creating a worker thread and dispatching the connection to it by emitting the [
signal@Gio.ThreadedSocketService:GThreadedSocketService:run
signal] in the new thread.
The signal handler may perform blocking I/O and need not return until the connection is closed.
The service is implemented using a thread pool, so there is a limited amount of threads available to serve incoming requests. The service automatically stops the [class@Gio.SocketService] from accepting new connections when all threads are busy.
As with [class@Gio.SocketService], you may connect to [signal@Gio.ThreadedSocketService:GThreadedSocketService:run
], or subclass
and override the default handler.
Example: Threaded socket service:
Use telnet localhost 1024
or telnet localhost 1025
to create connections
Send "shutdown" to close the server.
public class Source : Object {
public uint16 port { private set; get; }
public Source (uint16 port) {
this.port = port;
}
}
public static int main (string[] args) {
MainLoop loop = new MainLoop ();
// Create a new SocketService:
ThreadedSocketService service = new ThreadedSocketService (10);
try {
// Listen on port 1024 and 1025.
// Source is used as source-identifier.
service.add_inet_port (1024, new Source (1024));
service.add_inet_port (1025, new Source (1025));
} catch (Error e) {
print ("Error: %s\n", e.message);
return 0;
}
// Used to shutdown the program:
Cancellable cancellable = new Cancellable ();
cancellable.cancelled.connect (() => {
service.stop ();
loop.quit ();
});
// The run signal is emitted in a worker thread:
service.run.connect ((connection, source_object) => {
try {
Source source = source_object as Source;
print ("Accepted! (Source: %d)\n", source.port);
DataInputStream istream = new DataInputStream (connection.input_stream);
DataOutputStream ostream = new DataOutputStream (connection.output_stream);
// Get the received message:
string message = istream.read_line (null, cancellable);
message._strip ();
print ("Received: %s\n", message);
// Response:
ostream.put_string (message, cancellable);
ostream.put_byte ('\n', cancellable);
if (message == "shutdown") {
cancellable.cancel ();
return true;
}
} catch (Error e) {
print ("Error: %s\n", e.message);
}
return false;
});
service.start ();
loop.run ();
return 0;
}
valac --pkg gio-2.0 GLib.ThreadedSocketService.vala