Initial commit

This commit is contained in:
Melledy
2022-04-17 05:43:07 -07:00
commit 7925d1cda3
354 changed files with 20869 additions and 0 deletions

View 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);
}

View File

@@ -0,0 +1,85 @@
package emu.grasscutter.netty;
import java.net.SocketAddress;
import java.nio.channels.SelectableChannel;
import java.util.List;
import io.netty.channel.Channel;
import io.netty.channel.ChannelConfig;
import io.netty.channel.ChannelMetadata;
import io.netty.channel.ChannelOutboundBuffer;
import io.netty.channel.nio.AbstractNioMessageChannel;
public class MihoyoKcpHandshaker extends AbstractNioMessageChannel {
protected MihoyoKcpHandshaker(Channel parent, SelectableChannel ch, int readInterestOp) {
super(parent, ch, readInterestOp);
}
@Override
public ChannelConfig config() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isActive() {
// TODO Auto-generated method stub
return false;
}
@Override
public ChannelMetadata metadata() {
// TODO Auto-generated method stub
return null;
}
@Override
protected int doReadMessages(List<Object> buf) throws Exception {
// TODO Auto-generated method stub
return 0;
}
@Override
protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception {
// TODO Auto-generated method stub
return false;
}
@Override
protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
// TODO Auto-generated method stub
return false;
}
@Override
protected void doFinishConnect() throws Exception {
// TODO Auto-generated method stub
}
@Override
protected SocketAddress localAddress0() {
// TODO Auto-generated method stub
return null;
}
@Override
protected SocketAddress remoteAddress0() {
// TODO Auto-generated method stub
return null;
}
@Override
protected void doBind(SocketAddress localAddress) throws Exception {
// TODO Auto-generated method stub
}
@Override
protected void doDisconnect() throws Exception {
// TODO Auto-generated method stub
}
}

View File

@@ -0,0 +1,94 @@
package emu.grasscutter.netty;
import java.net.InetSocketAddress;
import emu.grasscutter.Grasscutter;
import io.jpower.kcp.netty.ChannelOptionHelper;
import io.jpower.kcp.netty.UkcpChannelOption;
import io.jpower.kcp.netty.UkcpServerChannel;
import io.netty.bootstrap.UkcpServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
@SuppressWarnings("rawtypes")
public class MihoyoKcpServer extends Thread {
private EventLoopGroup group;
private UkcpServerBootstrap bootstrap;
private ChannelInitializer serverInitializer;
private InetSocketAddress address;
public MihoyoKcpServer(InetSocketAddress address) {
this.address = address;
this.setName("Netty Server Thread");
}
public InetSocketAddress getAddress() {
return this.address;
}
public ChannelInitializer getServerInitializer() {
return serverInitializer;
}
public void setServerInitializer(ChannelInitializer serverInitializer) {
this.serverInitializer = serverInitializer;
}
@Override
public void run() {
if (getServerInitializer() == null) {
this.setServerInitializer(new MihoyoKcpServerInitializer());
}
try {
group = new NioEventLoopGroup();
bootstrap = new UkcpServerBootstrap();
bootstrap.group(group)
.channel(UkcpServerChannel.class)
.childHandler(this.getServerInitializer());
ChannelOptionHelper
.nodelay(bootstrap, true, 20, 2, true)
.childOption(UkcpChannelOption.UKCP_MTU, 1200);
// Start handler
this.onStart();
// Start the server.
ChannelFuture f = bootstrap.bind(getAddress()).sync();
// Start finish handler
this.onStartFinish();
// Wait until the server socket is closed.
f.channel().closeFuture().sync();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// Close
finish();
}
}
public void onStart() {
}
public void onStartFinish() {
}
private void finish() {
try {
group.shutdownGracefully();
} catch (Exception e) {
}
Grasscutter.getLogger().info("Game Server closed");
}
}

View File

@@ -0,0 +1,15 @@
package emu.grasscutter.netty;
import io.jpower.kcp.netty.UkcpChannel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
@SuppressWarnings("unused")
public class MihoyoKcpServerInitializer extends ChannelInitializer<UkcpChannel> {
@Override
protected void initChannel(UkcpChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
}
}