protected override void ConnectCore( ConnectingContext context )
 {
     if ( !context.Client.Socket.ConnectAsync( context.Client.SocketContext ) )
     {
         this.OnConnected( context, true );
     }
 }
        protected override sealed void OnConnected( ConnectingContext context, bool completedSynchronously )
        {
            if ( context.Client.SocketContext.ConnectByNameError != null )
            {
                // error
                context.Client.OnConnectError(
                    RpcError.NetworkUnreacheableError,
                    context.Client.SocketContext.ConnectByNameError,
                    completedSynchronously,
                    context.UserAsyncState
                );
                return;
            }

            if ( context.Client.SocketContext.SocketError != SocketError.Success )
            {
                // error
                context.Client.OnConnectError(
                    RpcError.NetworkUnreacheableError,
                    new SocketException( ( int )context.Client.SocketContext.SocketError ),
                    completedSynchronously,
                    context.UserAsyncState
                );
                return;
            }

            base.OnConnected( context, completedSynchronously );
        }
Exemplo n.º 3
0
        public void TestConnect_FailToConnect()
        {
            Exception globalError = null;
            foreach ( var target in
                CreateTestTargets(
                    () => new RpcClientOptions(),
                    () => 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() )
                {
                    var connectClientMock =
                        new AsyncConnectClientMock( socket, target.CreateSocketContext( new IPEndPoint( IPAddress.Loopback, 57128 ) ) );
                    object expectedAsyncState = new object();
                    var connectContext = new ConnectingContext( connectClientMock, expectedAsyncState );
                    int connected = 0;
                    connectClientMock.Connected +=
                        ( context, completedSynchronously, asyncState ) =>
                        {
                            Assert.IsFalse( completedSynchronously );
                            Assert.AreSame( expectedAsyncState, asyncState );
                            connected++;
                            connectCompleted.Set();
                        };
                    Exception errorOnTest = null;
                    connectClientMock.ConnectError +=
                        ( context, error, completedSynchronously, asyncState ) =>
                        {
                            Assert.AreSame( expectedAsyncState, asyncState );
                            errorOnTest = error;
                            Console.Error.WriteLine( "Error::CompletedSynchronously:{0}", completedSynchronously );
                            connectCompleted.Set();
                        };
                    target.Connect( connectContext );
                    Assert.IsTrue( connectCompleted.Wait( 5000 ) );
                    if ( globalError != null )
                    {
                        Assert.Fail( globalError.ToString() );
                    }

                    Assert.AreEqual( 0, connected );
                    Assert.NotNull( errorOnTest );
                    Assert.IsInstanceOf<SocketException>( errorOnTest, errorOnTest.ToString() );
                }
            }
        }
Exemplo n.º 4
0
 public void OnConnected( ConnectingContext context, bool completedSynchronously, object asyncState )
 {
     var handler = this.Connected;
     if ( handler != null )
     {
         handler( context, completedSynchronously, asyncState );
     }
 }
Exemplo n.º 5
0
 public void OnConnected( ConnectingContext e, bool completedSynchronously )
 {
     Contract.Assume( e != null );
     Contract.Assume( e.Client.SocketContext.ConnectSocket != null );
     base.Complete( completedSynchronously );
 }
Exemplo n.º 6
0
			public void OnConnected( ConnectingContext context, bool completedSynchronously, object asyncState )
			{
				Contract.Assume( context.Client.SocketContext == this._context );
				Contract.Assume( context.Client.SocketContext.ConnectSocket != null );

				var pool = asyncState as ConnectionPool;
				try { }
				finally
				{
					if ( !pool._statusTable.TryUpdate( this._context, LeaseStatus.Ready, LeaseStatus.Initializing ) )
					{
						LeaseStatus lastStatus;
						if ( pool._statusTable.TryGetValue( this._context, out lastStatus ) )
						{
							throw new InvalidOperationException( "ConectionPool is in inconsistent state. Initialized socket is not ininitializing. Actual is :" + lastStatus );
						}
						else
						{
							throw new InvalidOperationException( "ConectionPool is in inconsistent state. Initialized socket is not recognized." );
						}
					}

					if ( !pool._availableSockets.TryAdd( this._context, 0 ) )
					{
						// overflow.
						this._socketToConnect.Shutdown( SocketShutdown.Both );
						this._socketToConnect.Dispose();
					}

					Interlocked.Decrement( ref pool._initializingSocketCount );
				}
			}