private static bool DecodeError(MsgPackReader reader, out JsError error)
 {
     error = default;
     if (!reader.ReadArrayHeader(out var errorLen) || errorLen != 3)
     {
         return(false);
     }
     error.Name    = reader.ReadString();
     error.Message = reader.ReadString();
     error.Stack   = reader.ReadString();
     return(true);
 }
        private Message ReadCompletion(MsgPackReader reader)
        {
            // https://github.com/aspnet/AspNetCore/blob/master/src/SignalR/docs/specs/HubProtocol.md#completion-message-encoding-1

            ReadHeaders(reader);
            string invocationId = reader.ReadString();
            byte   resultKind   = reader.ReadByte();

            switch (resultKind)
            {
            // 1 - Error result - Result contains a String with the error message
            case 1:
                string error = reader.ReadString();
                return(new Message
                {
                    type = MessageTypes.Completion,
                    invocationId = invocationId,
                    error = error
                });

            // 2 - Void result - Result is absent
            case 2:
                return(new Message
                {
                    type = MessageTypes.Completion,
                    invocationId = invocationId
                });

            // 3 - Non-Void result - Result contains the value returned by the server
            case 3:
                object item = ReadItem(reader, invocationId);
                return(new Message
                {
                    type = MessageTypes.Completion,
                    invocationId = invocationId,
                    item = item,
                    result = item
                });

            default:
                throw new NotImplementedException("Unknown resultKind: " + resultKind);
            }
        }
        public void NullString()
        {
            MsgPackWriter writer = new MsgPackWriter(Allocator.Temp);

            writer.WriteNil();

            MsgPackReader reader = new MsgPackReader(writer.ToArray(Allocator.Temp));

            Assert.That(reader.ReadString(), Is.Null);
        }
        private Message ReadInvocation(MsgPackReader reader)
        {
            // https://github.com/aspnet/AspNetCore/blob/master/src/SignalR/docs/specs/HubProtocol.md#invocation-message-encoding-1

            ReadHeaders(reader);
            string invocationId = reader.ReadString();
            string target       = reader.ReadString();

            object[] arguments = ReadArguments(reader, target);
            string[] streamIds = ReadStreamIds(reader);

            return(new Message
            {
                type = MessageTypes.Invocation,
                invocationId = invocationId,
                target = target,
                arguments = arguments,
                streamIds = streamIds
            });
        }
        public void StringFixedLen()
        {
            MsgPackWriter writer     = new MsgPackWriter(Allocator.Temp);
            var           testString = GetRandomizedAsciiCharactersString(4);

            writer.WriteString(testString);

            MsgPackReader reader = new MsgPackReader(writer.ToArray(Allocator.Temp));

            Assert.That(reader.ReadString(), Is.EqualTo(testString));
        }
        public void LargeUnicodeString()
        {
            MsgPackWriter writer     = new MsgPackWriter(Allocator.Temp);
            var           testString = GetRandomizedUnicodeCharactersString(ushort.MaxValue + 1);

            writer.WriteString(testString);

            MsgPackReader reader = new MsgPackReader(writer.ToArray(Allocator.Temp));

            Assert.That(reader.ReadString(), Is.EqualTo(testString));
        }
        private Message ReadCancelInvocation(MsgPackReader reader)
        {
            // https://github.com/aspnet/AspNetCore/blob/master/src/SignalR/docs/specs/HubProtocol.md#cancelinvocation-message-encoding-1

            ReadHeaders(reader);
            string invocationId = reader.ReadString();

            return(new Message
            {
                type = MessageTypes.CancelInvocation,
                invocationId = invocationId
            });
        }
        private Message ReadStreamItem(MsgPackReader reader)
        {
            // https://github.com/aspnet/AspNetCore/blob/master/src/SignalR/docs/specs/HubProtocol.md#streamitem-message-encoding-1

            ReadHeaders(reader);
            string invocationId = reader.ReadString();
            object item         = ReadItem(reader, invocationId);

            return(new Message
            {
                type = MessageTypes.StreamItem,
                invocationId = invocationId,
                item = item
            });
        }
        public static Sphere ReadSphere(this MsgPackReader reader)
        {
            var sphere = new Sphere();

            reader.ReadPrefix(TypePrefixes.FixMap);
            for (int i = 0; i < 2; i++)
            {
                var key = reader.ReadString();
                switch (key)
                {
                case "center": sphere.Center = reader.ReadPoint3d(); break;

                case "radius": sphere.Radius = reader.ReadDouble(); break;

                default: throw new InvalidOperationException(Resources.ParseError);
                }
            }
            return(sphere);
        }
        private Message ReadClose(MsgPackReader reader)
        {
            // https://github.com/aspnet/AspNetCore/blob/master/src/SignalR/docs/specs/HubProtocol.md#close-message-encoding-1

            string error          = reader.ReadString();
            bool   allowReconnect = false;

            try
            {
                allowReconnect = reader.ReadBoolean();
            }
            catch { }

            return(new Message
            {
                type = MessageTypes.Close,
                error = error,
                allowReconnect = allowReconnect
            });
        }
        public static Ellipse ReadEllipse(this MsgPackReader reader)
        {
            var ellipse = new Ellipse();

            reader.ReadPrefix(TypePrefixes.FixMap);
            for (int i = 0; i < 3; i++)
            {
                var key = reader.ReadString();
                switch (key)
                {
                case "center": ellipse.Center = reader.ReadPoint2d(); break;

                case "angle": ellipse.Angle = reader.ReadDouble(); break;

                case "axes": ellipse.Axes = reader.ReadPoint2d(); break;

                default: throw new InvalidOperationException(Resources.ParseError);
                }
            }
            return(ellipse);
        }
        public static Circle3d ReadCircle3d(this MsgPackReader reader)
        {
            var circle = new Circle3d();

            reader.ReadPrefix(TypePrefixes.FixMap);
            for (int i = 0; i < 3; i++)
            {
                var key = reader.ReadString();
                switch (key)
                {
                case "center": circle.Center = reader.ReadPoint3d(); break;

                case "radius": circle.Radius = reader.ReadDouble(); break;

                case "normal": circle.Normal = reader.ReadPoint3d(); break;

                default: throw new InvalidOperationException(Resources.ParseError);
                }
            }
            return(circle);
        }
