Ejemplo n.º 1
0
        internal void AddCallback(PeerConnectionEventHandler Callback, object State)
        {
            if (this.callbacks is null)
            {
                this.callbacks = new LinkedList <KeyValuePair <PeerConnectionEventHandler, object> >();
            }

            this.callbacks.AddLast(new KeyValuePair <PeerConnectionEventHandler, object>(Callback, State));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Peer connection state.
        /// </summary>
        /// <param name="Peer">Peer connection.</param>
        /// <param name="Parent">Parent object.</param>
        /// <param name="RemoteFullJID">Remote Full JID</param>
        /// <param name="StreamHeader">Stream header</param>
        /// <param name="StreamFooter">Stream footer</param>
        /// <param name="StreamId">Stream ID</param>
        /// <param name="Version">Protocol version</param>
        /// <param name="Callback">Callback method</param>
        /// <param name="State">State object</param>
        public PeerState(PeerConnection Peer, XmppServerlessMessaging Parent, string RemoteFullJID, string StreamHeader, string StreamFooter,
                         string StreamId, double Version, PeerConnectionEventHandler Callback, object State)
        {
            this.parent        = Parent;
            this.peer          = Peer;
            this.remoteFullJid = RemoteFullJID;
            this.streamHeader  = StreamHeader;
            this.streamFooter  = StreamFooter;
            this.streamId      = StreamId;
            this.version       = Version;
            this.parentFullJid = Parent.FullJid;

            this.callbacks = new LinkedList <KeyValuePair <PeerConnectionEventHandler, object> >();
            this.callbacks.AddLast(new KeyValuePair <PeerConnectionEventHandler, object>(Callback, State));

            this.AddPeerHandlers();
        }
Ejemplo n.º 3
0
        internal void NewXmppClient(XmppClient Client, string LocalJid, string RemoteJid)
        {
            /*foreach (ISniffer Sniffer in this.Sniffers)
             *      Client.Add(Sniffer);*/

            PeerConnectionEventHandler h = this.OnNewXmppClient;

            if (h != null)
            {
                try
                {
                    h(this, new PeerConnectionEventArgs(Client, null, LocalJid, RemoteJid));
                }
                catch (Exception ex)
                {
                    Log.Critical(ex);
                }
            }
        }
Ejemplo n.º 4
0
        internal void NewXmppClient(XmppClient Client, string LocalJid, string RemoteJid)
        {
            this.Information("Serverless XMPP connection established with " + RemoteJid);

            /*foreach (ISniffer Sniffer in this.Sniffers)
             *      Client.Add(Sniffer);*/

            PeerConnectionEventHandler h = this.OnNewXmppClient;

            if (!(h is null))
            {
                try
                {
                    h(this, new PeerConnectionEventArgs(Client, null, LocalJid, RemoteJid));
                }
                catch (Exception ex)
                {
                    Log.Critical(ex);
                }
            }
        }
Ejemplo n.º 5
0
        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);
                }
            });
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Gets a peer XMPP connection.
 /// </summary>
 /// <param name="BareJID">Bare JID of peer to connect to.</param>
 /// <param name="Callback">Method to call when connection is established.</param>
 /// <param name="State">State object to pass on to callback method.</param>
 public void GetPeerConnection(string BareJID, PeerConnectionEventHandler Callback, object State)
 {
     this.GetPeerConnection(BareJID, Callback, State, this.OnResynch);
 }
Ejemplo n.º 7
0
        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;
                }
            }