Arcturus-Community/src/main/java/com/eu/habbo/messages/ServerMessage.java

188 lines
4.5 KiB
Java
Raw Normal View History

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