예제 #1
0
        internal static EnginePacket DecodePacket(string data, bool utf8decode = false)
        {
            if (data.StartsWith("b"))
            {
                return(DecodeBase64Packet(data.Substring(1)));
            }

            int type;
            var s = data.Substring(0, 1);

            if (!int.TryParse(s, out type))
            {
                type = -1;
            }

            if (utf8decode)
            {
                try
                {
                    data = UTF8.Decode(data);
                }
                catch (Exception)
                {
                    return(_err);
                }
            }

            if (type < 0 || type >= _packetsList.Count)
            {
                return(_err);
            }

            if (data.Length > 1)
            {
                return(new EnginePacket(_packetsList[(byte)type], data.Substring(1)));
            }
            return(new EnginePacket(_packetsList[(byte)type], null));
        }
예제 #2
0
        internal void Encode(IEncodeCallback callback, bool utf8encode = false)
        {
            if (Data is byte[])
            {
                if (!SupportsBinary)
                {
                    EncodeBase64Packet(callback);
                    return;
                }
                EncodeByteArray(callback);
                return;
            }
            var encodedStringBuilder = new StringBuilder();

            encodedStringBuilder.Append(_packets[Type]);

            if (Data != null)
            {
                encodedStringBuilder.Append(utf8encode ? UTF8.Encode((string)Data) : (string)Data);
            }

            callback.Call(encodedStringBuilder.ToString());
        }