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 == 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 == 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; } } if (b) { try { Callback(this, new PeerConnectionEventArgs(Result.XmppClient, State, this.fullJid, FullJID)); } 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(FullJID, Info); } catch (Exception ex) { this.Error(ex.Message); Connection = null; if (ResynchMethod != null) { try { ResynchEventArgs e = new ResynchEventArgs(FullJID, (sender, e2) => { try { if (e2.Ok) { this.GetPeerConnection(FullJID, Callback, State, null); } else { lock (this.peersByFullJid) { this.peersByFullJid.Remove(FullJID); } Result.CallCallbacks(); } } catch (Exception ex2) { Log.Critical(ex2); } }); ResynchMethod(this, e); } catch (Exception ex2) { Log.Critical(ex2); } return; } } if (Connection == null) { lock (this.peersByFullJid) { this.peersByFullJid.Remove(FullJID); } Result.CallCallbacks(); } else { Result.Peer = Connection; Connection.Start((sender, e) => { if (ResynchMethod != null) { try { ResynchMethod(this, new ResynchEventArgs(FullJID, async(sender2, e2) => { try { if (e2.Ok) { Result.Peer = null; Connection = await this.ConnectToAsync(FullJID, 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 P2PNetwork_OnPeerConnected(object Listener, PeerConnection Peer) { PeerState State = new PeerState(Peer, this); this.Information("Peer connected from " + Peer.RemoteEndpoint.ToString()); }
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; } }