I am trying to implement an XMPP client for UWP in C #, and for this I use StreamSocketfor TCP.
The problem is that for a handshake and SASL authI need a sequence of "write and read" from the client to the server and without delay I can not read all the data sent by the server; I want to remove the delay.
This is my code:
// Start handshake
await XmppWrite("<?xml version='1.0'?><stream:stream to='" + serverHostName.Domain + "' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>");
var responseStream = await XmppRead();
SaslPlain saslPlain = new SaslPlain(Username, Password);
responseStream = null;
String xml = null;
while (!saslPlain.IsCompleted && (xml = saslPlain.GetNextXml(responseStream)) != null)
{
await XmppWrite(xml);
responseStream = await XmppRead();
}
if (!saslPlain.IsSuccess) return;
// Request bind
await XmppWrite("<?xml version='1.0'?><stream:stream to='" + serverHostName.Domain + "' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>");
responseStream = await XmppRead();
// Bind
await XmppWrite("<iq id='" + GenerateId() + "' type='set'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/></iq>");
responseStream = await XmppRead();
if (responseStream.Jid == null) return;
Jid = responseStream.Jid;
// Open session
await XmppWrite("<iq id='" + GenerateId() + "' type='set'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>");
responseStream = await XmppRead();
The XmppRead () method is as follows:
private async Task<XmppStream> XmppRead()
{
await Task.Delay(100);
var x = await dataReader.LoadAsync(BUFFER_SIZE);
if (dataReader.UnconsumedBufferLength > 0)
{
Debug.WriteLine(x + ", " + dataReader.UnconsumedBufferLength);
var stream = new XmppStream(dataReader.ReadString(dataReader.UnconsumedBufferLength));
if (stream.IsError)
{
if (Error != null)
{
Error(this, new XmppClientEventArgs(stream));
}
throw new Exception(stream.Error);
}
return stream;
}
return new XmppStream();
}
So, I think the problem is that I'm trying to read before the server finishes sending all my data, so I can only read a partial message. How can I check if there is other data without a block?
Thanks.
source
share