use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.
the class LanternChannelRegistrar method sendPayload.
void sendPayload(Player player, String channel, Consumer<ByteBuffer> payload) {
checkNotNull(player, "player");
checkNotNull(payload, "payload");
final NetworkSession session = ((LanternPlayer) player).getConnection();
if (session.getRegisteredChannels().contains(channel)) {
final ByteBuffer buf = ByteBufferAllocator.unpooled().buffer();
payload.accept(buf);
session.send(new MessagePlayInOutChannelPayload(channel, buf));
}
}
use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.
the class LanternChannelRegistrar method sendPayloadToAll.
void sendPayloadToAll(String channel, Consumer<ByteBuffer> payload) {
checkNotNull(payload, "payload");
final Iterator<Player> players = this.server.getOnlinePlayers().stream().filter(player -> ((LanternPlayer) player).getConnection().getRegisteredChannels().contains(channel)).iterator();
if (players.hasNext()) {
final ByteBuffer buf = ByteBufferAllocator.unpooled().buffer();
payload.accept(buf);
final Message msg = new MessagePlayInOutChannelPayload(channel, buf);
players.forEachRemaining(player -> ((LanternPlayer) player).getConnection().send(msg));
}
}
use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.
the class LanternIndexedMessageChannel method encode.
private void encode(Message message, ByteBuffer buf) {
final IndexedMessageRegistration registration = getRegistrations(Platform.Type.SERVER).classToRegistration.get(message.getClass());
checkArgument(registration != null, "The specified message type %s is not registered", message.getClass().getName());
final ByteBuffer content = ByteBufferAllocator.unpooled().buffer();
message.writeTo(content);
// noinspection ConstantConditions
buf.writeByte(registration.opcode);
buf.writeBytes(content);
}
use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.
the class LanternIndexedMessageChannel method handlePayload.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
void handlePayload(ByteBuffer buf, RemoteConnection connection) {
final byte opcode = buf.readByte();
final IndexedMessageRegistration registration = getRegistrations(Platform.Type.SERVER).opcodeToRegistration.get(opcode);
if (registration == null) {
Lantern.getLogger().warn("Received unexpected message type with id: {}" + " in the indexed message channel: {}", opcode, getName());
return;
}
final Message message;
try {
message = registration.messageType.newInstance();
} catch (Exception e) {
Lantern.getLogger().error("Failed to instantiate message: {}", registration.messageType.getName(), e);
return;
}
final ByteBuffer content = buf.slice();
try {
message.readFrom(content);
} catch (Exception e) {
Lantern.getLogger().error("Failed to deserialize message: {}", registration.messageType.getName(), e);
return;
}
registration.handlers.forEach(handler -> ((MessageHandler) handler).handleMessage(message, connection, Platform.Type.SERVER));
}
use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.
the class CodecPlayInOutCustomPayload method encode0.
@Override
protected MessageResult encode0(CodecContext context, Message message) throws CodecException {
if (message instanceof MessageForgeHandshakeInOutAck) {
return new MessageResult("FML|HS", context.byteBufAlloc().buffer(2).writeByte((byte) FML_HANDSHAKE_ACK).writeByte((byte) ((ForgeServerHandshakePhase) ((MessageForgeHandshakeInOutAck) message).getPhase()).ordinal()));
} else if (message instanceof MessageForgeHandshakeInOutHello) {
return new MessageResult("FML|HS", context.byteBufAlloc().buffer(2).writeByte((byte) FML_HANDSHAKE_SERVER_HELLO).writeByte((byte) FORGE_PROTOCOL));
} else if (message instanceof MessageForgeHandshakeInOutModList) {
Map<String, String> entries = ((MessageForgeHandshakeInOutModList) message).getEntries();
ByteBuffer buf = context.byteBufAlloc().buffer();
buf.writeByte((byte) FML_HANDSHAKE_MOD_LIST);
buf.writeVarInt(entries.size());
for (Map.Entry<String, String> en : entries.entrySet()) {
buf.writeString(en.getKey());
buf.writeString(en.getValue());
}
return new MessageResult("FML|HS", buf);
} else if (message instanceof MessageForgeHandshakeOutReset) {
return new MessageResult("FML|HS", context.byteBufAlloc().buffer(1).writeByte((byte) FML_HANDSHAKE_RESET));
}
throw new EncoderException("Unsupported message type: " + message);
}
Aggregations