2012-02-18 23:02:56 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2011 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.socket.nio;
|
|
|
|
|
|
|
|
import io.netty.channel.AbstractChannel;
|
|
|
|
import io.netty.channel.Channel;
|
2012-05-02 14:05:53 +02:00
|
|
|
import io.netty.channel.ChannelException;
|
|
|
|
import io.netty.channel.ChannelFuture;
|
2012-02-18 23:02:56 +01:00
|
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
2012-05-02 08:01:58 +02:00
|
|
|
import java.nio.channels.SelectableChannel;
|
2012-05-02 14:05:53 +02:00
|
|
|
import java.nio.channels.SelectionKey;
|
2012-02-18 23:02:56 +01:00
|
|
|
|
2012-05-02 08:01:58 +02:00
|
|
|
public abstract class AbstractNioChannel extends AbstractChannel {
|
2012-02-18 23:02:56 +01:00
|
|
|
|
2012-05-02 14:05:53 +02:00
|
|
|
private final SelectableChannel ch;
|
2012-02-18 23:02:56 +01:00
|
|
|
|
|
|
|
private volatile InetSocketAddress localAddress;
|
2012-05-02 14:05:53 +02:00
|
|
|
private volatile InetSocketAddress remoteAddress;
|
2012-05-02 08:01:58 +02:00
|
|
|
|
2012-05-02 14:05:53 +02:00
|
|
|
private volatile SelectionKey selectionKey;
|
2012-05-02 08:01:58 +02:00
|
|
|
|
|
|
|
protected AbstractNioChannel(Integer id, Channel parent, SelectableChannel ch) {
|
|
|
|
super(id, parent);
|
|
|
|
this.ch = ch;
|
2012-02-18 23:02:56 +01:00
|
|
|
}
|
|
|
|
|
2012-05-02 08:01:58 +02:00
|
|
|
protected AbstractNioChannel(Channel parent, SelectableChannel ch) {
|
|
|
|
super(parent);
|
|
|
|
this.ch = ch;
|
2012-03-29 17:07:19 +02:00
|
|
|
}
|
2012-05-02 08:01:58 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected SelectableChannel javaChannel() {
|
|
|
|
return ch;
|
2012-03-06 19:26:32 +01:00
|
|
|
}
|
2012-05-02 08:01:58 +02:00
|
|
|
|
2012-05-02 14:05:53 +02:00
|
|
|
protected SelectionKey selectionKey() {
|
|
|
|
assert selectionKey != null;
|
|
|
|
return selectionKey;
|
|
|
|
}
|
|
|
|
|
2012-02-18 23:02:56 +01:00
|
|
|
@Override
|
2012-05-02 08:01:58 +02:00
|
|
|
public InetSocketAddress localAddress() {
|
2012-02-18 23:02:56 +01:00
|
|
|
InetSocketAddress localAddress = this.localAddress;
|
|
|
|
if (localAddress == null) {
|
|
|
|
try {
|
|
|
|
this.localAddress = localAddress =
|
2012-05-02 08:01:58 +02:00
|
|
|
(InetSocketAddress) unsafe().localAddress();
|
2012-02-18 23:02:56 +01:00
|
|
|
} catch (Throwable t) {
|
|
|
|
// Sometimes fails on a closed socket in Windows.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return localAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-05-02 08:01:58 +02:00
|
|
|
public InetSocketAddress remoteAddress() {
|
2012-02-18 23:02:56 +01:00
|
|
|
InetSocketAddress remoteAddress = this.remoteAddress;
|
|
|
|
if (remoteAddress == null) {
|
|
|
|
try {
|
|
|
|
this.remoteAddress = remoteAddress =
|
2012-05-02 08:01:58 +02:00
|
|
|
(InetSocketAddress) unsafe().remoteAddress();
|
2012-02-18 23:02:56 +01:00
|
|
|
} catch (Throwable t) {
|
|
|
|
// Sometimes fails on a closed socket in Windows.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return remoteAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-05-02 08:01:58 +02:00
|
|
|
public abstract NioChannelConfig config();
|
2012-05-02 14:05:53 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doRegister(ChannelFuture future) {
|
|
|
|
if (!(eventLoop() instanceof SelectorEventLoop)) {
|
|
|
|
throw new ChannelException("unsupported event loop: " + eventLoop().getClass().getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
SelectorEventLoop loop = (SelectorEventLoop) eventLoop();
|
|
|
|
try {
|
|
|
|
selectionKey = javaChannel().register(loop.selector, javaChannel().validOps() & ~SelectionKey.OP_WRITE, this);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ChannelException("failed to register a channel", e);
|
|
|
|
}
|
|
|
|
}
|
2012-02-18 23:02:56 +01:00
|
|
|
}
|