RuneHive-Game
Loading...
Searching...
No Matches
GameSession.java
Go to the documentation of this file.
1package com.runehive.net.session;
2
3import com.runehive.Config;
4import com.runehive.game.world.World;
5import com.runehive.game.world.entity.mob.player.Player;
6import com.runehive.net.packet.GamePacket;
7import com.runehive.net.packet.PacketRepository;
8import io.netty.channel.Channel;
9import io.netty.channel.ChannelFuture;
10import org.apache.logging.log4j.LogManager;
11import org.apache.logging.log4j.Logger;
12import org.jctools.queues.MessagePassingQueue;
13import org.jctools.queues.MpscArrayQueue;
14
15/**
16 * Represents a {@link Session} when a {@link Player}
17 * has been authenticated and active in the game world.
18 *
19 * @author nshusa
20 */
21public final class GameSession extends Session {
22
23 private static final Logger logger = LogManager.getLogger(GameSession.class);
24
25 private final Player player;
26
27 private final MessagePassingQueue<GamePacket> incomingPackets
28 = new MpscArrayQueue<>(Config.CLIENT_PACKET_THRESHOLD);
29
30 private final MessagePassingQueue<GamePacket> outgoingPackets
31 = new MpscArrayQueue<>(Config.SERVER_PACKET_THRESHOLD);
32
34 super(channel);
35 this.player = player;
36 }
37
38 @Override
39 public void handleClientPacket(Object o) {
40 if (o instanceof GamePacket) {
42 }
43 }
44
45 @Override
46 protected void onClose(ChannelFuture f) {
48 }
49
50 private void queueClientPacket(final GamePacket packet) {
52 }
53
54 public void processClientPackets() {
55 final MessagePassingQueue<GamePacket> incomingPackets = this.incomingPackets;
56 final Player player = this.player;
57
58 for (int i = 0; i < Config.CLIENT_PACKET_THRESHOLD; i++) {
59 final GamePacket packet = incomingPackets.poll();
60 if (packet == null) break;
61
62 try {
64 } catch (Exception ex) {
65 logger.error(String.format("error processing client packet for %s", player), ex);
66 } finally {
67 packet.release();
68 }
69 }
70 }
71
75
77 final Channel channel = this.channel;
78 final boolean channelActive = channel.isActive();
79
80 final MessagePassingQueue<GamePacket> outgoingPackets = this.outgoingPackets;
81
82 int count = 0;
83 for (; count < Config.SERVER_PACKET_THRESHOLD; count++) {
84 final GamePacket packet = outgoingPackets.poll();
85 if (packet == null) break;
86 if (channelActive) {
87 try {
88 channel.write(packet, channel.voidPromise());
89 } catch (final Exception ex) {
90 logger.error(String.format("error writing packet %s for %s", packet, player));
91 }
92 } else {
93 if (packet.refCnt() > 0) {
94 packet.release();
95 }
96 }
97 }
98 if (channelActive && count > 0) {
99 channel.flush();
100 }
101 }
102
103 public Player getPlayer() {
104 return player;
105 }
106
107}
The class that contains setting-related constants for the server.
Definition Config.java:24
static final int CLIENT_PACKET_THRESHOLD
The amount of client (incoming) packets that can be handled by the server each tick.
Definition Config.java:60
static final int SERVER_PACKET_THRESHOLD
The amount of server (outgoing) packets that can be written by the server each tick.
Definition Config.java:64
Represents the game world.
Definition World.java:46
static void queueLogout(Player player)
Handles queueing the player logouts.
Definition World.java:138
This class represents a character controlled by a player.
Definition Player.java:125
Represents a single game packet.
The repository that stores packets sizes and listeners for how to execute the packets.
static void sendToListener(Player player, GamePacket packet)
void handleClientPacket(Object o)
The method that is called when the client sends packets to the server.
final MessagePassingQueue< GamePacket > incomingPackets
GameSession(Channel channel, Player player)
void queueClientPacket(final GamePacket packet)
void queueServerPacket(GamePacket packet)
final MessagePassingQueue< GamePacket > outgoingPackets
void onClose(ChannelFuture f)
The method called after a session has been closed.
final Channel channel
The channel attached to this session.
Definition Session.java:19
Session(Channel channel)
Creates a new Session.
Definition Session.java:32