2018-07-06 13:30:00 +00:00
|
|
|
package com.eu.habbo.messages;
|
|
|
|
|
2020-05-05 00:20:07 +02:00
|
|
|
import com.eu.habbo.util.PacketUtils;
|
2018-07-06 13:30:00 +00:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.ByteBufOutputStream;
|
|
|
|
import io.netty.buffer.Unpooled;
|
2020-05-03 01:46:07 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-07-06 13:30:00 +00:00
|
|
|
|
2018-09-28 19:25:00 +00:00
|
|
|
import java.io.IOException;
|
2020-05-09 21:29:49 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2018-09-28 19:25:00 +00:00
|
|
|
|
2020-05-13 20:55:07 +02:00
|
|
|
public class ServerMessage {
|
2020-05-03 16:07:27 +02:00
|
|
|
|
2020-05-03 01:46:07 +02:00
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(ServerMessage.class);
|
2020-05-09 21:29:49 +02:00
|
|
|
private boolean initialized;
|
2020-05-03 01:46:07 +02:00
|
|
|
|
2018-07-06 13:30:00 +00:00
|
|
|
private int header;
|
2020-05-09 21:29:49 +02:00
|
|
|
private AtomicInteger refs;
|
2018-07-06 13:30:00 +00:00
|
|
|
private ByteBufOutputStream stream;
|
|
|
|
private ByteBuf channelBuffer;
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public ServerMessage() {
|
2020-05-09 21:29:49 +02:00
|
|
|
|
2018-09-28 19:25:00 +00:00
|
|
|
}
|
2018-07-06 13:30:00 +00:00
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public ServerMessage(int header) {
|
2020-05-09 21:29:49 +02:00
|
|
|
this.init(header);
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public ServerMessage init(int id) {
|
2020-05-09 21:29:49 +02:00
|
|
|
if (this.initialized) {
|
|
|
|
throw new ServerMessageException("ServerMessage was already initialized.");
|
|
|
|
}
|
|
|
|
|
2020-05-13 20:55:07 +02:00
|
|
|
this.initialized = true;
|
2018-07-06 13:30:00 +00:00
|
|
|
this.header = id;
|
2020-05-09 21:29:49 +02:00
|
|
|
this.refs = new AtomicInteger(0);
|
2018-07-06 13:30:00 +00:00
|
|
|
this.channelBuffer = Unpooled.buffer();
|
|
|
|
this.stream = new ByteBufOutputStream(this.channelBuffer);
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
try {
|
2018-07-06 13:30:00 +00:00
|
|
|
this.stream.writeInt(0);
|
|
|
|
this.stream.writeShort(id);
|
2020-05-29 22:29:14 -04:00
|
|
|
} catch (IOException e) {
|
2020-05-09 21:29:49 +02:00
|
|
|
throw new ServerMessageException(e);
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|
2020-05-09 21:29:49 +02:00
|
|
|
|
2018-07-06 13:30:00 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public void appendRawBytes(byte[] bytes) {
|
|
|
|
try {
|
2018-07-06 13:30:00 +00:00
|
|
|
this.stream.write(bytes);
|
2019-05-26 21:14:53 +03:00
|
|
|
} catch (IOException e) {
|
2020-05-09 21:29:49 +02:00
|
|
|
throw new ServerMessageException(e);
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public void appendString(String obj) {
|
|
|
|
if (obj == null) {
|
2018-07-06 13:30:00 +00:00
|
|
|
this.appendString("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
try {
|
2018-07-06 13:30:00 +00:00
|
|
|
byte[] data = obj.getBytes();
|
|
|
|
this.stream.writeShort(data.length);
|
|
|
|
this.stream.write(data);
|
2019-05-26 21:14:53 +03:00
|
|
|
} catch (IOException e) {
|
2020-05-09 21:29:49 +02:00
|
|
|
throw new ServerMessageException(e);
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public void appendChar(int obj) {
|
|
|
|
try {
|
2018-07-06 13:30:00 +00:00
|
|
|
this.stream.writeChar(obj);
|
2019-05-26 21:14:53 +03:00
|
|
|
} catch (IOException e) {
|
2020-05-09 21:29:49 +02:00
|
|
|
throw new ServerMessageException(e);
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public void appendChars(Object obj) {
|
|
|
|
try {
|
2018-07-06 13:30:00 +00:00
|
|
|
this.stream.writeChars(obj.toString());
|
2019-05-26 21:14:53 +03:00
|
|
|
} catch (IOException e) {
|
2020-05-09 21:29:49 +02:00
|
|
|
throw new ServerMessageException(e);
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public void appendInt(Integer obj) {
|
|
|
|
try {
|
2019-03-18 01:22:00 +00:00
|
|
|
this.stream.writeInt(obj);
|
2019-05-26 21:14:53 +03:00
|
|
|
} catch (IOException e) {
|
2020-05-09 21:29:49 +02:00
|
|
|
throw new ServerMessageException(e);
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public void appendInt(Short obj) {
|
2018-07-06 13:30:00 +00:00
|
|
|
this.appendShort(0);
|
|
|
|
this.appendShort(obj);
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public void appendInt(Byte obj) {
|
|
|
|
try {
|
2018-07-06 13:30:00 +00:00
|
|
|
this.stream.writeInt((int) obj);
|
2019-05-26 21:14:53 +03:00
|
|
|
} catch (IOException e) {
|
2020-05-09 21:29:49 +02:00
|
|
|
throw new ServerMessageException(e);
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public void appendInt(Boolean obj) {
|
|
|
|
try {
|
2018-07-06 13:30:00 +00:00
|
|
|
this.stream.writeInt(obj ? 1 : 0);
|
2019-05-26 21:14:53 +03:00
|
|
|
} catch (IOException e) {
|
2020-05-09 21:29:49 +02:00
|
|
|
throw new ServerMessageException(e);
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public void appendShort(int obj) {
|
|
|
|
try {
|
2018-07-06 13:30:00 +00:00
|
|
|
this.stream.writeShort((short) obj);
|
2019-05-26 21:14:53 +03:00
|
|
|
} catch (IOException e) {
|
2020-05-09 21:29:49 +02:00
|
|
|
throw new ServerMessageException(e);
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public void appendByte(Integer b) {
|
|
|
|
try {
|
2019-03-18 01:22:00 +00:00
|
|
|
this.stream.writeByte(b);
|
2019-05-26 21:14:53 +03:00
|
|
|
} catch (IOException e) {
|
2020-05-09 21:29:49 +02:00
|
|
|
throw new ServerMessageException(e);
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public void appendBoolean(Boolean obj) {
|
|
|
|
try {
|
2019-03-18 01:22:00 +00:00
|
|
|
this.stream.writeBoolean(obj);
|
2019-05-26 21:14:53 +03:00
|
|
|
} catch (IOException e) {
|
2020-05-09 21:29:49 +02:00
|
|
|
throw new ServerMessageException(e);
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public void appendDouble(double d) {
|
|
|
|
try {
|
2018-07-06 13:30:00 +00:00
|
|
|
this.stream.writeDouble(d);
|
2019-05-26 21:14:53 +03:00
|
|
|
} catch (IOException e) {
|
2020-05-09 21:29:49 +02:00
|
|
|
throw new ServerMessageException(e);
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public void appendDouble(Double obj) {
|
|
|
|
try {
|
2019-03-18 01:22:00 +00:00
|
|
|
this.stream.writeDouble(obj);
|
2019-05-26 21:14:53 +03:00
|
|
|
} catch (IOException e) {
|
2020-05-09 21:29:49 +02:00
|
|
|
throw new ServerMessageException(e);
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public ServerMessage appendResponse(ServerMessage obj) {
|
|
|
|
try {
|
2018-07-06 13:30:00 +00:00
|
|
|
this.stream.write(obj.get().array());
|
2019-05-26 21:14:53 +03:00
|
|
|
} catch (IOException e) {
|
2020-05-09 21:29:49 +02:00
|
|
|
throw new ServerMessageException(e);
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public void append(ISerialize obj) {
|
2018-07-06 13:30:00 +00:00
|
|
|
obj.serialize(this);
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public String getBodyString() {
|
2020-05-05 00:20:07 +02:00
|
|
|
return PacketUtils.formatPacket(this.channelBuffer);
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public int getHeader() {
|
2018-07-06 13:30:00 +00:00
|
|
|
return this.header;
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:14:53 +03:00
|
|
|
public ByteBuf get() {
|
2018-07-06 13:30:00 +00:00
|
|
|
this.channelBuffer.setInt(0, this.channelBuffer.writerIndex() - 4);
|
|
|
|
|
|
|
|
return this.channelBuffer.copy();
|
|
|
|
}
|
2020-05-02 03:17:59 +02:00
|
|
|
|
2018-07-06 13:30:00 +00:00
|
|
|
}
|