Search in sources :

Example 1 with ServerInfoRequestHandler

use of org.terasology.engine.network.internal.ServerInfoRequestHandler in project Terasology by MovingBlocks.

the class InfoRequestPipelineFactory method initChannel.

@Override
protected void initChannel(Channel ch) throws Exception {
    JoinStatusImpl joinStatus = new JoinStatusImpl();
    ChannelPipeline p = ch.pipeline();
    p.addLast(MetricRecordingHandler.NAME, new MetricRecordingHandler());
    p.addLast("inflateDecoder", new Lz4FrameDecoder());
    p.addLast("lengthFrameDecoder", new LengthFieldBasedFrameDecoder(8388608, 0, 3, 0, 3));
    p.addLast("protobufDecoder", new ProtobufDecoder(NetData.NetMessage.getDefaultInstance()));
    p.addLast("deflateEncoder", new Lz4FrameEncoder(true));
    p.addLast("frameLengthEncoder", new LengthFieldPrepender(3));
    p.addLast("protobufEncoder", new ProtobufEncoder());
    p.addLast("authenticationHandler", new ClientHandshakeHandler(joinStatus));
    p.addLast("connectionHandler", new ServerInfoRequestHandler());
}
Also used : Lz4FrameEncoder(io.netty.handler.codec.compression.Lz4FrameEncoder) ProtobufEncoder(io.netty.handler.codec.protobuf.ProtobufEncoder) ClientHandshakeHandler(org.terasology.engine.network.internal.ClientHandshakeHandler) MetricRecordingHandler(org.terasology.engine.network.internal.MetricRecordingHandler) ProtobufDecoder(io.netty.handler.codec.protobuf.ProtobufDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) JoinStatusImpl(org.terasology.engine.network.internal.JoinStatusImpl) ChannelPipeline(io.netty.channel.ChannelPipeline) Lz4FrameDecoder(io.netty.handler.codec.compression.Lz4FrameDecoder) ServerInfoRequestHandler(org.terasology.engine.network.internal.ServerInfoRequestHandler)

Example 2 with ServerInfoRequestHandler

use of org.terasology.engine.network.internal.ServerInfoRequestHandler in project Terasology by MovingBlocks.

the class ServerInfoService method requestInfo.

public Future<ServerInfoMessage> requestInfo(final String address, final int port) {
    SettableFuture<ServerInfoMessage> resultFuture = SettableFuture.create();
    InetSocketAddress remoteAddress = new InetSocketAddress(address, port);
    ChannelFuture connectCheck = bootstrap.connect(remoteAddress).addListener(connectFuture -> {
        if (!connectFuture.isSuccess()) {
            if (connectFuture.cause() != null && connectFuture.cause().getCause() != null) {
                // java's network exception.
                resultFuture.setException(connectFuture.cause().getCause());
            } else if (connectFuture.cause() != null) {
                // netty's exception, if it is not java's
                resultFuture.setException(connectFuture.cause());
            } else {
                // fallback exception when connecting not success.
                resultFuture.setException(new RuntimeException("Cannot connect to server"));
            }
        }
    });
    Channel channel = connectCheck.channel();
    channel.closeFuture().addListener(channelFuture -> {
        if (channelFuture.isSuccess()) {
            ServerInfoRequestHandler handler = channel.pipeline().get(ServerInfoRequestHandler.class);
            resultFuture.set(handler.getServerInfo());
        } else {
            resultFuture.setException(channelFuture.cause());
        }
    });
    return resultFuture;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ServerInfoRequestHandler(org.terasology.engine.network.internal.ServerInfoRequestHandler)

Aggregations

ServerInfoRequestHandler (org.terasology.engine.network.internal.ServerInfoRequestHandler)2 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)1 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)1 Lz4FrameDecoder (io.netty.handler.codec.compression.Lz4FrameDecoder)1 Lz4FrameEncoder (io.netty.handler.codec.compression.Lz4FrameEncoder)1 ProtobufDecoder (io.netty.handler.codec.protobuf.ProtobufDecoder)1 ProtobufEncoder (io.netty.handler.codec.protobuf.ProtobufEncoder)1 InetSocketAddress (java.net.InetSocketAddress)1 ClientHandshakeHandler (org.terasology.engine.network.internal.ClientHandshakeHandler)1 JoinStatusImpl (org.terasology.engine.network.internal.JoinStatusImpl)1 MetricRecordingHandler (org.terasology.engine.network.internal.MetricRecordingHandler)1