예제 #1
0
        public void AsynchronousOpen()
        {
            ManualResetEvent evt = new ManualResetEvent(false);
            Uri    serverUri     = new Uri(SizedTcpTransportBindingElement.SizedTcpScheme + "://" + Environment.MachineName + ":" + Port);
            object channel       = ReflectionHelper.CreateInstance(
                typeof(SizedTcpTransportBindingElement),
                "JsonRpcOverTcp.Channels.SizedTcpRequestChannel",
                new ByteStreamMessageEncodingBindingElement().CreateMessageEncoderFactory().Encoder,
                BufferManager.CreateBufferManager(int.MaxValue, int.MaxValue),
                Mocks.GetChannelManagerBase(),
                new EndpointAddress(serverUri),
                serverUri);
            object      state       = new object();
            bool        success     = true;
            ChannelBase channelBase = (ChannelBase)channel;

            channelBase.BeginOpen(delegate(IAsyncResult asyncResult)
            {
                try
                {
                    if (!Object.ReferenceEquals(asyncResult.AsyncState, state))
                    {
                        success = false;
                        Console.WriteLine("Error, state not preserved");
                    }
                    else
                    {
                        try
                        {
                            channelBase.EndOpen(asyncResult);

                            Assert.Equal(CommunicationState.Opened, channelBase.State);
                            Socket socket = (Socket)ReflectionHelper.GetField(channel, "socket");
                            Assert.True(socket.Connected);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Error: " + e);
                            success = false;
                        }
                    }
                }
                finally
                {
                    evt.Set();
                }
            }, state);

            evt.WaitOne();
            Assert.True(success, "Error in callback");

            channelBase.Close();
        }
        public void AsynchronousSendSynchronousReceive()
        {
            byte[] length = new byte[4];
            byte[] data   = new byte[36];
            Random rndGen = new Random();

            rndGen.NextBytes(data);
            Formatting.SizeToBytes(data.Length, length, 0);
            byte[] toSend = new byte[length.Length + data.Length];
            Array.Copy(length, 0, toSend, 0, length.Length);
            Array.Copy(data, 0, toSend, length.Length, data.Length);

            ManualResetEvent evt     = new ManualResetEvent(false);
            object           channel = ReflectionHelper.CreateInstance(
                typeof(SizedTcpTransportBindingElement),
                "JsonRpcOverTcp.Channels.SizedTcpBaseChannel",
                null,
                BufferManager.CreateBufferManager(int.MaxValue, int.MaxValue),
                Mocks.GetChannelManagerBase());
            Socket socket = Mocks.GetConnectedSocket(Port);

            ReflectionHelper.SetField(channel, "socket", socket);

            object state   = new object();
            bool   success = true;

            ReflectionHelper.CallMethod(channel, "BeginSocketSend", toSend, new AsyncCallback(delegate(IAsyncResult asyncResult)
            {
                try
                {
                    if (!Object.ReferenceEquals(asyncResult.AsyncState, state))
                    {
                        success = false;
                        Console.WriteLine("Error, state not preserved");
                    }
                    else
                    {
                        ReflectionHelper.CallMethod(channel, "EndSocketSend", asyncResult);
                        ReflectionHelper.CallMethod(channel, "SocketReceiveBytes", toSend.Length);

                        try
                        {
                            Assert.Equal(2, this.server.ReceivedBytes.Count);
                            Assert.Equal(length, this.server.ReceivedBytes[0], new ArrayComparer <byte>());
                            Assert.Equal(data, this.server.ReceivedBytes[1], new ArrayComparer <byte>());
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Error: " + e);
                            success = false;
                        }
                    }
                }
                finally
                {
                    evt.Set();
                }
            }), state);

            evt.WaitOne();
            Assert.True(success, "Error in callback");
            socket.Close();
        }
