public static T Deserialize <T>(Stream stream, IFormatterResolver resolver)
        {
            if (resolver == null)
            {
                resolver = DefaultResolver;
            }
            var formatter = resolver.GetFormatterWithVerify <T>();

#if NETSTANDARD1_4
            var ms = stream as MemoryStream;
            if (ms != null)
            {
                // optimize for MemoryStream
                ArraySegment <byte> buffer;
                if (ms.TryGetBuffer(out buffer))
                {
                    int readSize;
                    return(formatter.Deserialize(buffer.Array, buffer.Offset, resolver, out readSize));
                }
            }
#endif

            // no else.
            {
                var buffer = InternalMemoryPool.GetBuffer();

                FillFromStream(stream, ref buffer);

                int readSize;
                return(formatter.Deserialize(buffer, 0, resolver, out readSize));
            }
        }
Example #2
0
        public static T Deserialize <T>(Stream stream, IFormatterResolver resolver, bool readStrict)
        {
            if (resolver == null)
            {
                resolver = defaultResolver;
            }
            var formatter = resolver.GetFormatterWithVerify <T>();

            if (!readStrict)
            {
                var buffer = InternalMemoryPool.GetBuffer();

                FillFromStream(stream, ref buffer);

                int readSize;
                return(formatter.Deserialize(buffer, 0, resolver, out readSize));
            }
            else
            {
                int _;
                var bytes = MessagePackBinary.ReadMessageBlockFromStreamUnsafe(stream, false, out _);
                int readSize;
                return(formatter.Deserialize(bytes, 0, resolver, out readSize));
            }
        }
Example #3
0
    public static byte[] Serialize(Dictionary <string, object> obj)
    {
        var buffer = InternalMemoryPool.GetBuffer();

        var len = Serialize(ref buffer, 0, obj);

        // do not return MemoryPool.Buffer.
        return(MessagePackBinary.FastCloneWithResize(buffer, len));
    }
Example #4
0
    public static Dictionary <string, object> Deserialize <T>(Stream stream)
    {
        var buffer = InternalMemoryPool.GetBuffer();

        FillFromStream(stream, ref buffer);

        int readSize;

        return(Deserialize(buffer, 0, out readSize));
    }
Example #5
0
        /// <summary>
        /// return buffer is from memory pool, be careful to use.
        /// </summary>
        internal static ArraySegment <byte> FromJsonUnsafe(TextReader reader)
        {
            var offset = 0;

            byte[] binary = InternalMemoryPool.GetBuffer();  // from memory pool.
            using (var jr = new TinyJsonReader(reader, false))
            {
                FromJsonCore(jr, ref binary, ref offset);
            }
            return(new ArraySegment <byte>(binary, 0, offset));
        }
Example #6
0
        public static byte[] Decode(Stream stream, bool readStrict = false)
        {
            if (!readStrict)
            {
                var buffer = InternalMemoryPool.GetBuffer(); // use MessagePackSerializer.Pool!
                var len    = FillFromStream(stream, ref buffer);
                return(Decode(new ArraySegment <byte>(buffer, 0, len)));
            }

            int blockSize;
            var bytes = MessagePackBinary.ReadMessageBlockFromStreamUnsafe(stream, false, out blockSize);

            return(Decode(new ArraySegment <byte>(bytes, 0, blockSize)));
        }
Example #7
0
        public static T Deserialize <T>(Stream stream, IFormatterResolver resolver, bool readStrict)
        {
            if (!readStrict)
            {
                var buffer = InternalMemoryPool.GetBuffer(); // use MessagePackSerializer.Pool!
                var len    = FillFromStream(stream, ref buffer);
                return(DeserializeCore <T>(new ArraySegment <byte>(buffer, 0, len), resolver));
            }

            int blockSize;
            var bytes = MessagePackBinary.ReadMessageBlockFromStreamUnsafe(stream, false, out blockSize);

            return(DeserializeCore <T>(new ArraySegment <byte>(bytes, 0, blockSize), resolver));
        }
Example #8
0
        /// <summary>COM_STATISTICS, Get a human readable string of internal statistics.</summary>
        public string Statistics()
        {
            using (var stream = client.GetStream())
            {
                var readWriteBuffer = InternalMemoryPool.GetBuffer();
                var writer          = PacketWriter.Create(readWriteBuffer);
                ProtocolWriter.WriteStatistics(ref writer);

                var reader = SyncWriteAndRead(ref writer, 0, stream);
                reader.ThrowIfErrorPacket();

                var result = reader.ReadString(reader.Remaining);
                return(result);
            }
        }
        /// <summary>
        /// Serialize to binary with specified resolver. Get the raw memory pool byte[]. The result can not share across thread and can not hold, so use quickly.
        /// </summary>
        public static ArraySegment <byte> SerializeUnsafe <T>(T obj, IFormatterResolver resolver)
        {
            if (resolver == null)
            {
                resolver = DefaultResolver;
            }
            var formatter = resolver.GetFormatterWithVerify <T>();

            var buffer = InternalMemoryPool.GetBuffer();

            var len = formatter.Serialize(ref buffer, 0, obj, resolver);

            // return raw memory pool, unsafe!
            return(new ArraySegment <byte>(buffer, 0, len));
        }
        /// <summary>
        /// Serialize to stream with specified resolver.
        /// </summary>
        public static void Serialize <T>(Stream stream, T obj, IFormatterResolver resolver)
        {
            if (resolver == null)
            {
                resolver = DefaultResolver;
            }
            var formatter = resolver.GetFormatterWithVerify <T>();

            var buffer = InternalMemoryPool.GetBuffer();

            var len = formatter.Serialize(ref buffer, 0, obj, resolver);

            // do not need resize.
            stream.Write(buffer, 0, len);
        }
        /// <summary>
        /// Serialize to binary with specified resolver.
        /// </summary>
        public static byte[] Serialize <T>(T obj, IFormatterResolver resolver)
        {
            if (resolver == null)
            {
                resolver = DefaultResolver;
            }
            var formatter = resolver.GetFormatterWithVerify <T>();

            var buffer = InternalMemoryPool.GetBuffer();

            var len = formatter.Serialize(ref buffer, 0, obj, resolver);

            // do not return MemoryPool.Buffer.
            return(MessagePackBinary.FastCloneWithResize(buffer, len));
        }
