From 34c2a5c2914a6b7110a1f7b8c14e31ff474de62d Mon Sep 17 00:00:00 2001 From: Steven Smith Date: Fri, 21 Aug 2015 21:05:52 -0700 Subject: [PATCH] Ensure channel is set before returning from connect(). --- .../org/spacehq/packetlib/tcp/TcpClientSession.java | 12 +++++++++--- .../spacehq/packetlib/tcp/TcpConnectionListener.java | 2 -- .../org/spacehq/packetlib/tcp/TcpServerSession.java | 7 +++++++ .../java/org/spacehq/packetlib/tcp/TcpSession.java | 5 ----- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/spacehq/packetlib/tcp/TcpClientSession.java b/src/main/java/org/spacehq/packetlib/tcp/TcpClientSession.java index d8b0f6a3..c525b741 100644 --- a/src/main/java/org/spacehq/packetlib/tcp/TcpClientSession.java +++ b/src/main/java/org/spacehq/packetlib/tcp/TcpClientSession.java @@ -93,13 +93,20 @@ public class TcpClientSession extends TcpSession { future.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { - System.err.println("Future start."); if(!future.isSuccess()) { exceptionCaught(null, future.cause()); } - System.err.println("Future end."); } }).await(); + + if(future.isSuccess()) { + while(!isConnected()) { + try { + Thread.sleep(5); + } catch(InterruptedException e) { + } + } + } } catch(Throwable t) { exceptionCaught(null, t); } @@ -108,7 +115,6 @@ public class TcpClientSession extends TcpSession { if(wait) { connectTask.run(); - System.err.println("Connect completed."); } else { new Thread(connectTask).start(); } diff --git a/src/main/java/org/spacehq/packetlib/tcp/TcpConnectionListener.java b/src/main/java/org/spacehq/packetlib/tcp/TcpConnectionListener.java index 32c9fafe..e0eb4a0d 100644 --- a/src/main/java/org/spacehq/packetlib/tcp/TcpConnectionListener.java +++ b/src/main/java/org/spacehq/packetlib/tcp/TcpConnectionListener.java @@ -85,8 +85,6 @@ public class TcpConnectionListener implements ConnectionListener { pipeline.addLast("sizer", new TcpPacketSizer(session)); pipeline.addLast("codec", new TcpPacketCodec(session)); pipeline.addLast("manager", session); - - server.addSession(session); } }).group(this.group).localAddress(this.host, this.port).bind(); diff --git a/src/main/java/org/spacehq/packetlib/tcp/TcpServerSession.java b/src/main/java/org/spacehq/packetlib/tcp/TcpServerSession.java index 8662dfc8..f9564bab 100644 --- a/src/main/java/org/spacehq/packetlib/tcp/TcpServerSession.java +++ b/src/main/java/org/spacehq/packetlib/tcp/TcpServerSession.java @@ -21,6 +21,13 @@ public class TcpServerSession extends TcpSession { return ret; } + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + super.channelActive(ctx); + + this.server.addSession(this); + } + @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); diff --git a/src/main/java/org/spacehq/packetlib/tcp/TcpSession.java b/src/main/java/org/spacehq/packetlib/tcp/TcpSession.java index 04381734..cc74912f 100644 --- a/src/main/java/org/spacehq/packetlib/tcp/TcpSession.java +++ b/src/main/java/org/spacehq/packetlib/tcp/TcpSession.java @@ -308,18 +308,13 @@ public abstract class TcpSession extends SimpleChannelInboundHandler imp @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - System.err.println("Channel active?"); if(this.disconnected || this.channel != null) { ctx.channel().close(); return; } - System.err.println("Channel active."); - this.channel = ctx.channel(); - System.err.println("Channel set."); - this.packetHandleThread = new Thread(new Runnable() { @Override public void run() {