예제 #1
0
        public void TestSwap()
        {
            using ( var target = new RpcOutputBuffer( ChunkBuffer.CreateDefault() ) )
            {
                using ( var stream = target.OpenWriteStream() )
                {
                    stream.Write( Enumerable.Range( 1, 10 ).Select( i => ( byte )i ).ToArray(), 0, 10 );
                }

                using ( var swapper = target.CreateSwapper() )
                {
                    CollectionAssert.AreEqual(
                        Enumerable.Range( 1, 10 ).Select( i => ( byte )i ).ToArray(),
                        swapper.ReadBytes()
                    );

                    swapper.WriteBytes( Enumerable.Range( 11, 30 ).Select( i => ( byte )i ) );
                }

                CollectionAssert.AreEqual(
                    Enumerable.Range( 11, 30 ).Select( i => ( byte )i ).ToArray(),
                    target.ReadBytes().ToArray()
                );
            }
        }
예제 #2
0
        public void TestSwap_WriteBytesNull()
        {
            using ( var target = new RpcOutputBuffer( ChunkBuffer.CreateDefault() ) )
            {
                using ( var stream = target.OpenWriteStream() )
                {
                    stream.Write( Enumerable.Range( 1, 10 ).Select( i => ( byte )i ).ToArray(), 0, 10 );
                }

                using ( var swapper = target.CreateSwapper() )
                {
                    swapper.WriteBytes( null );
                }
            }
        }
        /// <summary>
        ///		Serialize RPC call to specified buffer.
        /// </summary>
        /// <param name="messageId">ID of message. If message is notification message, specify null.</param>
        /// <param name="method">Method name to be called.</param>
        /// <param name="arguments">Arguments of method call.</param>
        /// <param name="buffer">Buffer to be set serialized stream.</param>
        /// <returns>Error message of serialization.</returns>
        /// <exception cref="ArgumentNullException">
        ///		<paramref name="method"/>, <paramref name="arguments"/>, or <paramref name="buffer"/> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///		<paramref name="method"/> is illegal.
        /// </exception>
        public RpcErrorMessage Serialize( int? messageId, string method, IList<object> arguments, RpcOutputBuffer buffer )
        {
            if ( method == null )
            {
                throw new ArgumentNullException( "method" );
            }

            // TODO: more strict validation.
            if ( String.IsNullOrWhiteSpace( method ) )
            {
                throw new ArgumentException( "'method' must not be empty nor blank.", "method" );
            }

            if ( arguments == null )
            {
                throw new ArgumentNullException( "arguments" );
            }

            if ( buffer == null )
            {
                throw new ArgumentNullException( "buffer" );
            }

            Contract.EndContractBlock();

            var context = new RequestMessageSerializationContext( buffer, messageId, method, arguments );

            foreach ( var preSerializationFilter in this._preSerializationFilters )
            {
                preSerializationFilter.GetFilter().Process( context );
                if ( !context.SerializationError.IsSuccess )
                {
                    return context.SerializationError;
                }
            }

            SerializeCore( buffer, messageId, context );
            if ( !context.SerializationError.IsSuccess )
            {
                return context.SerializationError;
            }

            foreach ( var postSerializationFilter in this._postSerializationFilters )
            {
                using ( var swapper = buffer.CreateSwapper() )
                {
                    swapper.WriteBytes( postSerializationFilter.GetFilter().Process( swapper.ReadBytes(), context ) );
                    if ( !context.SerializationError.IsSuccess )
                    {
                        return context.SerializationError;
                    }
                }
            }

            return RpcErrorMessage.Success;
        }
예제 #4
0
        public void TestSwap_WriteBytesEmpty()
        {
            using ( var target = new RpcOutputBuffer( ChunkBuffer.CreateDefault() ) )
            {
                using ( var stream = target.OpenWriteStream() )
                {
                    stream.Write( Enumerable.Range( 1, 10 ).Select( i => ( byte )i ).ToArray(), 0, 10 );
                }

                using ( var swapper = target.CreateSwapper() )
                {
                    swapper.WriteBytes( Enumerable.Empty<byte>() );
                }

                Assert.AreEqual( 0, target.ReadBytes().Count() );
            }
        }
        /// <summary>
        ///		Serialize response message to specified buffer.
        /// </summary>
        /// <param name="messageId">ID of message.</param>
        /// <param name="returnValue">Return value of the method.</param>
        /// <param name="isVoid">If the method is void, then true.</param>
        /// <param name="exception">Exception thrown from the method.</param>
        /// <param name="buffer">Buffer to be stored serialized response stream.</param>
        /// <returns>Error information.</returns>
        public RpcErrorMessage Serialize( int messageId, object returnValue, bool isVoid, RpcException exception, RpcOutputBuffer buffer )
        {
            var context =
                exception != null
                ? new ResponseMessageSerializationContext( buffer, messageId, exception, isVoid )
                : ( isVoid ? new ResponseMessageSerializationContext( buffer, messageId ) : new ResponseMessageSerializationContext( buffer, messageId, returnValue ) );

            foreach ( var preSerializationFilter in this._preSerializationFilters )
            {
                preSerializationFilter.GetFilter().Process( context );
                if ( !context.SerializationError.IsSuccess )
                {
                    return context.SerializationError;
                }
            }

            SerializeCore( MessageType.Response, messageId, context );
            if ( !context.SerializationError.IsSuccess )
            {
                return context.SerializationError;
            }

            foreach ( var postSerializationFilter in this._postSerializationFilters )
            {
                using ( var swapper = buffer.CreateSwapper() )
                {
                    swapper.WriteBytes( postSerializationFilter.GetFilter().Process( swapper.ReadBytes(), context ) );
                    if ( !context.SerializationError.IsSuccess )
                    {
                        return context.SerializationError;
                    }
                }
            }

            return RpcErrorMessage.Success;
        }