Skip to content

Commit

Permalink
Moved CloseCode enum to own class and updated documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Feb 20, 2017
1 parent 5a352da commit 0621b7c
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 73 deletions.
13 changes: 7 additions & 6 deletions src/main/java/net/dv8tion/jda/core/events/DisconnectEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import com.neovisionaries.ws.client.WebSocketFrame;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.requests.WebSocketClient;
import net.dv8tion.jda.core.requests.CloseCode;

import java.time.OffsetDateTime;

Expand All @@ -27,7 +27,8 @@
* unless {@link net.dv8tion.jda.core.JDABuilder#setAutoReconnect(boolean) JDABuilder.setAutoReconnect(Boolean)}
* has been provided {@code false}!
*
* <p>When reconnecting was successful a {@link net.dv8tion.jda.core.events.ReconnectedEvent RecconectEvent} is fired
* <p>When reconnecting was successful either a {@link net.dv8tion.jda.core.events.ReconnectedEvent ReconnectEvent}
* or a {@link net.dv8tion.jda.core.events.ResumedEvent ResumedEvent} is fired
*/
public class DisconnectEvent extends Event
{
Expand All @@ -47,14 +48,14 @@ public DisconnectEvent(JDA api, WebSocketFrame serverCloseFrame, WebSocketFrame
}

/**
* Possibly-null {@link net.dv8tion.jda.core.requests.WebSocketClient.CloseCode CloseCode}
* Possibly-null {@link net.dv8tion.jda.core.requests.CloseCode CloseCode}
* representing the meaning for this DisconnectEvent
*
* @return Possibly-null {@link net.dv8tion.jda.core.requests.WebSocketClient.CloseCode CloseCode}
* @return Possibly-null {@link net.dv8tion.jda.core.requests.CloseCode CloseCode}
*/
public WebSocketClient.CloseCode getCloseCode()
public CloseCode getCloseCode()
{
return serverCloseFrame != null ? WebSocketClient.CloseCode.from(serverCloseFrame.getCloseCode()) : null;
return serverCloseFrame != null ? CloseCode.from(serverCloseFrame.getCloseCode()) : null;
}

public WebSocketFrame getServiceCloseFrame()
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/net/dv8tion/jda/core/events/ShutdownEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package net.dv8tion.jda.core.events;

import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.requests.WebSocketClient;
import net.dv8tion.jda.core.requests.CloseCode;

import java.time.OffsetDateTime;

Expand Down Expand Up @@ -49,23 +49,23 @@ public OffsetDateTime getShutdownTime()
}

/**
* Possibly-null {@link net.dv8tion.jda.core.requests.WebSocketClient.CloseCode CloseCode}
* Possibly-null {@link net.dv8tion.jda.core.requests.CloseCode CloseCode}
* representing the meaning for this ShutdownEvent.
* <br>The raw close code can be retrieved from {@link #getCode()}
* <br>If this is {@code null}, JDA does not know what the meaning for the connection loss was.
*
* @return Possibly-null {@link net.dv8tion.jda.core.requests.WebSocketClient.CloseCode CloseCode}
* @return Possibly-null {@link net.dv8tion.jda.core.requests.CloseCode CloseCode}
*/
public WebSocketClient.CloseCode getCloseCode()
public CloseCode getCloseCode()
{
return WebSocketClient.CloseCode.from(code);
return CloseCode.from(code);
}

/**
* The server close code that was in the disconnect close frame
* of this JDA instance.
*
* @return int close code of the
* @return int close code of the Server Close-Frame
*/
public int getCode()
{
Expand Down
111 changes: 111 additions & 0 deletions src/main/java/net/dv8tion/jda/core/requests/CloseCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2015-2017 Austin Keener & Michael Ritter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.core.requests;

/**
* Constants representing {@link net.dv8tion.jda.core.requests.WebSocketClient WebSocketClient} close codes
* with association to an explaining message.
* <br>This was inspired from the <a target="_blank" href="https://discordapp.com/developers/docs/topics/gateway#disconnections">official documentation</a>
*/
public enum CloseCode
{
GRACEFUL_CLOSE( 1000, "The connection was closed gracefully or your heartbeats timed out."),
CLOUD_FLARE_LOAD( 1001, "The connection was closed due to CloudFlare load balancing."),
INTERNAL_SERVER_ERROR(1006, "Something broke on the remote's end, sorry 'bout that... Try reconnecting!"),
UNKNOWN_ERROR( 4000, "The server is not sure what went wrong. Try reconnecting?"),
UNKNOWN_OPCODE( 4001, "You sent an invalid Gateway OP Code. Don't do that!"),
DECODE_ERROR( 4002, "You sent an invalid payload to the server. Don't do that!"),
NOT_AUTHENTICATED( 4003, "You sent a payload prior to identifying."),
AUTHENTICATION_FAILED(4004, "The account token sent with your identify payload is incorrect.", false),
ALREADY_AUTHENTICATED(4005, "You sent more than one identify payload. Don't do that!"),
INVALID_SEQ( 4007, "The sent sent when resuming the session was invalid. Reconnect and start a new session."),
RATE_LIMITED( 4008, "Woah nelly! You're sending payloads to us too quickly. Slow it down!"),
SESSION_TIMEOUT( 4009, "Your session timed out. Reconnect and start a new one."),
INVALID_SHARD( 4010, "You sent an invalid shard when identifying.", false),
SHARDING_REQUIRED( 4011, "The session would have handled too many guilds - you are required to shard your connection in order to connect.", false);

private final int code;
private final boolean isReconnect;
private final String meaning;

CloseCode(int code, String meaning)
{
this(code, meaning, true);
}

CloseCode(int code, String meaning, boolean isReconnect)
{
this.code = code;
this.meaning = meaning;
this.isReconnect = isReconnect;
}

/**
* The integer code in the form of {@code 4xxx}/{@code 1xxx}
*
* @return The integer representation for this CloseCode
*/
public int getCode()
{
return code;
}

/**
* The message which further explains the reason
* for this close code's occurrence
*
* @return The reason for this close
*/
public String getMeaning()
{
return meaning;
}

/**
* Whether the {@link net.dv8tion.jda.core.requests.WebSocketClient WebSocketClient}
* will attempt to reconnect when this close code appears
*
* @return Whether the WebSocketClient will attempt to reconnect
*/
public boolean isReconnect()
{
return isReconnect;
}

@Override
public String toString()
{
return "CloseCode[" + code + " / " + meaning + "]";
}

/**
* Retrieves the CloseCode representation
* for the specified integer close code
*
* @param code
* The close code to match
*
* @return The CloseCode field matching the specified integer
* or {@code null} if no match was found
*/
public static CloseCode from(int code)
{
for (CloseCode c : values())
if (c.code == code) return c;
return null;
}
}
61 changes: 0 additions & 61 deletions src/main/java/net/dv8tion/jda/core/requests/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -888,66 +888,5 @@ protected String handleInternally(JSONObject content)
}
}

