mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-18 01:46:44 +01:00
Initial commit
This commit is contained in:
89
src/main/java/emu/grasscutter/netty/MihoyoKcpChannel.java
Normal file
89
src/main/java/emu/grasscutter/netty/MihoyoKcpChannel.java
Normal file
@@ -0,0 +1,89 @@
|
||||
package emu.grasscutter.netty;
|
||||
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import io.jpower.kcp.netty.UkcpChannel;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufUtil;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
|
||||
public abstract class MihoyoKcpChannel extends ChannelInboundHandlerAdapter {
|
||||
private UkcpChannel kcpChannel;
|
||||
private ChannelHandlerContext ctx;
|
||||
private boolean isActive;
|
||||
|
||||
public UkcpChannel getChannel() {
|
||||
return kcpChannel;
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return this.isActive;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
this.kcpChannel = (UkcpChannel) ctx.channel();
|
||||
this.ctx = ctx;
|
||||
this.isActive = true;
|
||||
|
||||
this.onConnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
this.isActive = false;
|
||||
|
||||
this.onDisconnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
||||
ByteBuf data = (ByteBuf) msg;
|
||||
onMessage(ctx, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelReadComplete(ChannelHandlerContext ctx) {
|
||||
ctx.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||
cause.printStackTrace();
|
||||
close();
|
||||
}
|
||||
|
||||
protected void send(byte[] data) {
|
||||
if (!isActive()) {
|
||||
return;
|
||||
}
|
||||
ByteBuf packet = Unpooled.wrappedBuffer(data);
|
||||
kcpChannel.writeAndFlush(packet);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
if (getChannel() != null) {
|
||||
getChannel().close();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
protected void logPacket(ByteBuffer buf) {
|
||||
ByteBuf b = Unpooled.wrappedBuffer(buf.array());
|
||||
logPacket(b);
|
||||
}
|
||||
*/
|
||||
|
||||
protected void logPacket(ByteBuf buf) {
|
||||
Grasscutter.getLogger().info("Received: \n" + ByteBufUtil.prettyHexDump(buf));
|
||||
}
|
||||
|
||||
// Events
|
||||
|
||||
protected abstract void onConnect();
|
||||
|
||||
protected abstract void onDisconnect();
|
||||
|
||||
public abstract void onMessage(ChannelHandlerContext ctx, ByteBuf data);
|
||||
}
|
||||
Reference in New Issue
Block a user