Ensure channel is set before returning from connect().

This commit is contained in:
Steven Smith 2015-08-21 21:05:52 -07:00
parent 310de7c490
commit 34c2a5c291
4 changed files with 16 additions and 10 deletions

View file

@ -93,13 +93,20 @@ public class TcpClientSession extends TcpSession {
future.addListener(new ChannelFutureListener() { future.addListener(new ChannelFutureListener() {
@Override @Override
public void operationComplete(ChannelFuture future) throws Exception { public void operationComplete(ChannelFuture future) throws Exception {
System.err.println("Future start.");
if(!future.isSuccess()) { if(!future.isSuccess()) {
exceptionCaught(null, future.cause()); exceptionCaught(null, future.cause());
} }
System.err.println("Future end.");
} }
}).await(); }).await();
if(future.isSuccess()) {
while(!isConnected()) {
try {
Thread.sleep(5);
} catch(InterruptedException e) {
}
}
}
} catch(Throwable t) { } catch(Throwable t) {
exceptionCaught(null, t); exceptionCaught(null, t);
} }
@ -108,7 +115,6 @@ public class TcpClientSession extends TcpSession {
if(wait) { if(wait) {
connectTask.run(); connectTask.run();
System.err.println("Connect completed.");
} else { } else {
new Thread(connectTask).start(); new Thread(connectTask).start();
} }

View file

@ -85,8 +85,6 @@ public class TcpConnectionListener implements ConnectionListener {
pipeline.addLast("sizer", new TcpPacketSizer(session)); pipeline.addLast("sizer", new TcpPacketSizer(session));
pipeline.addLast("codec", new TcpPacketCodec(session)); pipeline.addLast("codec", new TcpPacketCodec(session));
pipeline.addLast("manager", session); pipeline.addLast("manager", session);
server.addSession(session);
} }
}).group(this.group).localAddress(this.host, this.port).bind(); }).group(this.group).localAddress(this.host, this.port).bind();

View file

@ -21,6 +21,13 @@ public class TcpServerSession extends TcpSession {
return ret; return ret;
} }
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
this.server.addSession(this);
}
@Override @Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception { public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx); super.channelInactive(ctx);

View file

@ -308,18 +308,13 @@ public abstract class TcpSession extends SimpleChannelInboundHandler<Packet> imp
@Override @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception { public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.err.println("Channel active?");
if(this.disconnected || this.channel != null) { if(this.disconnected || this.channel != null) {
ctx.channel().close(); ctx.channel().close();
return; return;
} }
System.err.println("Channel active.");
this.channel = ctx.channel(); this.channel = ctx.channel();
System.err.println("Channel set.");
this.packetHandleThread = new Thread(new Runnable() { this.packetHandleThread = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {