public PacketLaneReliableOrdered()
 {
     for (int i = 0; i < 256; i++)
     {
         _sent[i] = new Bitstream.Buffer();
         _recv[i] = new Bitstream.Buffer();
     }
 }
Exemplo n.º 2
0
        public static void Insert(Buffer dest, Buffer source)
        {
            Bitstream.Buffer tmp = new Bitstream.Buffer();
            Copy(tmp, source);

            while (tmp.BitsLeft() > 0)
            {
                if (tmp.bitpos > 0)
                {
                    int bits = 8 - tmp.bitpos;
                    if (bits > tmp.BitsLeft())
                        bits = tmp.BitsLeft();
                    Bitstream.PutBits(dest, bits, Bitstream.ReadBits(tmp, bits));
                }
                if (tmp.BitsLeft() > 32)
                    Bitstream.PutBits(dest, 32, Bitstream.ReadBits(tmp, 32));
                if (tmp.BitsLeft() >= 8)
                    Bitstream.PutBits(dest, 8, Bitstream.ReadBits(tmp, 8));
            }
        }
Exemplo n.º 3
0
    public void Run(string[] args)
    {
        string sockfile = "/tmp/" + System.IO.Path.GetRandomFileName();

        Console.WriteLine("Waiting for turbo at [" + sockfile + "]");

        Socket s = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);

        s.Bind(new Mono.Unix.UnixEndPoint(sockfile));
        s.Listen(1);

        System.Diagnostics.Process.Start("/tmp/turbonet", sockfile);

        Socket conn = s.Accept();

        Console.WriteLine("Cube connected to turbo process.");

        byte[] buf = new byte[256 * 1024];
        int    readpos = 0, parsepos = 0;

        netki.Bitstream.Buffer bufwrap = new netki.Bitstream.Buffer();
        bufwrap.buf = buf;

        while (true)
        {
            int rd = conn.Receive(buf, readpos, buf.Length - readpos, SocketFlags.None);
            if (rd <= 0)
            {
                Console.WriteLine("Socket closed!");
                break;
            }

            readpos += rd;

            // Start parsing from the start.
            bufwrap.bitpos  = 0;
            bufwrap.bytepos = parsepos;
            bufwrap.error   = 0;
            bufwrap.bufsize = readpos;

            int parsed = 0;
            while (true)
            {
                if (bufwrap.bufsize - bufwrap.bytepos < 2)
                {
                    break;
                }

                System.UInt32 id = netki.Bitstream.ReadBits(bufwrap, 16);
                if (bufwrap.error != 0)
                {
                    Console.WriteLine("read id error");
                    break;
                }

                bool decoded = false;
                switch (id)
                {
                case netki.CubeTurboInfo.TYPE_ID:
                {
                    netki.CubeTurboInfo res = new netki.CubeTurboInfo();
                    if (netki.CubeTurboInfo.ReadFromBitstream(bufwrap, res))
                    {
                        OnTurboInfo(res);
                        decoded = true;
                    }
                    break;
                }

                case netki.CubeClientConnect.TYPE_ID:
                {
                    netki.CubeClientConnect res = new netki.CubeClientConnect();
                    if (netki.CubeClientConnect.ReadFromBitstream(bufwrap, res))
                    {
                        OnTurboClientConnect(res);
                        decoded = true;
                    }
                    break;
                }

                case netki.CubeClientDisconnect.TYPE_ID:
                {
                    netki.CubeClientDisconnect res = new netki.CubeClientDisconnect();
                    if (netki.CubeClientDisconnect.ReadFromBitstream(bufwrap, res))
                    {
                        OnTurboClientDisconnect(res);
                        decoded = true;
                    }
                    break;
                }

                case netki.CubeClientData.TYPE_ID:
                {
                    netki.CubeClientData res = new netki.CubeClientData();
                    if (netki.CubeClientData.ReadFromBitstream(bufwrap, res))
                    {
                        OnTurboClientData(res);
                        decoded = true;
                    }
                    break;
                }

                default:
                    Console.WriteLine("Turbo: unknown packet id " + id);
                    break;
                }

                if (decoded)
                {
                    netki.Bitstream.SyncByte(bufwrap);
                    parsed = bufwrap.bytepos;
                }
                else
                {
                    break;
                }
            }

            if (parsed > 0)
            {
                parsepos = parsed;
            }

            if (readpos == buf.Length)
            {
                Console.WriteLine("Turbo: read buffer exhausted!");
                break;
            }

            if (readpos == parsepos)
            {
                readpos  = 0;
                parsepos = 0;
            }
            else if (parsepos > 65536)
            {
                for (int i = parsepos; i < readpos; i++)
                {
                    buf[i - parsepos] = buf[i];
                }
                readpos -= parsepos;
                parsepos = 0;
            }
        }
    }
        public netki.Bitstream.Buffer Update(float dt, PacketLaneOutput outputFn)
        {
            // dequeue in order.
            if (_recv[_recvPos].buf != null)
            {
                _recvPending--;
                Bitstream.Buffer ret = new Bitstream.Buffer();
                Bitstream.Copy(ret, _recv[_recvPos]);
                _recv[_recvPos++].buf = null;
                return ret;
            }

            // no send permitted.
            if (dt < 0)
                return null;

            // advance ack tail.
            while (_sendAckTail != _sendHead && _sent[_sendAckTail].buf == null)
                _sendAckTail++;

            // all outgoing packets
            for (byte i = _sendAckTail; i != _sendHead; i++)
            {
                if (_sent[i].buf == null)
                    continue;

                // If pending sends
                if (_sendTimer[i] <= 0.0f)
                {
                    Bitstream.Buffer buf = Bitstream.Buffer.Make(new byte[1024]);
                    WrapOut(buf, _sent[i], i);
                    buf.Flip();
                    outputFn(buf);
                    _ackFlushTimer = 0.0f;
                    if (_sendTimer[i] < 0.0f)
                        _resentTotal++;
                    _sendTimer[i] = _resendTime;
                    _sentTotal++;
                }
                else
                {
                    _sendTimer[i] -= dt;
                    if (_sendTimer[i] == 0)
                        _sendTimer[i] = -0.01f;
                }
            }

            if (_ackOutHead != _ackOutTail)
            {
                _ackFlushTimer += dt;
                if (_ackFlushTimer > 0.30f * _resendTime)
                {
                    Bitstream.Buffer buf = Bitstream.Buffer.Make(new byte[1024]);
                    WrapOut(buf, null, 0);
                    buf.Flip();
                    outputFn(buf);
                }
            }

            return null;
        }
        public Bitstream.Buffer Update(float dt, PacketLaneOutput outputFn)
        {
            while (_recvTail != _recvHead)
            {
                if (_recv[_recvTail].buf != null)
                {
                    Bitstream.Buffer ret = new Bitstream.Buffer();
                    Bitstream.Copy(ret, _recv[_recvTail]);
                    _recv[_recvTail++].buf = null;
                    _recvTotal++;
                    return ret;
                }
                else
                {
                    _recvGaps++;
                    _recvTotal++;
                    _recvTail++;
                }
            }

            if (dt >= 0)
            {
                foreach (Bitstream.Buffer b in _send)
                {
                    outputFn(b);
                }
                _send.Clear();
            }

            return null;
        }
 public PacketLaneUnreliableOrdered()
 {
     for (int i = 0; i < 256; i++)
         _recv[i] = new Bitstream.Buffer();
 }