warning("failed to close data input: %s", e.message);
}
try {
if (this._dout != null)
this._dout.close();
} catch (Error e) {
warning("failed to close data output: %s", e.message);
}
try {
if (this._tls_conn != null)
this._tls_conn.close();
} catch (Error e) {
warning("failed to close TLS connection: %s", e.message);
}
try {
if (this._sock_conn != null)
this._sock_conn.close();
} catch (Error e) {
warning("failed to close connection: %s", e.message);
}
this._din = null;
this._dout = null;
this._sock_conn = null;
this._tls_conn = null;
this._socket = null;
this.peer_certificate = null;
}
/**
* send:
* Possibly blocking
*
* @param: instance of Packet
**/
public async void send(Packet pkt) {
string to_send = pkt.to_string() + "\n";
debug("send data: %s", to_send);
GLib.assert(this._dout != null);
try {
this._dout.put_string(to_send);
} catch (IOError e) {
warning("failed to send message: %s", e.message);
// TODO disconnect?
}
}
/**
* receive:
* Try to receive some data from channel
*
* @return false if channel was closed, true otherwise
*/
public bool receive() {
size_t line_len;
string data = null;
GLib.assert(this._din != null);
try {
// read line up to a newline
data = this._din.read_upto("\n", -1, out line_len, null);
// expecting \n
this._din.read_byte();
} catch (IOError ie) {
warning("I/O error: %s", ie.message);
}
if (data == null) {
debug("connection closed?");
return false;
}
vdebug("received line: %s", data);
Packet pkt = Packet.new_from_data(data);
if (pkt == null) {
warning("failed to build packet from data");
// data was received, hence connection is still alive
return true;
}
this.handle_packet(pkt);
return true;
}
private bool _io_ready(uint flags) {
debug("check for IO, conditions: 0x%x", flags);
bool res = this.receive();
if (res == false) {
// disconnected
this.disconnected();
}
return res;
}
private void handle_packet(Packet pkt) {
// debug("handle packet of type: %s", pkt.pkt_type);
if (pkt.pkt_type == Packet.ENCRYPTED) {
warning("received packet with eplicit encryption, this usually indicates a protocol version < 6 type packet, such pacckets are no longer supported, dropping..");
} else {
// signal that we got a packet
this.packet_received(pkt);
}
}
public signal void disconnected ();
public signal void packet_received (Packet pkt);
private InetSocketAddress _isa = null;
private SocketConnection _sock_conn = null;
private TlsConnection _tls_conn = null;
private DataOutputStream _dout = null;
private DataInputStream _din = null;
private uint _srcid = 0;
private Socket _socket = null;
public TlsCertificate peer_certificate = null;
public DeviceChannel (InetAddress host, uint port) {
this._isa = new InetSocketAddress (host, (uint16) port);
warning ("failed to close data input: %s", e.message);
}
try {
if (this._dout != null)
this._dout.close ();
} catch (Error e) {
warning ("failed to close data output: %s", e.message);
}
try {
if (this._tls_conn != null)
this._tls_conn.close ();
} catch (Error e) {
warning ("failed to close TLS connection: %s", e.message);
}
try {
if (this._sock_conn != null)
this._sock_conn.close ();
} catch (Error e) {
warning ("failed to close connection: %s", e.message);
}
this._din = null;
this._dout = null;
this._sock_conn = null;
this._tls_conn = null;
this._socket = null;
this.peer_certificate = null;
}
/**
* send:
* Possibly blocking
*
* @param: instance of Packet
**/
public async void send (Packet pkt) {
string to_send = pkt.to_string () + "\n";
debug ("send data: %s", to_send);
GLib.assert (this._dout != null);
try {
this._dout.put_string (to_send);
} catch (IOError e) {
warning ("failed to send message: %s", e.message);
// TODO disconnect?
}
}
/**
* receive:
* Try to receive some data from channel
*
* @return false if channel was closed, true otherwise
*/
public bool receive () {
size_t line_len;
string data = null;
GLib.assert (this._din != null);
try {
// read line up to a newline
data = this._din.read_upto ("\n", -1, out line_len, null);
// expecting \n
this._din.read_byte ();
} catch (IOError ie) {
warning ("I/O error: %s", ie.message);
}
if (data == null) {
debug ("connection closed?");
return false;
}
vdebug ("received line: %s", data);
Packet pkt = Packet.new_from_data (data);
if (pkt == null) {
warning ("failed to build packet from data");
// data was received, hence connection is still alive
return true;
}
this.handle_packet (pkt);
return true;
}
private bool _io_ready (uint flags) {
debug ("check for IO, conditions: 0x%x", flags);
bool res = this.receive ();
if (res == false) {
// disconnected
this.disconnected ();
}
return res;
}
private void handle_packet (Packet pkt) {
// debug("handle packet of type: %s", pkt.pkt_type);
if (pkt.pkt_type == Packet.ENCRYPTED) {
warning ("received packet with eplicit encryption, this usually indicates a protocol version < 6 type packet, such pacckets are no longer supported, dropping..");