добавлен пул для NettyConnectionContext
This commit is contained in:
@@ -1,14 +1,19 @@
|
||||
package mc.protocol;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Setter;
|
||||
import lombok.experimental.Accessors;
|
||||
import mc.protocol.api.ConnectionContext;
|
||||
import mc.protocol.packets.ServerSidePacket;
|
||||
import mc.protocol.pool.Passivable;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class NettyConnectionContext implements ConnectionContext {
|
||||
@EqualsAndHashCode
|
||||
public class NettyConnectionContext implements ConnectionContext, Passivable {
|
||||
|
||||
private final ChannelHandlerContext ctx;
|
||||
@Accessors(chain = true)
|
||||
@Setter
|
||||
private ChannelHandlerContext ctx;
|
||||
|
||||
@Override
|
||||
public State getState() {
|
||||
@@ -39,4 +44,9 @@ public class NettyConnectionContext implements ConnectionContext {
|
||||
public void disconnect() {
|
||||
ctx.disconnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void passivate() {
|
||||
this.ctx = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import mc.protocol.io.codec.ProtocolSplitter;
|
||||
import mc.protocol.packets.ClientSidePacket;
|
||||
import mc.protocol.event.EventBus;
|
||||
import mc.protocol.pool.PacketPool;
|
||||
import org.apache.commons.pool2.ObjectPool;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -29,6 +30,7 @@ import java.util.function.Consumer;
|
||||
@RequiredArgsConstructor
|
||||
public class NettyServer implements Server {
|
||||
|
||||
private final ObjectPool<NettyConnectionContext> poolNettyConnectionContext;
|
||||
private final PacketPool packetPool;
|
||||
private final EventBus eventBus;
|
||||
private Consumer<ConnectionContext> consumerNewConnection;
|
||||
@@ -88,9 +90,9 @@ public class NettyServer implements Server {
|
||||
|
||||
map.put("packet_splitter", new ProtocolSplitter());
|
||||
map.put("logger", new LoggingHandler(LogLevel.DEBUG));
|
||||
map.put("packet_decoder", new ProtocolDecoder(true, packetPool, consumerNewConnection, consumerDisconnect));
|
||||
map.put("packet_decoder", new ProtocolDecoder(true, poolNettyConnectionContext, packetPool, consumerNewConnection, consumerDisconnect));
|
||||
map.put("packet_encoder", new ProtocolEncoder());
|
||||
map.put("packet_handler", new PacketInboundHandler(packetPool, eventBus));
|
||||
map.put("packet_handler", new PacketInboundHandler(poolNettyConnectionContext, packetPool, eventBus));
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
@@ -6,18 +6,23 @@ import lombok.RequiredArgsConstructor;
|
||||
import mc.protocol.packets.ClientSidePacket;
|
||||
import mc.protocol.event.EventBus;
|
||||
import mc.protocol.pool.PacketPool;
|
||||
import org.apache.commons.pool2.ObjectPool;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class PacketInboundHandler extends SimpleChannelInboundHandler<ClientSidePacket> {
|
||||
|
||||
private final ObjectPool<NettyConnectionContext> poolNettyConnectionContext;
|
||||
private final PacketPool poolPackets;
|
||||
private final EventBus eventBus;
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, ClientSidePacket packet) throws Exception {
|
||||
State state = ctx.channel().attr(NetworkAttributes.STATE).get();
|
||||
eventBus.emit(state, new NettyConnectionContext(ctx), packet);
|
||||
|
||||
NettyConnectionContext context = poolNettyConnectionContext.borrowObject().setCtx(ctx);
|
||||
eventBus.emit(state, context, packet);
|
||||
|
||||
poolNettyConnectionContext.returnObject(context);
|
||||
poolPackets.returnObject(packet);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,14 @@ package mc.protocol.di;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import mc.protocol.NettyConnectionContext;
|
||||
import mc.protocol.NettyServer;
|
||||
import mc.protocol.State;
|
||||
import mc.protocol.api.Server;
|
||||
import mc.protocol.packets.ClientSidePacket;
|
||||
import mc.protocol.packets.UnknownPacket;
|
||||
import mc.protocol.event.EventBus;
|
||||
import mc.protocol.pool.NettyConnectionContextFactory;
|
||||
import mc.protocol.pool.PacketFactory;
|
||||
import mc.protocol.pool.PacketPool;
|
||||
import mc.protocol.event.SimpleEventBus;
|
||||
@@ -23,8 +25,8 @@ public class ProtocolModule {
|
||||
|
||||
@Provides
|
||||
@ServerScope
|
||||
Server provideServer(PacketPool packetPool, EventBus eventBus) {
|
||||
return new NettyServer(packetPool, eventBus);
|
||||
Server provideServer(ObjectPool<NettyConnectionContext> poolNettyConnectionContext, PacketPool packetPool, EventBus eventBus) {
|
||||
return new NettyServer(poolNettyConnectionContext, packetPool, eventBus);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@@ -47,4 +49,10 @@ public class ProtocolModule {
|
||||
EventBus provideEventBus() {
|
||||
return new SimpleEventBus();
|
||||
}
|
||||
|
||||
@Provides
|
||||
@ServerScope
|
||||
ObjectPool<NettyConnectionContext> providePoolNettyConnectionContext() {
|
||||
return new GenericObjectPool<>(new NettyConnectionContextFactory());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import mc.protocol.io.NetByteBuf;
|
||||
import mc.protocol.packets.ClientSidePacket;
|
||||
import mc.protocol.packets.UnknownPacket;
|
||||
import mc.protocol.pool.PacketPool;
|
||||
import org.apache.commons.pool2.ObjectPool;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.List;
|
||||
@@ -24,19 +25,26 @@ import java.util.function.Consumer;
|
||||
public class ProtocolDecoder extends ByteToMessageDecoder {
|
||||
|
||||
private final boolean readUnknownPackets;
|
||||
private final ObjectPool<NettyConnectionContext> poolNettyConnectionContext;
|
||||
private final PacketPool poolPackets;
|
||||
private final Consumer<ConnectionContext> consumerNewConnection;
|
||||
private final Consumer<ConnectionContext> consumerDisconnect;
|
||||
|
||||
@Override
|
||||
public void channelActive(@Nonnull ChannelHandlerContext ctx) throws Exception {
|
||||
consumerNewConnection.accept(new NettyConnectionContext(ctx));
|
||||
NettyConnectionContext context = poolNettyConnectionContext.borrowObject().setCtx(ctx);
|
||||
consumerNewConnection.accept(context);
|
||||
|
||||
poolNettyConnectionContext.returnObject(context);
|
||||
super.channelActive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(@Nonnull ChannelHandlerContext ctx) throws Exception {
|
||||
consumerDisconnect.accept(new NettyConnectionContext(ctx));
|
||||
NettyConnectionContext context = poolNettyConnectionContext.borrowObject().setCtx(ctx);
|
||||
consumerDisconnect.accept(context);
|
||||
|
||||
poolNettyConnectionContext.returnObject(context);
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
package mc.protocol.packets;
|
||||
|
||||
import mc.protocol.io.NetByteBuf;
|
||||
import mc.protocol.pool.Passivable;
|
||||
|
||||
/**
|
||||
* Пакеты отправляемые клиентом.
|
||||
*/
|
||||
public interface ClientSidePacket extends Packet {
|
||||
public interface ClientSidePacket extends Packet, Passivable {
|
||||
|
||||
void readSelf(NetByteBuf netByteBuf);
|
||||
|
||||
void passivate();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package mc.protocol.pool;
|
||||
|
||||
import mc.protocol.NettyConnectionContext;
|
||||
import org.apache.commons.pool2.BasePooledObjectFactory;
|
||||
import org.apache.commons.pool2.PooledObject;
|
||||
import org.apache.commons.pool2.impl.DefaultPooledObject;
|
||||
|
||||
public class NettyConnectionContextFactory extends BasePooledObjectFactory<NettyConnectionContext> {
|
||||
|
||||
@Override
|
||||
public NettyConnectionContext create() throws Exception {
|
||||
return new NettyConnectionContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PooledObject<NettyConnectionContext> wrap(NettyConnectionContext context) {
|
||||
return new DefaultPooledObject<>(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void passivateObject(PooledObject<NettyConnectionContext> pooledObj) {
|
||||
pooledObj.getObject().passivate();
|
||||
}
|
||||
}
|
||||
6
protocol/src/main/java/mc/protocol/pool/Passivable.java
Normal file
6
protocol/src/main/java/mc/protocol/pool/Passivable.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package mc.protocol.pool;
|
||||
|
||||
public interface Passivable {
|
||||
|
||||
void passivate();
|
||||
}
|
||||
Reference in New Issue
Block a user