2018-07-06 15:30:00 +02:00
|
|
|
package com.eu.habbo.messages;
|
|
|
|
|
2020-05-09 21:29:49 +02:00
|
|
|
import com.eu.habbo.util.DebugUtils;
|
2020-05-05 00:20:07 +02:00
|
|
|
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-09 21:29:49 +02:00
|
|
|
import io.netty.util.IllegalReferenceCountException;
|
2020-05-03 16:07:27 +02:00
|
|
|
import io.netty.util.ReferenceCounted;
|
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;
|
2020-05-09 21:29:49 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2018-09-28 21:25:00 +02:00
|
|
|
|
2020-05-03 16:07:27 +02:00
|
|
|
public class ServerMessage implements ReferenceCounted {
|
|
|
|
|
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 15:30:00 +02:00
|
|
|
private int header;
|
2020-05-09 21:29:49 +02:00
|
|
|
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() {
|
2020-05-09 21:29:49 +02:00
|
|
|
|
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) {
|
2020-05-09 21:29:49 +02:00
|
|
|
this.init(header);
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public ServerMessage init(int id) {
|
2020-05-09 21:29:49 +02:00
|
|
|
if (this.initialized) {
|
|
|
|
throw new ServerMessageException("ServerMessage was already initialized.");
|
|
|
|
}
|
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
this.header = id;
|
2020-05-09 21:29:49 +02:00
|
|
|
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);
|
2019-05-26 20:14:53 +02:00
|
|
|
} catch (Exception e) {
|
2020-05-09 21:29:49 +02:00
|
|
|
throw new ServerMessageException(e);
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
2020-05-09 21:29:49 +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) {
|
2020-05-09 21:29:49 +02:00
|
|
|
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) {
|
2020-05-09 21:29:49 +02:00
|
|
|
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) {
|
2020-05-09 21:29:49 +02:00
|
|
|
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) {
|
2020-05-09 21:29:49 +02:00
|
|
|
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) {
|
2020-05-09 21:29:49 +02:00
|
|
|
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) {
|
2020-05-09 21:29:49 +02:00
|
|
|
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) {
|
2020-05-09 21:29:49 +02:00
|
|
|
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) {
|
2020-05-09 21:29:49 +02:00
|
|
|
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) {
|
2020-05-09 21:29:49 +02:00
|
|
|
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) {
|
2020-05-09 21:29:49 +02:00
|
|
|
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) {
|
2020-05-09 21:29:49 +02:00
|
|
|
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) {
|
2020-05-09 21:29:49 +02:00
|
|
|
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) {
|
2020-05-09 21:29:49 +02:00
|
|
|
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() {
|
2020-05-05 00:20:07 +02:00
|
|
|
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();
|
|
|
|
}
|
2020-05-02 03:17:59 +02:00
|
|
|
|
2020-05-03 16:07:27 +02:00
|
|
|
@Override
|
|
|
|
public int refCnt() {
|
2020-05-09 21:29:49 +02:00
|
|
|
return this.refs.get();
|
2020-05-03 16:07:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ReferenceCounted retain() {
|
2020-05-09 21:29:49 +02:00
|
|
|
int result = this.refs.incrementAndGet();
|
|
|
|
|
|
|
|
if (this.header == 1167 || this.header == 2024 || this.header == 2505) {
|
|
|
|
System.out.printf("retain Packet: %d Count: %d From: %s%n", this.header, result, DebugUtils.getCallerCallerStacktrace());
|
|
|
|
}
|
|
|
|
|
2020-05-03 16:07:27 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ReferenceCounted retain(int i) {
|
2020-05-09 21:29:49 +02:00
|
|
|
int result = this.refs.addAndGet(i);
|
|
|
|
|
|
|
|
if (this.header == 1167 || this.header == 2024 || this.header == 2505) {
|
|
|
|
System.out.printf("retain Packet: %d Count: %d From: %s%n", this.header, result, DebugUtils.getCallerCallerStacktrace());
|
|
|
|
}
|
|
|
|
|
2020-05-03 16:07:27 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ReferenceCounted touch() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ReferenceCounted touch(Object o) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-05-09 21:29:49 +02:00
|
|
|
@Override
|
2020-05-03 16:07:27 +02:00
|
|
|
public boolean release() {
|
2020-05-09 21:29:49 +02:00
|
|
|
return this.release(1);
|
2020-05-03 16:07:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean release(int i) {
|
2020-05-09 21:29:49 +02:00
|
|
|
int value = this.refs.addAndGet(-i);
|
|
|
|
|
|
|
|
if (this.header == 1167 || this.header == 2024 || this.header == 2505) {
|
|
|
|
System.out.printf("release Packet: %d Count: %d From: %s%n", this.header, value, DebugUtils.getCallerCallerStacktrace());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
throw new IllegalReferenceCountException("Decremented below 0 (packet " + this.header + " value " + value + ").");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value == 0) {
|
|
|
|
this.channelBuffer.release();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2020-05-02 03:17:59 +02:00
|
|
|
}
|
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|