예제 #3
0
        public void AsynchronousSendMessageSyncReceiveMessage()
        {
            byte[] data   = new byte[36];
            Random rndGen = new Random();

            rndGen.NextBytes(data);

            byte[] length = new byte[4];
            Formatting.SizeToBytes(data.Length, length, 0);

            Message input = Formatting.BytesToMessage(data);

            ManualResetEvent evt = new ManualResetEvent(false);
            Uri    serverUri     = new Uri(SizedTcpTransportBindingElement.SizedTcpScheme + "://" + Environment.MachineName + ":" + Port);
            object channel       = ReflectionHelper.CreateInstance(
                typeof(SizedTcpTransportBindingElement),
                "JsonRpcOverTcp.Channels.SizedTcpRequestChannel",
                new ByteStreamMessageEncodingBindingElement().CreateMessageEncoderFactory().Encoder,
                BufferManager.CreateBufferManager(int.MaxValue, int.MaxValue),
                Mocks.GetChannelManagerBase(),
                new EndpointAddress(serverUri),
                serverUri);

            ReflectionHelper.CallMethod(channel, "Open");

            object state   = new object();
            bool   success = true;

            ReflectionHelper.CallMethod(channel, "BeginSendMessage", input, TimeSpan.FromMinutes(1), new AsyncCallback(delegate(IAsyncResult asyncResult)
            {
                try
                {
                    if (!Object.ReferenceEquals(asyncResult.AsyncState, state))
                    {
                        success = false;
                        Console.WriteLine("Error, state not preserved");
                    }
                    else
                    {
                        ReflectionHelper.CallMethod(channel, "EndSendMessage", asyncResult);
                        Message output = (Message)ReflectionHelper.CallMethod(channel, "ReceiveMessage", TimeSpan.FromMinutes(1));

                        try
                        {
                            Assert.Equal(2, this.server.ReceivedBytes.Count);
                            Assert.Equal(length, this.server.ReceivedBytes[0], new ArrayComparer <byte>());
                            Assert.Equal(data, this.server.ReceivedBytes[1], new ArrayComparer <byte>());

                            byte[] outputBytes = Formatting.MessageToBytes(output);
                            Assert.Equal(data, outputBytes, new ArrayComparer <byte>());
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Error: " + e);
                            success = false;
                        }
                    }
                }
                finally
                {
                    evt.Set();
                }
            }), state);

            evt.WaitOne();
            Assert.True(success, "Error in callback");
            ReflectionHelper.CallMethod(channel, "Close");
        }
예제 #4
0
        public void AsynchronousRequest()
        {
            byte[] data   = new byte[36];
            Random rndGen = new Random();

            rndGen.NextBytes(data);

            Message input = Formatting.BytesToMessage(data);

            ManualResetEvent evt = new ManualResetEvent(false);
            Uri    serverUri     = new Uri(SizedTcpTransportBindingElement.SizedTcpScheme + "://" + Environment.MachineName + ":" + Port);
            object channel       = ReflectionHelper.CreateInstance(
                typeof(SizedTcpTransportBindingElement),
                "JsonRpcOverTcp.Channels.SizedTcpRequestChannel",
                new ByteStreamMessageEncodingBindingElement().CreateMessageEncoderFactory().Encoder,
                BufferManager.CreateBufferManager(int.MaxValue, int.MaxValue),
                Mocks.GetChannelManagerBase(),
                new EndpointAddress(serverUri),
                serverUri);

            ChannelBase     channelBase    = (ChannelBase)channel;
            IRequestChannel requestChannel = (IRequestChannel)channel;

            channelBase.Open();

            object state   = new object();
            bool   success = true;

            requestChannel.BeginRequest(input, new AsyncCallback(delegate(IAsyncResult asyncResult)
            {
                try
                {
                    if (!Object.ReferenceEquals(asyncResult.AsyncState, state))
                    {
                        success = false;
                        Console.WriteLine("Error, state not preserved");
                    }
                    else
                    {
                        Message output = requestChannel.EndRequest(asyncResult);

                        try
                        {
                            byte[] outputBytes = Formatting.MessageToBytes(output);
                            Assert.Equal(data, outputBytes, new ArrayComparer <byte>());
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Error: " + e);
                            success = false;
                        }
                    }
                }
                finally
                {
                    evt.Set();
                }
            }), state);

            evt.WaitOne();
            Assert.True(success, "Error in callback");
            channelBase.Close();
        }