Пример #1
0
 public NetworkReader(byte[] buffer, NetworkSchema schema)
 {
     m_Input          = new ByteInputStream(buffer);
     m_Schema         = schema;
     m_CurrentField   = null;
     m_NextFieldIndex = 0;
 }
Пример #2
0
        public void Update()
        {
            if (m_Socket.Poll(0, SelectMode.SelectRead))
            {
                int read = m_Socket.ReceiveFrom(m_Buffer, m_Buffer.Length, SocketFlags.None, ref endpoint);
                if (read > 0)
                {
                    var reader = new ByteInputStream(m_Buffer);
                    var header = new SQPHeader();
                    header.FromStream(ref reader);

                    switch (m_State)
                    {
                    case SQPClientState.Idle:
                        break;

                    case SQPClientState.WaitingForChallange:
                        if ((SQPMessageType)header.Type == SQPMessageType.ChallangeResponse)
                        {
                            if (endpoint.Equals(m_Server))
                            {
                                ChallangeId = header.ChallangeId;
                                SendServerInfoQuery();
                            }
                        }
                        break;

                    case SQPClientState.WaitingForResponse:
                        if ((SQPMessageType)header.Type == SQPMessageType.QueryResponse)
                        {
                            reader.Reset();
                            var rsp = new SQP.ServerInfo();
                            rsp.FromStream(ref reader);
                            Debug.Log(string.Format("ServerName: {0}, BuildId: {1}, Current Players: {2}, Max Players: {3}, GameType: {4}, Map: {5}, Port: {6}",
                                                    rsp.ServerInfoData.ServerName,
                                                    rsp.ServerInfoData.BuildId,
                                                    (ushort)rsp.ServerInfoData.CurrentPlayers,
                                                    (ushort)rsp.ServerInfoData.MaxPlayers,
                                                    rsp.ServerInfoData.GameType,
                                                    rsp.ServerInfoData.Map,
                                                    (ushort)rsp.ServerInfoData.Port));
                            m_State   = SQPClientState.Success;
                            StartTime = NetworkUtils.stopwatch.ElapsedMilliseconds;
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
            var now = NetworkUtils.stopwatch.ElapsedMilliseconds;

            if (now - StartTime > 1000000)
            {
                Debug.Log("Failed");
                m_State = SQPClientState.Failure;
            }
        }
Пример #3
0
 public void FromStream(ref ByteInputStream reader)
 {
     Header.FromStream(ref reader);
     Version       = reader.ReadUInt16_NBO();
     CurrentPacket = reader.ReadUInt8();
     LastPacket    = reader.ReadUInt8();
     Length        = reader.ReadUInt16_NBO();
 }
Пример #4
0
            public void FromStream(ref ByteInputStream reader)
            {
                CurrentPlayers = reader.ReadUInt16_NBO();
                MaxPlayers     = reader.ReadUInt16_NBO();

                ServerName = reader.ReadString(encoding);
                GameType   = reader.ReadString(encoding);
                BuildId    = reader.ReadString(encoding);
                Map        = reader.ReadString(encoding);

                Port = reader.ReadUInt16_NBO();
            }
Пример #5
0
    public static void CopyFieldsFromBuffer <TOutputStream>(NetworkSchema schema, byte[] inputBuffer, ref TOutputStream output) where TOutputStream : NetworkCompression.IOutputStream
    {
        var input = new ByteInputStream(inputBuffer);

        int fieldIndex = 0;

        for (; fieldIndex < schema.fields.Count; ++fieldIndex)
        {
            var field = schema.fields[fieldIndex];
            switch (field.fieldType)
            {
            case NetworkSchema.FieldType.Bool:
            case NetworkSchema.FieldType.UInt:
            case NetworkSchema.FieldType.Int:
            case NetworkSchema.FieldType.Float:
                output.WriteRawBits(input.ReadBits(field.bits), field.bits);
                break;

            case NetworkSchema.FieldType.Vector2:
                output.WriteRawBits(input.ReadUInt32(), field.bits);
                output.WriteRawBits(input.ReadUInt32(), field.bits);
                break;

            case NetworkSchema.FieldType.Vector3:
                output.WriteRawBits(input.ReadUInt32(), field.bits);
                output.WriteRawBits(input.ReadUInt32(), field.bits);
                output.WriteRawBits(input.ReadUInt32(), field.bits);
                break;

            case NetworkSchema.FieldType.Quaternion:
                output.WriteRawBits(input.ReadUInt32(), field.bits);
                output.WriteRawBits(input.ReadUInt32(), field.bits);
                output.WriteRawBits(input.ReadUInt32(), field.bits);
                output.WriteRawBits(input.ReadUInt32(), field.bits);
                break;

            case NetworkSchema.FieldType.String:
            case NetworkSchema.FieldType.ByteArray:
            {
                byte[] data;
                int    dataIndex;
                int    dataSize;
                input.GetByteArray(out data, out dataIndex, out dataSize, field.arraySize);
                output.WritePackedUInt((uint)dataSize, field.startContext);
                output.WriteRawBytes(data, dataIndex, dataSize);
            }
            break;

            default: GameDebug.Assert(false); break;
            }
        }
    }
Пример #6
0
    public void CopyByteArray(ref ByteInputStream input, int maxCount)
    {
        int count = input.ReadUInt16();

        GameDebug.Assert(count <= maxCount);

        WriteUInt16((ushort)count);
        input.ReadBytes(m_Buffer, m_CurrentByteIdx, count, maxCount);
        for (int i = count; i < maxCount; i++)
        {
            m_Buffer[m_CurrentByteIdx + i] = 0;
        }
        m_CurrentByteIdx += maxCount;
    }
Пример #7
0
 public void FromStream(ref ByteInputStream reader)
 {
     Header.FromStream(ref reader);
     Version         = reader.ReadUInt16_NBO();
     RequestedChunks = reader.ReadUInt8();
 }
Пример #8
0
 public void Setup()
 {
     reader = new ByteInputStream(m_Buffer);
     writer = new ByteOutputStream(m_Buffer);
 }
Пример #9
0
    // Predict snapshot from baselines. Returns true if prediction is different from baseline 0 (if it need to be automatically predicted next frame).
    public static void PredictSnapshot(byte[] outputData, byte[] fieldsChangedPrediction, NetworkSchema schema, uint numBaselines, uint time0, byte[] baselineData0, uint time1, byte[] baselineData1, uint time2, byte[] baselineData2, uint time, byte fieldMask)
    {
        for (int i = 0, l = fieldsChangedPrediction.Length; i < l; ++i)
        {
            fieldsChangedPrediction[i] = 0;
        }

        if (numBaselines < 3)
        {
            System.Array.Copy(baselineData0, outputData, schema.GetByteSize());
            return;
        }

        var baselineStream0 = new ByteInputStream(baselineData0);
        var baselineStream1 = new ByteInputStream(baselineData1);
        var baselineStream2 = new ByteInputStream(baselineData2);
        var outputStream    = new ByteOutputStream(outputData);

        for (int i = 0; i < schema.fields.Count; ++i)
        {
            GameDebug.Assert(schema.fields[i].byteOffset == baselineStream0.GetBytePosition());
            GameDebug.Assert(schema.fields[i].byteOffset == baselineStream1.GetBytePosition());
            GameDebug.Assert(schema.fields[i].byteOffset == baselineStream2.GetBytePosition());

            var field = schema.fields[i];

            byte fieldByteOffset = (byte)((uint)i >> 3);
            byte fieldBitOffset  = (byte)((uint)i & 0x7);

            bool masked = (field.fieldMask & fieldMask) != 0;

            switch (field.fieldType)
            {
            case NetworkSchema.FieldType.Bool:
            {
                uint baseline0  = baselineStream0.ReadUInt8();
                uint baseline1  = baselineStream1.ReadUInt8();
                uint baseline2  = baselineStream2.ReadUInt8();
                uint prediction = baseline0;

                if (!masked)
                {
                    if (baseline0 != baseline1 && baseline1 != baseline2)
                    {
                        fieldsChangedPrediction[fieldByteOffset] |= (byte)(1 << fieldBitOffset);
                    }
                }

                outputStream.WriteUInt8((byte)prediction);
                break;
            }

            case NetworkSchema.FieldType.UInt:
            case NetworkSchema.FieldType.Int:
            case NetworkSchema.FieldType.Float:
            {
                uint baseline0 = (uint)baselineStream0.ReadBits(field.bits);
                uint baseline1 = (uint)baselineStream1.ReadBits(field.bits);
                uint baseline2 = (uint)baselineStream2.ReadBits(field.bits);

                uint prediction = baseline0;
                if (!masked)
                {
                    if (field.delta)
                    {
                        bool predictionLikelyWrong;
                        prediction = NetworkPrediction.PredictUint(numBaselines, time0, baseline0, time1, baseline1, time2, baseline2, time, out predictionLikelyWrong);
                        if (predictionLikelyWrong)
                        {
                            fieldsChangedPrediction[fieldByteOffset] |= (byte)(1 << fieldBitOffset);
                        }
                    }
                    else
                    {
                        if (baseline0 != baseline1 && baseline1 != baseline2)
                        {
                            fieldsChangedPrediction[fieldByteOffset] |= (byte)(1 << fieldBitOffset);
                        }
                    }
                }

                outputStream.WriteBits(prediction, field.bits);          //RUTODO: fix this
                break;
            }

            case NetworkSchema.FieldType.Vector2:
            {
                uint bx0 = baselineStream0.ReadUInt32();
                uint by0 = baselineStream0.ReadUInt32();

                uint bx1 = baselineStream1.ReadUInt32();
                uint by1 = baselineStream1.ReadUInt32();

                uint bx2 = baselineStream2.ReadUInt32();
                uint by2 = baselineStream2.ReadUInt32();

                uint px = bx0;
                uint py = by0;
                if (!masked)
                {
                    if (field.delta)
                    {
                        bool predictionLikelyWrongX;
                        bool predictionLikelyWrongY;
                        px = NetworkPrediction.PredictUint(numBaselines, time0, bx0, time1, bx1, time2, bx2, time, out predictionLikelyWrongX);
                        py = NetworkPrediction.PredictUint(numBaselines, time0, by0, time1, by1, time2, by2, time, out predictionLikelyWrongY);
                        if (predictionLikelyWrongX || predictionLikelyWrongY)
                        {
                            fieldsChangedPrediction[fieldByteOffset] |= (byte)(1 << fieldBitOffset);
                        }
                    }
                    else
                    {
                        if ((bx0 != bx1 || by0 != by1) && (bx1 != bx2 || by1 != by2))
                        {
                            fieldsChangedPrediction[fieldByteOffset] |= (byte)(1 << fieldBitOffset);
                        }
                    }
                }

                outputStream.WriteUInt32(px);
                outputStream.WriteUInt32(py);
                break;
            }

            case NetworkSchema.FieldType.Vector3:
            {
                uint bx0 = baselineStream0.ReadUInt32();
                uint by0 = baselineStream0.ReadUInt32();
                uint bz0 = baselineStream0.ReadUInt32();

                uint bx1 = baselineStream1.ReadUInt32();
                uint by1 = baselineStream1.ReadUInt32();
                uint bz1 = baselineStream1.ReadUInt32();

                uint bx2 = baselineStream2.ReadUInt32();
                uint by2 = baselineStream2.ReadUInt32();
                uint bz2 = baselineStream2.ReadUInt32();

                uint px = bx0;
                uint py = by0;
                uint pz = bz0;

                if (!masked)
                {
                    if (field.delta)
                    {
                        bool predictionLikelyWrongX;
                        bool predictionLikelyWrongY;
                        bool predictionLikelyWrongZ;
                        px = NetworkPrediction.PredictUint(numBaselines, time0, bx0, time1, bx1, time2, bx2, time, out predictionLikelyWrongX);
                        py = NetworkPrediction.PredictUint(numBaselines, time0, by0, time1, by1, time2, by2, time, out predictionLikelyWrongY);
                        pz = NetworkPrediction.PredictUint(numBaselines, time0, bz0, time1, bz1, time2, bz2, time, out predictionLikelyWrongZ);

                        if (predictionLikelyWrongX || predictionLikelyWrongY || predictionLikelyWrongZ)
                        {
                            fieldsChangedPrediction[fieldByteOffset] |= (byte)(1 << fieldBitOffset);
                        }
                    }
                    else
                    {
                        if ((bx0 != bx1 || by0 != by1 || bz0 != bz1) && (bx1 != bx2 || by1 != by2 || bz1 != bz2))
                        {
                            fieldsChangedPrediction[fieldByteOffset] |= (byte)(1 << fieldBitOffset);
                        }
                    }
                }

                outputStream.WriteUInt32(px);
                outputStream.WriteUInt32(py);
                outputStream.WriteUInt32(pz);
                break;
            }

            case NetworkSchema.FieldType.Quaternion:
            {
                uint bx0 = baselineStream0.ReadUInt32();
                uint by0 = baselineStream0.ReadUInt32();
                uint bz0 = baselineStream0.ReadUInt32();
                uint bw0 = baselineStream0.ReadUInt32();

                uint bx1 = baselineStream1.ReadUInt32();
                uint by1 = baselineStream1.ReadUInt32();
                uint bz1 = baselineStream1.ReadUInt32();
                uint bw1 = baselineStream1.ReadUInt32();

                uint bx2 = baselineStream2.ReadUInt32();
                uint by2 = baselineStream2.ReadUInt32();
                uint bz2 = baselineStream2.ReadUInt32();
                uint bw2 = baselineStream2.ReadUInt32();

                uint px = bx0;
                uint py = by0;
                uint pz = bz0;
                uint pw = bw0;

                if (!masked)
                {
                    if (field.delta)
                    {
                        bool predictionLikelyWrongX;
                        bool predictionLikelyWrongY;
                        bool predictionLikelyWrongZ;
                        bool predictionLikelyWrongW;
                        px = NetworkPrediction.PredictUint(numBaselines, time0, bx0, time1, bx1, time2, bx2, time, out predictionLikelyWrongX);
                        py = NetworkPrediction.PredictUint(numBaselines, time0, by0, time1, by1, time2, by2, time, out predictionLikelyWrongY);
                        pz = NetworkPrediction.PredictUint(numBaselines, time0, bz0, time1, bz1, time2, bz2, time, out predictionLikelyWrongZ);
                        pw = NetworkPrediction.PredictUint(numBaselines, time0, bw0, time1, bw1, time2, bw2, time, out predictionLikelyWrongW);

                        if (predictionLikelyWrongX || predictionLikelyWrongY || predictionLikelyWrongZ || predictionLikelyWrongW)
                        {
                            fieldsChangedPrediction[fieldByteOffset] |= (byte)(1 << fieldBitOffset);
                        }
                    }
                    else
                    {
                        if ((bx0 != bx1 || by0 != by1 || bz0 != bz1 || bw0 != bw1) && (bx1 != bx2 || by1 != by2 || bz1 != bz2 || bw1 != bw2))
                        {
                            fieldsChangedPrediction[fieldByteOffset] |= (byte)(1 << fieldBitOffset);
                        }
                    }
                }

                outputStream.WriteUInt32(px);
                outputStream.WriteUInt32(py);
                outputStream.WriteUInt32(pz);
                outputStream.WriteUInt32(pw);
                break;
            }

            case NetworkSchema.FieldType.String:
            case NetworkSchema.FieldType.ByteArray:
            {
                baselineStream1.SkipByteArray(field.arraySize);
                baselineStream2.SkipByteArray(field.arraySize);
                outputStream.CopyByteArray(ref baselineStream0, field.arraySize);
                //TODO: predict me!
            }
            break;
            }
        }

        //fieldsChangedPrediction = 0;

        outputStream.Flush();
    }
Пример #10
0
    public static int Read <TInputStream>(ref TInputStream input, NetworkSchema schema, byte[] outputData, byte[] baselineData, byte[] fieldsChangedPrediction, byte fieldMask, ref uint hash) where TInputStream : NetworkCompression.IInputStream
    {
        GameDebug.Assert(baselineData != null);
        var outputStream   = new ByteOutputStream(outputData);
        var baselineStream = new ByteInputStream(baselineData);

        int numFields = schema.fields.Count;

        int skipContext = schema.id * NetworkConfig.maxContextsPerSchema + NetworkConfig.firstSchemaContext;

        for (int i = 0; i * 8 < numFields; i++)
        {
            uint value = input.ReadPackedNibble(skipContext + 2 * i + 0);
            value |= input.ReadPackedNibble(skipContext + 2 * i + 1) << 4;
            fieldsNotPredicted[i] = (byte)(value ^ fieldsChangedPrediction[i]);
        }

        for (int i = 0; i < numFields; ++i)
        {
            GameDebug.Assert(schema.fields[i].byteOffset == baselineStream.GetBytePosition());
            int fieldStartContext = schema.fields[i].startContext;

            var field = schema.fields[i];

            byte fieldByteOffset = (byte)((uint)i >> 3);
            byte fieldBitOffset  = (byte)((uint)i & 0x7);

            bool skip   = (fieldsNotPredicted[fieldByteOffset] & (1 << fieldBitOffset)) == 0;
            bool masked = ((field.fieldMask & fieldMask) != 0);

            skip = skip || masked;

            switch (field.fieldType)
            {
            case NetworkSchema.FieldType.Bool:
            {
                uint value = baselineStream.ReadUInt8();
                if (!skip)
                {
                    value = input.ReadRawBits(1);
                }

                if (!masked)
                {
                    hash = NetworkUtils.SimpleHashStreaming(hash, value);
                }

                outputStream.WriteUInt8((byte)value);
                break;
            }

            case NetworkSchema.FieldType.UInt:
            case NetworkSchema.FieldType.Int:
            case NetworkSchema.FieldType.Float:
            {
                uint baseline = (uint)baselineStream.ReadBits(field.bits);

                uint value = baseline;
                if (!skip)
                {
                    if (field.delta)
                    {
                        value = input.ReadPackedUIntDelta(baseline, fieldStartContext);
                    }
                    else
                    {
                        value = input.ReadRawBits(field.bits);
                    }
                }

                if (!masked)
                {
                    hash = NetworkUtils.SimpleHashStreaming(hash, value);
                }

                outputStream.WriteBits(value, field.bits);          //RUTODO: fix this
                break;
            }

            case NetworkSchema.FieldType.Vector2:
            {
                uint bx = baselineStream.ReadUInt32();
                uint by = baselineStream.ReadUInt32();

                uint vx = bx;
                uint vy = by;
                if (!skip)
                {
                    if (field.delta)
                    {
                        vx = input.ReadPackedUIntDelta(bx, fieldStartContext + 0);
                        vy = input.ReadPackedUIntDelta(by, fieldStartContext + 1);
                    }
                    else
                    {
                        vx = input.ReadRawBits(field.bits);
                        vy = input.ReadRawBits(field.bits);
                    }
                }

                if (!masked)
                {
                    hash = NetworkUtils.SimpleHashStreaming(hash, vx);
                    hash = NetworkUtils.SimpleHashStreaming(hash, vy);
                }

                outputStream.WriteUInt32(vx);
                outputStream.WriteUInt32(vy);

                break;
            }

            case NetworkSchema.FieldType.Vector3:
            {
                uint bx = baselineStream.ReadUInt32();
                uint by = baselineStream.ReadUInt32();
                uint bz = baselineStream.ReadUInt32();

                uint vx = bx;
                uint vy = by;
                uint vz = bz;

                if (!skip)
                {
                    if (field.delta)
                    {
                        vx = input.ReadPackedUIntDelta(bx, fieldStartContext + 0);
                        vy = input.ReadPackedUIntDelta(by, fieldStartContext + 1);
                        vz = input.ReadPackedUIntDelta(bz, fieldStartContext + 2);
                    }
                    else
                    {
                        vx = input.ReadRawBits(field.bits);
                        vy = input.ReadRawBits(field.bits);
                        vz = input.ReadRawBits(field.bits);
                    }
                }

                if (!masked)
                {
                    hash = NetworkUtils.SimpleHashStreaming(hash, vx);
                    hash = NetworkUtils.SimpleHashStreaming(hash, vy);
                    hash = NetworkUtils.SimpleHashStreaming(hash, vz);
                }

                outputStream.WriteUInt32(vx);
                outputStream.WriteUInt32(vy);
                outputStream.WriteUInt32(vz);
                break;
            }

            case NetworkSchema.FieldType.Quaternion:
            {
                uint bx = baselineStream.ReadUInt32();
                uint by = baselineStream.ReadUInt32();
                uint bz = baselineStream.ReadUInt32();
                uint bw = baselineStream.ReadUInt32();

                uint vx = bx;
                uint vy = by;
                uint vz = bz;
                uint vw = bw;

                if (!skip)
                {
                    if (field.delta)
                    {
                        vx = input.ReadPackedUIntDelta(bx, fieldStartContext + 0);
                        vy = input.ReadPackedUIntDelta(by, fieldStartContext + 1);
                        vz = input.ReadPackedUIntDelta(bz, fieldStartContext + 2);
                        vw = input.ReadPackedUIntDelta(bw, fieldStartContext + 3);
                        //RUTODO: normalize
                    }
                    else
                    {
                        vx = input.ReadRawBits(field.bits);
                        vy = input.ReadRawBits(field.bits);
                        vz = input.ReadRawBits(field.bits);
                        vw = input.ReadRawBits(field.bits);
                    }
                }

                if (!masked)
                {
                    hash = NetworkUtils.SimpleHashStreaming(hash, vx);
                    hash = NetworkUtils.SimpleHashStreaming(hash, vy);
                    hash = NetworkUtils.SimpleHashStreaming(hash, vz);
                    hash = NetworkUtils.SimpleHashStreaming(hash, vw);
                }

                outputStream.WriteUInt32(vx);
                outputStream.WriteUInt32(vy);
                outputStream.WriteUInt32(vz);
                outputStream.WriteUInt32(vw);
                break;
            }

            case NetworkSchema.FieldType.String:
            case NetworkSchema.FieldType.ByteArray:
            {
                // TODO : Do a better job with deltaing strings and buffers
                if (!skip)
                {
                    baselineStream.SkipByteArray(field.arraySize);
                    outputStream.CopyByteArray <TInputStream>(ref input, field.arraySize, fieldStartContext);
                }
                else
                {
                    outputStream.CopyByteArray(ref baselineStream, field.arraySize);
                }

                if (!masked)
                {
                    hash += 0;         // TODO (hash strings and bytearrays as well)
                }
            }
            break;
            }
        }
        outputStream.Flush();
        return(outputStream.GetBytePosition());
    }
Пример #11
0
 public void CopyBytes(ref ByteInputStream input, int count)
 {
     input.ReadBytes(m_Buffer, m_CurrentByteIdx, count, count);
     m_CurrentByteIdx += count;
 }