private static JToken ReadTypeEncoded(this IBinaryInputStream stream)
        {
            switch (stream.ReadByte())
            {
            case 1:
                return(stream.ReadBooleanEncoded());

            case 2:
                return(stream.ReadArrayEncoded());

            case 3:
                return(stream.ReadIntEncoded());

            case 4:
                return(stream.ReadJsonEncoded());

            case 5:
                return(stream.ReadStringEncoded());

            case 6:
                return(stream.ReadByte());

            case 7:
                return(stream.ReadLongEncoded());

            case 8:
                return(stream.ReadFloatEncoded());

            case 9:
                return(stream.ReadDoubleEncoded());
            }
            return(null);
        }
        public static string ReadStringEncoded(this IBinaryInputStream stream)
        {
            byte[] value = stream.ReadBytes();
            if (value.Length <= 0)
            {
                return(null);
            }

            return(Encoding.UTF8.GetString(value));
        }
        public static double?ReadDoubleEncoded(this IBinaryInputStream stream)
        {
            double value = stream.ReadDouble();

            if (value == int.MinValue)
            {
                return(null);
            }

            return(value);
        }
        public static float?ReadFloatEncoded(this IBinaryInputStream stream)
        {
            float value = stream.ReadFloat();

            if (value == int.MinValue)
            {
                return(null);
            }

            return(value);
        }
        public static long?ReadLongEncoded(this IBinaryInputStream stream)
        {
            long value = stream.ReadLong();

            if (value == int.MinValue)
            {
                return(null);
            }

            return(value);
        }
        public static int?ReadIntEncoded(this IBinaryInputStream stream)
        {
            int value = stream.ReadInt();

            if (value == int.MinValue)
            {
                return(null);
            }

            return(value);
        }
        public static bool?ReadBooleanEncoded(this IBinaryInputStream stream)
        {
            byte value = stream.ReadByte();

            if (value == 0)
            {
                return(null);
            }

            return(value == 2);
        }
Exemplo n.º 8
0
        public static object Decode(IBinaryInputStream data)
        {
            byte type = data.ReadByte();

            switch (type)
            {
            case 42:
                return(new LowLevelConnectMessage());

            case APlayCodec.MSGTYPE_PAYLOAD:
                APlayStringMessage msg = new APlayStringMessage();
                msg.SetDataAsString(data.ReadString());
                return(msg);

            case APlayCodec.MSGTYPE_PAYLOAD_BINARY_JSON:
                GameContext.Logger.LogDebug($"Package [MSGTYPE_PAYLOAD_BINARY_JSON] received: {data.ReadInt()} byte(s)");

                APlayStringMessage msg2 = new APlayStringMessage();
                msg2.SetDataAsJson(APlayBinaryMessage.ReadJsonEncoded(data));
                return(msg2);

            case APlayCodec.MSGTYPE_PAYLOAD_GZIP:     // this even used?
                APlayStringMessage msg3 = new APlayStringMessage();
                msg3.SetDataAsString(StringCompressor.Decompress(data.ReadBytes()));
                return(msg3);

            case APlayCodec.MSGTYPE_LOWLEVEL_PING:
                GameContext.Logger.LogDebug($"Package [MSGTYPE_LOWLEVEL_PING] received: {data.ReadInt()} byte(s)");

                return(new LowLevelPingMessage {
                    PingTime = data.ReadLong(),
                    LastRTT = data.ReadInt()
                });

            case APlayCodec.MSGTYPE_LOWLEVEL_PONG:
                GameContext.Logger.LogDebug($"Package [MSGTYPE_LOWLEVEL_PONG] received: {data.ReadInt()} byte(s)");

                return(new LowLevelPongMessage {
                    PingTime = data.ReadLong()
                });

            case APlayCodec.MSGTYPE_LOWLEVEL_INTRODUCTION:
                return(new LowLevelIntroductionMessage {
                    AddressString = data.ReadString()
                });
            }
            return(null);
        }
        public static JObject ReadJsonEncoded(this IBinaryInputStream stream)
        {
            int size = stream.ReadInt();

            if (size < 0)
            {
                return(null);
            }

            JObject keyValues = new JObject();

            for (int i = 0; i < size; i++)
            {
                keyValues.Add(stream.ReadStringEncoded(), stream.ReadTypeEncoded());
            }
            return(keyValues);
        }
        public static JArray ReadArrayEncoded(this IBinaryInputStream stream)
        {
            int size = stream.ReadInt();

            if (size < 0)
            {
                return(null);
            }

            JArray holder = new JArray();

            for (int i = 0; i < size; i++)
            {
                holder.Add(ReadTypeEncoded(stream));
            }
            return(holder);
        }