Example #13
0
        public IncomingPacket Parse(SocketManager manager, BufferSegment data, TransportEventTypes transportEvent = TransportEventTypes.Unknown)
        {
            using (var stream = new System.IO.MemoryStream(data.Data, data.Offset, data.Count))
            {
                var buff = BufferPool.Get(MsgPackReader.DEFAULT_BUFFER_SIZE, true);
                try
                {
                    var context = new SerializationContext
                    {
                        Options = SerializationOptions.SuppressTypeInformation/*,
                                                                               * ExtensionTypeHandler = CustomMessagePackExtensionTypeHandler.Instance*/
                    };
                    IJsonReader reader = new MsgPackReader(stream, context, Endianness.BigEndian, buff);

                    reader.ReadObjectBegin();

                    int    type = -1, id = -1;
                    string nsp = null;

                    bool hasData = false, readData = false;

                    IncomingPacket packet = IncomingPacket.Empty;

READ:

                    while (reader.Token != JsonToken.EndOfObject)
                    {
                        string key = reader.ReadMember();

                        switch (key)
                        {
                        case "type":
                            type = reader.ReadByte();
                            break;

                        case "nsp":
                            nsp = reader.ReadString();
                            break;

                        case "id":
                            id = reader.ReadInt32();
                            break;

                        case "data":
                            if (!hasData)
                            {
                                hasData = true;
                                SkipData(reader, (SocketIOEventTypes)type);
                            }
                            else
                            {
                                readData = true;

                                packet = new IncomingPacket(transportEvent != TransportEventTypes.Unknown ? transportEvent : TransportEventTypes.Message, (SocketIOEventTypes)type, nsp, id);
                                (string eventName, object[] args) = ReadData(manager, packet, reader);

                                packet.EventName = eventName;
                                if (args != null)
                                {
                                    if (args.Length == 1)
                                    {
                                        packet.DecodedArg = args[0];
                                    }
                                    else
                                    {
                                        packet.DecodedArgs = args;
                                    }
                                }
                            }
                            break;
                        }
                    }

                    // type, nsp, id and data can come in any order. To read data strongly typed we need to know all the additional fields before processing the data field.
                    // In order to do it, when we first encounter the data field we skip it than we do a reset and an additional turn but reading the data too now.
                    if (hasData && !readData)
                    {
                        reader.Reset();
                        stream.Position = 0;
                        reader.ReadObjectBegin();

                        goto READ;
                    }

                    reader.ReadObjectEnd();

                    return(packet.Equals(IncomingPacket.Empty) ? new IncomingPacket(transportEvent != TransportEventTypes.Unknown ? transportEvent : TransportEventTypes.Message, (SocketIOEventTypes)type, nsp, id) : packet);
                }
                finally
                {
                    BufferPool.Release(buff);
                }
            }
        }