Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/gameclients/GameClient.java

130 lines
4.2 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.gameclients;
import com.eu.habbo.Emulator;
import com.eu.habbo.core.Logging;
import com.eu.habbo.habbohotel.users.Habbo;
import com.eu.habbo.messages.PacketManager;
import com.eu.habbo.messages.ServerMessage;
2020-02-27 20:06:36 +01:00
import com.eu.habbo.messages.incoming.MessageHandler;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.messages.outgoing.MessageComposer;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
2018-10-07 00:28:00 +02:00
import java.util.concurrent.ConcurrentHashMap;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public class GameClient {
public final ConcurrentHashMap<Integer, Integer> incomingPacketCounter = new ConcurrentHashMap<>(25);
2018-07-06 15:30:00 +02:00
private final Channel channel;
2019-05-26 20:14:53 +02:00
public long lastPacketCounterCleared = Emulator.getIntUnixTimestamp();
2018-07-06 15:30:00 +02:00
private Habbo habbo;
private String machineId = "";
2020-02-27 20:06:36 +01:00
public final ConcurrentHashMap<Class<? extends MessageHandler>, Long> messageTimestamps = new ConcurrentHashMap<>();
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public GameClient(Channel channel) {
2018-07-06 15:30:00 +02:00
this.channel = channel;
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
public void sendResponse(MessageComposer composer) {
if (this.channel.isOpen()) {
try {
2018-07-06 15:30:00 +02:00
ServerMessage msg = composer.compose();
2019-03-18 02:22:00 +01:00
this.sendResponse(msg);
2019-05-26 20:14:53 +02:00
} catch (Exception e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logPacketError(e);
}
}
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
public void sendResponse(ServerMessage response) {
if (this.channel.isOpen()) {
if (response == null || response.getHeader() <= 0) {
2018-07-06 15:30:00 +02:00
return;
}
if (PacketManager.DEBUG_SHOW_PACKETS)
Emulator.getLogging().logPacketLine("[" + Logging.ANSI_PURPLE + "SERVER" + Logging.ANSI_RESET + "] => [" + response.getHeader() + "] -> " + response.getBodyString());
this.channel.write(response.get(), this.channel.voidPromise());
this.channel.flush();
}
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
public void sendResponses(ArrayList<ServerMessage> responses) {
2018-07-06 15:30:00 +02:00
ByteBuf buffer = Unpooled.buffer();
2019-05-26 20:14:53 +02:00
if (this.channel.isOpen()) {
for (ServerMessage response : responses) {
if (response == null || response.getHeader() <= 0) {
2018-07-06 15:30:00 +02:00
return;
}
if (PacketManager.DEBUG_SHOW_PACKETS)
Emulator.getLogging().logPacketLine("[" + Logging.ANSI_PURPLE + "SERVER" + Logging.ANSI_RESET + "] => [" + response.getHeader() + "] -> " + response.getBodyString());
buffer.writeBytes(response.get());
}
this.channel.write(buffer.copy(), this.channel.voidPromise());
this.channel.flush();
}
buffer.release();
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
public void dispose() {
try {
2018-07-06 15:30:00 +02:00
this.channel.close();
2019-05-26 20:14:53 +02:00
if (this.habbo != null) {
if (this.habbo.isOnline()) {
2018-07-06 15:30:00 +02:00
this.habbo.getHabboInfo().setOnline(false);
this.habbo.disconnect();
}
this.habbo = null;
}
2019-05-26 20:14:53 +02:00
} catch (Exception e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logErrorLine(e);
}
}
2019-05-26 20:14:53 +02:00
public Channel getChannel() {
2018-07-06 15:30:00 +02:00
return this.channel;
}
2019-05-26 20:14:53 +02:00
public Habbo getHabbo() {
2018-07-06 15:30:00 +02:00
return this.habbo;
}
2019-05-26 20:14:53 +02:00
public void setHabbo(Habbo habbo) {
2018-07-06 15:30:00 +02:00
this.habbo = habbo;
}
2019-05-26 20:14:53 +02:00
public String getMachineId() {
2018-07-06 15:30:00 +02:00
return this.machineId;
}
2019-05-26 20:14:53 +02:00
public void setMachineId(String machineId) {
if (machineId == null) {
2018-07-06 15:30:00 +02:00
throw new RuntimeException("Cannot set machineID to NULL");
}
this.machineId = machineId;
2019-05-26 20:14:53 +02:00
if (this.habbo != null) {
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("UPDATE users SET machine_id = ? WHERE id = ? LIMIT 1")) {
2018-07-06 15:30:00 +02:00
statement.setString(1, this.machineId);
statement.setInt(2, this.habbo.getHabboInfo().getId());
statement.execute();
2019-05-26 20:14:53 +02:00
} catch (SQLException e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logSQLException(e);
}
}
}
}