public void Incoming(Bitstream.Buffer stream)
        {
            byte seq = (byte)Bitstream.ReadBits(stream, 8);

            if (stream.error != 0)
            {
                Error("Broken packet");
                return;
            }

            byte diff = (byte)(seq - _recvTail);
            if (diff > 200)
            {
                // too old.
                return;
            }
            else
            {
                // update head if newer.
                byte newHead = (byte)(seq + 1);
                diff = (byte)(newHead - _recvHead);
                if (diff < 100)
                    _recvHead = newHead;
            }

            Bitstream.SyncByte(stream);
            Bitstream.Copy(_recv[seq], stream);
        }
 public void Send(Bitstream.Buffer stream)
 {
     Bitstream.Buffer buf = Bitstream.Buffer.Make(new byte[stream.bufsize + 8]);
     Bitstream.PutBits(buf, 8, _sendPos++);
     Bitstream.Insert(buf, stream);
     buf.Flip();
     _send.Add(buf);
 }
        public void Incoming(Bitstream.Buffer stream)
        {
            // read ack numbers
            while (true)
            {
                uint hasAck = Bitstream.ReadBits(stream, 1);
                if (hasAck == 0 || stream.error != 0)
                    break;
                byte idx = (byte)Bitstream.ReadBits(stream, 8);
                if (stream.error != 0)
                {
                    Error("Broken packet");
                    return;
                }
                // free up the slot.
                _sent[idx].buf = null;
            }

            if (stream.BitsLeft() < 8)
                return;

            byte seq = (byte)Bitstream.ReadBits(stream, 8);
            if (stream.error != 0)
            {
                Error("Broken packet");
                return;
            }

            // add to ack list.
            if ((byte)(_ackOutHead + 1) == _ackOutTail)
                Error("AckOut is full");
            else
                _ackOut[_ackOutHead++] = seq;

            byte diff = (byte)(seq - _recvPos);
            if (diff > 128)
                // old packet, thorw.
                return;

            if (_recv[seq].buf == null)
                _recvPending++;

            Bitstream.SyncByte(stream);
            Bitstream.Copy(_recv[seq], stream);
        }
 public void Send(Bitstream.Buffer stream)
 {
     if (_sent[_sendHead].buf != null)
         Error("Send queue full!");
     _sendTimer[_sendHead] = 0.0f;
     Bitstream.Copy(_sent[_sendHead++], stream);
 }
        public void WrapOut(Bitstream.Buffer dest, Bitstream.Buffer src, byte seq)
        {
            // Format in all the ack outs.
            while (_ackOutTail != _ackOutHead)
            {
                Bitstream.PutBits(dest, 1, 1);
                Bitstream.PutBits(dest, 8, _ackOut[_ackOutTail]);
                _ackOutTail++;
            }

            Bitstream.PutBits(dest, 1, 0);

            if (src != null)
            {
                int bytepos = src.bytepos;
                int bitpos = src.bitpos;
                Bitstream.PutBits(dest, 8, seq);

                // byte align where actual data is
                Bitstream.SyncByte(dest);
                Bitstream.Insert(dest, src);
                src.bytepos = bytepos;
                src.bitpos = bitpos;
            }
        }