RuneHive-Game
Loading...
Searching...
No Matches
ChannelHandler.java
Go to the documentation of this file.
1package com.runehive.net;
2
3import com.runehive.Config;
4import com.runehive.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
12/**
13 * A {@link SimpleChannelInboundHandler} implementation for re-routing
14 * channel-events to its bound {@link Session}.
15 *
16 * @author nshusa
17 */
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}
The class that contains setting-related constants for the server.
Definition Config.java:24
static final ObjectSet< String > IGNORED_EXCEPTIONS
The list of exceptions that are ignored and discarded by the.
Definition Config.java:85
static final AttributeKey< Session > SESSION_KEY
The session key.
Definition Config.java:90
A SimpleChannelInboundHandler implementation for re-routing channel-events to its bound Session.
void channelInactive(ChannelHandlerContext ctx)
void exceptionCaught(ChannelHandlerContext ctx, Throwable e)
void userEventTriggered(ChannelHandlerContext ctx, Object evt)
void channelRead0(ChannelHandlerContext ctx, Object o)
Represents a session between a user and the server.
Definition Session.java:14