public enum CloseCode
{
GRACEFUL_CLOSE( 1000, "The connection was closed gracefully or your heartbeats timed out."),
CLOUD_FLARE_LOAD( 1001, "The connection was closed due to CloudFlare load balancing."),
INTERNAL_SERVER_ERROR(1006, "Something broke on the remote's end, sorry 'bout that... Try reconnecting!"),
UNKNOWN_ERROR( 4000, "The server is not sure what went wrong. Try reconnecting?"),
UNKNOWN_OPCODE( 4001, "You sent an invalid Gateway OP Code. Don't do that!"),
DECODE_ERROR( 4002, "You sent an invalid payload to the server. Don't do that!"),
NOT_AUTHENTICATED( 4003, "You sent a payload prior to identifying."),
AUTHENTICATION_FAILED(4004, "The account token sent with your identify payload is incorrect.", false),
ALREADY_AUTHENTICATED(4005, "You sent more than one identify payload. Don't do that!"),
INVALID_SEQ( 4007, "The sent sent when resuming the session was invalid. Reconnect and start a new session."),
RATE_LIMITED( 4008, "Woah nelly! You're sending payloads to us too quickly. Slow it down!"),
SESSION_TIMEOUT( 4009, "Your session timed out. Reconnect and start a new one."),
INVALID_SHARD( 4010, "You sent an invalid shard when identifying.", false),
SHARDING_REQUIRED( 4011, "The session would have handled too many guilds - you are required to shard your connection in order to connect.", false);

private final int code;
private final boolean isReconnect;
private final String meaning;

CloseCode(int code, String meaning)
{
this(code, meaning, true);
}

CloseCode(int code, String meaning, boolean isReconnect)
{
this.code = code;
this.meaning = meaning;
this.isReconnect = isReconnect;
}

public int getCode()
{
return code;
}

public String getMeaning()
{
return meaning;
}

public boolean isReconnect()
{
return isReconnect;
}

@Override
public String toString()
{
return code + ": " + meaning;
}

public static CloseCode from(int code)
{
for (CloseCode c : values())
if (c.code == code) return c;
return null;
}
}
}

0 comments on commit 0621b7c

Please sign in to comment.