Example #12
0
        /// <summary>COM_PING, check if the server is alive.</summary>
        public OkPacket Ping()
        {
            using (var stream = client.GetStream())
            {
                var readWriteBuffer = InternalMemoryPool.GetBuffer();
                var writer          = PacketWriter.Create(readWriteBuffer);
                ProtocolWriter.WritePing(ref writer);

                var reader = SyncWriteAndRead(ref writer, 0, stream);

                var response = ProtocolReader.ReadResponsePacket(ref reader);
                response.ThrowIfError();

                return((OkPacket)response);
            }
        }
Example #13
0
        public TextResultSet Query(string query)
        {
            var stream = client.GetStream();

            var readWriteBuffer = InternalMemoryPool.GetBuffer();
            var writer          = PacketWriter.Create(readWriteBuffer);

            ProtocolWriter.WriteQuery(ref writer, query);

            var reader = SyncWriteAndRead(ref writer, 0, stream);

            // TODO: Ok or ResultSet?
            var set = ProtocolReader.ReadTextResultSet(ref reader);

            return(set);
        }
Example #14
0
        public StatementPrepareOk Prepare(string query)
        {
            var stream = client.GetStream();

            var readWriteBuffer = InternalMemoryPool.GetBuffer();
            var writer          = PacketWriter.Create(readWriteBuffer);

            ProtocolWriter.WritePrepareStatement(ref writer, query);

            var reader = SyncWriteAndRead(ref writer, 0, stream);

            // COM_STMT_PREPARE_OK on success, ERR_Packet otherwise
            if (reader.IsErrorPacket())
            {
                throw ErrorPacket.Parse(ref reader).ToMySqlException();
            }

            return(ProtocolReader.ReadStatementPrepareOk(ref reader));
        }
        public static T Deserialize <T>(Stream stream, IFormatterResolver resolver, bool readStrict)
        {
            if (resolver == null)
            {
                resolver = DefaultResolver;
            }
            var formatter = resolver.GetFormatterWithVerify <T>();


            if (!readStrict)
            {
#if NETSTANDARD1_4 && !NET45
                var ms = stream as MemoryStream;
                if (ms != null)
                {
                    // optimize for MemoryStream
                    ArraySegment <byte> buffer;
                    if (ms.TryGetBuffer(out buffer))
                    {
                        int readSize;
                        return(formatter.Deserialize(buffer.Array, buffer.Offset, resolver, out readSize));
                    }
                }
#endif

                // no else.
                {
                    var buffer = InternalMemoryPool.GetBuffer();

                    FillFromStream(stream, ref buffer);

                    int readSize;
                    return(formatter.Deserialize(buffer, 0, resolver, out readSize));
                }
            }
            else
            {
                int _;
                var bytes = MessagePackBinary.ReadMessageBlockFromStreamUnsafe(stream, false, out _);
                int readSize;
                return(formatter.Deserialize(bytes, 0, resolver, out readSize));
            }
        }
Example #16
0
        public BinaryResultSet Execute(int statementId, params object[] parameters) // how to avoid boxing?
        {
            var stream = client.GetStream();

            var readWriteBuffer = InternalMemoryPool.GetBuffer();
            var writer          = PacketWriter.Create(readWriteBuffer);

            ProtocolWriter.WriteExecuteStatement(ref writer, statementId, parameters);

            var reader = SyncWriteAndRead(ref writer, 0, stream);

            // COM_STMT_PREPARE_OK on success, ERR_Packet otherwise
            if (reader.IsErrorPacket())
            {
                throw ErrorPacket.Parse(ref reader).ToMySqlException();
            }

            // TODO: Ok or ResultSet?
            var set = ProtocolReader.ReadBinaryResultSet(ref reader);

            return(set);
        }
Example #17
0
        public TextResultSet Query(FormattableString query)
        {
            if (query.ArgumentCount == 0)
            {
                return(Query(query.Format));
            }

            var stream = client.GetStream();

            var readWriteBuffer = InternalMemoryPool.GetBuffer();
            var writer          = PacketWriter.Create(readWriteBuffer);

            var charBuffer  = InternalMemoryPool.GetCharBuffer();
            var stringCount = FastQueryParser.Parse(ref charBuffer, query);

            ProtocolWriter.WriteQuery(ref writer, charBuffer, stringCount);

            var reader = SyncWriteAndRead(ref writer, 0, stream);

            // TODO: Ok or ResultSet?
            var set = ProtocolReader.ReadTextResultSet(ref reader);

            return(set);
        }