Skip to content

Commit

Permalink
Merge pull request #236 from discord-jar/feature/gatewayv3
Browse files Browse the repository at this point in the history
Gateway v3
  • Loading branch information
seailz authored Sep 27, 2023
2 parents 13f10b8 + 7568477 commit a877d2e
Show file tree
Hide file tree
Showing 10 changed files with 776 additions and 732 deletions.
79 changes: 24 additions & 55 deletions src/main/java/com/seailz/discordjar/DiscordJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.seailz.discordjar.command.listeners.slash.SubCommandListener;
import com.seailz.discordjar.events.DiscordListener;
import com.seailz.discordjar.events.EventDispatcher;
import com.seailz.discordjar.gateway.GatewayFactory;
import com.seailz.discordjar.gateway.Gateway;
import com.seailz.discordjar.gateway.GatewayTransportCompressionType;
import com.seailz.discordjar.http.HttpOnlyApplication;
import com.seailz.discordjar.model.api.APIRelease;
Expand Down Expand Up @@ -49,6 +49,7 @@
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.socket.CloseStatus;

import java.io.IOException;
import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -82,7 +83,7 @@ public class DiscordJar {
/**
* Used to manage the gateway connection
*/
private GatewayFactory gatewayFactory;
private Gateway gatewayFactory;
/**
* Stores the logger
*/
Expand Down Expand Up @@ -144,9 +145,9 @@ public class DiscordJar {
private Status status;

public int gatewayConnections = 0;
public List<GatewayFactory> gatewayFactories = new ArrayList<>();
private final List<String> memberCachingDisabledGuilds = new ArrayList<>();
private final GatewayTransportCompressionType gatewayTransportCompressionType;
private final APIVersion apiVersion;

/**
* @deprecated Use {@link DiscordJarBuilder} instead.
Expand Down Expand Up @@ -216,6 +217,7 @@ public DiscordJar(String token, EnumSet<Intent> intents, APIVersion version, boo
this.eventDispatcher = new EventDispatcher(this);
this.token = token;
this.intents = intents;
this.apiVersion = version;
this.cacheTypes = cacheTypes;
new URLS(release, version);
logger = Logger.getLogger("DISCORD.JAR");
Expand Down Expand Up @@ -296,7 +298,11 @@ public DiscordJar(String token, EnumSet<Intent> intents, APIVersion version, boo
this.numShards = numShards;

if (!httpOnly) {
this.gatewayFactory = new GatewayFactory(this, debug, shardId, numShards, gwCompressionType);
this.gatewayFactory = Gateway.builder(this)
.setShardCount(numShards)
.setShardId(shardId)
.setTransportCompressionType(gwCompressionType)
.build();
}

}
Expand Down Expand Up @@ -341,43 +347,10 @@ protected void initiateNoShutdown() {
}, "djar-shutdown-prevention").start();
}

public GatewayFactory getGateway() {
public Gateway getGateway() {
return gatewayFactory;
}

/**
* Kills the gateway connection and destroys the {@link GatewayFactory} instance.
* This method will also initiate garbage collection to avoid memory leaks. This probably shouldn't be used unless in {@link #restartGateway()}.
*/
public void killGateway() {
try {
if (gatewayFactory != null) gatewayFactory.killConnection();
} catch (IOException ignored) {}
gatewayFactory = null;
// init garbage collection to avoid memory leaks
System.gc();
}

/**
* Restarts the gateway connection and creates a new {@link GatewayFactory} instance.
* This will invalidate and destroy the old {@link GatewayFactory} instance.
* This method will also initiate garbage collection to avoid memory leaks.
*
* @see GatewayFactory
* @see #killGateway()
*/
public void restartGateway() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException ignored) {}
killGateway();
try {
gatewayFactory = new GatewayFactory(this, debug, shardId, numShards, gatewayTransportCompressionType);
gatewayFactories.add(gatewayFactory);
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
}

protected void initiateShutdownHooks() {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
Expand All @@ -387,11 +360,7 @@ protected void initiateShutdownHooks() {
throw new RuntimeException(e);
}
if (gatewayFactory != null) {
try {
gatewayFactory.killConnectionNicely();
} catch (IOException e) {
throw new RuntimeException(e);
}
gatewayFactory.disconnect(CloseStatus.GOING_AWAY);
}
}, "djar--shutdown-hook"));
}
Expand All @@ -416,10 +385,6 @@ public void updateVoiceState(VoiceState state) {
}
}

public void setGatewayFactory(GatewayFactory gatewayFactory) {
this.gatewayFactory = gatewayFactory;
}

public List<Bucket> getBuckets() {
return buckets;
}
Expand Down Expand Up @@ -470,12 +435,14 @@ public void disableMemberCachingForGuild(String guildId) {
public void setStatus(@NotNull Status status) {
if (gatewayFactory == null)
throw new IllegalStateException("Cannot set status on an HTTP-only bot. See the constructor for more information.");
JSONObject json = new JSONObject();
json.put("d", status.compile());
json.put("op", 3);
gatewayFactory.queueMessage(json);
gatewayFactory.setStatus(status);
this.status = status;
new Thread(() -> {
JSONObject json = new JSONObject();
json.put("d", status.compile());
json.put("op", 3);
gatewayFactory.queueMessageUntilReady(json);
gatewayFactory.setStatus(status);
this.status = status;
}).start();
}

public Status getStatus() {
Expand Down Expand Up @@ -1554,7 +1521,7 @@ public EnumSet<CacheType> getCacheTypes() {
* <br>The time is in milliseconds.
*/
public List<Long> getGatewayPingHistory() {
return GatewayFactory.pingHistoryMs;
return Gateway.pingHistoryMs;
}

/**
Expand All @@ -1570,5 +1537,7 @@ public Long getAverageGatewayPing() {
return sum / getGatewayPingHistory().size();
}


public APIVersion getApiVersion() {
return apiVersion;
}
}
Loading

0 comments on commit a877d2e

Please sign in to comment.