Esempio n. 1
0
        public void write(Packet packet, Channel c, int length)
        {
            while(true)
            {
                if(in_kex)
                {
                    try{Thread.Sleep(10);}
                    catch(ThreadInterruptedException e){};
                    continue;
                }
                lock(c)
                {
                    if(c.rwsize>=length)
                    {
                        c.rwsize-=length;
                        break;
                    }
                }
                if(c._close || !c.isConnected())
                {
                    throw new IOException("channel is broken");
                }

                bool sendit=false;
                int s=0;
                byte command=0;
                int recipient=-1;
                lock(c)
                {
                    if(c.rwsize>0)
                    {
                        int len=c.rwsize;
                        if(len>length)
                        {
                            len=length;
                        }
                        if(len!=length)
                        {
                            s=packet.shift(len, (c2smac!=null ? c2smac.getBlockSize() : 0));
                        }
                        command=packet.buffer.buffer[5];
                        recipient=c.getRecipient();
                        length-=len;
                        c.rwsize-=len;
                        sendit=true;
                    }
                }
                if(sendit)
                {
                    _write(packet);
                    if(length==0)
                    {
                        return;
                    }
                    packet.unshift(command, recipient, s, length);
                    lock(c)
                    {
                        if(c.rwsize>=length)
                        {
                            c.rwsize-=length;
                            break;
                        }
                    }
                }

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