All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.datasecs.hydra.shared.handler.HydraSession Maven / Gradle / Ivy

There is a newer version: 1.6.5
Show newest version
package de.datasecs.hydra.shared.handler;

import de.datasecs.hydra.shared.protocol.HydraProtocol;
import de.datasecs.hydra.shared.protocol.packets.Packet;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.net.SocketAddress;

/**
 * Created with love by DataSecs on 29.09.2017.
 */
public class HydraSession extends SimpleChannelInboundHandler implements Session {

    private Channel channel;

    private HydraProtocol protocol;

    public HydraSession(Channel channel, HydraProtocol protocol) {
        this.channel = channel;
        this.protocol = protocol;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext context, Packet packet) {
        protocol.callPacketListener(packet, this);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) {
        protocol.callSessionListener(false, this);
        protocol.removeSession(this);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
    }

    @Override
    public void send(Packet packet) {
        channel.writeAndFlush(packet);
    }

    @Override
    public void close() {
        channel.disconnect();
    }

    @Override
    public boolean isConnected() {
        return channel.isActive();
    }

    @Override
    public SocketAddress getAddress() {
        SocketAddress address = channel.remoteAddress();
        return address == null ? channel.localAddress() : address;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy