2014-04-11 21:08:26 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2012 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
package io.netty.channel.epoll;
|
|
|
|
|
|
|
|
import io.netty.buffer.ByteBufAllocator;
|
|
|
|
import io.netty.channel.ChannelOption;
|
|
|
|
import io.netty.channel.DefaultChannelConfig;
|
|
|
|
import io.netty.channel.FixedRecvByteBufAllocator;
|
|
|
|
import io.netty.channel.MessageSizeEstimator;
|
|
|
|
import io.netty.channel.RecvByteBufAllocator;
|
|
|
|
import io.netty.channel.socket.DatagramChannelConfig;
|
|
|
|
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.NetworkInterface;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
public final class EpollDatagramChannelConfig extends DefaultChannelConfig implements DatagramChannelConfig {
|
|
|
|
private static final RecvByteBufAllocator DEFAULT_RCVBUF_ALLOCATOR = new FixedRecvByteBufAllocator(2048);
|
|
|
|
private final EpollDatagramChannel datagramChannel;
|
|
|
|
private boolean activeOnOpen;
|
|
|
|
|
|
|
|
EpollDatagramChannelConfig(EpollDatagramChannel channel) {
|
|
|
|
super(channel);
|
2014-04-10 08:27:27 +02:00
|
|
|
datagramChannel = channel;
|
2014-04-11 21:08:26 +02:00
|
|
|
setRecvByteBufAllocator(DEFAULT_RCVBUF_ALLOCATOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Map<ChannelOption<?>, Object> getOptions() {
|
|
|
|
return getOptions(
|
|
|
|
super.getOptions(),
|
|
|
|
ChannelOption.SO_BROADCAST, ChannelOption.SO_RCVBUF, ChannelOption.SO_SNDBUF,
|
|
|
|
ChannelOption.SO_REUSEADDR, ChannelOption.IP_MULTICAST_LOOP_DISABLED,
|
|
|
|
ChannelOption.IP_MULTICAST_ADDR, ChannelOption.IP_MULTICAST_IF, ChannelOption.IP_MULTICAST_TTL,
|
|
|
|
ChannelOption.IP_TOS, ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@Override
|
|
|
|
public <T> T getOption(ChannelOption<T> option) {
|
|
|
|
if (option == ChannelOption.SO_BROADCAST) {
|
|
|
|
return (T) Boolean.valueOf(isBroadcast());
|
|
|
|
}
|
|
|
|
if (option == ChannelOption.SO_RCVBUF) {
|
|
|
|
return (T) Integer.valueOf(getReceiveBufferSize());
|
|
|
|
}
|
|
|
|
if (option == ChannelOption.SO_SNDBUF) {
|
|
|
|
return (T) Integer.valueOf(getSendBufferSize());
|
|
|
|
}
|
|
|
|
if (option == ChannelOption.SO_REUSEADDR) {
|
|
|
|
return (T) Boolean.valueOf(isReuseAddress());
|
|
|
|
}
|
|
|
|
if (option == ChannelOption.IP_MULTICAST_LOOP_DISABLED) {
|
|
|
|
return (T) Boolean.valueOf(isLoopbackModeDisabled());
|
|
|
|
}
|
|
|
|
if (option == ChannelOption.IP_MULTICAST_ADDR) {
|
|
|
|
T i = (T) getInterface();
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
if (option == ChannelOption.IP_MULTICAST_IF) {
|
|
|
|
T i = (T) getNetworkInterface();
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
if (option == ChannelOption.IP_MULTICAST_TTL) {
|
|
|
|
return (T) Integer.valueOf(getTimeToLive());
|
|
|
|
}
|
|
|
|
if (option == ChannelOption.IP_TOS) {
|
|
|
|
return (T) Integer.valueOf(getTrafficClass());
|
|
|
|
}
|
|
|
|
if (option == ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) {
|
|
|
|
return (T) Boolean.valueOf(activeOnOpen);
|
|
|
|
}
|
|
|
|
return super.getOption(option);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <T> boolean setOption(ChannelOption<T> option, T value) {
|
|
|
|
validate(option, value);
|
|
|
|
|
|
|
|
if (option == ChannelOption.SO_BROADCAST) {
|
|
|
|
setBroadcast((Boolean) value);
|
|
|
|
} else if (option == ChannelOption.SO_RCVBUF) {
|
|
|
|
setReceiveBufferSize((Integer) value);
|
|
|
|
} else if (option == ChannelOption.SO_SNDBUF) {
|
|
|
|
setSendBufferSize((Integer) value);
|
|
|
|
} else if (option == ChannelOption.SO_REUSEADDR) {
|
|
|
|
setReuseAddress((Boolean) value);
|
|
|
|
} else if (option == ChannelOption.IP_MULTICAST_LOOP_DISABLED) {
|
|
|
|
setLoopbackModeDisabled((Boolean) value);
|
|
|
|
} else if (option == ChannelOption.IP_MULTICAST_ADDR) {
|
|
|
|
setInterface((InetAddress) value);
|
|
|
|
} else if (option == ChannelOption.IP_MULTICAST_IF) {
|
|
|
|
setNetworkInterface((NetworkInterface) value);
|
|
|
|
} else if (option == ChannelOption.IP_MULTICAST_TTL) {
|
|
|
|
setTimeToLive((Integer) value);
|
|
|
|
} else if (option == ChannelOption.IP_TOS) {
|
|
|
|
setTrafficClass((Integer) value);
|
|
|
|
} else if (option == ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION) {
|
|
|
|
setActiveOnOpen((Boolean) value);
|
|
|
|
} else {
|
|
|
|
return super.setOption(option, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setActiveOnOpen(boolean activeOnOpen) {
|
|
|
|
if (channel.isRegistered()) {
|
|
|
|
throw new IllegalStateException("Can only changed before channel was registered");
|
|
|
|
}
|
|
|
|
this.activeOnOpen = activeOnOpen;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EpollDatagramChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
|
|
|
|
super.setMessageSizeEstimator(estimator);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EpollDatagramChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
|
|
|
|
super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EpollDatagramChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
|
|
|
|
super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EpollDatagramChannelConfig setAutoClose(boolean autoClose) {
|
|
|
|
super.setAutoClose(autoClose);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EpollDatagramChannelConfig setAutoRead(boolean autoRead) {
|
|
|
|
super.setAutoRead(autoRead);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EpollDatagramChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
|
|
|
|
super.setRecvByteBufAllocator(allocator);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EpollDatagramChannelConfig setWriteSpinCount(int writeSpinCount) {
|
|
|
|
super.setWriteSpinCount(writeSpinCount);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EpollDatagramChannelConfig setAllocator(ByteBufAllocator allocator) {
|
|
|
|
super.setAllocator(allocator);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EpollDatagramChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
|
|
|
|
super.setConnectTimeoutMillis(connectTimeoutMillis);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EpollDatagramChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
|
|
|
|
super.setMaxMessagesPerRead(maxMessagesPerRead);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSendBufferSize() {
|
|
|
|
return Native.getSendBufferSize(datagramChannel.fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EpollDatagramChannelConfig setSendBufferSize(int sendBufferSize) {
|
|
|
|
Native.setSendBufferSize(datagramChannel.fd, sendBufferSize);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getReceiveBufferSize() {
|
|
|
|
return Native.getReceiveBufferSize(datagramChannel.fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EpollDatagramChannelConfig setReceiveBufferSize(int receiveBufferSize) {
|
|
|
|
Native.setReceiveBufferSize(datagramChannel.fd, receiveBufferSize);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getTrafficClass() {
|
|
|
|
return Native.getTrafficClass(datagramChannel.fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EpollDatagramChannelConfig setTrafficClass(int trafficClass) {
|
|
|
|
Native.setTrafficClass(datagramChannel.fd, trafficClass);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isReuseAddress() {
|
|
|
|
return Native.isReuseAddress(datagramChannel.fd) == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EpollDatagramChannelConfig setReuseAddress(boolean reuseAddress) {
|
|
|
|
Native.setReuseAddress(datagramChannel.fd, reuseAddress ? 1 : 0);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isBroadcast() {
|
|
|
|
return Native.isBroadcast(datagramChannel.fd) == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EpollDatagramChannelConfig setBroadcast(boolean broadcast) {
|
|
|
|
Native.setBroadcast(datagramChannel.fd, broadcast ? 1 : 0);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isLoopbackModeDisabled() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public DatagramChannelConfig setLoopbackModeDisabled(boolean loopbackModeDisabled) {
|
|
|
|
throw new UnsupportedOperationException("Multicast not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getTimeToLive() {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EpollDatagramChannelConfig setTimeToLive(int ttl) {
|
|
|
|
throw new UnsupportedOperationException("Multicast not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InetAddress getInterface() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EpollDatagramChannelConfig setInterface(InetAddress interfaceAddress) {
|
|
|
|
throw new UnsupportedOperationException("Multicast not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NetworkInterface getNetworkInterface() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EpollDatagramChannelConfig setNetworkInterface(NetworkInterface networkInterface) {
|
|
|
|
throw new UnsupportedOperationException("Multicast not supported");
|
|
|
|
}
|
2014-04-10 08:27:27 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void autoReadCleared() {
|
|
|
|
datagramChannel.clearEpollIn();
|
|
|
|
}
|
2014-04-11 21:08:26 +02:00
|
|
|
}
|