Skip to content
This repository was archived by the owner on Mar 7, 2026. It is now read-only.

Commit f6cfeb4

Browse files
authored
Release 1.6.1
Release 1.6.1
2 parents 2aed22b + faf5a05 commit f6cfeb4

8 files changed

Lines changed: 41 additions & 86 deletions

File tree

Cacher.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
Transport=nio
21
Threads=2
32
StatsPPS=true
43
StatsbPS=true

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.aayushatharva</groupId>
88
<artifactId>SourceEngineQueryCacher</artifactId>
9-
<version>1.6.0</version>
9+
<version>1.6.1</version>
1010

1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

src/main/java/com/aayushatharva/sourcecenginequerycacher/Main.java

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@
99
import io.netty.bootstrap.Bootstrap;
1010
import io.netty.buffer.ByteBufAllocator;
1111
import io.netty.buffer.PooledByteBufAllocator;
12+
import io.netty.channel.AdaptiveRecvByteBufAllocator;
1213
import io.netty.channel.ChannelFuture;
1314
import io.netty.channel.ChannelFutureListener;
1415
import io.netty.channel.ChannelOption;
1516
import io.netty.channel.EventLoopGroup;
16-
import io.netty.channel.FixedRecvByteBufAllocator;
1717
import io.netty.channel.epoll.Epoll;
1818
import io.netty.channel.epoll.EpollDatagramChannel;
1919
import io.netty.channel.epoll.EpollEventLoopGroup;
20-
import io.netty.channel.nio.NioEventLoopGroup;
2120
import io.netty.channel.socket.InternetProtocolFamily;
22-
import io.netty.channel.socket.nio.NioDatagramChannel;
2321
import io.netty.channel.unix.UnixChannelOption;
2422
import org.apache.logging.log4j.LogManager;
2523
import org.apache.logging.log4j.Logger;
@@ -33,7 +31,7 @@
3331
public final class Main {
3432
private static final Logger logger = LogManager.getLogger(Main.class);
3533

36-
public static final ByteBufAllocator BYTE_BUF_ALLOCATOR = new PooledByteBufAllocator(true);
34+
public static final ByteBufAllocator BYTE_BUF_ALLOCATOR = PooledByteBufAllocator.DEFAULT;
3735
public static EventLoopGroup eventLoopGroup;
3836
private static Stats stats;
3937
private static CacheCleaner cacheCleaner;
@@ -48,53 +46,39 @@ public static void main(String[] args) {
4846
Config.setup(args);
4947

5048
// Use Epoll when available
51-
if (Config.Transport.equalsIgnoreCase("epoll")) {
52-
if (Epoll.isAvailable()) {
53-
eventLoopGroup = new EpollEventLoopGroup(Config.Threads);
54-
} else {
55-
// Epoll is requested but Epoll is not available so we'll throw error and shut down.
56-
System.err.println("Epoll Transport is not available, shutting down...");
57-
System.exit(1);
58-
}
59-
} else if (Config.Transport.equalsIgnoreCase("nio")) {
60-
eventLoopGroup = new NioEventLoopGroup(Config.Threads);
49+
if (Epoll.isAvailable()) {
50+
eventLoopGroup = new EpollEventLoopGroup(Config.Threads);
6151
} else {
62-
System.err.println("Invalid Transport Type: " + Config.Transport + ", shutting down...");
52+
// Epoll is requested but Epoll is not available so we'll throw error and shut down.
53+
System.err.println("Epoll Transport is not available, shutting down...");
6354
System.exit(1);
6455
}
6556

6657
List<ChannelFuture> channelFutureList = new ArrayList<>();
6758

6859
Bootstrap bootstrap = new Bootstrap()
6960
.group(eventLoopGroup)
70-
.channelFactory(() -> {
71-
if (Config.Transport.equalsIgnoreCase("epoll")) {
72-
return new EpollDatagramChannel(InternetProtocolFamily.IPv4);
73-
} else {
74-
return new NioDatagramChannel(InternetProtocolFamily.IPv4);
75-
}
76-
})
61+
.channelFactory(() -> new EpollDatagramChannel(InternetProtocolFamily.IPv4))
7762
.option(ChannelOption.ALLOCATOR, BYTE_BUF_ALLOCATOR)
7863
.option(ChannelOption.SO_SNDBUF, Config.SendBufferSize)
7964
.option(ChannelOption.SO_RCVBUF, Config.ReceiveBufferSize)
80-
.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(Config.FixedReceiveAllocatorBufferSize))
65+
.option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator())
8166
.option(UnixChannelOption.SO_REUSEPORT, true)
8267
.handler(new Handler());
8368

8469
for (int i = 0; i < Config.Threads; i++) {
8570
// Bind and Start Server
86-
ChannelFuture channelFuture = bootstrap.bind(Config.LocalServer.getAddress(), Config.LocalServer.getPort())
87-
.addListener((ChannelFutureListener) future -> {
88-
if (future.isSuccess()) {
89-
logger.atInfo().log("Server Started on Address: {}:{}",
90-
((InetSocketAddress) future.channel().localAddress()).getAddress().getHostAddress(),
91-
((InetSocketAddress) future.channel().localAddress()).getPort());
92-
} else {
93-
logger.error("Caught Error While Starting Server", future.cause());
94-
System.err.println("Shutting down...");
95-
System.exit(1);
96-
}
97-
});
71+
ChannelFuture channelFuture = bootstrap.bind(Config.LocalServer.getAddress(), Config.LocalServer.getPort()).addListener((ChannelFutureListener) future -> {
72+
if (future.isSuccess()) {
73+
logger.atInfo().log("Server Started on Address: {}:{}",
74+
((InetSocketAddress) future.channel().localAddress()).getAddress().getHostAddress(),
75+
((InetSocketAddress) future.channel().localAddress()).getPort());
76+
} else {
77+
logger.error("Caught Error While Starting Server", future.cause());
78+
System.err.println("Shutting down...");
79+
System.exit(1);
80+
}
81+
});
9882

9983
channelFutureList.add(channelFuture);
10084
}

src/main/java/com/aayushatharva/sourcecenginequerycacher/gameserver/a2sinfo/InfoClient.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
import io.netty.channel.ChannelOption;
99
import io.netty.channel.FixedRecvByteBufAllocator;
1010
import io.netty.channel.epoll.EpollDatagramChannel;
11-
import io.netty.channel.socket.DatagramPacket;
12-
import io.netty.channel.socket.InternetProtocolFamily;
13-
import io.netty.channel.socket.nio.NioDatagramChannel;
1411
import org.apache.logging.log4j.LogManager;
1512
import org.apache.logging.log4j.Logger;
1613

@@ -30,23 +27,17 @@ public void run() {
3027

3128
Bootstrap bootstrap = new Bootstrap()
3229
.group(Main.eventLoopGroup)
33-
.channelFactory(() -> {
34-
if (Config.Transport.equalsIgnoreCase("epoll")) {
35-
return new EpollDatagramChannel(InternetProtocolFamily.IPv4);
36-
} else {
37-
return new NioDatagramChannel(InternetProtocolFamily.IPv4);
38-
}
39-
})
30+
.channelFactory(EpollDatagramChannel::new)
4031
.option(ChannelOption.ALLOCATOR, Main.BYTE_BUF_ALLOCATOR)
4132
.option(ChannelOption.SO_SNDBUF, Config.SendBufferSize)
4233
.option(ChannelOption.SO_RCVBUF, Config.ReceiveBufferSize)
4334
.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(Config.FixedReceiveAllocatorBufferSize))
4435
.handler(new InfoHandler());
4536

46-
Channel channel = bootstrap.bind(0).sync().channel();
37+
Channel channel = bootstrap.connect(Config.GameServer).sync().channel();
4738

4839
while (keepRunning) {
49-
channel.writeAndFlush(new DatagramPacket(Packets.A2S_INFO_REQUEST.retainedDuplicate(), Config.GameServer)).sync();
40+
channel.writeAndFlush(Packets.A2S_INFO_REQUEST.retainedDuplicate()).sync();
5041
sleep(Config.GameUpdateInterval);
5142
}
5243

src/main/java/com/aayushatharva/sourcecenginequerycacher/gameserver/a2sinfo/InfoHandler.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package com.aayushatharva.sourcecenginequerycacher.gameserver.a2sinfo;
22

33
import com.aayushatharva.sourcecenginequerycacher.utils.CacheHub;
4+
import io.netty.buffer.ByteBuf;
45
import io.netty.channel.ChannelHandlerContext;
56
import io.netty.channel.SimpleChannelInboundHandler;
6-
import io.netty.channel.socket.DatagramPacket;
77
import org.apache.logging.log4j.LogManager;
88
import org.apache.logging.log4j.Logger;
99

10-
final class InfoHandler extends SimpleChannelInboundHandler<DatagramPacket> {
10+
final class InfoHandler extends SimpleChannelInboundHandler<ByteBuf> {
1111

1212
private static final Logger logger = LogManager.getLogger(InfoHandler.class);
1313

1414
@Override
15-
protected void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket datagramPacket) {
15+
protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) {
1616
// Set new Packet Data
17-
CacheHub.A2S_INFO.clear().writeBytes(datagramPacket.content());
17+
CacheHub.A2S_INFO.clear();
18+
CacheHub.A2S_INFO.writeBytes(byteBuf);
1819

1920
logger.debug("New A2SInfo Update Cached Successfully");
2021
}

src/main/java/com/aayushatharva/sourcecenginequerycacher/gameserver/a2splayer/PlayerClient.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
import io.netty.channel.ChannelOption;
1010
import io.netty.channel.FixedRecvByteBufAllocator;
1111
import io.netty.channel.epoll.EpollDatagramChannel;
12-
import io.netty.channel.socket.DatagramPacket;
13-
import io.netty.channel.socket.InternetProtocolFamily;
14-
import io.netty.channel.socket.nio.NioDatagramChannel;
1512
import org.apache.logging.log4j.LogManager;
1613
import org.apache.logging.log4j.Logger;
1714

@@ -31,23 +28,17 @@ public void run() {
3128

3229
Bootstrap bootstrap = new Bootstrap()
3330
.group(Main.eventLoopGroup)
34-
.channelFactory(() -> {
35-
if (Config.Transport.equalsIgnoreCase("epoll")) {
36-
return new EpollDatagramChannel(InternetProtocolFamily.IPv4);
37-
} else {
38-
return new NioDatagramChannel(InternetProtocolFamily.IPv4);
39-
}
40-
})
31+
.channelFactory(EpollDatagramChannel::new)
4132
.option(ChannelOption.ALLOCATOR, Main.BYTE_BUF_ALLOCATOR)
4233
.option(ChannelOption.SO_SNDBUF, Config.SendBufferSize)
4334
.option(ChannelOption.SO_RCVBUF, Config.ReceiveBufferSize)
4435
.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(Config.FixedReceiveAllocatorBufferSize))
4536
.handler(new PlayerHandler());
4637

47-
Channel channel = bootstrap.bind(0).sync().channel();
38+
Channel channel = bootstrap.connect(Config.GameServer).sync().channel();
4839

4940
while (keepRunning) {
50-
channel.writeAndFlush(new DatagramPacket(Packets.A2S_PLAYER_CHALLENGE_REQUEST_2.retainedDuplicate(), Config.GameServer)).sync();
41+
channel.writeAndFlush(Packets.A2S_PLAYER_CHALLENGE_REQUEST_2.retainedDuplicate()).sync();
5142
sleep(Config.GameUpdateInterval);
5243
}
5344

src/main/java/com/aayushatharva/sourcecenginequerycacher/gameserver/a2splayer/PlayerHandler.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,27 @@
1010
import org.apache.logging.log4j.LogManager;
1111
import org.apache.logging.log4j.Logger;
1212

13-
final class PlayerHandler extends SimpleChannelInboundHandler<DatagramPacket> {
13+
final class PlayerHandler extends SimpleChannelInboundHandler<ByteBuf> {
1414

1515
private static final Logger logger = LogManager.getLogger(PlayerHandler.class);
1616

1717
@Override
18-
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket datagramPacket) {
18+
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) {
1919

20-
if (ByteBufUtil.equals(Packets.A2S_PLAYER_CHALLENGE_RESPONSE, datagramPacket.content().slice(0, 5))) {
21-
ByteBuf byteBuf = ctx.alloc().buffer()
20+
if (ByteBufUtil.equals(Packets.A2S_PLAYER_CHALLENGE_RESPONSE, byteBuf.slice(0, 5))) {
21+
ByteBuf responseBuf = ctx.alloc().buffer()
2222
.writeBytes(Packets.A2S_PLAYER_REQUEST_HEADER.retainedDuplicate())
23-
.writeBytes(datagramPacket.content().slice(5, 4));
23+
.writeBytes(byteBuf.slice(5, 4));
2424

25-
ctx.channel().writeAndFlush(new DatagramPacket(byteBuf, datagramPacket.sender()));
26-
} else if (ByteBufUtil.equals(Packets.A2S_PLAYER_RESPONSE_HEADER, datagramPacket.content().slice(0, 5))) {
25+
ctx.channel().writeAndFlush(responseBuf);
26+
} else if (ByteBufUtil.equals(Packets.A2S_PLAYER_RESPONSE_HEADER, byteBuf.slice(0, 5))) {
2727
// Set new Packet Data
28-
CacheHub.A2S_PLAYER.clear().writeBytes(datagramPacket.content());
28+
CacheHub.A2S_PLAYER.clear();
29+
CacheHub.A2S_PLAYER.writeBytes(byteBuf);
2930

3031
logger.atDebug().log("New A2SPlayer Update Cached Successfully");
3132
} else {
32-
logger.atError().log("Received unsupported A2S Player Response from Game Server: {}", ByteBufUtil.hexDump(datagramPacket.content()));
33+
logger.atError().log("Received unsupported A2S Player Response from Game Server: {}", ByteBufUtil.hexDump(byteBuf));
3334
}
3435
}
3536
}

src/main/java/com/aayushatharva/sourcecenginequerycacher/utils/Config.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ public final class Config {
1515
private static final Logger logger = LogManager.getLogger(Config.class);
1616
private static final Options options;
1717

18-
/**
19-
* Cacher Server Transport Type
20-
*/
21-
public static String Transport = "Nio";
22-
2318
/**
2419
* Cacher Server Threads
2520
*/
@@ -73,7 +68,6 @@ public final class Config {
7368
/*General Configuration*/
7469
.addOption("h", "help", false, "Display Usages")
7570
.addOption("c", "config", true, "Configuration File Path")
76-
.addOption("t", "transport", true, "Set Transport to be used [Epoll or Nio]")
7771
.addOption("w", "threads", true, "Number of Threads")
7872
.addOption("p", "ppsStats", false, "Enable Packets per Second Stats")
7973
.addOption("b", "bpsStats", false, "Enable Bits per Second Stats")
@@ -119,10 +113,6 @@ public static void setup(String[] args) throws ParseException, IOException {
119113
parseConfigFile(cmd.getOptionValue("config"));
120114
} else {
121115

122-
if (cmd.getOptionValue("transport") != null) {
123-
Transport = cmd.getOptionValue("transport");
124-
}
125-
126116
if (cmd.getOptionValue("threads") != null) {
127117
Threads = Integer.parseInt(cmd.getOptionValue("threads"));
128118
}
@@ -206,7 +196,6 @@ private static void parseConfigFile(String path) throws IOException {
206196
Data.load(new FileInputStream(path));
207197

208198
// Load all Data
209-
Transport = Data.getProperty("Transport", Transport);
210199
Threads = Integer.parseInt(Data.getProperty("Threads", String.valueOf(Threads)));
211200
Stats_PPS = Boolean.parseBoolean(Data.getProperty("StatsPPS", String.valueOf(Stats_PPS)));
212201
Stats_bPS = Boolean.parseBoolean(Data.getProperty("StatsbPS", String.valueOf(Stats_PPS)));
@@ -238,7 +227,6 @@ private static void parseConfigFile(String path) throws IOException {
238227

239228
private static void displayConfig() {
240229
logger.atDebug().log("----------------- CONFIGURATION -----------------");
241-
logger.atDebug().log("Transport: " + Transport);
242230
logger.atDebug().log("Threads: " + Threads);
243231
logger.atDebug().log("PPS: " + Stats_PPS);
244232
logger.atDebug().log("bPS: " + Stats_bPS);

0 commit comments

Comments
 (0)