RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
GameSession.java
1package com.osroyale.net.session;
2
3import com.osroyale.Config;
4import com.osroyale.game.world.World;
5import com.osroyale.game.world.entity.mob.player.Player;
6import com.osroyale.net.packet.GamePacket;
7import com.osroyale.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
55
56public final class GameSession extends Session {
57
58 private static final Logger logger = LogManager.getLogger(GameSession.class);
59
60 private final Player player;
61
62 private final MessagePassingQueue<GamePacket> incomingPackets
63 = new MpscArrayQueue<>(Config.CLIENT_PACKET_THRESHOLD);
64
65 private final MessagePassingQueue<GamePacket> outgoingPackets
66 = new MpscArrayQueue<>(Config.SERVER_PACKET_THRESHOLD);
67
68 GameSession(Channel channel, Player player) {
69 super(channel);
70 this.player = player;
71 }
72
73 @Override
74 public void handleClientPacket(Object o) {
75 if (o instanceof GamePacket) {
76 queueClientPacket((GamePacket) o);
77 }
78 }
79
80 @Override
81 protected void onClose(ChannelFuture f) {
82 World.queueLogout(player);
83 }
84
85 private void queueClientPacket(final GamePacket packet) {
86 incomingPackets.offer(packet);
87 }
88
89 public void processClientPackets() {
90 final MessagePassingQueue<GamePacket> incomingPackets = this.incomingPackets;
91 final Player player = this.player;
92
93 for (int i = 0; i < Config.CLIENT_PACKET_THRESHOLD; i++) {
94 final GamePacket packet = incomingPackets.poll();
95 if (packet == null) break;
96
97 try {
98 PacketRepository.sendToListener(player, packet);
99 } catch (Exception ex) {
100 logger.error(String.format("error processing client packet for %s", player), ex);
101 } finally {
102 packet.release();
103 }
104 }
105 }
106
107 public void queueServerPacket(GamePacket packet) {
108 outgoingPackets.offer(packet);
109 }
110
111 public void processServerPacketQueue() {
112 final Channel channel = this.channel;
113 final boolean channelActive = channel.isActive();
114
115 final MessagePassingQueue<GamePacket> outgoingPackets = this.outgoingPackets;
116
117 int count = 0;
118 for (; count < Config.SERVER_PACKET_THRESHOLD; count++) {
119 final GamePacket packet = outgoingPackets.poll();
120 if (packet == null) break;
121 if (channelActive) {
122 try {
123 channel.write(packet, channel.voidPromise());
124 } catch (final Exception ex) {
125 logger.error(String.format("error writing packet %s for %s", packet, player));
126 }
127 } else {
128 if (packet.refCnt() > 0) {
129 packet.release();
130 }
131 }
132 }
133 if (channelActive && count > 0) {
134 channel.flush();
135 }
136 }
137
138 public Player getPlayer() {
139 return player;
140 }
141
142}
static final int CLIENT_PACKET_THRESHOLD
Definition Config.java:101
static final int SERVER_PACKET_THRESHOLD
Definition Config.java:105
static void queueLogout(Player player)
Definition World.java:175