internal uint AddLink()

in src/Session.cs [186:233]


        internal uint AddLink(Link link)
        {
            this.ThrowIfEnded("AddLink");
            lock (this.ThisLock)
            {
                int index = -1;
                int count = this.localLinks.Length;
                for (int i = 0; i < count; ++i)
                {
                    Link item = this.localLinks[i];
                    if (item == null)
                    {
                        if (index < 0)
                        {
                            index = i;
                        }
                    }
                    else
                    {
                        var cached = LinkId.Create(this.Connection, item.Role, item.Name);
                        var creating = LinkId.Create(this.Connection, link.Role, link.Name);
                        if (cached.Equals(creating))
                        {
                            throw new AmqpException(ErrorCode.NotAllowed, link.Name + " has been attached by a link with the same role.");
                        }
                    }
                }

                if (index >= 0)
                {
                    this.localLinks[index] = link;
                    return (uint)index;
                }

                if (count - 1 < this.handleMax)
                {
                    int size = (int)Math.Min(count * 2 - 1, this.handleMax) + 1;
                    Link[] expanded = new Link[size];
                    Array.Copy(this.localLinks, expanded, count);
                    this.localLinks = expanded;
                    this.localLinks[count] = link;
                    return (ushort)count;
                }

                throw new AmqpException(ErrorCode.NotAllowed,
                    Fx.Format(SRAmqp.AmqpHandleExceeded, this.handleMax + 1));
            }
        }