changed a write on TCP/UDP/UDT to return a 'null channel', that does nothing, instead of actually return null -- so that implementing code doesn't throw a null pointer when trying to write to a socket after it has been closed. Just nothing happens now.

This commit is contained in:
nathan 2014-08-27 15:51:56 +02:00
parent 1be37a6c47
commit fb762bd1cd
2 changed files with 40 additions and 3 deletions

View File

@ -26,6 +26,7 @@ import dorkbox.network.connection.ping.PingFuture;
import dorkbox.network.connection.ping.PingMessage;
import dorkbox.network.connection.ping.PingUtil;
import dorkbox.network.connection.wrapper.ChannelNetworkWrapper;
import dorkbox.network.connection.wrapper.ChannelNull;
import dorkbox.network.connection.wrapper.ChannelWrapper;
@ -261,7 +262,8 @@ public class ConnectionImpl extends ChannelInboundHandlerAdapter
if (logger2.isDebugEnabled()) {
logger2.debug("writing TCP while closed: {}", message);
}
return null;
// we have to return something, otherwise dependent code will throw a null pointer exception
return ChannelNull.get();
}
}
@ -283,7 +285,8 @@ public class ConnectionImpl extends ChannelInboundHandlerAdapter
if (logger2.isDebugEnabled()) {
logger2.debug("writing UDP while closed: {}", message);
}
return null;
// we have to return something, otherwise dependent code will throw a null pointer exception
return ChannelNull.get();
}
}
@ -304,7 +307,8 @@ public class ConnectionImpl extends ChannelInboundHandlerAdapter
if (logger2.isDebugEnabled()) {
logger2.debug("writing UDT while closed: {}", message);
}
return null;
// we have to return something, otherwise dependent code will throw a null pointer exception
return ChannelNull.get();
}
}

View File

@ -0,0 +1,33 @@
package dorkbox.network.connection.wrapper;
import dorkbox.network.connection.ConnectionPoint;
public class ChannelNull implements ConnectionPoint {
private static final ConnectionPoint INSTANCE = new ChannelNull();
public static ConnectionPoint get() {
return INSTANCE;
}
private ChannelNull() {
}
/**
* Write an object to the underlying channel
*/
@Override
public void write(Object object) {
}
/**
* Waits for the last write to complete. Useful when sending large amounts of data at once.
* <b>DO NOT use this in the same thread as receiving messages! It will deadlock.</b>
*/
@Override
public void waitForWriteToComplete() {
}
@Override
public void flush() {
}
}