study-system/client/src/connection.vala

34 lines
1.1 KiB
Vala

namespace StudySystemClient {
public class Connection {
public signal void response_received(uint8[] response);
private TlsClientConnection tls_client;
public Connection(string cert_dir) throws Error {
var loopback = new InetAddress.loopback(SocketFamily.IPV6);
var host = new InetSocketAddress(loopback, 12888);
var db_type = TlsBackend.get_default().get_file_database_type();
var ca_path = cert_dir + "/ca.pem";
var db = Object.new(db_type, "anchors", ca_path) as TlsDatabase;
var cert_path = cert_dir + "/client.pem";
var cert = new TlsCertificate.from_file(cert_path);
var plain_client = new SocketClient();
var plain_connection = plain_client.connect(host);
tls_client = TlsClientConnection.new(plain_connection, host);
tls_client.set_database(db);
tls_client.set_certificate(cert);
tls_client.handshake();
}
public async void send(uint8[] message) throws Error {
yield tls_client.output_stream.write_async(message);
var response = new uint8[1024];
var len = yield tls_client.input_stream.read_async(response);
response_received(response[0:len]);
}
}
}