下面列出了io.netty.channel.kqueue.KQueueDatagramChannel#io.netty.channel.kqueue.KQueueServerSocketChannel 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
@SuppressWarnings("unchecked")
public <CHANNEL extends Channel> CHANNEL getChannel(Class<CHANNEL> channelClass) {
if (channelClass.equals(SocketChannel.class)) {
return (CHANNEL) new KQueueSocketChannel();
}
if (channelClass.equals(ServerSocketChannel.class)) {
return (CHANNEL) new KQueueServerSocketChannel();
}
if (channelClass.equals(DatagramChannel.class)) {
return (CHANNEL) new KQueueDatagramChannel();
}
if (channelClass.equals(DomainSocketChannel.class)) {
return (CHANNEL) new KQueueDomainSocketChannel();
}
if (channelClass.equals(ServerDomainSocketChannel.class)) {
return (CHANNEL) new KQueueServerDomainSocketChannel();
}
throw new IllegalArgumentException("Unsupported channel type: " + channelClass.getSimpleName());
}
public Class<? extends ServerChannel> getServerChannel() {
if (useNativeIo && Epoll.isAvailable()) {
return uds ? EpollServerDomainSocketChannel.class : EpollServerSocketChannel.class;
} else if (useNativeIo && KQueue.isAvailable()) {
return uds ? KQueueServerDomainSocketChannel.class : KQueueServerSocketChannel.class;
}
return NioServerSocketChannel.class;
}
/**
* Returns the correct {@link Class} to use with the given {@link EventLoopGroup}.
*
* @param group the {@link EventLoopGroup} for which the class is needed
* @param addressClass The class of the address that the server socket will be bound to.
* @return the class that should be used for bootstrapping
*/
public static Class<? extends ServerChannel> serverChannel(EventLoopGroup group,
Class<? extends SocketAddress> addressClass) {
if (useEpoll(group)) {
return DomainSocketAddress.class.isAssignableFrom(addressClass) ? EpollServerDomainSocketChannel.class :
EpollServerSocketChannel.class;
} else if (useKQueue(group)) {
return DomainSocketAddress.class.isAssignableFrom(addressClass) ? KQueueServerDomainSocketChannel.class :
KQueueServerSocketChannel.class;
} else {
return NioServerSocketChannel.class;
}
}
public Class<? extends ServerChannel> getServerChannel() {
if (useNativeIo && Epoll.isAvailable()) {
return uds ? EpollServerDomainSocketChannel.class : EpollServerSocketChannel.class;
} else if (useNativeIo && KQueue.isAvailable()) {
return uds ? KQueueServerDomainSocketChannel.class : KQueueServerSocketChannel.class;
}
return NioServerSocketChannel.class;
}
@Override
@SuppressWarnings("unchecked")
public <CHANNEL extends Channel> Class<? extends CHANNEL> getChannelClass(Class<CHANNEL> channelClass) {
if (channelClass.equals(SocketChannel.class)) {
return (Class<? extends CHANNEL>) KQueueSocketChannel.class;
}
if (channelClass.equals(ServerSocketChannel.class)) {
return (Class<? extends CHANNEL>) KQueueServerSocketChannel.class;
}
if (channelClass.equals(DatagramChannel.class)) {
return (Class<? extends CHANNEL>) KQueueDatagramChannel.class;
}
throw new IllegalArgumentException("Unsupported channel type: " + channelClass.getSimpleName());
}
@SuppressWarnings("unchecked")
@Override
public T newChannel() {
switch (channelType) {
case ACCEPTOR:
switch (socketType) {
case JAVA_NIO:
return (T) new NioServerSocketChannel();
case NATIVE_EPOLL:
return (T) new EpollServerSocketChannel();
case NATIVE_KQUEUE:
return (T) new KQueueServerSocketChannel();
case NATIVE_EPOLL_DOMAIN:
return (T) new EpollServerDomainSocketChannel();
case NATIVE_KQUEUE_DOMAIN:
return (T) new KQueueServerDomainSocketChannel();
default:
throw new IllegalStateException("Invalid socket type: " + socketType);
}
case CONNECTOR:
switch (socketType) {
case JAVA_NIO:
return (T) new NioSocketChannel();
case NATIVE_EPOLL:
return (T) new EpollSocketChannel();
case NATIVE_KQUEUE:
return (T) new KQueueSocketChannel();
case NATIVE_EPOLL_DOMAIN:
return (T) new EpollDomainSocketChannel();
case NATIVE_KQUEUE_DOMAIN:
return (T) new KQueueDomainSocketChannel();
default:
throw new IllegalStateException("Invalid socket type: " + socketType);
}
default:
throw new IllegalStateException("Invalid channel type: " + channelType);
}
}
public void run() throws Exception {
// Configure the server.
if (Epoll.isAvailable()) {
doRun(new EpollEventLoopGroup(), EpollServerSocketChannel.class, IoMultiplexer.EPOLL);
} else if (KQueue.isAvailable()) {
doRun(new EpollEventLoopGroup(), KQueueServerSocketChannel.class, IoMultiplexer.KQUEUE);
} else {
doRun(new NioEventLoopGroup(), NioServerSocketChannel.class, IoMultiplexer.JDK);
}
}
public static Class<? extends ServerSocketChannel> serverSocketChannel() {
return EPOLL ? EpollServerSocketChannel.class : KQueue.isAvailable() ? KQueueServerSocketChannel.class : NioServerSocketChannel.class;
}
/**
* https://netty.io/wiki/native-transports.html
*
* RHEL/CentOS/Fedora:
* sudo yum install autoconf automake libtool make tar \
* glibc-devel libaio-devel \
* libgcc.i686 glibc-devel.i686
* Debian/Ubuntu:
* sudo apt-get install autoconf automake libtool make tar \
* gcc-multilib libaio-dev
*
* brew install autoconf automake libtool
* */
private Class<? extends ServerChannel> selectorServerChannel() {
Class<? extends ServerChannel> channel = NioServerSocketChannel.class;
if (serverConfig.isNativeTransport()) {
if (OSInfo.isLinux()) {
channel = EpollServerSocketChannel.class;
} else if (OSInfo.isMacOS()) {
channel = KQueueServerSocketChannel.class;
}
}
LOG.info("Netty channel type is " + channel.getSimpleName());
return channel;
}