Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the packet sender thread a daemon so it doesn't block mc exit #13

Merged
merged 4 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 156 additions & 42 deletions build.gradle

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Add your dependencies here

dependencies {
compile("curse.maven:cofh-lib-220333:2388748")
api("curse.maven:cofh-lib-220333:2388748")
}
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
4 changes: 2 additions & 2 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,15 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down
7 changes: 0 additions & 7 deletions repositories.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
// Add any additional repositories for your dependencies here

repositories {
maven {
name = "GTNH Maven"
url = "http://jenkins.usrv.eu:8081/nexus/content/groups/public/"
}
maven {
url "https://cursemaven.com"
}
}
56 changes: 25 additions & 31 deletions src/main/java/buildcraft/BuildCraftMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import java.util.EnumMap;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
Expand Down Expand Up @@ -122,44 +122,36 @@ boolean isValid(EntityPlayer player) {

static class PacketSender implements Runnable {

private Queue<SendRequest> packets = new ConcurrentLinkedQueue<SendRequest>();
private BlockingQueue<SendRequest> packets = new LinkedBlockingQueue<>();

@Override
public void run() {
while (true) {
try {
Thread.sleep(10);
} catch (Exception e) {

}
SendRequest r = packets.take();
net.minecraft.network.Packet p = ThreadSafeUtils
.generatePacketFrom(r.packet, r.source.channels.get(Side.SERVER));
List<EntityPlayerMP> playerList = MinecraftServer.getServer()
.getConfigurationManager().playerEntityList;
for (EntityPlayerMP player : playerList.toArray(new EntityPlayerMP[playerList.size()])) {
if (r.isValid(player)) {
NetHandlerPlayServer handler = player.playerNetServerHandler;
if (handler == null) {
continue;
}

while (!packets.isEmpty()) {
try {
SendRequest r = packets.remove();
net.minecraft.network.Packet p = ThreadSafeUtils
.generatePacketFrom(r.packet, r.source.channels.get(Side.SERVER));
List<EntityPlayerMP> playerList = MinecraftServer.getServer()
.getConfigurationManager().playerEntityList;
for (EntityPlayerMP player : playerList.toArray(new EntityPlayerMP[playerList.size()])) {
if (r.isValid(player)) {
NetHandlerPlayServer handler = player.playerNetServerHandler;
if (handler == null) {
continue;
}

NetworkManager manager = handler.netManager;
if (manager == null || !manager.isChannelOpen()) {
continue;
}

manager.scheduleOutboundPacket(p);
NetworkManager manager = handler.netManager;
if (manager == null || !manager.isChannelOpen()) {
continue;
}

manager.scheduleOutboundPacket(p);
}
} catch (Exception e) {
BCLog.logger.error(
"The BuildCraft packet sender thread raised an exception! Please report to GitHub.");
e.printStackTrace();
}
} catch (Exception e) {
BCLog.logger
.error("The BuildCraft packet sender thread raised an exception! Please report to GitHub.");
e.printStackTrace();
}
}
}
Expand All @@ -170,6 +162,8 @@ public boolean add(SendRequest r) {
}

static {
senderThread.setDaemon(true);
senderThread.setName("BuildCraft packet sender thread");
senderThread.start();
}

Expand Down