Skip to content

Commit

Permalink
Fix #77: allow the user to access IqManager
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Oct 4, 2020
1 parent 448f4bc commit 5d9afe0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 53 deletions.
36 changes: 11 additions & 25 deletions SharpXMPP.Shared/XMPP/Client/IqManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,23 @@ public abstract class PayloadHandler

public class IqManager
{
protected readonly XmppConnection Connection;
public IqManager(XmppConnection connection)
{
Connection = connection;
}

public List<PayloadHandler> PayloadHandlers { get; set; }
public List<PayloadHandler> PayloadHandlers { get; } = new List<PayloadHandler>();

public void Handle(XMPPIq element)
public void Handle(XmppConnection sender, XMPPIq element)
{
if (PayloadHandlers != null)
{
bool handled = false;
PayloadHandlers.ForEach( (h) =>
{
handled |= h.Handle(Connection, element);
});
if (!handled)
HandleError(element);
else
{
return;
}
}
HandleError(element);
bool handled = false;
PayloadHandlers.ForEach( (h) =>
{
handled |= h.Handle(sender, element);
});
if (!handled)
HandleError(sender, element);
}

public void HandleError(XMPPIq element)
public void HandleError(XmppConnection connection, XMPPIq element)
{
if (element.IqType == XMPPIq.IqTypes.get || element.IqType == XMPPIq.IqTypes.set)
Connection.Send(element.Error());
connection.Send(element.Error());
}
}
}
14 changes: 14 additions & 0 deletions SharpXMPP.Shared/XmppConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System.Threading.Tasks;
using System.Xml.Linq;
using SharpXMPP.XMPP;
using SharpXMPP.XMPP.Client;
using SharpXMPP.XMPP.Client.Capabities;
using SharpXMPP.XMPP.Client.Disco;
using SharpXMPP.XMPP.Client.Disco.Elements;
using SharpXMPP.XMPP.Client.Elements;
using SharpXMPP.XMPP.Stream.Elements;
Expand Down Expand Up @@ -63,6 +65,16 @@ protected XmppConnection(string ns)
Namespaces.DiscoItems
}
};

IqManager = new IqManager
{
PayloadHandlers =
{
new InfoHandler(Capabilities),
new ItemsHandler()
}
};
Iq += IqManager.Handle;
}

public Features Features { get; set; }
Expand Down Expand Up @@ -153,6 +165,8 @@ public CapabilitiesManager Capabilities
set { _caps = value; }
}

public IqManager IqManager { get; }

public abstract XElement NextElement();

public virtual void Send(XElement data)
Expand Down
18 changes: 0 additions & 18 deletions SharpXMPP.Shared/XmppTcpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
using SharpXMPP.Errors;
using SharpXMPP.XMPP;
using SharpXMPP.XMPP.Bind;
using SharpXMPP.XMPP.Client;
using SharpXMPP.XMPP.Client.Disco;
using SharpXMPP.XMPP.Client.Disco.Elements;
using SharpXMPP.XMPP.Client.Elements;
using SharpXMPP.XMPP.SASL;
using SharpXMPP.XMPP.Stream.Elements;
Expand Down Expand Up @@ -112,7 +109,6 @@ public override void Dispose()
Reader = null;
ConnectionStream?.Dispose();
ConnectionStream = null;
Iq -= OnIqHandler;
}
base.Dispose();
}
Expand Down Expand Up @@ -149,7 +145,6 @@ public override async Task ConnectAsync(CancellationToken token)
{
List<IPAddress> HostAddresses = await ResolveHostAddresses();
await ConnectOverTcp(HostAddresses);
Iq += OnIqHandler;

RestartXmlStreams();

Expand Down Expand Up @@ -289,18 +284,5 @@ private async Task<bool> InitTlsIfSupported(Features features)
RestartXmlStreams();
return true;
}


private void OnIqHandler(XmppConnection sender, XMPPIq iq)
{
new IqManager(this)
{
PayloadHandlers = new List<PayloadHandler>
{
new InfoHandler(Capabilities),
new ItemsHandler()
}
}.Handle(iq);
}
}
}
10 changes: 0 additions & 10 deletions SharpXMPP.WebSocket/XmppWebSocketConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
using DnsClient;
using SharpXMPP.XMPP;
using SharpXMPP.XMPP.Bind.Elements;
using SharpXMPP.XMPP.Client;
using SharpXMPP.XMPP.Client.Capabities;
using SharpXMPP.XMPP.Client.Disco;
using SharpXMPP.XMPP.Client.Disco.Elements;
using SharpXMPP.XMPP.Client.Elements;
using SharpXMPP.XMPP.Framing.Elements;
Expand Down Expand Up @@ -70,14 +68,6 @@ public XmppWebSocketConnection(JID jid, string password, string websocketUri)
Namespaces.DiscoItems
}
};
Iq += (sender, iq) => new IqManager(this)
{
PayloadHandlers = new List<PayloadHandler>
{
new InfoHandler(Capabilities),
new ItemsHandler()
}
}.Handle(iq);
}
public override XElement NextElement()
{
Expand Down

0 comments on commit 5d9afe0

Please sign in to comment.