Minor fixes and cleanup.

This commit is contained in:
Steven Smith 2015-08-21 20:47:37 -07:00
parent 9c1ac6aa9b
commit 83a5cb405c
5 changed files with 74 additions and 18 deletions

View file

@ -56,4 +56,12 @@ public interface ConnectionListener {
* @param wait Whether to wait for the listener to finish closing. * @param wait Whether to wait for the listener to finish closing.
*/ */
public void close(boolean wait); public void close(boolean wait);
/**
* Closes the listener.
*
* @param wait Whether to wait for the listener to finish closing.
* @param callback Callback to call when the listener has finished closing.
*/
public void close(boolean wait, Runnable callback);
} }

View file

@ -248,7 +248,7 @@ public class Server {
* Closes the server. * Closes the server.
*/ */
public void close() { public void close() {
this.close(false); this.close(true);
} }
/** /**
@ -264,7 +264,11 @@ public class Server {
} }
} }
this.listener.close(wait); this.listener.close(wait, new Runnable() {
this.callEvent(new ServerClosedEvent(this)); @Override
public void run() {
callEvent(new ServerClosedEvent(Server.this));
}
});
} }
} }

View file

@ -93,7 +93,7 @@ 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 {
if(!future.isSuccess() && future.cause() != null) { if(!future.isSuccess()) {
exceptionCaught(null, future.cause()); exceptionCaught(null, future.cause());
} }
} }

View file

@ -11,6 +11,7 @@ import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.Future; import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import org.spacehq.packetlib.ConnectionListener; import org.spacehq.packetlib.ConnectionListener;
import org.spacehq.packetlib.Server; import org.spacehq.packetlib.Server;
import org.spacehq.packetlib.packet.PacketProtocol; import org.spacehq.packetlib.packet.PacketProtocol;
@ -57,7 +58,7 @@ public class TcpConnectionListener implements ConnectionListener {
} }
@Override @Override
public void bind(final boolean wait, final Runnable callback) { public void bind(boolean wait, final Runnable callback) {
if(this.group != null || this.channel != null) { if(this.group != null || this.channel != null) {
return; return;
} }
@ -96,17 +97,23 @@ public class TcpConnectionListener implements ConnectionListener {
} }
channel = future.channel(); channel = future.channel();
callback.run(); if(callback != null) {
callback.run();
}
} else { } else {
future.addListener(new ChannelFutureListener() { future.addListener(new ChannelFutureListener() {
@Override @Override
public void operationComplete(ChannelFuture channelFuture) throws Exception { public void operationComplete(ChannelFuture future) throws Exception {
if(channelFuture.isSuccess()) { if(future.isSuccess()) {
channel = channelFuture.channel(); channel = future.channel();
callback.run(); if(callback != null) {
} else if(channelFuture.cause() != null && !wait) { callback.run();
}
} else {
System.err.println("[ERROR] Failed to asynchronously bind connection listener."); System.err.println("[ERROR] Failed to asynchronously bind connection listener.");
channelFuture.cause().printStackTrace(); if(future.cause() != null) {
future.cause().printStackTrace();
}
} }
} }
}); });
@ -120,14 +127,39 @@ public class TcpConnectionListener implements ConnectionListener {
@Override @Override
public void close(boolean wait) { public void close(boolean wait) {
this.close(wait, null);
}
@Override
public void close(boolean wait, final Runnable callback) {
if(this.channel != null) { if(this.channel != null) {
if(this.channel.isOpen()) { if(this.channel.isOpen()) {
ChannelFuture future = this.channel.close(); ChannelFuture future = this.channel.close();
if(wait) { if(wait) {
try { try {
future.await(); future.sync();
} catch(InterruptedException e) { } catch(InterruptedException e) {
} }
if(callback != null) {
callback.run();
}
} else {
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if(future.isSuccess()) {
if(callback != null) {
callback.run();
}
} else {
System.err.println("[ERROR] Failed to asynchronously close connection listener.");
if(future.cause() != null) {
future.cause().printStackTrace();
}
}
}
});
} }
} }
@ -138,9 +170,21 @@ public class TcpConnectionListener implements ConnectionListener {
Future<?> future = this.group.shutdownGracefully(); Future<?> future = this.group.shutdownGracefully();
if(wait) { if(wait) {
try { try {
future.await(); future.sync();
} catch(InterruptedException e) { } catch(InterruptedException e) {
} }
} else {
future.addListener(new GenericFutureListener() {
@Override
public void operationComplete(Future future) throws Exception {
if(!future.isSuccess()) {
System.err.println("[ERROR] Failed to asynchronously close connection listener.");
if(future.cause() != null) {
future.cause().printStackTrace();
}
}
}
});
} }
this.group = null; this.group = null;

View file

@ -200,10 +200,10 @@ public abstract class TcpSession extends SimpleChannelInboundHandler<Packet> imp
ChannelFuture future = this.channel.writeAndFlush(packet).addListener(new ChannelFutureListener() { ChannelFuture future = this.channel.writeAndFlush(packet).addListener(new ChannelFutureListener() {
@Override @Override
public void operationComplete(ChannelFuture future) throws Exception { public void operationComplete(ChannelFuture future) throws Exception {
if(!future.isSuccess()) { if(future.isSuccess()) {
exceptionCaught(null, future.cause());
} else {
callEvent(new PacketSentEvent(TcpSession.this, packet)); callEvent(new PacketSentEvent(TcpSession.this, packet));
} else {
exceptionCaught(null, future.cause());
} }
} }
}); });
@ -248,7 +248,7 @@ public abstract class TcpSession extends SimpleChannelInboundHandler<Packet> imp
this.callEvent(new DisconnectingEvent(this, reason, cause)); this.callEvent(new DisconnectingEvent(this, reason, cause));
ChannelFuture future = this.channel.flush().close().addListener(new ChannelFutureListener() { ChannelFuture future = this.channel.flush().close().addListener(new ChannelFutureListener() {
@Override @Override
public void operationComplete(ChannelFuture channelFuture) throws Exception { public void operationComplete(ChannelFuture future) throws Exception {
callEvent(new DisconnectedEvent(TcpSession.this, reason != null ? reason : "Connection closed.", cause)); callEvent(new DisconnectedEvent(TcpSession.this, reason != null ? reason : "Connection closed.", cause));
} }
}); });