use of org.lanternpowered.server.network.message.Message in project LanternServer by LanternPowered.
the class AbstractCodecPlayInOutCustomPayload method decode0.
private Message decode0(CodecContext context, ByteBuffer content, String channel) {
if ("REGISTER".equals(channel)) {
Set<String> channels = decodeChannels(content);
Iterator<String> it = channels.iterator();
while (it.hasNext()) {
String channel0 = it.next();
if (channel0.startsWith("FML")) {
it.remove();
}
}
if (!channels.isEmpty()) {
return new MessagePlayInOutRegisterChannels(channels);
}
} else if ("UNREGISTER".equals(channel)) {
Set<String> channels = decodeChannels(content);
Iterator<String> it = channels.iterator();
while (it.hasNext()) {
String channel0 = it.next();
if (channel0.startsWith("FML")) {
it.remove();
}
}
if (!channels.isEmpty()) {
return new MessagePlayInOutUnregisterChannels(channels);
}
} else if ("FML|MP".equals(channel)) {
Attribute<MultiPartMessage> attribute = context.getChannel().attr(FML_MULTI_PART_MESSAGE);
MultiPartMessage message0 = attribute.get();
if (message0 == null) {
String channel0 = content.readString();
int parts = content.readByte() & 0xff;
int size = content.readInteger();
if (size <= 0 || size >= -16797616) {
throw new CodecException("Received FML MultiPart packet outside of valid length bounds, Max: -16797616, Received: " + size);
}
attribute.set(new MultiPartMessage(channel0, context.byteBufAlloc().buffer(size), parts));
} else {
int part = content.readByte() & 0xff;
if (part != message0.index) {
throw new CodecException("Received FML MultiPart packet out of order, Expected: " + message0.index + ", Got: " + part);
}
int len = content.available() - 1;
content.readBytes(message0.buffer, message0.offset, len);
message0.offset += len;
message0.index++;
if (message0.index >= message0.parts) {
final Message message = decode0(context, message0.channel, message0.buffer);
attribute.set(null);
return message;
}
}
} else {
return decode0(context, channel, content);
}
return NullMessage.INSTANCE;
}
use of org.lanternpowered.server.network.message.Message in project LanternServer by LanternPowered.
the class LanternBossBar method sendStyleUpdate.
private void sendStyleUpdate() {
if (!this.viewers.isEmpty()) {
final Message message = new MessagePlayOutBossBar.UpdateStyle(this.uniqueId, this.color, this.overlay);
this.viewers.forEach(player -> player.getConnection().send(message));
}
}
use of org.lanternpowered.server.network.message.Message in project LanternServer by LanternPowered.
the class LanternBossBar method sendMiscUpdate.
private void sendMiscUpdate() {
if (!this.viewers.isEmpty()) {
final Message message = new MessagePlayOutBossBar.UpdateMisc(this.uniqueId, this.darkenSky, this.playEndBossMusic || this.createFog);
this.viewers.forEach(player -> player.getConnection().send(message));
}
}
use of org.lanternpowered.server.network.message.Message in project LanternServer by LanternPowered.
the class NetworkSession method send.
/**
* Sends a iterable of {@link Message}s.
*
* @param messages The messages
*/
public void send(Iterable<Message> messages) {
checkNotNull(messages, "messages");
final Iterator<Message> it = messages.iterator();
checkArgument(it.hasNext(), "messages cannot be empty");
Message message = it.next();
// Don't bother checking if we are in the event loop,
// there is only one message.
final ChannelPromise voidPromise = this.channel.voidPromise();
if (!it.hasNext()) {
this.channel.writeAndFlush(message, voidPromise);
} else {
final EventLoop eventLoop = this.channel.eventLoop();
if (eventLoop.inEventLoop()) {
for (Message message0 : messages) {
this.channel.writeAndFlush(message0, voidPromise);
}
} else {
// If there are more then one message, combine them inside the
// event loop to reduce overhead of wakeup calls and object creation
// Create a copy of the list, to avoid concurrent modifications
final List<Message> messages0 = ImmutableList.copyOf(messages);
eventLoop.submit(() -> {
for (Message message0 : messages0) {
this.channel.writeAndFlush(message0, voidPromise);
}
});
}
}
}
use of org.lanternpowered.server.network.message.Message in project LanternServer by LanternPowered.
the class NetworkSession method sendWithFuture.
/**
* Sends a iterable of {@link Message}s.
*
* @param messages The messages
*/
public ChannelFuture sendWithFuture(Iterable<Message> messages) {
checkNotNull(messages, "messages");
final Iterator<Message> it = messages.iterator();
checkArgument(it.hasNext(), "messages cannot be empty");
final ChannelPromise promise = this.channel.newPromise();
if (!this.channel.isActive()) {
return promise;
}
promise.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
Message message = it.next();
// there is only one message.
if (!it.hasNext()) {
this.channel.writeAndFlush(message, promise);
} else {
final EventLoop eventLoop = this.channel.eventLoop();
final ChannelPromise voidPromise = this.channel.voidPromise();
if (eventLoop.inEventLoop()) {
while (true) {
final boolean next = it.hasNext();
// Only use a normal channel promise for the last message
this.channel.writeAndFlush(message, next ? voidPromise : promise);
if (!next) {
break;
}
message = it.next();
ReferenceCountUtil.retain(message);
}
} else {
// If there are more then one message, combine them inside the
// event loop to reduce overhead of wakeup calls and object creation
// Create a copy of the list, to avoid concurrent modifications
final List<Message> messages0 = ImmutableList.copyOf(messages);
messages0.forEach(ReferenceCountUtil::retain);
eventLoop.submit(() -> {
final Iterator<Message> it0 = messages0.iterator();
do {
final Message message0 = it0.next();
// Only use a normal channel promise for the last message
this.channel.writeAndFlush(message0, it0.hasNext() ? voidPromise : promise);
} while (it0.hasNext());
});
}
}
return promise;
}
Aggregations