Esempio n. 1
0
        public void write(Packet packet, Channel c, int length)
        {
            while (true)
            {
                if (m_in_kex)
                {
                    try
                    {
                        Thread.Sleep(10);
                    }
                    catch (ThreadInterruptedException)
                    { }
                    continue;
                }
                lock (c)
                {
                    if (c.RemoteWindowSize >= length)
                    {
                        c.RemoteWindowSize -= length;
                        break;
                    }
                }

                if (c.IsClosed || !c.isConnected())
                    throw new IOException("channel is broken");

                bool sendit = false;
                int s = 0;
                byte command = 0;
                int recipient = -1;
                lock (c)
                {
                    if (c.RemoteWindowSize > 0)
                    {
                        int len = c.RemoteWindowSize;
                        if (len > length)
                            len = length;

                        if (len != length)
                            s = packet.shift(len, (m_c2smac != null ? m_c2smac.BlockSize : 0));

                        command = packet.m_buffer.m_buffer[5];
                        recipient = c.Recipient;
                        length -= len;
                        c.RemoteWindowSize -= len;
                        sendit = true;
                    }
                }
                if (sendit)
                {
                    _write(packet);
                    if (length == 0)
                        return;

                    packet.unshift(command, recipient, s, length);
                    lock (c)
                    {
                        if (c.RemoteWindowSize >= length)
                        {
                            c.RemoteWindowSize -= length;
                            break;
                        }
                    }
                }

                try
                {
                    Thread.Sleep(100);
                }
                catch (ThreadInterruptedException)
                { }
            }
            _write(packet);
        }