Packet sending during Netty migration

This commit is contained in:
P0nk
2021-06-25 14:43:05 +02:00
parent 80cacd609a
commit f6aca2018e
3 changed files with 35 additions and 4 deletions

View File

@@ -30,7 +30,9 @@ import io.netty.handler.timeout.IdleStateEvent;
import net.MaplePacketHandler;
import net.PacketProcessor;
import net.netty.InvalidPacketHeaderException;
import net.packet.ByteBufOutPacket;
import net.packet.InPacket;
import net.packet.OutPacket;
import net.server.Server;
import net.server.audit.locks.MonitoredLockType;
import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
@@ -1160,7 +1162,7 @@ public class MapleClient extends ChannelInboundHandlerAdapter {
try {
if (lastPong < pingedAt) {
if (ioChannel.isActive()) {
log.info("Disconnected {} due to being idle. Idle state: {}", remoteAddress, event.state());
log.info("Disconnected {} due to being idle. Cause: {}", remoteAddress, event.state());
updateLoginState(MapleClient.LOGIN_NOTLOGGEDIN);
disconnectSession();
}
@@ -1462,10 +1464,34 @@ public class MapleClient extends ChannelInboundHandlerAdapter {
}
}
@Deprecated(forRemoval = true, since = "Netty migration")
public void announce(final byte[] packet) { // thanks GitGud for noticing an opportunity for improvement by overcoming "synchronized announce"
announcerLock.lock();
try {
session.write(packet);
// session.write(packet);
sendPacket(packet);
} finally {
announcerLock.unlock();
}
}
// Workaround for old packets. All uses of Client#announce(byte[]) should be migrated to Client#sendPacket(OutPacket)
private void sendPacket(final byte[] packet) {
announcerLock.lock();
try {
OutPacket outPacket = new ByteBufOutPacket();
outPacket.writeBytes(packet);
ioChannel.writeAndFlush(outPacket);
} finally {
announcerLock.unlock();
}
}
public void sendPacket(OutPacket outPacket) {
announcerLock.lock();
try {
ioChannel.writeAndFlush(outPacket.getBytes());
} finally {
announcerLock.unlock();
}

View File

@@ -11,11 +11,11 @@ public class LoginServerInitializer extends ServerChannelInitializer {
@Override
public void initChannel(SocketChannel socketChannel) {
final String clientIp = socketChannel.remoteAddress().getHostName();
final String clientIp = socketChannel.remoteAddress().getHostString();
log.debug("Client connected to login server from {} ", clientIp);
PacketProcessor packetProcessor = PacketProcessor.getLoginServerProcessor();
final MapleClient client = new MapleClient(packetProcessor, LoginServer.WORLD, LoginServer.CHANNEL);
final MapleClient client = new MapleClient(packetProcessor, LoginServer.WORLD_ID, LoginServer.CHANNEL_ID);
client.setSessionId(sessionId.getAndIncrement());
initPipeline(socketChannel, client);

View File

@@ -12,6 +12,11 @@ import java.awt.*;
public class ByteBufOutPacket implements OutPacket {
private final ByteBuf byteBuf;
@Deprecated(forRemoval = true)
public ByteBufOutPacket() {
this.byteBuf = Unpooled.buffer();
}
public ByteBufOutPacket(SendOpcode op) {
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeShortLE((short) op.getValue());