2009-06-11 02:48:49 +02:00
|
|
|
/*
|
2011-12-09 06:18:34 +01:00
|
|
|
* Copyright 2011 The Netty Project
|
2009-06-11 02:48:49 +02:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* 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:
|
2009-06-11 02:48:49 +02:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-06-11 02:48:49 +02:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 06:18:34 +01:00
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2009-08-28 09:15:49 +02:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2009-06-11 02:48:49 +02:00
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
package io.netty.channel.socket.nio;
|
2009-06-11 02:48:49 +02:00
|
|
|
|
2012-02-18 23:02:56 +01:00
|
|
|
import static io.netty.channel.Channels.fireChannelOpen;
|
|
|
|
import io.netty.channel.ChannelException;
|
|
|
|
import io.netty.channel.ChannelFactory;
|
|
|
|
import io.netty.channel.ChannelPipeline;
|
|
|
|
import io.netty.channel.ChannelSink;
|
|
|
|
import io.netty.channel.socket.DatagramChannelConfig;
|
2009-06-11 02:48:49 +02:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2009-06-11 08:10:46 +02:00
|
|
|
import java.net.InetAddress;
|
2009-06-11 02:48:49 +02:00
|
|
|
import java.net.InetSocketAddress;
|
2009-06-11 08:10:46 +02:00
|
|
|
import java.net.NetworkInterface;
|
2009-06-11 02:48:49 +02:00
|
|
|
import java.nio.channels.DatagramChannel;
|
|
|
|
|
|
|
|
/**
|
2011-12-09 04:38:59 +01:00
|
|
|
* Provides an NIO based {@link io.netty.channel.socket.DatagramChannel}.
|
2009-06-11 02:48:49 +02:00
|
|
|
*/
|
2012-02-18 23:02:56 +01:00
|
|
|
final class NioDatagramChannel extends AbstractNioChannel<DatagramChannel>
|
2011-12-09 04:38:59 +01:00
|
|
|
implements io.netty.channel.socket.DatagramChannel {
|
2009-06-11 02:48:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The {@link DatagramChannelConfig}.
|
|
|
|
*/
|
|
|
|
private final NioDatagramChannelConfig config;
|
|
|
|
|
2012-02-18 23:02:56 +01:00
|
|
|
|
2011-11-02 19:17:10 +01:00
|
|
|
static NioDatagramChannel create(ChannelFactory factory,
|
|
|
|
ChannelPipeline pipeline, ChannelSink sink, NioDatagramWorker worker) {
|
|
|
|
NioDatagramChannel instance =
|
|
|
|
new NioDatagramChannel(factory, pipeline, sink, worker);
|
|
|
|
fireChannelOpen(instance);
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private NioDatagramChannel(final ChannelFactory factory,
|
2009-06-11 02:48:49 +02:00
|
|
|
final ChannelPipeline pipeline, final ChannelSink sink,
|
2009-06-12 04:47:57 +02:00
|
|
|
final NioDatagramWorker worker) {
|
2012-02-18 23:02:56 +01:00
|
|
|
super(null, factory, pipeline, sink, worker, openNonBlockingChannel());
|
|
|
|
config = new DefaultNioDatagramChannelConfig(channel.socket());
|
2009-06-11 02:48:49 +02:00
|
|
|
}
|
|
|
|
|
2012-02-18 23:02:56 +01:00
|
|
|
private static DatagramChannel openNonBlockingChannel() {
|
2009-06-11 02:48:49 +02:00
|
|
|
try {
|
|
|
|
final DatagramChannel channel = DatagramChannel.open();
|
|
|
|
channel.configureBlocking(false);
|
|
|
|
return channel;
|
|
|
|
} catch (final IOException e) {
|
|
|
|
throw new ChannelException("Failed to open a DatagramChannel.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2009-06-11 02:48:49 +02:00
|
|
|
public boolean isBound() {
|
2012-02-18 23:02:56 +01:00
|
|
|
return isOpen() && channel.socket().isBound();
|
2009-06-11 02:48:49 +02:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2009-06-11 02:48:49 +02:00
|
|
|
public boolean isConnected() {
|
2012-02-18 23:02:56 +01:00
|
|
|
return channel.isConnected();
|
2009-06-11 02:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean setClosed() {
|
|
|
|
return super.setClosed();
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2009-06-11 02:48:49 +02:00
|
|
|
public NioDatagramChannelConfig getConfig() {
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
DatagramChannel getDatagramChannel() {
|
2012-02-18 23:02:56 +01:00
|
|
|
return channel;
|
2009-06-11 02:48:49 +02:00
|
|
|
}
|
2009-06-11 08:10:46 +02:00
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2009-06-11 08:10:46 +02:00
|
|
|
public void joinGroup(InetAddress multicastAddress) {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2009-06-11 08:10:46 +02:00
|
|
|
public void joinGroup(InetSocketAddress multicastAddress,
|
|
|
|
NetworkInterface networkInterface) {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2009-06-11 08:10:46 +02:00
|
|
|
public void leaveGroup(InetAddress multicastAddress) {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2009-06-11 08:10:46 +02:00
|
|
|
public void leaveGroup(InetSocketAddress multicastAddress,
|
|
|
|
NetworkInterface networkInterface) {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
2012-02-18 23:02:56 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
InetSocketAddress getLocalSocketAddress() throws Exception {
|
|
|
|
return (InetSocketAddress) channel.socket().getLocalSocketAddress();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
InetSocketAddress getRemoteSocketAddress() throws Exception {
|
|
|
|
return (InetSocketAddress) channel.socket().getRemoteSocketAddress();
|
|
|
|
}
|
2009-06-11 02:48:49 +02:00
|
|
|
}
|