Search in sources :

Example 1 with CodecContext

use of org.lanternpowered.server.network.message.codec.CodecContext in project LanternServer by LanternPowered.

the class NetworkManager method init0.

@Override
protected ChannelFuture init0(SocketAddress address, boolean epoll) {
    this.bootstrap = new ServerBootstrap();
    // Take advantage of the fast thread local threads,
    // this is also provided by the default thread factory
    final ThreadFactory threadFactory = ThreadHelper.newFastThreadLocalThreadFactory(() -> "netty-" + threadCounter.getAndIncrement());
    this.bossGroup = createEventLoopGroup(epoll, threadFactory);
    this.workerGroup = createEventLoopGroup(epoll, threadFactory);
    this.socketAddress = address;
    return this.bootstrap.group(this.bossGroup, this.workerGroup).channel(getServerSocketChannelClass(epoll)).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            final ChannelPipeline pipeline = ch.pipeline();
            final NetworkSession networkSession = new NetworkSession(ch, server, NetworkManager.this);
            final CodecContext codecContext = new SimpleCodecContext(new LanternByteBufferAllocator(ch.alloc()), ch, networkSession);
            pipeline.addLast(new ReadTimeoutHandler(NetworkSession.READ_TIMEOUT_SECONDS)).addLast(NetworkSession.LEGACY_PING, new LegacyProtocolHandler(networkSession)).addLast(NetworkSession.ENCRYPTION, NoopHandler.INSTANCE).addLast(NetworkSession.FRAMING, new MessageFramingHandler()).addLast(NetworkSession.COMPRESSION, NoopHandler.INSTANCE).addLast(NetworkSession.CODECS, new MessageCodecHandler(codecContext)).addLast(NetworkSession.PROCESSOR, new MessageProcessorHandler(codecContext)).addLast(NetworkSession.HANDLER, networkSession);
        }
    }).childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT).childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true).bind(address);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) SocketChannel(io.netty.channel.socket.SocketChannel) SimpleCodecContext(org.lanternpowered.server.network.message.codec.SimpleCodecContext) CodecContext(org.lanternpowered.server.network.message.codec.CodecContext) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) LegacyProtocolHandler(org.lanternpowered.server.network.pipeline.LegacyProtocolHandler) LanternByteBufferAllocator(org.lanternpowered.server.network.buffer.LanternByteBufferAllocator) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) MessageFramingHandler(org.lanternpowered.server.network.pipeline.MessageFramingHandler) SimpleCodecContext(org.lanternpowered.server.network.message.codec.SimpleCodecContext) MessageProcessorHandler(org.lanternpowered.server.network.pipeline.MessageProcessorHandler) MessageCodecHandler(org.lanternpowered.server.network.pipeline.MessageCodecHandler)

Example 2 with CodecContext

use of org.lanternpowered.server.network.message.codec.CodecContext in project LanternServer by LanternPowered.

the class CodecPlayOutEffect method encode.

@Override
public ByteBuffer encode(CodecContext context, Message message) throws CodecException {
    final ByteBuffer buf = context.byteBufAlloc().buffer();
    if (message instanceof MessagePlayOutEffect) {
        final MessagePlayOutEffect message1 = (MessagePlayOutEffect) message;
        buf.writeInteger(message1.getType());
        buf.write(Types.VECTOR_3_I, message1.getPosition());
        buf.writeInteger(message1.getData());
        buf.writeBoolean(message1.isBroadcast());
    } else if (message instanceof MessagePlayOutRecord) {
        final MessagePlayOutRecord message1 = (MessagePlayOutRecord) message;
        buf.writeInteger(1010);
        buf.write(Types.VECTOR_3_I, message1.getPosition());
        buf.writeInteger(message1.getRecord().map(type -> 2256 + ((LanternRecordType) type).getInternalId()).orElse(0));
        buf.writeBoolean(false);
    } else {
        throw new EncoderException("Unsupported message type: " + message.getClass().getName());
    }
    return buf;
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) CodecException(io.netty.handler.codec.CodecException) Codec(org.lanternpowered.server.network.message.codec.Codec) MessagePlayOutRecord(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutRecord) LanternRecordType(org.lanternpowered.server.data.type.record.LanternRecordType) MessagePlayOutEffect(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEffect) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) Types(org.lanternpowered.server.network.buffer.objects.Types) CodecContext(org.lanternpowered.server.network.message.codec.CodecContext) Message(org.lanternpowered.server.network.message.Message) EncoderException(io.netty.handler.codec.EncoderException) MessagePlayOutEffect(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEffect) MessagePlayOutRecord(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutRecord) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer)

Aggregations

CodecContext (org.lanternpowered.server.network.message.codec.CodecContext)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 CodecException (io.netty.handler.codec.CodecException)1 EncoderException (io.netty.handler.codec.EncoderException)1 ReadTimeoutHandler (io.netty.handler.timeout.ReadTimeoutHandler)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 LanternRecordType (org.lanternpowered.server.data.type.record.LanternRecordType)1 ByteBuffer (org.lanternpowered.server.network.buffer.ByteBuffer)1 LanternByteBufferAllocator (org.lanternpowered.server.network.buffer.LanternByteBufferAllocator)1 Types (org.lanternpowered.server.network.buffer.objects.Types)1 Message (org.lanternpowered.server.network.message.Message)1 Codec (org.lanternpowered.server.network.message.codec.Codec)1 SimpleCodecContext (org.lanternpowered.server.network.message.codec.SimpleCodecContext)1 LegacyProtocolHandler (org.lanternpowered.server.network.pipeline.LegacyProtocolHandler)1 MessageCodecHandler (org.lanternpowered.server.network.pipeline.MessageCodecHandler)1 MessageFramingHandler (org.lanternpowered.server.network.pipeline.MessageFramingHandler)1 MessageProcessorHandler (org.lanternpowered.server.network.pipeline.MessageProcessorHandler)1 MessagePlayOutEffect (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEffect)1