protected abstract void SendCore( SendingContext context );
 ChunkBuffer ITransportReceiveHandler.GetBufferForReceive( SendingContext context )
 {
     return this.GetBufferForReceiveCore( context );
 }
 protected virtual ChunkBuffer GetBufferForReceiveCore( SendingContext context )
 {
     // Reuse sending buffer.
     return context.SendingBuffer.Chunks;
 }
 protected override sealed void SendCore( SendingContext context )
 {
     this.EventLoop.Send( context );
 }
        public void OnMessageSent( SendingContext e, Exception error, bool completedSynchronously )
        {
            Contract.Assume( e != null );
            Contract.Assume( e.MessageId == null );

            if ( error != null )
            {
                base.OnError( error, completedSynchronously );
            }
            else
            {
                base.Complete( completedSynchronously );
            }
        }
        protected override sealed void SendToCore( SendingContext context )
        {
            if ( context.CancellationToken.IsCancellationRequested )
            {
                // TODO : trace
                return;
            }

            if ( !context.SocketContext.SendToAsync() )
            {
                context.SocketContext.OnSent( true );
            }
        }
 protected override sealed void OnSent( SendingContext context, bool completedSynchronously )
 {
     base.OnSent( context, completedSynchronously );
 }
 protected override sealed void SendCore( SendingContext context )
 {
     this.EventLoop.SendTo( context );
     throw new NotImplementedException();
     //this.EventLoop.ReceiveFrom()
 }
 public ChunkBuffer GetBufferForReceive( SendingContext context )
 {
     return context.SendingBuffer.DebugGetChunk();
 }
        private static void TestSendAndReceiveCore( int bufferSegmentSize, int bufferSegmentCount, String messageToSend )
        {
            Exception globalError = null;
            foreach ( var target in
                CreateTestTargets(
                    () => new RpcClientOptions() { BufferSegmentSize = bufferSegmentSize, BufferSegmentCount = bufferSegmentCount },
                    () => new EventHandler<RpcTransportErrorEventArgs>( ( sender, e ) =>
                    {
                        globalError = e.RpcError.Value.ToException();
                        return;
                    } ),
                    () => null
                ) )
            {
                using ( var socket
                    = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ) )
                using ( var server = ServerMock.CreateTcp( 57129, 8192 ) )
                using ( var connectCompleted = new ManualResetEventSlim() )
                using ( var receiveCompleted = new ManualResetEventSlim() )
                {
                    server.Received +=
                        ( sender, e ) =>
                        {
                            Console.WriteLine(
                                "Received. Type:{0}, ID:{1}, Method:'{2}', Arguments:'{3}'",
                                e.GetRequest().MessageType,
                                e.GetRequest().MessageId,
                                e.GetRequest().Method,
                                e.GetRequest().Arguments.Aggregate(
                                    new StringBuilder( "[" ),
                                    ( buffer, item ) => ( buffer.Length == 1 ? buffer : buffer.Append( ", " ) ).Append( item )
                                ).Append( ']' )
                            );
                            Assert.AreEqual( "Test", e.GetRequest().Method );
                            e.Reply( 1, messageToSend );
                        };
                    var socketContext = target.CreateSocketContext( new IPEndPoint( IPAddress.Loopback, 57129 ) );
                    var connectClientMock = new AsyncConnectClientMock( socket, socketContext );
                    connectClientMock.Connected +=
                        ( context, completedSynchronously, asyncState ) =>
                        {
                            connectCompleted.Set();
                        };
                    Exception errorOnTest = null;
                    connectClientMock.ConnectError +=
                        ( context, error, completedSynchronously, asyncState ) =>
                        {
                            errorOnTest = error;
                            connectCompleted.Set();
                        };
                    target.Connect( new ConnectingContext( connectClientMock, null ) );
                    Assert.IsTrue( connectCompleted.Wait( 3000 ) );
                    if ( errorOnTest != null )
                    {
                        Assert.Fail( errorOnTest.ToString() );
                    }
                    if ( globalError != null )
                    {
                        Assert.Fail( globalError.ToString() );
                    }

                    Assert.IsNotNull( socketContext.ConnectSocket );
                    var receiverMock = new TransportReceiveHandlerMock();
                    MessagePackObject? returned = null;
                    receiverMock.Received +=
                        ( receiveContext ) =>
                        {
                            var message = SerializationUtility.DeserializeResponse( receiveContext.ReceivingBuffer );
                            if ( message.Error != null )
                            {
                                errorOnTest = message.Error;
                            }
                            else
                            {
                                returned = message.ReturnValue;
                            }

                            receiveCompleted.Set();
                        };
                    var sendingContext =
                        new SendingContext(
                            new ClientSessionContext(
                                receiverMock,
                                new RpcClientOptions(),
                                socketContext
                            ),
                            new RpcOutputBuffer( ChunkBuffer.CreateDefault() ),
                            1,
                            ( _sendingContext, error, completedSynchronously ) =>
                            {
                                errorOnTest = error;
                            }
                        );
                    var requestBytes = CreateDefaultRequest();
                    sendingContext.SendingBuffer.OpenWriteStream().Write( requestBytes, 0, requestBytes.Length );
                    sendingContext.SocketContext.BufferList = sendingContext.SendingBuffer.DebugGetChunk();
                    target.Send( sendingContext );
                    Assert.IsTrue( receiveCompleted.Wait( MillisecondsTimeout ), "Timeout" );
                    Assert.IsTrue( returned.HasValue );
                    if ( errorOnTest != null )
                    {
                        Assert.Fail( errorOnTest.ToString() );
                    }
                    if ( globalError != null )
                    {
                        Assert.Fail( globalError.ToString() );
                    }

                    Assert.AreEqual( messageToSend, returned.Value.AsString() );
                    socket.Shutdown( SocketShutdown.Both );
                }
            }
        }