Writing Packets - Valkryst/VUDP GitHub Wiki

In these examples, we make use of the Package interface (that comes with VUDP) which supplies helper methods such as toBytes, toPackage, fromBytes, and fromPackage. These helper functions hide-away the code used to serialize an object, compress it with GZIP, and pack it into a DatagramPacket.

With a PacketWriter

package com.valkryst.VUDP;

import lombok.Getter;
import lombok.NonNull;

import java.io.IOException;
import java.net.*;

public class Driver {
    public static void main(String[] args) throws SocketException, UnknownHostException {
        // Create a PacketWriter
        final PacketWriter writer = new PacketWriter(new DatagramSocket());
        writer.start();

        // Continually Write Packets
        final InetAddress serverAddress = InetAddress.getByName("server.com");
        final int serverPort = 1234;

        while (true) {
            try {
                final Message message = new Message("Test Message");

                final DatagramPacket packet = message.toPacket();
                packet.setAddress(serverAddress);
                packet.setPort(serverPort);

                writer.queuePacket(message.toPacket());
            } catch (final InterruptedException e) {
                // If interrupted while waiting to take a packet from the queue.
                e.printStackTrace();
            } catch (final IOException e) {
                // If there's an issue packing the message into a packet.
                e.printStackTrace();
            }
        }
    }

    private static class Message implements Package, Serializable {
        /** The message. */
        @Getter private final String message;

        /**
         * Constructs a new Message.
         *
         * @param message
         *          The message.
         */
        public Message(final @NonNull String message) {
            this.message = message;
        }
    }
}

With a PacketReadWriter

package com.valkryst.VUDP;

import lombok.Getter;
import lombok.NonNull;

import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;

public class Driver {
    public static void main(String[] args) throws SocketException, UnknownHostException {
        // Create a PacketReadWriter
        final String serverHost = "server.com";
        final int serverPort = 1234;
        final int clientPort = 1234;
        final PacketReadWriter readWriter = new PacketReadWriter("server-hostname.com", serverPort, clientPort);
        readWriter.start();

        // Continually Write Packets
        while (true) {
            try {
                final Message message = new Message("Test Message");
                readWriter.queuePacket(message.toPacket());
            } catch (final InterruptedException e) {
                // If interrupted while waiting to take a packet from the queue.
                e.printStackTrace();
            } catch (final IOException e) {
                // If there's an issue packing the message into a packet.
                e.printStackTrace();
            }
        }
    }

    private static class Message implements Package, Serializable {
        /** The message. */
        @Getter private final String message;

        /**
         * Constructs a new Message.
         *
         * @param message
         *          The message.
         */
        public Message(final @NonNull String message) {
            this.message = message;
        }
    }
}