private static SocketIOPacket CreateMessagePacket(JArray JsonArray, int PacketID)
        {
            SocketIOPacket Packet = new SocketIOPacket
            {
                JsonData = JsonArray,
                IsJson   = true
            };

            if (PacketID >= 0)
            {
                Packet.ID = PacketID;
            }

            using (Deconstructor Deconstructor = new Deconstructor())
            {
                Deconstructor.SetPacket(Packet);
                Packet = Deconstructor.Deconstruct();

                if (Packet.Attachments.Count > 0)
                {
                    Packet.IsJson = false;
                }
            }

            return(Packet);
        }
        internal static SocketIOPacket CreateEventPacket(JArray JsonArray, SocketIOAck Ack)
        {
            SocketIOPacket Packet = CreateMessagePacket(JsonArray, Ack);

            if (Packet.IsJson)
            {
                Packet.Type = SocketIOPacketType.EVENT;
            }
            else
            {
                Packet.Type = SocketIOPacketType.BINARY_EVENT;
            }

            Packet.IsJson = true;
            return(Packet);
        }
        internal static SocketIOPacket CreateAckPacket(JArray JsonArray, int PacketID)
        {
            if (PacketID >= 0)
            {
                SocketIOPacket Packet = CreateMessagePacket(JsonArray, PacketID);

                if (Packet.IsJson)
                {
                    Packet.Type = SocketIOPacketType.ACK;
                }
                else
                {
                    Packet.Type = SocketIOPacketType.BINARY_ACK;
                }

                Packet.IsJson = true;
                return(Packet);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 4
0
        private SocketIOPacket(SocketIOPacket Packet)
        {
            Type = Packet.Type;

            if (Packet.Attachments != null)
            {
                Attachments = new Queue <SocketIOPacket>(Packet.Attachments);
            }
            else
            {
                Attachments = null;
            }

            Namespace = Packet.Namespace;
            ID        = Packet.ID;

            IsJson   = Packet.IsJson;
            IsBinary = Packet.IsBinary;

            if (Packet.JsonData != null)
            {
                JsonData = Packet.JsonData.DeepClone();
            }
            else
            {
                JsonData = null;
            }

            if (Packet.RawData != null)
            {
                RawData = new List <byte>(Packet.RawData).ToArray();
            }
            else
            {
                RawData = null;
            }
        }
        internal static SocketIOPacket Decode(string Data)
        {
            SocketIOPacket Packet = new SocketIOPacket();
            int            Offset = 0;

            Packet.Type = (SocketIOPacketType)(Data[Offset] - '0');

            if (Data.Length > 1)
            {
                if (Packet.Type == SocketIOPacketType.BINARY_EVENT || Packet.Type == SocketIOPacketType.BINARY_ACK)
                {
                    StringBuilder Builder = new StringBuilder();

                    while (Offset < Data.Length - 1)
                    {
                        char c = Data[++Offset];

                        if (char.IsNumber(c))
                        {
                            Builder.Append(c);
                        }
                        else
                        {
                            break;
                        }
                    }

                    Packet.Attachments = new Queue <SocketIOPacket>(new SocketIOPacket[int.Parse(Builder.ToString())]);
                }

                if ('/' == Data[Offset + 1])
                {
                    StringBuilder Builder = new StringBuilder();

                    while (Offset < Data.Length - 1 && Data[++Offset] != ',')
                    {
                        Builder.Append(Data[Offset]);
                    }

                    Packet.Namespace = Builder.ToString();
                }
                else
                {
                    Packet.Namespace = "/";
                }

                char Next = Data[Offset + 1];

                if (!char.IsWhiteSpace(Next) && char.IsNumber(Next))
                {
                    StringBuilder Builder = new StringBuilder();

                    while (Offset < Data.Length - 1)
                    {
                        char c = Data[++Offset];

                        if (char.IsNumber(c))
                        {
                            Builder.Append(c);
                        }
                        else
                        {
                            --Offset;
                            break;
                        }
                    }

                    Packet.ID = int.Parse(Builder.ToString());
                }

                if (++Offset < Data.Length - 1)
                {
                    Packet.JsonData = (JToken)JsonConvert.DeserializeObject(Data.Substring(Offset));
                    Packet.IsJson   = true;
                }
            }

            return(Packet);
        }