mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-06-26 18:14:50 +08:00
Compare commits
No commits in common. "b3f11032927c288e83d728248dbc48ac4e7200e0" and "a329b3bdac7f903a10f976972430f5ee907e3ea5" have entirely different histories.
b3f1103292
...
a329b3bdac
@ -28,7 +28,7 @@ EN | [中文](README_zh-CN.md)
|
|||||||
|
|
||||||
**Note:** If you just want to **run it**, then **jre** only is fine.
|
**Note:** If you just want to **run it**, then **jre** only is fine.
|
||||||
|
|
||||||
* [MongoDB](https://www.mongodb.com/try/download/community) (recommended 4.0+)
|
* MongoDB (recommended 4.0+)
|
||||||
|
|
||||||
* Proxy daemon: mitmproxy (mitmdump, recommended), Fiddler Classic, etc.
|
* Proxy daemon: mitmproxy (mitmdump, recommended), Fiddler Classic, etc.
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
|
|
||||||
**注:** 如果您仅仅想要简单地**运行服务端**, 那么使用 **jre** 便足够了
|
**注:** 如果您仅仅想要简单地**运行服务端**, 那么使用 **jre** 便足够了
|
||||||
|
|
||||||
* [MongoDB](https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-5.0.9-signed.msi) (推荐 4.0+)
|
* MongoDB (推荐 4.0+)
|
||||||
|
|
||||||
* Proxy daemon: mitmproxy (推荐使用mitmdump), Fiddler Classic, 等
|
* Proxy daemon: mitmproxy (推荐使用mitmdump), Fiddler Classic, 等
|
||||||
|
|
||||||
|
BIN
lib/kcp-netty-1.5.0.jar
Normal file
BIN
lib/kcp-netty-1.5.0.jar
Normal file
Binary file not shown.
BIN
lib/kcp.jar
BIN
lib/kcp.jar
Binary file not shown.
89
src/main/java/emu/grasscutter/netty/KcpChannel.java
Normal file
89
src/main/java/emu/grasscutter/netty/KcpChannel.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 KcpChannel 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);
|
||||||
|
}
|
85
src/main/java/emu/grasscutter/netty/KcpHandshaker.java
Normal file
85
src/main/java/emu/grasscutter/netty/KcpHandshaker.java
Normal 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 KcpHandshaker extends AbstractNioMessageChannel {
|
||||||
|
|
||||||
|
protected KcpHandshaker(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
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
93
src/main/java/emu/grasscutter/netty/KcpServer.java
Normal file
93
src/main/java/emu/grasscutter/netty/KcpServer.java
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
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 KcpServer extends Thread {
|
||||||
|
private EventLoopGroup group;
|
||||||
|
private UkcpServerBootstrap bootstrap;
|
||||||
|
|
||||||
|
private ChannelInitializer serverInitializer;
|
||||||
|
private InetSocketAddress address;
|
||||||
|
|
||||||
|
public KcpServer(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 KcpServerInitializer());
|
||||||
|
}
|
||||||
|
|
||||||
|
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 exception) {
|
||||||
|
Grasscutter.getLogger().error("Unable to start game server.", exception);
|
||||||
|
} finally {
|
||||||
|
// Close
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onStart() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onStartFinish() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void finish() {
|
||||||
|
try {
|
||||||
|
group.shutdownGracefully();
|
||||||
|
} catch (Exception e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
Grasscutter.getLogger().info("Game Server closed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -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 KcpServerInitializer extends ChannelInitializer<UkcpChannel> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void initChannel(UkcpChannel ch) throws Exception {
|
||||||
|
ChannelPipeline pipeline = ch.pipeline();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -21,13 +21,13 @@ import emu.grasscutter.game.tower.TowerScheduleManager;
|
|||||||
import emu.grasscutter.game.world.World;
|
import emu.grasscutter.game.world.World;
|
||||||
import emu.grasscutter.net.packet.PacketHandler;
|
import emu.grasscutter.net.packet.PacketHandler;
|
||||||
import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail;
|
import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail;
|
||||||
|
import emu.grasscutter.netty.KcpServer;
|
||||||
import emu.grasscutter.server.event.types.ServerEvent;
|
import emu.grasscutter.server.event.types.ServerEvent;
|
||||||
import emu.grasscutter.server.event.game.ServerTickEvent;
|
import emu.grasscutter.server.event.game.ServerTickEvent;
|
||||||
import emu.grasscutter.server.event.internal.ServerStartEvent;
|
import emu.grasscutter.server.event.internal.ServerStartEvent;
|
||||||
import emu.grasscutter.server.event.internal.ServerStopEvent;
|
import emu.grasscutter.server.event.internal.ServerStopEvent;
|
||||||
import emu.grasscutter.task.TaskMap;
|
import emu.grasscutter.task.TaskMap;
|
||||||
import kcp.highway.ChannelConfig;
|
import emu.grasscutter.BuildConfig;
|
||||||
import kcp.highway.KcpServer;
|
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
@ -60,7 +60,7 @@ public final class GameServer extends KcpServer {
|
|||||||
private final TowerScheduleManager towerScheduleManager;
|
private final TowerScheduleManager towerScheduleManager;
|
||||||
|
|
||||||
private static InetSocketAddress getAdapterInetSocketAddress(){
|
private static InetSocketAddress getAdapterInetSocketAddress(){
|
||||||
InetSocketAddress inetSocketAddress;
|
InetSocketAddress inetSocketAddress = null;
|
||||||
if(GAME_INFO.bindAddress.equals("")){
|
if(GAME_INFO.bindAddress.equals("")){
|
||||||
inetSocketAddress=new InetSocketAddress(GAME_INFO.bindPort);
|
inetSocketAddress=new InetSocketAddress(GAME_INFO.bindPort);
|
||||||
}else{
|
}else{
|
||||||
@ -75,17 +75,9 @@ public final class GameServer extends KcpServer {
|
|||||||
this(getAdapterInetSocketAddress());
|
this(getAdapterInetSocketAddress());
|
||||||
}
|
}
|
||||||
public GameServer(InetSocketAddress address) {
|
public GameServer(InetSocketAddress address) {
|
||||||
ChannelConfig channelConfig = new ChannelConfig();
|
super(address);
|
||||||
channelConfig.nodelay(true,40,2,true);
|
|
||||||
channelConfig.setMtu(1400);
|
|
||||||
channelConfig.setSndwnd(256);
|
|
||||||
channelConfig.setRcvwnd(256);
|
|
||||||
channelConfig.setTimeoutMillis(30*1000);//30s
|
|
||||||
channelConfig.setUseConvChannel(true);
|
|
||||||
channelConfig.setAckNoDelay(false);
|
|
||||||
|
|
||||||
this.init(GameSessionManager.getListener(),channelConfig,address);
|
|
||||||
|
|
||||||
|
this.setServerInitializer(new GameServerInitializer(this));
|
||||||
this.address = address;
|
this.address = address;
|
||||||
this.packetHandler = new GameServerPacketHandler(PacketHandler.class);
|
this.packetHandler = new GameServerPacketHandler(PacketHandler.class);
|
||||||
this.questHandler = new ServerQuestHandler();
|
this.questHandler = new ServerQuestHandler();
|
||||||
@ -104,7 +96,6 @@ public final class GameServer extends KcpServer {
|
|||||||
this.expeditionManager = new ExpeditionManager(this);
|
this.expeditionManager = new ExpeditionManager(this);
|
||||||
this.combineManger = new CombineManger(this);
|
this.combineManger = new CombineManger(this);
|
||||||
this.towerScheduleManager = new TowerScheduleManager(this);
|
this.towerScheduleManager = new TowerScheduleManager(this);
|
||||||
|
|
||||||
// Hook into shutdown event.
|
// Hook into shutdown event.
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(this::onServerShutdown));
|
Runtime.getRuntime().addShutdownHook(new Thread(this::onServerShutdown));
|
||||||
}
|
}
|
||||||
@ -173,7 +164,6 @@ public final class GameServer extends KcpServer {
|
|||||||
return towerScheduleManager;
|
return towerScheduleManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public TaskMap getTaskMap() {
|
public TaskMap getTaskMap() {
|
||||||
return this.taskMap;
|
return this.taskMap;
|
||||||
}
|
}
|
||||||
@ -231,7 +221,7 @@ public final class GameServer extends KcpServer {
|
|||||||
return DatabaseHelper.getAccountByName(username);
|
return DatabaseHelper.getAccountByName(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void onTick(){
|
public void onTick() throws Exception {
|
||||||
Iterator<World> it = this.getWorlds().iterator();
|
Iterator<World> it = this.getWorlds().iterator();
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
World world = it.next();
|
World world = it.next();
|
||||||
@ -259,7 +249,8 @@ public final class GameServer extends KcpServer {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
@Override
|
||||||
|
public synchronized void start() {
|
||||||
// Schedule game loop.
|
// Schedule game loop.
|
||||||
Timer gameLoop = new Timer();
|
Timer gameLoop = new Timer();
|
||||||
gameLoop.scheduleAtFixedRate(new TimerTask() {
|
gameLoop.scheduleAtFixedRate(new TimerTask() {
|
||||||
@ -272,10 +263,15 @@ public final class GameServer extends KcpServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, new Date(), 1000L);
|
}, new Date(), 1000L);
|
||||||
|
|
||||||
|
super.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStartFinish() {
|
||||||
Grasscutter.getLogger().info(translate("messages.status.free_software"));
|
Grasscutter.getLogger().info(translate("messages.status.free_software"));
|
||||||
Grasscutter.getLogger().info(translate("messages.game.port_bind", Integer.toString(address.getPort())));
|
Grasscutter.getLogger().info(translate("messages.game.port_bind", Integer.toString(address.getPort())));
|
||||||
ServerStartEvent event = new ServerStartEvent(ServerEvent.Type.GAME, OffsetDateTime.now());
|
ServerStartEvent event = new ServerStartEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); event.call();
|
||||||
event.call();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onServerShutdown() {
|
public void onServerShutdown() {
|
||||||
|
@ -0,0 +1,22 @@
|
|||||||
|
package emu.grasscutter.server.game;
|
||||||
|
|
||||||
|
import emu.grasscutter.netty.KcpServerInitializer;
|
||||||
|
import io.jpower.kcp.netty.UkcpChannel;
|
||||||
|
import io.netty.channel.ChannelPipeline;
|
||||||
|
|
||||||
|
public class GameServerInitializer extends KcpServerInitializer {
|
||||||
|
private GameServer server;
|
||||||
|
|
||||||
|
public GameServerInitializer(GameServer server) {
|
||||||
|
this.server = server;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void initChannel(UkcpChannel ch) throws Exception {
|
||||||
|
ChannelPipeline pipeline=null;
|
||||||
|
if(ch!=null){
|
||||||
|
pipeline = ch.pipeline();
|
||||||
|
}
|
||||||
|
new GameSession(server,pipeline);
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,9 @@ package emu.grasscutter.server.game;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import emu.grasscutter.Grasscutter;
|
import emu.grasscutter.Grasscutter;
|
||||||
@ -11,18 +14,22 @@ import emu.grasscutter.game.player.Player;
|
|||||||
import emu.grasscutter.net.packet.BasePacket;
|
import emu.grasscutter.net.packet.BasePacket;
|
||||||
import emu.grasscutter.net.packet.PacketOpcodes;
|
import emu.grasscutter.net.packet.PacketOpcodes;
|
||||||
import emu.grasscutter.net.packet.PacketOpcodesUtil;
|
import emu.grasscutter.net.packet.PacketOpcodesUtil;
|
||||||
|
import emu.grasscutter.netty.KcpChannel;
|
||||||
import emu.grasscutter.server.event.game.SendPacketEvent;
|
import emu.grasscutter.server.event.game.SendPacketEvent;
|
||||||
import emu.grasscutter.utils.Crypto;
|
import emu.grasscutter.utils.Crypto;
|
||||||
import emu.grasscutter.utils.FileUtils;
|
import emu.grasscutter.utils.FileUtils;
|
||||||
import emu.grasscutter.utils.Utils;
|
import emu.grasscutter.utils.Utils;
|
||||||
|
import io.jpower.kcp.netty.UkcpChannel;
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
import static emu.grasscutter.Configuration.*;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import static emu.grasscutter.utils.Language.translate;
|
import io.netty.channel.ChannelPipeline;
|
||||||
|
|
||||||
public class GameSession implements GameSessionManager.KcpChannel {
|
import static emu.grasscutter.utils.Language.translate;
|
||||||
|
import static emu.grasscutter.Configuration.*;
|
||||||
|
|
||||||
|
public class GameSession extends KcpChannel {
|
||||||
private final GameServer server;
|
private final GameServer server;
|
||||||
private GameSessionManager.KcpTunnel tunnel;
|
|
||||||
|
|
||||||
private Account account;
|
private Account account;
|
||||||
private Player player;
|
private Player player;
|
||||||
@ -34,10 +41,29 @@ public class GameSession implements GameSessionManager.KcpChannel {
|
|||||||
private long lastPingTime;
|
private long lastPingTime;
|
||||||
private int lastClientSeq = 10;
|
private int lastClientSeq = 10;
|
||||||
|
|
||||||
|
private final ChannelPipeline pipeline;
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
setState(SessionState.INACTIVE);
|
||||||
|
//send disconnection pack in case of reconnection
|
||||||
|
try {
|
||||||
|
send(new BasePacket(PacketOpcodes.ServerDisconnectClientNotify));
|
||||||
|
}catch (Throwable ignore){
|
||||||
|
|
||||||
|
}
|
||||||
|
super.close();
|
||||||
|
}
|
||||||
public GameSession(GameServer server) {
|
public GameSession(GameServer server) {
|
||||||
|
this(server,null);
|
||||||
|
}
|
||||||
|
public GameSession(GameServer server, ChannelPipeline pipeline) {
|
||||||
this.server = server;
|
this.server = server;
|
||||||
this.state = SessionState.WAITING_FOR_TOKEN;
|
this.state = SessionState.WAITING_FOR_TOKEN;
|
||||||
this.lastPingTime = System.currentTimeMillis();
|
this.lastPingTime = System.currentTimeMillis();
|
||||||
|
this.pipeline = pipeline;
|
||||||
|
if(pipeline!=null) {
|
||||||
|
pipeline.addLast(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public GameServer getServer() {
|
public GameServer getServer() {
|
||||||
@ -45,11 +71,10 @@ public class GameSession implements GameSessionManager.KcpChannel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public InetSocketAddress getAddress() {
|
public InetSocketAddress getAddress() {
|
||||||
try{
|
if (this.getChannel() == null) {
|
||||||
return tunnel.getAddress();
|
|
||||||
}catch (Throwable ignore){
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
return this.getChannel().remoteAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean useSecretKey() {
|
public boolean useSecretKey() {
|
||||||
@ -111,6 +136,36 @@ public class GameSession implements GameSessionManager.KcpChannel {
|
|||||||
return ++lastClientSeq;
|
return ++lastClientSeq;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onConnect() {
|
||||||
|
Grasscutter.getLogger().info(translate("messages.game.connect", this.getAddress().getHostString().toLowerCase()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected synchronized void onDisconnect() { // Synchronize so we don't add character at the same time.
|
||||||
|
Grasscutter.getLogger().info(translate("messages.game.disconnect", this.getAddress().getHostString().toLowerCase()));
|
||||||
|
|
||||||
|
// Set state so no more packets can be handled
|
||||||
|
this.setState(SessionState.INACTIVE);
|
||||||
|
|
||||||
|
// Save after disconnecting
|
||||||
|
if (this.isLoggedIn()) {
|
||||||
|
Player player = getPlayer();
|
||||||
|
// Call logout event.
|
||||||
|
player.onLogout();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
pipeline.remove(this);
|
||||||
|
} catch (Throwable ignore) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void logPacket(ByteBuffer buf) {
|
||||||
|
ByteBuf b = Unpooled.wrappedBuffer(buf.array());
|
||||||
|
logPacket(b);
|
||||||
|
}
|
||||||
|
|
||||||
public void replayPacket(int opcode, String name) {
|
public void replayPacket(int opcode, String name) {
|
||||||
String filePath = PACKET(name);
|
String filePath = PACKET(name);
|
||||||
File p = new File(filePath);
|
File p = new File(filePath);
|
||||||
@ -145,16 +200,13 @@ public class GameSession implements GameSessionManager.KcpChannel {
|
|||||||
|
|
||||||
// Log
|
// Log
|
||||||
if (SERVER.debugLevel == ServerDebugMode.ALL) {
|
if (SERVER.debugLevel == ServerDebugMode.ALL) {
|
||||||
if (!loopPacket.contains(packet.getOpcode())) {
|
logPacket(packet);
|
||||||
Grasscutter.getLogger().info("SEND: " + PacketOpcodesUtil.getOpcodeName(packet.getOpcode()) + " (" + packet.getOpcode() + ")");
|
|
||||||
System.out.println(Utils.bytesToHex(packet.getData()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invoke event.
|
// Invoke event.
|
||||||
SendPacketEvent event = new SendPacketEvent(this, packet); event.call();
|
SendPacketEvent event = new SendPacketEvent(this, packet); event.call();
|
||||||
if(!event.isCanceled()) { // If event is not cancelled, continue.
|
if(!event.isCanceled()) // If event is not cancelled, continue.
|
||||||
tunnel.writeData(event.getPacket().build());
|
this.send(event.getPacket().build());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Set<Integer> loopPacket = Set.of(
|
private static final Set<Integer> loopPacket = Set.of(
|
||||||
@ -165,104 +217,78 @@ public class GameSession implements GameSessionManager.KcpChannel {
|
|||||||
PacketOpcodes.QueryPathReq
|
PacketOpcodes.QueryPathReq
|
||||||
);
|
);
|
||||||
|
|
||||||
@Override
|
private void logPacket(BasePacket packet) {
|
||||||
public void onConnected(GameSessionManager.KcpTunnel tunnel) {
|
if (!loopPacket.contains(packet.getOpcode())) {
|
||||||
this.tunnel = tunnel;
|
Grasscutter.getLogger().info("SEND: " + PacketOpcodesUtil.getOpcodeName(packet.getOpcode()) + " (" + packet.getOpcode() + ")");
|
||||||
Grasscutter.getLogger().info(translate("messages.game.connect", this.getAddress().toString()));
|
System.out.println(Utils.bytesToHex(packet.getData()));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleReceive(byte[] bytes) {
|
public void onMessage(ChannelHandlerContext ctx, ByteBuf data) {
|
||||||
// Decrypt and turn back into a packet
|
// Decrypt and turn back into a packet
|
||||||
Crypto.xor(bytes, useSecretKey() ? Crypto.ENCRYPT_KEY : Crypto.DISPATCH_KEY);
|
byte[] byteData = Utils.byteBufToArray(data);
|
||||||
ByteBuf packet = Unpooled.wrappedBuffer(bytes);
|
Crypto.xor(byteData, useSecretKey() ? Crypto.ENCRYPT_KEY : Crypto.DISPATCH_KEY);
|
||||||
|
ByteBuf packet = Unpooled.wrappedBuffer(byteData);
|
||||||
|
|
||||||
// Log
|
// Log
|
||||||
//logPacket(packet);
|
//logPacket(packet);
|
||||||
|
|
||||||
// Handle
|
// Handle
|
||||||
try {
|
try {
|
||||||
boolean allDebug = SERVER.debugLevel == ServerDebugMode.ALL;
|
|
||||||
while (packet.readableBytes() > 0) {
|
while (packet.readableBytes() > 0) {
|
||||||
// Length
|
// Length
|
||||||
if (packet.readableBytes() < 12) {
|
if (packet.readableBytes() < 12) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Packet sanity check
|
// Packet sanity check
|
||||||
int const1 = packet.readShort();
|
int const1 = packet.readShort();
|
||||||
if (const1 != 17767) {
|
if (const1 != 17767) {
|
||||||
if(allDebug){
|
|
||||||
Grasscutter.getLogger().error("Bad Data Package Received: got {} ,expect 17767",const1);
|
|
||||||
}
|
|
||||||
return; // Bad packet
|
return; // Bad packet
|
||||||
}
|
}
|
||||||
|
|
||||||
// Data
|
// Data
|
||||||
int opcode = packet.readShort();
|
int opcode = packet.readShort();
|
||||||
int headerLength = packet.readShort();
|
int headerLength = packet.readShort();
|
||||||
int payloadLength = packet.readInt();
|
int payloadLength = packet.readInt();
|
||||||
|
|
||||||
byte[] header = new byte[headerLength];
|
byte[] header = new byte[headerLength];
|
||||||
byte[] payload = new byte[payloadLength];
|
byte[] payload = new byte[payloadLength];
|
||||||
|
|
||||||
packet.readBytes(header);
|
packet.readBytes(header);
|
||||||
packet.readBytes(payload);
|
packet.readBytes(payload);
|
||||||
|
|
||||||
// Sanity check #2
|
// Sanity check #2
|
||||||
int const2 = packet.readShort();
|
int const2 = packet.readShort();
|
||||||
if (const2 != -30293) {
|
if (const2 != -30293) {
|
||||||
if(allDebug){
|
|
||||||
Grasscutter.getLogger().error("Bad Data Package Received: got {} ,expect -30293",const2);
|
|
||||||
}
|
|
||||||
return; // Bad packet
|
return; // Bad packet
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log packet
|
// Log packet
|
||||||
if (allDebug) {
|
if (SERVER.debugLevel == ServerDebugMode.ALL) {
|
||||||
if (!loopPacket.contains(opcode)) {
|
if (!loopPacket.contains(opcode)) {
|
||||||
Grasscutter.getLogger().info("RECV: " + PacketOpcodesUtil.getOpcodeName(opcode) + " (" + opcode + ")");
|
Grasscutter.getLogger().info("RECV: " + PacketOpcodesUtil.getOpcodeName(opcode) + " (" + opcode + ")");
|
||||||
System.out.println(Utils.bytesToHex(payload));
|
System.out.println(Utils.bytesToHex(payload));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle
|
// Handle
|
||||||
getServer().getPacketHandler().handle(this, opcode, header, payload);
|
getServer().getPacketHandler().handle(this, opcode, header, payload);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
//byteBuf.release(); //Needn't
|
data.release();
|
||||||
packet.release();
|
packet.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleClose() {
|
|
||||||
setState(SessionState.INACTIVE);
|
|
||||||
//send disconnection pack in case of reconnection
|
|
||||||
Grasscutter.getLogger().info(translate("messages.game.disconnect", this.getAddress().toString()));
|
|
||||||
// Save after disconnecting
|
|
||||||
if (this.isLoggedIn()) {
|
|
||||||
Player player = getPlayer();
|
|
||||||
// Call logout event.
|
|
||||||
player.onLogout();
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
send(new BasePacket(PacketOpcodes.ServerDisconnectClientNotify));
|
|
||||||
}catch (Throwable ignore){
|
|
||||||
Grasscutter.getLogger().warn("closing {} error",getAddress().getAddress().getHostAddress());
|
|
||||||
}
|
|
||||||
tunnel = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void close() {
|
|
||||||
tunnel.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isActive() {
|
|
||||||
return getState() == SessionState.ACTIVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum SessionState {
|
public enum SessionState {
|
||||||
INACTIVE,
|
INACTIVE,
|
||||||
WAITING_FOR_TOKEN,
|
WAITING_FOR_TOKEN,
|
||||||
WAITING_FOR_LOGIN,
|
WAITING_FOR_LOGIN,
|
||||||
PICKING_CHARACTER,
|
PICKING_CHARACTER,
|
||||||
ACTIVE
|
ACTIVE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,110 +0,0 @@
|
|||||||
package emu.grasscutter.server.game;
|
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
import emu.grasscutter.Grasscutter;
|
|
||||||
import emu.grasscutter.utils.Crypto;
|
|
||||||
import emu.grasscutter.utils.Utils;
|
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
import io.netty.buffer.Unpooled;
|
|
||||||
import io.netty.channel.DefaultEventLoop;
|
|
||||||
import kcp.highway.KcpListener;
|
|
||||||
import kcp.highway.Ukcp;
|
|
||||||
|
|
||||||
public class GameSessionManager {
|
|
||||||
private static final DefaultEventLoop logicThread = new DefaultEventLoop();
|
|
||||||
private static final ConcurrentHashMap<Ukcp,GameSession> sessions = new ConcurrentHashMap<>();
|
|
||||||
private static final KcpListener listener = new KcpListener(){
|
|
||||||
@Override
|
|
||||||
public void onConnected(Ukcp ukcp) {
|
|
||||||
int times = 0;
|
|
||||||
GameServer server = Grasscutter.getGameServer();
|
|
||||||
while (server==null){//Waiting server to establish
|
|
||||||
try {
|
|
||||||
Thread.sleep(1000);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
ukcp.close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(times++>5){
|
|
||||||
Grasscutter.getLogger().error("Service is not available!");
|
|
||||||
ukcp.close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
server = Grasscutter.getGameServer();
|
|
||||||
}
|
|
||||||
GameSession conversation = new GameSession(server);
|
|
||||||
conversation.onConnected(new KcpTunnel(){
|
|
||||||
@Override
|
|
||||||
public InetSocketAddress getAddress() {
|
|
||||||
return ukcp.user().getRemoteAddress();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void writeData(byte[] bytes) {
|
|
||||||
ByteBuf buf = Unpooled.wrappedBuffer(bytes);
|
|
||||||
ukcp.write(buf);
|
|
||||||
buf.release();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close() {
|
|
||||||
ukcp.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getSrtt() {
|
|
||||||
return ukcp.srtt();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
sessions.put(ukcp,conversation);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleReceive(ByteBuf buf, Ukcp kcp) {
|
|
||||||
byte[] byteData = Utils.byteBufToArray(buf);
|
|
||||||
logicThread.execute(() -> {
|
|
||||||
try {
|
|
||||||
GameSession conversation = sessions.get(kcp);
|
|
||||||
if(conversation!=null) {
|
|
||||||
conversation.handleReceive(byteData);
|
|
||||||
}
|
|
||||||
}catch (Exception e){
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleException(Throwable ex, Ukcp ukcp) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleClose(Ukcp ukcp) {
|
|
||||||
GameSession conversation = sessions.get(ukcp);
|
|
||||||
if(conversation!=null) {
|
|
||||||
conversation.handleClose();
|
|
||||||
sessions.remove(ukcp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static KcpListener getListener() {
|
|
||||||
return listener;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface KcpTunnel{
|
|
||||||
InetSocketAddress getAddress();
|
|
||||||
void writeData(byte[] bytes);
|
|
||||||
void close();
|
|
||||||
int getSrtt();
|
|
||||||
}
|
|
||||||
interface KcpChannel{
|
|
||||||
void onConnected(KcpTunnel tunnel);
|
|
||||||
void handleClose();
|
|
||||||
void handleReceive(byte[] bytes);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user