2018-01-12 20:03:09 +00:00
|
|
|
namespace Xmpp {
|
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
public class StanzaWriter {
|
2020-07-21 13:48:42 +00:00
|
|
|
public signal void cancel();
|
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-01-31 14:13:12 +00:00
|
|
|
public async void write_node(StanzaNode node, int io_priority = Priority.DEFAULT, Cancellable? cancellable = null) throws IOError {
|
|
|
|
yield write_data(node.to_xml().data, io_priority, cancellable);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
2023-01-31 14:13:12 +00:00
|
|
|
public async void write_nodes(StanzaNode node1, StanzaNode node2, int io_priority = Priority.DEFAULT, Cancellable? cancellable = null) throws IOError {
|
2020-06-28 13:53:41 +00:00
|
|
|
var data1 = node1.to_xml().data;
|
|
|
|
var data2 = node2.to_xml().data;
|
|
|
|
|
|
|
|
uint8[] concat = new uint8[data1.length + data2.length];
|
|
|
|
int i = 0;
|
|
|
|
foreach (var datum in data1) {
|
|
|
|
concat[i++] = datum;
|
|
|
|
}
|
|
|
|
foreach (var datum in data2) {
|
|
|
|
concat[i++] = datum;
|
|
|
|
}
|
|
|
|
|
2023-01-31 14:13:12 +00:00
|
|
|
yield write_data(concat, io_priority, cancellable);
|
2020-06-28 13:53:41 +00:00
|
|
|
}
|
|
|
|
|
2023-01-31 14:13:12 +00:00
|
|
|
public async void write(string s, int io_priority = Priority.DEFAULT, Cancellable? cancellable = null) throws IOError {
|
|
|
|
yield write_data(s.data, io_priority, cancellable);
|
2017-11-22 19:06:50 +00:00
|
|
|
}
|
|
|
|
|
2023-01-31 14:13:12 +00:00
|
|
|
private async void write_data(owned uint8[] data, int io_priority = Priority.DEFAULT, Cancellable? cancellable = null) throws IOError {
|
2017-11-22 19:06:50 +00:00
|
|
|
if (running) {
|
|
|
|
queue.push_tail(new SourceFuncWrapper(write_data.callback));
|
|
|
|
yield;
|
|
|
|
}
|
|
|
|
running = true;
|
2017-03-02 14:37:32 +00:00
|
|
|
try {
|
2023-01-31 14:13:12 +00:00
|
|
|
yield output.write_all_async(data, io_priority, cancellable, null);
|
2023-01-31 14:12:39 +00:00
|
|
|
} catch (IOError e) {
|
|
|
|
cancel();
|
|
|
|
throw e;
|
2019-12-22 18:28:40 +00:00
|
|
|
} catch (GLib.Error e) {
|
2020-07-21 13:48:42 +00:00
|
|
|
cancel();
|
2023-01-31 14:12:39 +00:00
|
|
|
throw new IOError.FAILED("Error in GLib: %s".printf(e.message));
|
2019-12-22 18:28:40 +00:00
|
|
|
} finally {
|
2017-11-22 19:06:50 +00:00
|
|
|
SourceFuncWrapper? sfw = queue.pop_head();
|
|
|
|
if (sfw != null) {
|
|
|
|
sfw.sfun();
|
2019-12-22 18:28:40 +00:00
|
|
|
} else {
|
|
|
|
running = false;
|
2017-11-22 19:06:50 +00:00
|
|
|
}
|
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
|
|
|
}
|