рефакторинг EventBus
This commit is contained in:
@@ -3,14 +3,12 @@ package mc.protocol;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import mc.protocol.api.ConnectionContext;
|
||||
import mc.protocol.packets.ClientSidePacket;
|
||||
import mc.protocol.packets.ServerSidePacket;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class NettyConnectionContext<P extends ClientSidePacket> implements ConnectionContext<P> {
|
||||
public class NettyConnectionContext implements ConnectionContext {
|
||||
|
||||
private final ChannelHandlerContext ctx;
|
||||
private final P packet;
|
||||
|
||||
@Override
|
||||
public State getState() {
|
||||
@@ -22,11 +20,6 @@ public class NettyConnectionContext<P extends ClientSidePacket> implements Conne
|
||||
ctx.channel().attr(NetworkAttributes.STATE).set(state);
|
||||
}
|
||||
|
||||
@Override
|
||||
public P clientPacket() {
|
||||
return packet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(ServerSidePacket packet) {
|
||||
ctx.write(packet);
|
||||
|
||||
@@ -31,8 +31,8 @@ public class NettyServer implements Server {
|
||||
|
||||
private final PacketPool packetPool;
|
||||
private final EventBus eventBus;
|
||||
private Consumer<ConnectionContext<?>> consumerNewConnection;
|
||||
private Consumer<ConnectionContext<?>> consumerDisconnect;
|
||||
private Consumer<ConnectionContext> consumerNewConnection;
|
||||
private Consumer<ConnectionContext> consumerDisconnect;
|
||||
|
||||
@Override
|
||||
public void bind(String host, int port) {
|
||||
@@ -48,12 +48,12 @@ public class NettyServer implements Server {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNewConnect(Consumer<ConnectionContext<?>> consumer) {
|
||||
public void onNewConnect(Consumer<ConnectionContext> consumer) {
|
||||
this.consumerNewConnection = consumer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisonnect(Consumer<ConnectionContext<?>> consumer) {
|
||||
public void onDisonnect(Consumer<ConnectionContext> consumer) {
|
||||
this.consumerDisconnect = consumer;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ public class PacketInboundHandler extends SimpleChannelInboundHandler<ClientSide
|
||||
@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));
|
||||
eventBus.emit(state, new NettyConnectionContext(ctx), packet);
|
||||
|
||||
poolPackets.returnObject(packet);
|
||||
}
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
package mc.protocol.api;
|
||||
|
||||
import mc.protocol.State;
|
||||
import mc.protocol.packets.ClientSidePacket;
|
||||
import mc.protocol.packets.ServerSidePacket;
|
||||
|
||||
public interface ConnectionContext<P extends ClientSidePacket> {
|
||||
public interface ConnectionContext {
|
||||
|
||||
State getState();
|
||||
void setState(State state);
|
||||
|
||||
P clientPacket();
|
||||
|
||||
void send(ServerSidePacket packet);
|
||||
void sendNow(ServerSidePacket packet);
|
||||
void flushSending();
|
||||
|
||||
@@ -10,8 +10,8 @@ public interface Server {
|
||||
|
||||
void bind(String host, int port);
|
||||
|
||||
void onNewConnect(Consumer<ConnectionContext<?>> consumer);
|
||||
void onDisonnect(Consumer<ConnectionContext<?>> consumer);
|
||||
void onNewConnect(Consumer<ConnectionContext> consumer);
|
||||
void onDisonnect(Consumer<ConnectionContext> consumer);
|
||||
|
||||
<P extends ClientSidePacket> void listenPacket(State state, Class<P> packetClass, EventBus.EventHandler<P> eventHandler);
|
||||
}
|
||||
|
||||
@@ -25,18 +25,18 @@ public class ProtocolDecoder extends ByteToMessageDecoder {
|
||||
|
||||
private final boolean readUnknownPackets;
|
||||
private final PacketPool poolPackets;
|
||||
private final Consumer<ConnectionContext<?>> consumerNewConnection;
|
||||
private final Consumer<ConnectionContext<?>> consumerDisconnect;
|
||||
private final Consumer<ConnectionContext> consumerNewConnection;
|
||||
private final Consumer<ConnectionContext> consumerDisconnect;
|
||||
|
||||
@Override
|
||||
public void channelActive(@Nonnull ChannelHandlerContext ctx) throws Exception {
|
||||
consumerNewConnection.accept(new NettyConnectionContext<>(ctx, null));
|
||||
consumerNewConnection.accept(new NettyConnectionContext(ctx));
|
||||
super.channelActive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(@Nonnull ChannelHandlerContext ctx) throws Exception {
|
||||
consumerDisconnect.accept(new NettyConnectionContext<>(ctx, null));
|
||||
consumerDisconnect.accept(new NettyConnectionContext(ctx));
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,10 +8,10 @@ public interface EventBus {
|
||||
|
||||
<P extends ClientSidePacket> void subscribe(State state, Class<P> packetClass, EventHandler<P> eventHandler);
|
||||
|
||||
<P extends ClientSidePacket> void emit(State state, ConnectionContext<P> channelContext);
|
||||
<P extends ClientSidePacket> void emit(State state, ConnectionContext channelContext, P packet);
|
||||
|
||||
@FunctionalInterface
|
||||
interface EventHandler<P extends ClientSidePacket> {
|
||||
void handle(ConnectionContext<P> channelContext);
|
||||
void handle(ConnectionContext channelContext, P packet);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,11 +15,11 @@ public class SimpleEventBus implements EventBus {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <P extends ClientSidePacket> void emit(State state, ConnectionContext<P> channelContext) {
|
||||
EventHandler eventHandler = table.getColumnAndRow(state, channelContext.clientPacket().getClass());
|
||||
public <P extends ClientSidePacket> void emit(State state, ConnectionContext channelContext, P packet) {
|
||||
EventHandler eventHandler = table.getColumnAndRow(state, packet.getClass());
|
||||
|
||||
if (eventHandler != null) {
|
||||
eventHandler.handle(channelContext);
|
||||
eventHandler.handle(channelContext, packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,20 +34,21 @@ public class PacketHandler {
|
||||
private final Random random = new Random(System.currentTimeMillis());
|
||||
private final Config config;
|
||||
|
||||
public void onHandshake(ConnectionContext<HandshakePacket> context) {
|
||||
context.setState(context.clientPacket().getNextState());
|
||||
public void onHandshake(ConnectionContext context, HandshakePacket packet) {
|
||||
context.setState(packet.getNextState());
|
||||
}
|
||||
|
||||
public void onKeepAlive(ConnectionContext<PingPacket> context) {
|
||||
context.sendNow(context.clientPacket());
|
||||
public void onKeepAlive(ConnectionContext context, PingPacket packet) {
|
||||
context.sendNow(packet);
|
||||
context.disconnect();
|
||||
}
|
||||
|
||||
public void onKeepAlivePlay(ConnectionContext<PingPacket> context) {
|
||||
context.sendNow(context.clientPacket());
|
||||
public void onKeepAlivePlay(ConnectionContext context, PingPacket packet) {
|
||||
context.sendNow(packet);
|
||||
}
|
||||
|
||||
public void onServerStatus(ConnectionContext<StatusServerRequestPacket> context) {
|
||||
@SuppressWarnings("unused")
|
||||
public void onServerStatus(ConnectionContext context, StatusServerRequestPacket packet) {
|
||||
ServerInfo serverInfo = new ServerInfo();
|
||||
serverInfo.version().name(ProtocolConstant.PROTOCOL_NAME);
|
||||
serverInfo.version().protocol(ProtocolConstant.PROTOCOL_NUMBER);
|
||||
@@ -66,9 +67,7 @@ public class PacketHandler {
|
||||
context.sendNow(response);
|
||||
}
|
||||
|
||||
public void onLoginStart(ConnectionContext<LoginStartPacket> context) {
|
||||
LoginStartPacket loginStartPacket = context.clientPacket();
|
||||
|
||||
public void onLoginStart(ConnectionContext context, LoginStartPacket loginStartPacket) {
|
||||
var loginSuccessPacket = new LoginSuccessPacket();
|
||||
loginSuccessPacket.setUuid(UUID.randomUUID());
|
||||
loginSuccessPacket.setName(loginStartPacket.getName());
|
||||
|
||||
Reference in New Issue
Block a user