Skip to content

Commit

Permalink
Revert "Implementation of the latest stable Netty4 version (#902)"
Browse files Browse the repository at this point in the history
This reverts commit 2e1bfa9.
  • Loading branch information
SubJunk committed Jun 15, 2020
1 parent e19728f commit 60bf94d
Show file tree
Hide file tree
Showing 6 changed files with 466 additions and 445 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.50.Final</version>
<artifactId>netty</artifactId> <!-- Use 'netty-all' for 4.0 or above -->
<version>3.10.6.Final</version>
<scope>compile</scope>
</dependency>

Expand Down
78 changes: 37 additions & 41 deletions src/main/java/net/pms/network/HTTPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,21 @@
import java.net.*;
import java.nio.channels.ClosedByInterruptException;
import java.nio.channels.ServerSocketChannel;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import net.pms.PMS;
import net.pms.configuration.PmsConfiguration;
import net.pms.newgui.StatusTab.ConnectionState;
import org.apache.commons.lang3.StringUtils;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.util.ThreadNameDeterminer;
import org.jboss.netty.util.ThreadRenamingRunnable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -53,10 +50,10 @@ public class HTTPServer implements Runnable {
private boolean stop;
private Thread runnable;
private InetAddress iafinal;
private ChannelFactory factory;
private Channel channel;
private NetworkInterface networkInterface;
private EventLoopGroup bossGroup;
private EventLoopGroup workerGroup;
private ChannelGroup group;

// XXX not used
@Deprecated
Expand Down Expand Up @@ -114,32 +111,27 @@ public boolean start() throws IOException {
LOGGER.info("Created socket: {}", address);

if (configuration.isHTTPEngineV2()) { // HTTP Engine V2
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.childOption(ChannelOption.TCP_NODELAY, true)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_SNDBUF, 65536)
.childOption(ChannelOption.SO_RCVBUF, 65536);
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.localAddress(address)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("HttpHandler", new HttpServerCodec())
// eliminate the need to decode http chunks from the client
.addLast("aggregator", new HttpObjectAggregator(64 * 1024))
.addLast("chunkedWriter", new ChunkedWriteHandler())
.addLast("handler", new RequestHandlerV2());
}
});
ThreadRenamingRunnable.setThreadNameDeterminer(ThreadNameDeterminer.CURRENT);
group = new DefaultChannelGroup("HTTPServer");
factory = new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(new NettyBossThreadFactory()),
Executors.newCachedThreadPool(new NettyWorkerThreadFactory())
);

ServerBootstrap bootstrap = new ServerBootstrap(factory);
HttpServerPipelineFactory pipeline = new HttpServerPipelineFactory(group);
bootstrap.setPipelineFactory(pipeline);
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
bootstrap.setOption("reuseAddress", true);
bootstrap.setOption("child.reuseAddress", true);
bootstrap.setOption("child.sendBufferSize", 65536);
bootstrap.setOption("child.receiveBufferSize", 65536);

try {
channel = bootstrap.bind().channel();
channel = bootstrap.bind(address);

group.add(channel);
} catch (Exception e) {
LOGGER.error("Another program is using port " + port + ", which UMS needs.");
LOGGER.error("You can change the port UMS uses on the General Configuration tab.");
Expand Down Expand Up @@ -216,9 +208,13 @@ public void stop() {
}

if (channel != null) { // HTTP Engine V2
channel.close().syncUninterruptibly();
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
if (group != null) {
group.close().awaitUninterruptibly();
}

if (factory != null) {
factory.releaseExternalResources();
}
}

NetworkConfiguration.forgetConfiguration();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/pms/network/HttpException.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package net.pms.network;

import io.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;

public class HttpException extends Exception {
private static final long serialVersionUID = -4952529888336004924L;

private HttpResponseStatus status;

HttpException(HttpResponseStatus status) {
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/net/pms/network/HttpServerPipelineFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2005-2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package net.pms.network;

import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import static org.jboss.netty.channel.Channels.pipeline;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
import org.jboss.netty.handler.stream.ChunkedWriteHandler;

/**
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Andy Taylor (andy.taylor@jboss.org)
*/
public class HttpServerPipelineFactory implements ChannelPipelineFactory {
private ChannelGroup group;

public HttpServerPipelineFactory(ChannelGroup group) {
this.group = group;
}

@Override
public ChannelPipeline getPipeline() throws Exception {
// Create a default pipeline implementation.
ChannelPipeline pipeline = pipeline();
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("aggregator", new HttpChunkAggregator(65536)); // eliminate the need to decode http chunks from the client
pipeline.addLast("encoder", new HttpResponseEncoder());
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
pipeline.addLast("handler", new RequestHandlerV2(group));
return pipeline;
}
}

0 comments on commit 60bf94d

Please sign in to comment.