| 1 |
using Gee; |
| 2 |
|
| 3 |
public interface Xmpp.WriteNodeFunc : Object { |
| 4 |
public abstract async void write_stanza(XmppStream stream, StanzaNode node, int io_priority = Priority.DEFAULT, Cancellable? cancellable = null) throws IOError; |
| 5 |
} |
| 6 |
|
| 7 |
public abstract class Xmpp.IoXmppStream : XmppStream { |
| 8 |
private IOStream? stream; |
| 9 |
internal Cancellable cancellable; |
| 10 |
internal StanzaReader? reader; |
| 11 |
internal StanzaWriter? writer; |
| 12 |
|
| 13 |
internal WriteNodeFunc? write_obj = null; |
| 14 |
|
| 15 |
protected IoXmppStream(Jid remote_name, Cancellable? cancellable = null) { |
| 16 |
base(remote_name); |
| 17 |
this.cancellable = cancellable ?? new Cancellable(); |
| 18 |
} |
| 19 |
|
| 20 |
public void cancel() { |
| 21 |
cancellable.cancel(); |
| 22 |
} |
| 23 |
|
| 24 |
public override async void disconnect() throws IOError { |
| 25 |
disconnected = true; |
| 26 |
cancel(); |
| 27 |
if (writer == null || reader == null || stream == null) { |
| 28 |
throw new IOError.CLOSED("trying to disconnect, but no stream open"); |
| 29 |
} |
| 30 |
log.str("OUT", "</stream:stream>", this); |
| 31 |
yield writer.write("</stream:stream>", Priority.LOW, new Cancellable()); |
| 32 |
yield stream.close_async(); |
| 33 |
} |
| 34 |
|
| 35 |
public void reset_stream(IOStream stream) { |
| 36 |
this.stream = stream; |
| 37 |
reader = new StanzaReader.for_stream(stream.input_stream, cancellable); |
| 38 |
writer = new StanzaWriter.for_stream(stream.output_stream, cancellable); |
| 39 |
require_setup(); |
| 40 |
} |
| 41 |
|
| 42 |
public override async StanzaNode read() throws IOError { |
| 43 |
StanzaReader? reader = this.reader; |
| 44 |
if (reader == null) throw new IOError.NOT_CONNECTED("trying to read, but no stream open"); |
| 45 |
StanzaNode node = yield ((!)reader).read_node(); |
| 46 |
log.node("IN", node, this); |
| 47 |
return node; |
| 48 |
} |
| 49 |
|
| 50 |
[Version (deprecated = true, deprecated_since = "0.1", replacement = "write_async")] |
| 51 |
public override void write(StanzaNode node, int io_priority = Priority.DEFAULT) { |
| 52 |
write_async.begin(node, io_priority, null, (obj, res) => { |
| 53 |
try { |
| 54 |
write_async.end(res); |
| 55 |
} catch (Error e) { |
| 56 |
warning("Error while writing: %s", e.message); |
| 57 |
} |
| 58 |
}); |
| 59 |
} |
| 60 |
|
| 61 |
public override async void write_async(StanzaNode node, int io_priority = Priority.DEFAULT, Cancellable? cancellable = null) throws IOError { |
| 62 |
if (write_obj != null) { |
| 63 |
yield write_obj.write_stanza(this, node, io_priority, cancellable ?? this.cancellable); |
| 64 |
} else { |
| 65 |
StanzaWriter? writer = this.writer; |
| 66 |
if (writer == null) throw new IOError.NOT_CONNECTED("trying to write, but no stream open"); |
| 67 |
log.node("OUT", node, this); |
| 68 |
yield ((!)writer).write_node(node, io_priority, cancellable ?? this.cancellable); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
internal IOStream? get_stream() { |
| 73 |
return stream; |
| 74 |
} |
| 75 |
|
| 76 |
public override async void setup() throws IOError { |
| 77 |
StanzaNode outs = new StanzaNode.build("stream", "http://etherx.jabber.org/streams") |
| 78 |
.put_attribute("to", remote_name.to_string()) |
| 79 |
.put_attribute("version", "1.0") |
| 80 |
.put_attribute("xmlns", "jabber:client") |
| 81 |
.put_attribute("stream", "http://etherx.jabber.org/streams", XMLNS_URI); |
| 82 |
outs.has_nodes = true; |
| 83 |
log.node("OUT ROOT", outs, this); |
| 84 |
yield write_async(outs, Priority.HIGH, cancellable); |
| 85 |
received_root_node(this, yield read_root()); |
| 86 |
|
| 87 |
setup_needed = false; |
| 88 |
} |
| 89 |
|
| 90 |
private async StanzaNode read_root() throws IOError { |
| 91 |
StanzaReader? reader = this.reader; |
| 92 |
if (reader == null) throw new IOError.NOT_CONNECTED("trying to read, but no stream open"); |
| 93 |
StanzaNode node = yield ((!)reader).read_root_node(); |
| 94 |
log.node("IN ROOT", node, this); |
| 95 |
return node; |
| 96 |
} |
| 97 |
} |