Exemplo n.º 1
0
        /// <summary>
        ///     Indicate the given pipe as being ready for writing to by this socket.
        ///     This gets called by the WriteActivated method.
        /// </summary>
        /// <param name="pipe">the <c>Pipe</c> that is now becoming available for writing</param>
        protected override void XWriteActivated(Pipe pipe)
        {
            Outpipe outpipe = null;

            foreach (KeyValuePair <byte[], Outpipe> it in this.m_outpipes)
            {
                if (it.Value.Pipe == pipe)
                {
                    Debug.Assert(!it.Value.Active);
                    it.Value.Active = true;
                    outpipe         = it.Value;
                    break;
                }
            }

            Debug.Assert(outpipe != null);
        }
Exemplo n.º 2
0
        protected override void XWriteActivated(Pipe pipe)
        {
            Outpipe outpipe = null;

            foreach (var it in m_outpipes)
            {
                if (it.Value.Pipe == pipe)
                {
                    Debug.Assert(!it.Value.Active);
                    it.Value.Active = true;
                    outpipe         = it.Value;
                    break;
                }
            }

            Debug.Assert(outpipe != null);
        }
Exemplo n.º 3
0
        private void IdentifyPeer([NotNull] Pipe pipe)
        {
            // Always assign identity for raw-socket
            var identity = new byte[5];

            byte[] result = BitConverter.GetBytes(m_nextPeerId++);

            Buffer.BlockCopy(result, 0, identity, 1, 4);

            m_options.Identity = identity;

            pipe.Identity = identity;

            // Add the record into output pipes lookup table
            var outpipe = new Outpipe(pipe, true);

            m_outpipes.Add(identity, outpipe);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Register the pipe with this socket.
        /// </summary>
        /// <param name="pipe">the Pipe to attach</param>
        /// <param name="icanhasall">not used</param>
        protected override void XAttachPipe(Pipe pipe, bool icanhasall)
        {
            Debug.Assert(pipe != null);

            uint routingId = m_nextRoutingId++;

            if (routingId == 0)
            {
                routingId = m_nextRoutingId++; //  Never use Routing ID zero
            }
            pipe.RoutingId = routingId;
            //  Add the record into output pipes lookup table
            var outpipe = new Outpipe(pipe, true);

            m_outpipes.Add(routingId, outpipe);

            m_fairQueueing.Attach(pipe);
        }
Exemplo n.º 5
0
        private void IdentifyPeer(Pipe pipe)
        {
            Blob identity;

            // Always assign identity for raw-socket
            byte[] buf = new byte[5];
            buf[0] = 0;

            byte[] result = BitConverter.GetBytes(m_nextPeerId++);

            Buffer.BlockCopy(result, 0, buf, 1, 4);
            identity               = new Blob(buf);
            m_options.Identity     = buf;
            m_options.IdentitySize = (byte)buf.Length;

            pipe.Identity = identity;
            //  Add the record into output pipes lookup table
            Outpipe outpipe = new Outpipe(pipe, true);

            m_outpipes.Add(identity, outpipe);
        }
Exemplo n.º 6
0
        private bool IdentifyPeer([NotNull] Pipe pipe)
        {
            byte[] identity;

            if (m_options.RawSocket)
            {
                // Always assign identity for raw-socket
                identity    = new byte[5];
                identity[0] = 0;
                byte[] result = BitConverter.GetBytes(m_nextPeerId++);
                Buffer.BlockCopy(result, 0, identity, 1, 4);
            }
            else
            {
                //  Pick up handshake cases and also case where next identity is set

                var msg = new Msg();
                msg.InitEmpty();

                bool ok = pipe.Read(ref msg);

                if (!ok)
                {
                    return(false);
                }

                if (msg.Size == 0)
                {
                    //  Fall back on the auto-generation
                    identity    = new byte[5];
                    identity[0] = 0;

                    byte[] result = BitConverter.GetBytes(m_nextPeerId++);

                    Buffer.BlockCopy(result, 0, identity, 1, 4);

                    msg.Close();
                }
                else
                {
                    identity = new byte[msg.Size];
                    Buffer.BlockCopy(msg.Data, 0, identity, 0, msg.Size);

                    //  Ignore peers with duplicate ID.
                    if (m_outpipes.ContainsKey(identity))
                    {
                        msg.Close();
                        return(false);
                    }

                    msg.Close();
                }
            }

            pipe.Identity = identity;
            //  Add the record into output pipes lookup table
            var outpipe = new Outpipe(pipe, true);

            m_outpipes.Add(identity, outpipe);

            return(true);
        }
Exemplo n.º 7
0
        private bool IdentifyPeer([NotNull] Pipe pipe)
        {
            byte[] identity;

            if (m_options.RawSocket)
            {
                // Always assign identity for raw-socket
                identity = new byte[5];
                byte[] result = BitConverter.GetBytes(m_nextPeerId++);
                Buffer.BlockCopy(result, 0, identity, 1, 4);
            }
            else
            {
                // Pick up handshake cases and also case where next identity is set

                var msg = new Msg();
                msg.InitEmpty();

                bool ok = pipe.Read(ref msg);

                if (!ok)
                {
                    return(false);
                }

                if (msg.Size == 0)
                {
                    // Fall back on the auto-generation
                    identity = new byte[5];

                    byte[] result = BitConverter.GetBytes(m_nextPeerId++);

                    Buffer.BlockCopy(result, 0, identity, 1, 4);

                    msg.Close();
                }
                else
                {
                    identity = msg.CloneData();
                    msg.Close();

                    Outpipe existPipe;

                    if (m_outpipes.TryGetValue(identity, out existPipe))
                    {
                        if (!m_handover)
                        {
                            // Ignore peers with duplicate ID.
                            return(false);
                        }
                        else
                        {
                            //  We will allow the new connection to take over this
                            //  identity. Temporarily assign a new identity to the
                            //  existing pipe so we can terminate it asynchronously.
                            var    newIdentity = new byte[5];
                            byte[] result      = BitConverter.GetBytes(m_nextPeerId++);
                            Buffer.BlockCopy(result, 0, newIdentity, 1, 4);
                            existPipe.Pipe.Identity = newIdentity;
                            m_outpipes.Add(newIdentity, existPipe);

                            //  Remove the existing identity entry to allow the new
                            //  connection to take the identity.
                            m_outpipes.Remove(identity);

                            if (existPipe.Pipe == m_currentIn)
                            {
                                m_closingCurrentIn = true;
                            }
                            else
                            {
                                existPipe.Pipe.Terminate(true);
                            }
                        }
                    }
                }
            }

            pipe.Identity = identity;
            // Add the record into output pipes lookup table
            var outpipe = new Outpipe(pipe, true);

            m_outpipes.Add(identity, outpipe);

            return(true);
        }
Exemplo n.º 8
0
        private bool IdentifyPeer(Pipe pipe)
        {
            Blob identity;

            Msg msg = pipe.Read ();
            if (msg == null)
                return false;

            if (msg.Size == 0) {
                //  Fall back on the auto-generation
                byte[] buf = new byte[5];

                buf[0] = 0;

                byte[] result = BitConverter.GetBytes(m_nextPeerId++);
                //if (BitConverter.IsLittleEndian)
                //{
                //    Array.Reverse(result);
                //}

                Buffer.BlockCopy(result, 0, buf, 1, 4);
                identity = new Blob(buf);
            }
            else {
                identity = new Blob(msg.Data);

                //  Ignore peers with duplicate ID.
                if (m_outpipes.ContainsKey(identity))
                    return false;
            }

            pipe.Identity = identity;
            //  Add the record into output pipes lookup table
            Outpipe outpipe = new Outpipe(pipe, true);
            m_outpipes.Add (identity, outpipe);

            return true;
        }
Exemplo n.º 9
0
        private bool IdentifyPeer( Pipe pipe)
        {
            byte[] identity;

            if (m_options.RawSocket)
            {
                // Always assign identity for raw-socket
                identity = new byte[5];
                byte[] result = BitConverter.GetBytes(m_nextPeerId++);
                Buffer.BlockCopy(result, 0, identity, 1, 4);
            }
            else
            {
                // Pick up handshake cases and also case where next identity is set

                var msg = new Msg();
                msg.InitEmpty();

                bool ok = pipe.Read(ref msg);

                if (!ok)
                    return false;

                if (msg.Size == 0)
                {
                    // Fall back on the auto-generation
                    identity = new byte[5];

                    byte[] result = BitConverter.GetBytes(m_nextPeerId++);

                    Buffer.BlockCopy(result, 0, identity, 1, 4);

                    msg.Close();
                }
                else
                {
                    identity = msg.CloneData();

                    // Ignore peers with duplicate ID.
                    if (m_outpipes.ContainsKey(identity))
                    {
                        msg.Close();
                        return false;
                    }

                    msg.Close();
                }
            }

            pipe.Identity = identity;
            // Add the record into output pipes lookup table
            var outpipe = new Outpipe(pipe, true);
            m_outpipes.Add(identity, outpipe);

            return true;
        }
Exemplo n.º 10
0
        private bool IdentifyPeer([NotNull] Pipe pipe)
        {
            byte[] identity;

            if (m_options.RawSocket)
            {
                // Always assign identity for raw-socket
                identity = new byte[5];
                byte[] result = BitConverter.GetBytes(m_nextPeerId++);
                Buffer.BlockCopy(result, 0, identity, 1, 4);
            }
            else
            {
                // Pick up handshake cases and also case where next identity is set

                var msg = new Msg();
                msg.InitEmpty();

                bool ok = pipe.Read(ref msg);

                if (!ok)
                    return false;

                if (msg.Size == 0)
                {
                    // Fall back on the auto-generation
                    identity = new byte[5];

                    byte[] result = BitConverter.GetBytes(m_nextPeerId++);

                    Buffer.BlockCopy(result, 0, identity, 1, 4);

                    msg.Close();
                }
                else
                {
                    identity = msg.CloneData();
                    msg.Close();

                    Outpipe existPipe;                 

                    if (m_outpipes.TryGetValue(identity, out existPipe))
                    {                        
                        if (!m_handover)
                        {
                            // Ignore peers with duplicate ID.
                            return false;
                        }
                        else
                        {
                            //  We will allow the new connection to take over this
                            //  identity. Temporarily assign a new identity to the
                            //  existing pipe so we can terminate it asynchronously.
                            var newIdentity = new byte[5];
                            byte[] result = BitConverter.GetBytes(m_nextPeerId++);
                            Buffer.BlockCopy(result, 0, newIdentity, 1, 4);
                            existPipe.Pipe.Identity = newIdentity;                        
                            m_outpipes.Add(newIdentity, existPipe);

                            //  Remove the existing identity entry to allow the new
                            //  connection to take the identity.
                            m_outpipes.Remove(identity);

                            if (existPipe.Pipe == m_currentIn)
                                m_closingCurrentIn = true;
                            else
                                existPipe.Pipe.Terminate(true);
                        }
                    }
                }
            }

            pipe.Identity = identity;
            // Add the record into output pipes lookup table
            var outpipe = new Outpipe(pipe, true);
            m_outpipes.Add(identity, outpipe);

            return true;
        }
Exemplo n.º 11
0
        private void IdentifyPeer(Pipe pipe)
        {
            Blob identity;

              // Always assign identity for raw-socket
              byte[] buf = new byte[5];
              buf[0] = 0;

              byte[] result = BitConverter.GetBytes(m_nextPeerId++);

              Buffer.BlockCopy(result, 0, buf, 1, 4);
              identity = new Blob(buf);
              m_options.Identity = buf;
              m_options.IdentitySize = (byte)buf.Length;

              pipe.Identity = identity;
              //  Add the record into output pipes lookup table
              Outpipe outpipe = new Outpipe(pipe, true);
              m_outpipes.Add(identity, outpipe);
        }
Exemplo n.º 12
0
        private void IdentifyPeer([NotNull] Pipe pipe)
        {
            // Always assign identity for raw-socket
            var identity = new byte[5];

            byte[] result = BitConverter.GetBytes(m_nextPeerId++);

            Buffer.BlockCopy(result, 0, identity, 1, 4);

            m_options.Identity = identity;
            m_options.IdentitySize = (byte)identity.Length;

            pipe.Identity = identity;

            // Add the record into output pipes lookup table
            var outpipe = new Outpipe(pipe, true);
            m_outpipes.Add(identity, outpipe);
        }