/// <summary> /// Returns a new <see cref="IConnectFuture"/> which is already marked as 'failed to connect'. /// </summary> public static IConnectFuture NewFailedFuture(Exception exception) { DefaultConnectFuture failedFuture = new DefaultConnectFuture(); failedFuture.Exception = exception; return(failedFuture); }
public void TestConnectFuture() { DefaultConnectFuture future = new DefaultConnectFuture(); Assert.IsFalse(future.Done); Assert.IsFalse(future.Connected); Assert.IsNull(future.Session); Assert.IsNull(future.Exception); TestThread thread = new TestThread(future); thread.Start(); IoSession session = new DummySession(); future.SetSession(session); thread.Join(); Assert.IsTrue(thread.success); Assert.IsTrue(future.Done); Assert.IsTrue(future.Connected); Assert.AreSame(session, future.Session); Assert.IsNull(future.Exception); future = new DefaultConnectFuture(); thread = new TestThread(future); thread.Start(); future.Exception = new IOException(); thread.Join(); Assert.IsTrue(thread.success); Assert.IsTrue(future.Done); Assert.IsFalse(future.Connected); Assert.IsTrue(future.Exception is IOException); try { IoSession s = future.Session; Assert.Fail("IOException should be thrown."); } catch (Exception) { // Signifies a successful test execution Assert.IsTrue(true); } }
/// <summary> /// Returns a new <see cref="IConnectFuture"/> which is already marked as 'failed to connect'. /// </summary> public static IConnectFuture NewFailedFuture(Exception exception) { DefaultConnectFuture failedFuture = new DefaultConnectFuture(); failedFuture.Exception = exception; return failedFuture; }
/// <inheritdoc/> protected override IConnectFuture Connect0(EndPoint remoteEP, EndPoint localEP, Action<IoSession, IConnectFuture> sessionInitializer) { LoopbackPipe entry; if (!LoopbackAcceptor.BoundHandlers.TryGetValue(remoteEP, out entry)) return DefaultConnectFuture.NewFailedFuture(new IOException("Endpoint unavailable: " + remoteEP)); DefaultConnectFuture future = new DefaultConnectFuture(); // Assign the local end point dynamically, LoopbackEndPoint actualLocalEP; try { actualLocalEP = NextLocalEP(); } catch (IOException e) { return DefaultConnectFuture.NewFailedFuture(e); } LoopbackSession localSession = new LoopbackSession(this, actualLocalEP, Handler, entry); InitSession(localSession, future, sessionInitializer); // and reclaim the local end point when the connection is closed. localSession.CloseFuture.Complete += ReclaimLocalEP; // initialize connector session try { IoFilterChain filterChain = localSession.FilterChain; this.FilterChainBuilder.BuildFilterChain(filterChain); // The following sentences don't throw any exceptions. IoServiceSupport serviceSupport = this as IoServiceSupport; if (serviceSupport != null) serviceSupport.FireSessionCreated(localSession); } catch (Exception ex) { future.Exception = ex; return future; } // initialize acceptor session LoopbackSession remoteSession = localSession.RemoteSession; ((LoopbackAcceptor)remoteSession.Service).DoFinishSessionInitialization(remoteSession, null); try { IoFilterChain filterChain = remoteSession.FilterChain; entry.Acceptor.FilterChainBuilder.BuildFilterChain(filterChain); // The following sentences don't throw any exceptions. IoServiceSupport serviceSupport = entry.Acceptor as IoServiceSupport; if (serviceSupport != null) serviceSupport.FireSessionCreated(remoteSession); } catch (Exception ex) { ExceptionMonitor.Instance.ExceptionCaught(ex); remoteSession.Close(true); } // Start chains, and then allow and messages read/written to be processed. This is to ensure that // sessionOpened gets received before a messageReceived ((LoopbackFilterChain)localSession.FilterChain).Start(); ((LoopbackFilterChain)remoteSession.FilterChain).Start(); return future; }