RuneHive-Game
Loading...
Searching...
No Matches
Session.java
Go to the documentation of this file.
1package com.runehive.net.session;
2
3import com.runehive.Config;
4import io.netty.channel.Channel;
5import io.netty.channel.ChannelFuture;
6
7import java.net.InetSocketAddress;
8
9/**
10 * Represents a session between a user and the server.
11 *
12 * @author nshusa
13 */
14public class Session {
15
16 /**
17 * The channel attached to this session.
18 */
19 protected final Channel channel;
20
21 /**
22 * The users host address.
23 */
24 protected final String host;
25
26 /**
27 * Creates a new {@link Session}.
28 *
29 * @param channel
30 * The channel that is attached to this session.
31 */
32 public Session(Channel channel) {
33 this.channel = channel;
34 this.host = ((InetSocketAddress) channel.remoteAddress()).getAddress().getHostAddress();
35 }
36
37 /**
38 * The method to close this session.
39 */
40 public final void close() {
41 onClose(channel.close());
42 }
43
44 /**
45 * The method called after a session has been closed.
46 *
47 * @param future
48 * The result of the session being closed.
49 */
50 protected void onClose(ChannelFuture future) {
51
52 }
53
54 /**
55 * The method that is called when the client sends packets to the server.
56 *
57 * @param o
58 * The vague object packet.
59 */
60 public void handleClientPacket(Object o) {
61
62 }
63
64 /**
65 * Gets the underlying {@link Channel} for this {@link Session}.
66 *
67 * @return The underlying channel.
68 */
69 public Channel getChannel() {
70 return channel;
71 }
72
73 /**
74 * Gets the users host address
75 */
76 public String getHost() {
77 final Channel channel = getChannel();
78 if (channel != null && channel.hasAttr(Config.SOURCE_ADDRESS)) {
79 return channel.attr(Config.SOURCE_ADDRESS).get();
80 }
81 return host;
82 }
83
84}
The class that contains setting-related constants for the server.
Definition Config.java:24
static final AttributeKey< String > SOURCE_ADDRESS
Definition Config.java:92
final void close()
The method to close this session.
Definition Session.java:40
String getHost()
Gets the users host address.
Definition Session.java:76
final Channel channel
The channel attached to this session.
Definition Session.java:19
Session(Channel channel)
Creates a new Session.
Definition Session.java:32
Channel getChannel()
Gets the underlying Channel for this Session.
Definition Session.java:69
void handleClientPacket(Object o)
The method that is called when the client sends packets to the server.
Definition Session.java:60
final String host
The users host address.
Definition Session.java:24
void onClose(ChannelFuture future)
The method called after a session has been closed.
Definition Session.java:50