Support unbindAddress and bindAddress for SCTP. See #560

This commit is contained in:
Norman Maurer 2012-09-08 20:20:02 +02:00
parent 36ff79951b
commit 1eea3cf503
2 changed files with 71 additions and 3 deletions

View File

@ -19,9 +19,7 @@ import com.sun.nio.sctp.Association;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Set;
@ -72,10 +70,21 @@ public interface SctpChannel extends Channel {
@Override
SocketAddress remoteAddress();
/**
* Return all remote addresses of the SCTP server channel.
* Please note that, it will return more than one address if the remote is using multi-homing.
*/
Set<SocketAddress> allRemoteAddresses();
/**
* Bind a address to the already bound channel to enable multi-homing.
* The Channel bust be bound and yet to be connected.
*/
ChannelFuture bindAddress(InetAddress localAddress);
/**
* Unbind the address from channel's multi-homing address list.
* The address should be added already in multi-homing address list.
*/
ChannelFuture unbindAddress(InetAddress localAddress);
}

View File

@ -25,6 +25,7 @@ import io.netty.buffer.MessageBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelMetadata;
import io.netty.channel.socket.DefaultSctpChannelConfig;
import io.netty.channel.socket.SctpChannelConfig;
@ -34,6 +35,7 @@ import io.netty.logging.InternalLogger;
import io.netty.logging.InternalLoggerFactory;
import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
@ -48,6 +50,7 @@ public class NioSctpChannel extends AbstractNioMessageChannel implements io.nett
private final SctpChannelConfig config;
@SuppressWarnings("rawtypes")
private final NotificationHandler notificationHandler;
private static SctpChannel newSctpChannel() {
@ -216,6 +219,7 @@ public class NioSctpChannel extends AbstractNioMessageChannel implements io.nett
javaChannel().close();
}
@SuppressWarnings("unchecked")
@Override
protected int doReadMessages(MessageBuf<Object> buf) throws Exception {
SctpChannel ch = javaChannel();
@ -277,4 +281,59 @@ public class NioSctpChannel extends AbstractNioMessageChannel implements io.nett
}
return 1;
}
@Override
public ChannelFuture bindAddress(InetAddress localAddress) {
ChannelFuture future = newFuture();
doBindAddress(localAddress, future);
return future;
}
void doBindAddress(final InetAddress localAddress, final ChannelFuture future) {
if (eventLoop().inEventLoop()) {
try {
javaChannel().bindAddress(localAddress);
future.setSuccess();
// TODO: Do we want to fire an event ?
} catch (Throwable t) {
future.setFailure(t);
pipeline().fireExceptionCaught(t);
}
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
doBindAddress(localAddress, future);
}
});
}
}
@Override
public ChannelFuture unbindAddress(InetAddress localAddress) {
ChannelFuture future = newFuture();
doUnbindAddress(localAddress, future);
return future;
}
void doUnbindAddress(final InetAddress localAddress, final ChannelFuture future) {
if (eventLoop().inEventLoop()) {
try {
javaChannel().unbindAddress(localAddress);
future.setSuccess();
// TODO: Do we want to fire an event ?
} catch (Throwable t) {
future.setFailure(t);
pipeline().fireExceptionCaught(t);
}
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
doUnbindAddress(localAddress, future);
}
});
}
}
}