I've been playing around with networking in Vala but I can't find any documentation on how to read a signal packet or tell when one packet ends and another one starts in the incoming data stream.
Here is my code so far:
bool on_incoming_connection (SocketService socket, SocketConnection conn, Object? unuesd) {
stdout.printf ("Got incoming connection\n");
// Process the request asynchronously
process_request.begin (conn, socket);
return true;
}
async void process_request (SocketConnection conn, SocketService socket) {
try {
var dis = new DataInputStream (conn.input_stream);
var dos = new DataOutputStream (conn.output_stream);
uint8 byte = 0;
do {
byte = dis.read_byte();
stdout.printf ("Got: %d\n", byte);
} while (byte != 0);
// The connection is automatically closed because conn is destroyed upon function return.
} catch (Error e) {
stderr.printf ("%s\n", e.message);
}
}
void main () {
MainLoop loop = new MainLoop ();
var service = new SocketService ();
Cancellable cancellable = new Cancellable ();
cancellable.cancelled.connect (() => {
service.stop ();
loop.quit ();
});
try {
service.add_inet_port (33333, null);
} catch (Error e) {
stderr.printf ("%s\n", e.message);
}
service.incoming.connect (on_incoming_connection);
service.start ();
loop.run ();
}