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