コード例 #1
0
        public static object ReadJsonBuffered(
            this NetworkStream stream,
            Type type,
            int?maxBytes   = null,
            int bufferSize = 0xFFF, Encoding encoding = null)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            return(PapercutIPCommSerializer.FromJson(type, stream.ReadStringBuffered(maxBytes, bufferSize, encoding)));
        }
コード例 #2
0
        public static async Task <object> ReadJsonBufferedAsync(
            this NetworkStream stream,
            Type type,
            int?maxBytes   = null,
            int bufferSize = 0xFFF, Encoding encoding = null, CancellationToken token = default)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var readStringBufferedAsync = await stream.ReadStringBufferedAsync(maxBytes, bufferSize, encoding, token : token);

            return(PapercutIPCommSerializer.FromJson(type, readStringBufferedAsync));
        }
コード例 #3
0
        public static object ReadJsonBuffered(this NetworkStream stream, Type type, int payloadSize, Encoding encoding = null)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            encoding = encoding ?? Encoding.UTF8;

            using (var memoryStream = new MemoryStream())
            {
                stream.CopyBufferedTo(memoryStream, payloadSize);

                return(PapercutIPCommSerializer.FromJson(type, encoding.GetString(memoryStream.ToArray())));
            }
        }
コード例 #4
0
        public static async Task <object> ReadJsonBufferedAsync(this NetworkStream stream, Type type, int payloadSize, Encoding encoding = null, CancellationToken token = default)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            encoding = encoding ?? Encoding.UTF8;

            using (var memoryStream = new MemoryStream())
            {
                await stream.CopyBufferedToAsync(memoryStream, payloadSize, token : token);

                return(PapercutIPCommSerializer.FromJson(type, encoding.GetString(memoryStream.ToArray())));
            }
        }
コード例 #5
0
        public static async Task SendJson(this Connection connection, Type type, object instance)
        {
            var json = PapercutIPCommSerializer.ToJson(type, instance);

            await connection.SendData(connection.Encoding.GetBytes(json));
        }