Example usage for java.net DatagramPacket DatagramPacket

List of usage examples for java.net DatagramPacket DatagramPacket

Introduction

In this page you can find the example usage for java.net DatagramPacket DatagramPacket.

Prototype

public DatagramPacket(byte buf[], int length, SocketAddress address) 

Source Link

Document

Constructs a datagram packet for sending packets of length length to the specified port number on the specified host.

Usage

From source file:net.dv8tion.jda.audio.AudioWebSocket.java

private void setupUdpKeepAliveThread() {
    udpKeepAliveThread = new Thread("AudioWebSocket UDP-KeepAlive Guild: " + guild.getId()) {
        @Override//w  w w  .j  a v  a 2 s  . c  o  m
        public void run() {
            while (socket.isOpen() && !udpSocket.isClosed() && !this.isInterrupted()) {
                long seq = 0;
                try {
                    ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES + 1);
                    buffer.put((byte) 0xC9);
                    buffer.putLong(seq);
                    DatagramPacket keepAlivePacket = new DatagramPacket(buffer.array(), buffer.array().length,
                            address);
                    udpSocket.send(keepAlivePacket);

                    Thread.sleep(5000); //Wait 5 seconds to send next keepAlivePacket.
                } catch (NoRouteToHostException e) {
                    LOG.warn("Closing AudioConnection due to inability to ping audio packets.");
                    LOG.warn("Cannot send audio packet because JDA navigate the route to Discord.\n"
                            + "Are you sure you have internet connection? It is likely that you've lost connection.");
                    AudioWebSocket.this.close(true, -1);
                    break;
                } catch (IOException e) {
                    LOG.log(e);
                } catch (InterruptedException e) {
                    //We were asked to close.
                    //                        e.printStackTrace();
                }
            }
        }
    };
    udpKeepAliveThread.setPriority(Thread.NORM_PRIORITY + 1);
    udpKeepAliveThread.setDaemon(true);
    udpKeepAliveThread.start();
}

From source file:net.dv8tion.jda.core.audio.AudioWebSocket.java

private void setupKeepAlive(final int keepAliveInterval) {
    if (keepAliveHandle != null)
        LOG.fatal("Setting up a KeepAlive runnable while the previous one seems to still be active!!");

    Runnable keepAliveRunnable = () -> {
        if (socket.isOpen() && socket.isOpen() && !udpSocket.isClosed()) {
            send(new JSONObject().put("op", 3).put("d", System.currentTimeMillis()).toString());

            long seq = 0;
            try {
                ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES + 1);
                buffer.put((byte) 0xC9);
                buffer.putLong(seq);//from   w w w. j a  va 2s.  com
                DatagramPacket keepAlivePacket = new DatagramPacket(buffer.array(), buffer.array().length,
                        address);
                udpSocket.send(keepAlivePacket);

            } catch (NoRouteToHostException e) {
                LOG.warn("Closing AudioConnection due to inability to ping audio packets.");
                LOG.warn("Cannot send audio packet because JDA navigate the route to Discord.\n"
                        + "Are you sure you have internet connection? It is likely that you've lost connection.");
                AudioWebSocket.this.close(ConnectionStatus.ERROR_LOST_CONNECTION);
            } catch (IOException e) {
                LOG.log(e);
            }
        }
    };

    try {
        keepAliveHandle = keepAlivePool.scheduleAtFixedRate(keepAliveRunnable, 0, keepAliveInterval,
                TimeUnit.MILLISECONDS);
    } catch (RejectedExecutionException ignored) {
    } //ignored because this is probably caused due to a race condition
      // related to the threadpool shutdown.
}