Exemplo n.º 1
0
        //  Put the message to all active pipes.
        private void Distribute(ref Msg msg, SendReceiveOptions flags)
        {
            //  If there are no matching pipes available, simply drop the message.
            if (m_matching == 0)
            {
                msg.Close();
                msg.InitEmpty();

                return;
            }

            if (msg.MsgType != MsgType.Pool)
            {
                for (int i = 0; i < m_matching; ++i)
                {
                    if (!Write(m_pipes[i], ref msg))
                    {
                        --i; //  Retry last write because index will have been swapped
                    }
                }

                msg.Close();
                msg.InitEmpty();

                return;
            }

            //  Add matching-1 references to the message. We already hold one reference,
            //  that's why -1.
            msg.AddReferences(m_matching - 1);


            //  Push copy of the message to each matching pipe.
            int failed = 0;

            for (int i = 0; i < m_matching; ++i)
            {
                if (!Write(m_pipes[i], ref msg))
                {
                    ++failed;
                    --i; //  Retry last write because index will have been swapped
                }
            }
            if (failed != 0)
            {
                msg.RemoveReferences(failed);
            }

            //  Detach the original message from the data buffer. Note that we don't
            //  close the message. That's because we've already used all the references.
            msg.InitEmpty();
        }