Skip to content

Commit

Permalink
fix sendObject()
Browse files Browse the repository at this point in the history
  • Loading branch information
Quackster committed Jul 12, 2024
1 parent b64de80 commit 42c8117
Showing 1 changed file with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,52 @@
import org.alexdev.havana.messages.types.MessageComposer;
import org.alexdev.havana.messages.types.PlayerMessageComposer;
import org.alexdev.havana.server.netty.streams.NettyResponse;
import org.alexdev.havana.util.StringUtil;
import org.alexdev.havana.util.config.ServerConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.Buffer;
import java.util.List;

public class NetworkEncoder extends MessageToMessageEncoder<MessageComposer> {
public class NetworkEncoder extends MessageToMessageEncoder<Object> {
final private static Logger log = LoggerFactory.getLogger(NetworkEncoder.class);

@Override
protected void encode(ChannelHandlerContext ctx, MessageComposer msg, List<Object> out) throws Exception {
protected void encode(ChannelHandlerContext ctx, Object outgoing, List<Object> out) throws Exception {
Player player = ctx.channel().attr(Player.PLAYER_KEY).get();
ByteBuf buffer = Unpooled.buffer();

if (msg instanceof PlayerMessageComposer) {
PlayerMessageComposer playerMessageComposer = (PlayerMessageComposer) msg;
playerMessageComposer.setPlayer(player);
}
if (outgoing instanceof MessageComposer) {
var msg = (MessageComposer) outgoing;

NettyResponse response = new NettyResponse(msg.getHeader(), buffer);
if (msg instanceof PlayerMessageComposer) {
PlayerMessageComposer playerMessageComposer = (PlayerMessageComposer) msg;
playerMessageComposer.setPlayer(player);
}

try {
msg.compose(response);
} catch (Exception ex) {
Log.getErrorLogger().error("Error occurred when composing (" + response.getHeader() + "):", ex);
return;
}
NettyResponse response = new NettyResponse(msg.getHeader(), buffer);

try {
msg.compose(response);
} catch (Exception ex) {
Log.getErrorLogger().error("Error occurred when composing (" + response.getHeader() + "):", ex);
return;
}

if (!response.isFinalised()) {
buffer.writeByte(1);
response.setFinalised(true);
}

if (!response.isFinalised()) {
buffer.writeByte(1);
response.setFinalised(true);
if (ServerConfiguration.getBoolean("log.sent.packets")) {
log.info("SENT 1: {} / {}", msg.getHeader(), response.getBodyString());
}
}

if (ServerConfiguration.getBoolean("log.sent.packets")) {
log.info("SENT 1: {} / {}", msg.getHeader(), response.getBodyString());
if (outgoing instanceof String) {
var msgString = (String) outgoing;
buffer.writeBytes(msgString.getBytes(StringUtil.getCharset()));
}

out.add(buffer);
Expand Down

0 comments on commit 42c8117

Please sign in to comment.