private void GetPeerConnection(string BareJID, PeerConnectionEventHandler Callback, object State, ResynchEventHandler ResynchMethod) { PeerState Result; PeerState Old = null; AddressInfo Info; string Header = null; bool b; if (this.p2pNetwork == null || this.p2pNetwork.State != PeerToPeerNetworkState.Ready) { if (Callback != null) { try { Callback(this, new PeerConnectionEventArgs(null, State, this.bareJid, BareJID)); } catch (Exception ex) { Log.Critical(ex); } } return; } lock (this.addressesByJid) { b = this.addressesByJid.TryGetValue(BareJID, out Info); } if (!b) { Callback(this, new PeerConnectionEventArgs(null, State, this.bareJid, BareJID)); return; } lock (this.peersByJid) { b = this.peersByJid.TryGetValue(BareJID, out Result); if (b) { if (Result.AgeSeconds >= 10 && (Result.HasCallbacks || Result.XmppClient == null || !Result.Peer.Tcp.Connected)) { this.peersByJid.Remove(BareJID); Old = Result; Result = null; b = false; } else if (Result.State != XmppState.Connected) { Result.AddCallback(Callback, State); return; } } if (!b) { Header = "<?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' from='" + this.bareJid + "' to='" + BareJID + "' version='1.0'>"; Result = new PeerState(null, this, BareJID, Header, "</stream:stream>", string.Empty, 1.0, Callback, State); this.peersByJid[BareJID] = Result; } } if (b) { try { Callback(this, new PeerConnectionEventArgs(Result.XmppClient, State, this.bareJid, BareJID)); } catch (Exception ex) { Log.Critical(ex); } return; } else if (Old != null) { Old.CallCallbacks(); Old.Dispose(); } Task.Run(async() => { PeerConnection Connection; try { Connection = await this.ConnectToAsync(BareJID, Info); } catch (Exception ex) { this.Error(ex.Message); Connection = null; if (ResynchMethod != null) { try { ResynchEventArgs e = new ResynchEventArgs(BareJID, (sender, e2) => { try { if (e2.Ok) { this.GetPeerConnection(BareJID, Callback, State, null); } else { lock (this.peersByJid) { this.peersByJid.Remove(BareJID); } Result.CallCallbacks(); } } catch (Exception ex2) { Log.Critical(ex2); } }); ResynchMethod(this, e); } catch (Exception ex2) { Log.Critical(ex2); } return; } } if (Connection == null) { lock (this.peersByJid) { this.peersByJid.Remove(BareJID); } Result.CallCallbacks(); } else { Result.Peer = Connection; Connection.Start((sender, e) => { if (ResynchMethod != null) { try { ResynchMethod(this, new ResynchEventArgs(BareJID, async(sender2, e2) => { try { if (e2.Ok) { Result.Peer = null; Connection = await this.ConnectToAsync(BareJID, Info); Result.Peer = Connection; Connection.Start(); Result.HeaderSent = true; Result.Send(Header); this.TransmitText(Header); } else { Result.CallCallbacks(); } } catch (Exception ex) { Log.Critical(ex); } })); } catch (Exception ex) { Log.Critical(ex); Result.CallCallbacks(); } } else { Result.CallCallbacks(); } }); Result.HeaderSent = true; Result.Send(Header); this.TransmitText(Header); } }); }
private void GetPeerConnection(string FullJID, PeerConnectionEventHandler Callback, object State, ResynchEventHandler ResynchMethod) { PeerState Result; PeerState Old = null; AddressInfo Info; string Header = null; bool b; if (this.p2pNetwork is null || this.p2pNetwork.State != PeerToPeerNetworkState.Ready) { if (Callback != null) { try { Callback(this, new PeerConnectionEventArgs(null, State, this.fullJid, FullJID)); } catch (Exception ex) { Log.Critical(ex); } } return; } lock (this.addressesByFullJid) { b = this.addressesByFullJid.TryGetValue(FullJID, out Info); } if (!b) { Callback(this, new PeerConnectionEventArgs(null, State, this.fullJid, FullJID)); return; } lock (this.peersByFullJid) { b = this.peersByFullJid.TryGetValue(FullJID, out Result); if (b) { if (Result.AgeSeconds >= 30 && (Result.HasCallbacks || Result.XmppClient is null || !Result.Peer.Tcp.Connected)) { this.peersByFullJid.Remove(FullJID); Old = Result; Result = null; b = false; } else if (Result.State != XmppState.Connected) { Result.AddCallback(Callback, State); return; } } if (!b) { Header = "<?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' from='" + this.fullJid + "' to='" + FullJID + "' version='1.0'>"; Result = new PeerState(null, this, FullJID, Header, "</stream:stream>", string.Empty, 1.0, Callback, State); this.peersByFullJid[FullJID] = Result; } }