Search in sources :

Example 6 with ConnectionRequest

use of net.dv8tion.jda.internal.audio.ConnectionRequest in project JDA by DV8FromTheWorld.

the class WebSocketSendingThread method run.

@Override
public void run() {
    // Make sure that we don't send any packets before sending auth info.
    if (!client.sentAuthInfo) {
        scheduleIdle();
        return;
    }
    ConnectionRequest audioRequest = null;
    DataObject chunkRequest = null;
    try {
        api.setContext();
        attemptedToSend = false;
        needRateLimit = false;
        // We do this outside of the lock because otherwise we could potentially deadlock here
        audioRequest = client.getNextAudioConnectRequest();
        if (!queueLock.tryLock() && !queueLock.tryLock(10, TimeUnit.SECONDS)) {
            scheduleNext();
            return;
        }
        chunkRequest = chunkQueue.peek();
        if (chunkRequest != null)
            handleChunkSync(chunkRequest);
        else if (audioRequest != null)
            handleAudioRequest(audioRequest);
        else
            handleNormalRequest();
    } catch (InterruptedException ignored) {
        LOG.debug("Main WS send thread interrupted. Most likely JDA is disconnecting the websocket.");
        return;
    } catch (Throwable ex) {
        // Log error
        LOG.error("Encountered error in gateway worker", ex);
        if (!attemptedToSend) {
            // Try to remove the failed request
            if (chunkRequest != null)
                client.chunkSyncQueue.remove(chunkRequest);
            else if (audioRequest != null)
                client.removeAudioConnection(audioRequest.getGuildIdLong());
        }
        // Rethrow if error to kill thread
        if (ex instanceof Error)
            throw (Error) ex;
    } finally {
        // on any exception that might cause this lock to not release
        client.maybeUnlock();
    }
    scheduleNext();
}
Also used : ConnectionRequest(net.dv8tion.jda.internal.audio.ConnectionRequest) DataObject(net.dv8tion.jda.api.utils.data.DataObject)

Aggregations

ConnectionRequest (net.dv8tion.jda.internal.audio.ConnectionRequest)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ConnectionListener (net.dv8tion.jda.api.audio.hooks.ConnectionListener)1 AudioChannel (net.dv8tion.jda.api.entities.AudioChannel)1 Guild (net.dv8tion.jda.api.entities.Guild)1 IPermissionContainer (net.dv8tion.jda.api.entities.IPermissionContainer)1 DataObject (net.dv8tion.jda.api.utils.data.DataObject)1 ConnectionStage (net.dv8tion.jda.internal.audio.ConnectionStage)1