diff options
author | fiaxh <git@mx.ax.lt> | 2017-11-22 20:06:50 +0100 |
---|---|---|
committer | fiaxh <git@mx.ax.lt> | 2017-11-23 01:28:29 +0100 |
commit | 9165c4db278b2d3da636d53e89c3b80cff66977f (patch) | |
tree | 84f227a12389ec2b3c3c7a578f913bc763760b53 /xmpp-vala/src/core/stanza_writer.vala | |
parent | de133218dab0ac1729dbfc32177979133c15f53b (diff) | |
download | dino-9165c4db278b2d3da636d53e89c3b80cff66977f.tar.gz dino-9165c4db278b2d3da636d53e89c3b80cff66977f.zip |
Async service lookup, connect and write
Diffstat (limited to 'xmpp-vala/src/core/stanza_writer.vala')
-rw-r--r-- | xmpp-vala/src/core/stanza_writer.vala | 42 |
1 files changed, 32 insertions, 10 deletions
diff --git a/xmpp-vala/src/core/stanza_writer.vala b/xmpp-vala/src/core/stanza_writer.vala index e67920db..270d898d 100644 --- a/xmpp-vala/src/core/stanza_writer.vala +++ b/xmpp-vala/src/core/stanza_writer.vala @@ -2,26 +2,48 @@ namespace Xmpp.Core { public class StanzaWriter { private OutputStream output; + private Queue<SourceFuncWrapper> queue = new Queue<SourceFuncWrapper>(); + private bool running = false; + public StanzaWriter.for_stream(OutputStream output) { this.output = output; } - public void write_node(StanzaNode node) throws XmlError { - try { - lock(output) { - output.write_all(node.to_xml().data, null); - } - } catch (GLib.IOError e) { - throw new XmlError.IO_ERROR(@"IOError in GLib: $(e.message)"); - } + public async void write_node(StanzaNode node) throws XmlError { + yield write_data(node.to_xml().data); } public async void write(string s) throws XmlError { + 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; try { - output.write_all(s.data, null); - } catch (GLib.IOError e) { + yield output.write_all_async(data, 0, null, null); + SourceFuncWrapper? sfw = queue.pop_head(); + if (sfw != null) { + sfw.sfun(); + } + } catch (GLib.Error e) { throw new XmlError.IO_ERROR(@"IOError in GLib: $(e.message)"); + } finally { + running = false; } } } + +public class SourceFuncWrapper : Object { + + public SourceFunc sfun; + + public SourceFuncWrapper(owned SourceFunc sfun) { + this.sfun = (owned)sfun; + } +} + } |