Skip to content

Commit

Permalink
Move generic unix classes/interfaces out of epoll package
Browse files Browse the repository at this point in the history
Motivation:

As we plan to have other native transports soon (like a kqueue transport) we should move unix classes/interfaces out of the epoll package so we
introduce other implementations without breaking stuff before the next stable release.

Modifications:

Create a new io.netty.channel.unix package and move stuff over there.

Result:

Possible to introduce other native impls beside epoll.
  • Loading branch information
normanmaurer committed Feb 17, 2015
1 parent 4b83eee commit 6e508e6
Show file tree
Hide file tree
Showing 23 changed files with 282 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2015 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.
*/
#include <jni.h>
#include <unistd.h>
#include <errno.h>
#include "io_netty_channel_unix_FileDescriptor.h"

JNIEXPORT int JNICALL Java_io_netty_channel_unix_FileDescriptor_close(JNIEnv* env, jclass clazz, jint fd) {
if (close(fd) < 0) {
return -errno;
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright 2014 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.
*/
#include <jni.h>

int Java_io_netty_channel_unix_FileDescriptor_close(JNIEnv* env, jclass clazz, jint fd);
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
import io.netty.channel.Channel;
import io.netty.channel.ChannelMetadata;
import io.netty.channel.EventLoop;
import io.netty.channel.FileDescriptor;
import io.netty.channel.unix.FileDescriptor;
import io.netty.channel.unix.UnixChannel;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.internal.OneTimeTask;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.UnresolvedAddressException;

abstract class AbstractEpollChannel extends AbstractChannel {
abstract class AbstractEpollChannel extends AbstractChannel implements UnixChannel {
private static final ChannelMetadata DATA = new ChannelMetadata(false);
private final int readFlag;
private volatile FileDescriptor fileDescriptor;
Expand All @@ -44,7 +45,7 @@ abstract class AbstractEpollChannel extends AbstractChannel {
}

AbstractEpollChannel(Channel parent, int fd, int flag, boolean active) {
this(parent, new NativeFileDescriptor(fd), flag, active);
this(parent, new FileDescriptor(fd), flag, active);
}

AbstractEpollChannel(Channel parent, FileDescriptor fd, int flag, boolean active) {
Expand Down Expand Up @@ -76,9 +77,7 @@ boolean isFlagSet(int flag) {
return (flags & flag) != 0;
}

/**
* Returns the {@link FileDescriptor} that is used by this {@link Channel}.
*/
@Override
public final FileDescriptor fd() {
return fileDescriptor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoop;
import io.netty.channel.FileDescriptor;
import io.netty.channel.ServerChannel;
import io.netty.channel.unix.FileDescriptor;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@
import io.netty.channel.ChannelPromise;
import io.netty.channel.ConnectTimeoutException;
import io.netty.channel.DefaultFileRegion;
import io.netty.channel.EventLoop;
import io.netty.channel.FileDescriptor;
import io.netty.channel.RecvByteBufAllocator;
import io.netty.channel.socket.ChannelInputShutdownEvent;
import io.netty.channel.unix.FileDescriptor;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.StringUtil;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.netty.channel.epoll;

import io.netty.channel.ChannelOption;
import io.netty.channel.unix.DomainSocketReadMode;

public final class EpollChannelOption {
private static final Class<EpollChannelOption> T = EpollChannelOption.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import io.netty.channel.DefaultAddressedEnvelope;
import io.netty.channel.FileDescriptor;
import io.netty.channel.RecvByteBufAllocator;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.DatagramChannelConfig;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.unix.FileDescriptor;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.StringUtil;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import io.netty.channel.ChannelConfig;
import io.netty.channel.ChannelOutboundBuffer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.FileDescriptor;
import io.netty.channel.unix.DomainSocketAddress;
import io.netty.channel.unix.DomainSocketChannel;
import io.netty.channel.unix.FileDescriptor;

import java.net.SocketAddress;

public final class EpollDomainSocketChannel extends AbstractEpollStreamChannel {
public final class EpollDomainSocketChannel extends AbstractEpollStreamChannel implements DomainSocketChannel {
private final EpollDomainSocketChannelConfig config = new EpollDomainSocketChannelConfig(this);

private volatile DomainSocketAddress local;
Expand Down Expand Up @@ -107,7 +109,7 @@ protected boolean doWriteSingle(ChannelOutboundBuffer in, int writeSpinCount) th

@Override
protected Object filterOutboundMessage(Object msg) {
if (msg instanceof NativeFileDescriptor) {
if (msg instanceof FileDescriptor) {
return msg;
}
return super.filterOutboundMessage(msg);
Expand Down Expand Up @@ -150,7 +152,7 @@ private void epollInReadFd() {
readPending = false;

try {
pipeline.fireChannelRead(new NativeFileDescriptor(socketFd));
pipeline.fireChannelRead(new FileDescriptor(socketFd));
} catch (Throwable t) {
// keep on reading as we use epoll ET and need to consume everything from the socket
pipeline.fireChannelReadComplete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
import io.netty.channel.ChannelPipeline;
import io.netty.channel.MessageSizeEstimator;
import io.netty.channel.RecvByteBufAllocator;
import io.netty.channel.unix.DomainSocketChannelConfig;
import io.netty.channel.unix.DomainSocketReadMode;

import java.util.Map;

public final class EpollDomainSocketChannelConfig extends EpollChannelConfig {
public final class EpollDomainSocketChannelConfig extends EpollChannelConfig
implements DomainSocketChannelConfig {
private volatile DomainSocketReadMode mode =
DomainSocketReadMode.BYTES;

Expand Down Expand Up @@ -124,15 +127,7 @@ public EpollDomainSocketChannelConfig setEpollMode(EpollMode mode) {
return this;
}

/**
* Change the {@link DomainSocketReadMode} for the channel. The default is
* {@link DomainSocketReadMode#BYTES} which means bytes will be read from the
* {@link Channel} and passed through the pipeline. If
* {@link DomainSocketReadMode#FILE_DESCRIPTORS} is used
* {@link NativeFileDescriptor}s will be passed through the {@link ChannelPipeline}.
*
* This setting can be modified on the fly if needed.
*/
@Override
public EpollDomainSocketChannelConfig setReadMode(DomainSocketReadMode mode) {
if (mode == null) {
throw new NullPointerException("mode");
Expand All @@ -141,9 +136,7 @@ public EpollDomainSocketChannelConfig setReadMode(DomainSocketReadMode mode) {
return this;
}

/**
* Return the {@link DomainSocketReadMode} for the channel.
*/
@Override
public DomainSocketReadMode getReadMode() {
return mode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
package io.netty.channel.epoll;

import io.netty.channel.Channel;
import io.netty.channel.FileDescriptor;
import io.netty.channel.unix.DomainSocketAddress;
import io.netty.channel.unix.FileDescriptor;
import io.netty.channel.unix.ServerDomainSocketChannel;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

import java.io.File;
import java.net.SocketAddress;


public final class EpollServerDomainSocketChannel extends AbstractEpollServerChannel {
public final class EpollServerDomainSocketChannel extends AbstractEpollServerChannel
implements ServerDomainSocketChannel {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(
EpollServerDomainSocketChannel.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import io.netty.channel.Channel;
import io.netty.channel.EventLoop;
import io.netty.channel.FileDescriptor;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.unix.FileDescriptor;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelPromise;
import io.netty.channel.EventLoop;
import io.netty.channel.FileDescriptor;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.unix.FileDescriptor;
import io.netty.util.concurrent.GlobalEventExecutor;
import io.netty.util.internal.OneTimeTask;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.netty.channel.ChannelException;
import io.netty.channel.DefaultFileRegion;
import io.netty.channel.unix.DomainSocketAddress;
import io.netty.util.internal.EmptyArrays;
import io.netty.util.internal.NativeLibraryLoader;
import io.netty.util.internal.PlatformDependent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.channel.epoll;
package io.netty.channel.unix;

import java.io.File;
import java.net.SocketAddress;

/**
* A address for a
* <a href="http://en.wikipedia.org/wiki/Unix_domain_socket">Unix Domain Socket</a>.
*/
public final class DomainSocketAddress extends SocketAddress {
private final String socketPath;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2015 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.unix;

/**
* A {@link UnixChannel} that supports communication via
* <a href="http://en.wikipedia.org/wiki/Unix_domain_socket">Unix Domain Socket</a>.
*/
public interface DomainSocketChannel extends UnixChannel {
@Override
DomainSocketAddress remoteAddress();

@Override
DomainSocketAddress localAddress();

@Override
DomainSocketChannelConfig config();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2015 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.unix;

import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelConfig;
import io.netty.channel.MessageSizeEstimator;
import io.netty.channel.RecvByteBufAllocator;

/**
* Special {@link ChannelConfig} for {@link DomainSocketChannel}s.
*/
public interface DomainSocketChannelConfig extends ChannelConfig {

@Override
DomainSocketChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead);

@Override
DomainSocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis);

@Override
DomainSocketChannelConfig setWriteSpinCount(int writeSpinCount);

@Override
DomainSocketChannelConfig setAllocator(ByteBufAllocator allocator);

@Override
DomainSocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator);

@Override
DomainSocketChannelConfig setAutoRead(boolean autoRead);

@Override
DomainSocketChannelConfig setAutoClose(boolean autoClose);

@Override
DomainSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark);

@Override
DomainSocketChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark);

@Override
DomainSocketChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator);

/**
* Change the {@link DomainSocketReadMode} for the channel. The default is
* {@link DomainSocketReadMode#BYTES} which means bytes will be read from the
* {@link io.netty.channel.Channel} and passed through the pipeline. If
* {@link DomainSocketReadMode#FILE_DESCRIPTORS} is used
* {@link FileDescriptor}s will be passed through the {@link io.netty.channel.ChannelPipeline}.
*
* This setting can be modified on the fly if needed.
*/
DomainSocketChannelConfig setReadMode(DomainSocketReadMode mode);

/**
* Return the {@link DomainSocketReadMode} for the channel.
*/
DomainSocketReadMode getReadMode();
}
Loading

0 comments on commit 6e508e6

Please sign in to comment.