Search in sources :

Example 1 with PlayProtocol

use of net.glowstone.net.protocol.PlayProtocol in project Glowstone by GlowstoneMC.

the class GlowSession method disconnect.

/**
 * Disconnects the session with the specified reason.
 *
 * <p>This causes a KickMessage to be sent. When it has been delivered, the channel is closed.
 *
 * @param reason       The reason for disconnection.
 * @param overrideKick Whether to skip the kick event.
 */
public void disconnect(String reason, boolean overrideKick) {
    if (player != null && !overrideKick) {
        PlayerKickEvent event = EventFactory.getInstance().onPlayerKick(player, reason);
        if (event.isCancelled()) {
            return;
        }
        reason = event.getReason();
        if (player.isOnline() && event.getLeaveMessage() != null) {
            server.broadcastMessage(event.getLeaveMessage());
        }
    }
    // log that the player was kicked
    if (player != null) {
        GlowServer.logger.info(player.getName() + " kicked: " + reason);
    } else {
        GlowServer.logger.info("[" + address + "] kicked: " + reason);
    }
    // perform the kick, sending a kick message if possible
    if (isActive() && (getProtocol() instanceof LoginProtocol || getProtocol() instanceof PlayProtocol)) {
        // channel is both currently connected and in a protocol state allowing kicks
        ChannelFuture future = sendWithFuture(new KickMessage(reason));
        if (future != null) {
            future.addListener(ChannelFutureListener.CLOSE);
        }
    } else {
        getChannel().close();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) KickMessage(net.glowstone.net.message.KickMessage) PlayerKickEvent(org.bukkit.event.player.PlayerKickEvent) PlayProtocol(net.glowstone.net.protocol.PlayProtocol) LoginProtocol(net.glowstone.net.protocol.LoginProtocol)

Example 2 with PlayProtocol

use of net.glowstone.net.protocol.PlayProtocol in project Glowstone by GlowstoneMC.

the class GlowSession method idle.

/**
 * Notify that the session is currently idle.
 */
public void idle() {
    if (pingMessageId == 0 && getProtocol() instanceof PlayProtocol) {
        pingMessageId = System.currentTimeMillis();
        send(new PingMessage(pingMessageId));
    } else {
        disconnect("Timed out");
    }
}
Also used : PlayProtocol(net.glowstone.net.protocol.PlayProtocol) PingMessage(net.glowstone.net.message.play.game.PingMessage)

Example 3 with PlayProtocol

use of net.glowstone.net.protocol.PlayProtocol in project Dragonet-Legacy by DragonetMC.

the class BaseProtocolTest method testProtocol.

@Test
public void testProtocol() throws Exception {
    Map<Class<? extends Message>, Codec.CodecRegistration> inboundMap = getField(inboundCodecs, CodecLookupService.class, "messages");
    Map<Class<? extends Message>, Codec.CodecRegistration> outboundMap = getField(outboundCodecs, CodecLookupService.class, "messages");
    Set<Class<? extends Message>> inboundSet = new HashSet<>(inboundMap.keySet());
    Set<Class<? extends Message>> outboundSet = new HashSet<>(outboundMap.keySet());
    for (Message message : testMessages) {
        boolean any = false;
        Class<? extends Message> clazz = message.getClass();
        // test inbound
        Codec.CodecRegistration registration = inboundCodecs.find(clazz);
        if (registration != null) {
            inboundSet.remove(clazz);
            checkCodec(registration, message);
            any = true;
        }
        // test outbound
        registration = outboundCodecs.find(clazz);
        if (registration != null) {
            outboundSet.remove(clazz);
            checkCodec(registration, message);
            any = true;
        }
        assertTrue("Codec missing for: " + message, any);
    }
    // special case: HeldItemMessage is excluded from tests
    inboundSet.remove(HeldItemMessage.class);
    outboundSet.remove(HeldItemMessage.class);
    assertTrue("Did not test inbound classes: " + inboundSet, inboundSet.isEmpty());
    // todo: enable the outbound check for PlayProtocol
    if (!(protocol instanceof PlayProtocol)) {
        assertTrue("Did not test outbound classes: " + outboundSet, outboundSet.isEmpty());
    }
}
Also used : Codec(com.flowpowered.networking.Codec) HeldItemMessage(net.glowstone.net.message.play.inv.HeldItemMessage) Message(com.flowpowered.networking.Message) PlayProtocol(net.glowstone.net.protocol.PlayProtocol) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with PlayProtocol

use of net.glowstone.net.protocol.PlayProtocol in project Glowstone by GlowstoneMC.

the class BaseProtocolTest method testProtocol.

@Test
public void testProtocol() throws Exception {
    Map<Class<? extends Message>, Codec.CodecRegistration> inboundMap = getField(inboundCodecs, CodecLookupService.class, "messages");
    Map<Class<? extends Message>, Codec.CodecRegistration> outboundMap = getField(outboundCodecs, CodecLookupService.class, "messages");
    Set<Class<? extends Message>> inboundSet = new HashSet<>(inboundMap.keySet());
    Set<Class<? extends Message>> outboundSet = new HashSet<>(outboundMap.keySet());
    for (Message message : testMessages) {
        boolean any = false;
        Class<? extends Message> clazz = message.getClass();
        // test inbound
        Codec.CodecRegistration registration = inboundCodecs.find(clazz);
        if (registration != null) {
            inboundSet.remove(clazz);
            checkCodec(registration, message);
            any = true;
        }
        // test outbound
        registration = outboundCodecs.find(clazz);
        if (registration != null) {
            outboundSet.remove(clazz);
            checkCodec(registration, message);
            any = true;
        }
        assertThat("Codec missing for: " + message, any, is(true));
    }
    // special case: HeldItemMessage is excluded from tests
    inboundSet.remove(HeldItemMessage.class);
    outboundSet.remove(HeldItemMessage.class);
    assertThat("Did not test inbound classes: " + inboundSet, inboundSet.isEmpty(), is(true));
    // todo: enable the outbound check for PlayProtocol
    if (!(protocol instanceof PlayProtocol)) {
        assertThat("Did not test outbound classes: " + outboundSet, outboundSet.isEmpty(), is(true));
    }
}
Also used : Codec(com.flowpowered.network.Codec) HeldItemMessage(net.glowstone.net.message.play.inv.HeldItemMessage) Message(com.flowpowered.network.Message) PlayProtocol(net.glowstone.net.protocol.PlayProtocol) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

PlayProtocol (net.glowstone.net.protocol.PlayProtocol)4 HashSet (java.util.HashSet)2 HeldItemMessage (net.glowstone.net.message.play.inv.HeldItemMessage)2 Codec (com.flowpowered.network.Codec)1 Message (com.flowpowered.network.Message)1 Codec (com.flowpowered.networking.Codec)1 Message (com.flowpowered.networking.Message)1 ChannelFuture (io.netty.channel.ChannelFuture)1 KickMessage (net.glowstone.net.message.KickMessage)1 PingMessage (net.glowstone.net.message.play.game.PingMessage)1 LoginProtocol (net.glowstone.net.protocol.LoginProtocol)1 PlayerKickEvent (org.bukkit.event.player.PlayerKickEvent)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1