RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
ChannelHandler.java
1package com.osroyale.net;
2
3import com.osroyale.Config;
4import com.osroyale.net.session.Session;
5import io.netty.channel.ChannelHandlerContext;
6import io.netty.channel.SimpleChannelInboundHandler;
7import io.netty.handler.timeout.IdleState;
8import io.netty.handler.timeout.IdleStateEvent;
9import org.apache.logging.log4j.LogManager;
10import org.apache.logging.log4j.Logger;
11
18public final class ChannelHandler extends SimpleChannelInboundHandler<Object> {
19
20 private static final Logger logger = LogManager.getLogger(ChannelHandler.class);
21
22 @Override
23 protected void channelRead0(ChannelHandlerContext ctx, Object o) {
24 try {
25 final Session session = ctx.channel().attr(Config.SESSION_KEY).get();
26 if (session != null) {
27 session.handleClientPacket(o);
28 }
29 } catch (Exception ex) {
30 logger.error("Error reading channel!", ex);
31 }
32 }
33
34 @Override
35 public void channelInactive(ChannelHandlerContext ctx) {
36 final Session session = ctx.channel().attr(Config.SESSION_KEY).get();
37 if (session != null) {
38 session.close();
39 }
40 }
41
42 @Override
43 public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
44 if (evt instanceof IdleStateEvent) {
45 IdleStateEvent event = (IdleStateEvent) evt;
46 if (event.state() == IdleState.READER_IDLE) {
47 ctx.channel().close();
48 }
49 }
50 }
51
52 @Override
53 public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) {
54 if (!Config.IGNORED_EXCEPTIONS.contains(e.getMessage())) {
55 logger.error("Exception caught upstream!", e);
56 }
57
58 final Session session = ctx.channel().attr(Config.SESSION_KEY).get();
59 if (session != null) {
60 session.close();
61 }
62 }
63
64}
static final ObjectSet< String > IGNORED_EXCEPTIONS
Definition Config.java:126
static final AttributeKey< Session > SESSION_KEY
Definition Config.java:131