refactor eventlooploader
This commit is contained in:
parent
a966c61b0a
commit
1d1a2027e0
4 changed files with 28 additions and 32 deletions
|
@ -58,6 +58,7 @@ import proxy.network.Direction;
|
|||
import proxy.network.Decoder;
|
||||
import proxy.network.Splitter;
|
||||
import proxy.network.Encoder;
|
||||
import proxy.network.EventLoopLoader;
|
||||
import proxy.network.Prepender;
|
||||
import proxy.command.*;
|
||||
import proxy.handler.HandshakeHandler;
|
||||
|
@ -68,17 +69,16 @@ import proxy.packet.S40PacketDisconnect;
|
|||
import proxy.packet.ServerInfo;
|
||||
import proxy.util.Formatter;
|
||||
import proxy.util.User;
|
||||
import proxy.util.LazyLoader;
|
||||
import proxy.util.Log;
|
||||
|
||||
public class Proxy {
|
||||
public static final LazyLoader<NioEventLoopGroup> NIO_EVENT_LOOP = new LazyLoader<NioEventLoopGroup>() {
|
||||
protected NioEventLoopGroup load() {
|
||||
public static final EventLoopLoader<NioEventLoopGroup> NIO_EVENT_LOOP = new EventLoopLoader<NioEventLoopGroup>() {
|
||||
protected NioEventLoopGroup createEventLoop() {
|
||||
return new NioEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Server IO #%d").setDaemon(true).build());
|
||||
}
|
||||
};
|
||||
public static final LazyLoader<EpollEventLoopGroup> EPOLL_EVENT_LOOP = new LazyLoader<EpollEventLoopGroup>() {
|
||||
protected EpollEventLoopGroup load() {
|
||||
public static final EventLoopLoader<EpollEventLoopGroup> EPOLL_EVENT_LOOP = new EventLoopLoader<EpollEventLoopGroup>() {
|
||||
protected EpollEventLoopGroup createEventLoop() {
|
||||
return new EpollEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Epoll Server IO #%d").setDaemon(true).build());
|
||||
}
|
||||
};
|
||||
|
@ -229,7 +229,7 @@ public class Proxy {
|
|||
public void addLanEndpoint(InetAddress address, int port) throws IOException {
|
||||
synchronized(this.endpoints) {
|
||||
Class<? extends ServerSocketChannel> oclass;
|
||||
LazyLoader<? extends EventLoopGroup> lazyloadbase;
|
||||
EventLoopLoader<? extends EventLoopGroup> lazyloadbase;
|
||||
|
||||
if(Epoll.isAvailable() && this.epoll) {
|
||||
oclass = EpollServerSocketChannel.class;
|
||||
|
@ -262,7 +262,7 @@ public class Proxy {
|
|||
p_initChannel_1_.pipeline().addLast((String)"packet_handler", (ChannelHandler)networkmanager);
|
||||
networkmanager.setNetHandler(new HandshakeHandler(Proxy.this, networkmanager));
|
||||
}
|
||||
}).group(lazyloadbase.getValue()).localAddress(address, port)).bind().syncUninterruptibly());
|
||||
}).group(lazyloadbase.get()).localAddress(address, port)).bind().syncUninterruptibly());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -31,22 +31,21 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
|||
|
||||
import proxy.handler.Handler;
|
||||
import proxy.handler.Handler.ThreadQuickExitException;
|
||||
import proxy.util.LazyLoader;
|
||||
import proxy.util.Log;
|
||||
|
||||
public class Connection extends SimpleChannelInboundHandler<Packet>
|
||||
{
|
||||
public static final AttributeKey<Protocol> STATE = AttributeKey.<Protocol>valueOf("protocol");
|
||||
public static final LazyLoader<NioEventLoopGroup> CLIENT_NIO_EVENTLOOP = new LazyLoader<NioEventLoopGroup>()
|
||||
public static final EventLoopLoader<NioEventLoopGroup> CLIENT_NIO_EVENTLOOP = new EventLoopLoader<NioEventLoopGroup>()
|
||||
{
|
||||
protected NioEventLoopGroup load()
|
||||
protected NioEventLoopGroup createEventLoop()
|
||||
{
|
||||
return new NioEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Client IO #%d").setDaemon(true).build());
|
||||
}
|
||||
};
|
||||
public static final LazyLoader<EpollEventLoopGroup> CLIENT_EPOLL_EVENTLOOP = new LazyLoader<EpollEventLoopGroup>()
|
||||
public static final EventLoopLoader<EpollEventLoopGroup> CLIENT_EPOLL_EVENTLOOP = new EventLoopLoader<EpollEventLoopGroup>()
|
||||
{
|
||||
protected EpollEventLoopGroup load()
|
||||
protected EpollEventLoopGroup createEventLoop()
|
||||
{
|
||||
return new EpollEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Epoll Client IO #%d").setDaemon(true).build());
|
||||
}
|
||||
|
@ -302,7 +301,7 @@ public class Connection extends SimpleChannelInboundHandler<Packet>
|
|||
{
|
||||
final Connection networkmanager = new Connection(Direction.CLIENT);
|
||||
Class <? extends SocketChannel > oclass;
|
||||
LazyLoader <? extends EventLoopGroup > lazyloadbase;
|
||||
EventLoopLoader <? extends EventLoopGroup > lazyloadbase;
|
||||
|
||||
if (Epoll.isAvailable() && useNativeTransport)
|
||||
{
|
||||
|
@ -315,7 +314,7 @@ public class Connection extends SimpleChannelInboundHandler<Packet>
|
|||
lazyloadbase = CLIENT_NIO_EVENTLOOP;
|
||||
}
|
||||
|
||||
((Bootstrap)((Bootstrap)((Bootstrap)(new Bootstrap()).group(lazyloadbase.getValue())).handler(new ChannelInitializer<Channel>()
|
||||
((Bootstrap)((Bootstrap)((Bootstrap)(new Bootstrap()).group(lazyloadbase.get())).handler(new ChannelInitializer<Channel>()
|
||||
{
|
||||
protected void initChannel(Channel p_initChannel_1_) throws Exception
|
||||
{
|
||||
|
|
15
proxy/src/main/java/proxy/network/EventLoopLoader.java
Executable file
15
proxy/src/main/java/proxy/network/EventLoopLoader.java
Executable file
|
@ -0,0 +1,15 @@
|
|||
package proxy.network;
|
||||
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
|
||||
public abstract class EventLoopLoader<T extends EventLoopGroup> {
|
||||
private T group;
|
||||
|
||||
public T get() {
|
||||
if(this.group == null)
|
||||
this.group = this.createEventLoop();
|
||||
return this.group;
|
||||
}
|
||||
|
||||
protected abstract T createEventLoop();
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
package proxy.util;
|
||||
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
|
||||
public abstract class LazyLoader<T extends EventLoopGroup> {
|
||||
private T value;
|
||||
private boolean isLoaded = false;
|
||||
|
||||
public T getValue() {
|
||||
if(!this.isLoaded) {
|
||||
this.isLoaded = true;
|
||||
this.value = this.load();
|
||||
}
|
||||
return this.value;
|
||||
}
|
||||
|
||||
protected abstract T load();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue