2017-03-02 14:37:32 +00:00
|
|
|
namespace Xmpp.Core {
|
|
|
|
public class StanzaWriter {
|
|
|
|
private OutputStream output;
|
|
|
|
|
2017-11-22 19:06:50 +00:00
|
|
|
private Queue<SourceFuncWrapper> queue = new Queue<SourceFuncWrapper>();
|
|
|
|
private bool running = false;
|
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
public StanzaWriter.for_stream(OutputStream output) {
|
|
|
|
this.output = output;
|
|
|
|
}
|
|
|
|
|
2017-11-22 19:06:50 +00:00
|
|
|
public async void write_node(StanzaNode node) throws XmlError {
|
|
|
|
yield write_data(node.to_xml().data);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async void write(string s) throws XmlError {
|
2017-11-22 19:06:50 +00:00
|
|
|
yield write_data(s.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async void write_data(uint8[] data) throws XmlError {
|
|
|
|
if (running) {
|
|
|
|
queue.push_tail(new SourceFuncWrapper(write_data.callback));
|
|
|
|
yield;
|
|
|
|
}
|
|
|
|
running = true;
|
2017-03-02 14:37:32 +00:00
|
|
|
try {
|
2017-11-22 19:06:50 +00:00
|
|
|
yield output.write_all_async(data, 0, null, null);
|
|
|
|
SourceFuncWrapper? sfw = queue.pop_head();
|
|
|
|
if (sfw != null) {
|
|
|
|
sfw.sfun();
|
|
|
|
}
|
|
|
|
} catch (GLib.Error e) {
|
2018-01-04 20:13:44 +00:00
|
|
|
throw new XmlError.IO(@"IOError in GLib: $(e.message)");
|
2017-11-22 19:06:50 +00:00
|
|
|
} finally {
|
|
|
|
running = false;
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-22 19:06:50 +00:00
|
|
|
|
|
|
|
public class SourceFuncWrapper : Object {
|
|
|
|
|
|
|
|
public SourceFunc sfun;
|
|
|
|
|
|
|
|
public SourceFuncWrapper(owned SourceFunc sfun) {
|
|
|
|
this.sfun = (owned)sfun;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-11 20:29:13 +00:00
|
|
|
}
|