Updated forwarder to use new pswgcommon utilities

This commit is contained in:
Josh Larson
2018-05-18 08:36:21 -05:00
parent 13eb3c02dd
commit 9664883b52
24 changed files with 41 additions and 266 deletions
@@ -1,18 +1,17 @@
package com.projectswg.forwarder.intents.client;
import me.joshlarson.jlcommon.control.Intent;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.NotNull;
public class DataPacketInboundIntent extends Intent {
private final byte [] data;
public DataPacketInboundIntent(@Nonnull byte [] data) {
public DataPacketInboundIntent(@NotNull byte [] data) {
this.data = data;
}
@Nonnull
@NotNull
public byte [] getData() {
return data;
}
@@ -1,18 +1,17 @@
package com.projectswg.forwarder.intents.client;
import me.joshlarson.jlcommon.control.Intent;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.NotNull;
public class DataPacketOutboundIntent extends Intent {
private final byte [] data;
public DataPacketOutboundIntent(@Nonnull byte [] data) {
public DataPacketOutboundIntent(@NotNull byte [] data) {
this.data = data;
}
@Nonnull
@NotNull
public byte [] getData() {
return data;
}
@@ -2,18 +2,17 @@ package com.projectswg.forwarder.intents.client;
import com.projectswg.forwarder.resources.networking.packets.Packet;
import me.joshlarson.jlcommon.control.Intent;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.NotNull;
public class SonyPacketInboundIntent extends Intent {
private final Packet packet;
public SonyPacketInboundIntent(@Nonnull Packet packet) {
public SonyPacketInboundIntent(@NotNull Packet packet) {
this.packet = packet;
}
@Nonnull
@NotNull
public Packet getPacket() {
return packet;
}
@@ -2,8 +2,7 @@ package com.projectswg.forwarder.intents.client;
import com.projectswg.forwarder.resources.networking.data.ProtocolStack;
import me.joshlarson.jlcommon.control.Intent;
import javax.annotation.CheckForNull;
import org.jetbrains.annotations.Nullable;
public class UpdateStackIntent extends Intent {
@@ -13,7 +12,7 @@ public class UpdateStackIntent extends Intent {
this.stack = stack;
}
@CheckForNull
@Nullable
public ProtocolStack getStack() {
return stack;
}
@@ -6,8 +6,6 @@ import com.projectswg.common.network.packets.PacketType;
import com.projectswg.common.network.packets.swg.login.LoginClientId;
import com.projectswg.common.network.packets.swg.login.LoginClusterStatus;
import com.projectswg.forwarder.Forwarder.ForwarderData;
import me.joshlarson.jlcommon.log.Log;
import me.joshlarson.jlcommon.utilities.ByteUtilities;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@@ -1,169 +0,0 @@
/***********************************************************************************
* Copyright (c) 2015 /// Project SWG /// www.projectswg.com *
* *
* ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on *
* July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. *
* Our goal is to create an emulator which will provide a server for players to *
* continue playing a game similar to the one they used to play. We are basing *
* it on the final publish of the game prior to end-game events. *
* *
* This file is part of Holocore. *
* *
* -------------------------------------------------------------------------------- *
* *
* Holocore is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* Holocore is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with Holocore. If not, see <http://www.gnu.org/licenses/>. *
* *
***********************************************************************************/
package com.projectswg.forwarder.resources.networking;
import me.joshlarson.jlcommon.concurrency.Delay;
import me.joshlarson.jlcommon.log.Log;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.net.*;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
/**
* This class represents a UDP server that listens for packets and
* will call the callback when it receives one
*/
public class UDPServer {
private final byte [] dataBuffer;
private final AtomicBoolean running;
private final InetSocketAddress bindAddr;
private final Consumer<DatagramPacket> callback;
private DatagramSocket socket;
private Thread thread;
public UDPServer(@Nonnull InetSocketAddress bindAddr, @Nonnull Consumer<DatagramPacket> callback) {
this(bindAddr, 1024, callback);
}
public UDPServer(@Nonnull InetSocketAddress bindAddr, int packetSize, @Nonnull Consumer<DatagramPacket> callback) {
this.dataBuffer = new byte[packetSize];
this.running = new AtomicBoolean(false);
this.bindAddr = bindAddr;
this.callback = callback;
}
public void bind() throws SocketException {
bind(null);
}
public void bind(Consumer<DatagramSocket> customizationCallback) throws SocketException {
assert socket == null : "binding twice";
socket = new DatagramSocket(bindAddr);
if (customizationCallback != null)
customizationCallback.accept(socket);
start();
}
public void close() {
assert socket != null : "socket already closed";
stop();
socket.close();
socket = null;
}
public int getPort() {
int port = socket.getLocalPort();
while (port == 0) {
port = socket.getLocalPort();
if (!Delay.sleepMilli(5))
break;
}
return port;
}
public boolean isRunning() {
return running.get();
}
public boolean send(DatagramPacket packet) {
try {
socket.send(packet);
return true;
} catch (IOException e) {
String msg = e.getMessage();
if (msg == null || !msg.toLowerCase(Locale.US).contains("socket closed")) {
Log.e(e);
close();
}
}
return false;
}
public boolean send(int port, InetAddress addr, byte [] data) {
return send(new DatagramPacket(data, data.length, addr, port));
}
public boolean send(int port, String addr, byte [] data) {
try {
return send(port, InetAddress.getByName(addr), data);
} catch (UnknownHostException e) {
Log.e(e);
}
return false;
}
public boolean send(InetSocketAddress addr, byte [] data) {
return send(new DatagramPacket(data, data.length, addr));
}
private void start() {
running.set(true);
thread = new Thread(this::run);
thread.setName("UDPServer Port#" + getPort());
thread.start();
}
private void stop() {
running.set(false);
thread.interrupt();
}
private void run() {
try {
while (running.get()) {
DatagramPacket packet = new DatagramPacket(dataBuffer, dataBuffer.length);
try {
socket.receive(packet);
if (packet.getLength() > 0) {
byte [] buffer = new byte[packet.getLength()];
System.arraycopy(packet.getData(), 0, buffer, 0, packet.getLength());
packet.setData(buffer);
callback.accept(packet);
}
} catch (IOException e) {
String msg = e.getMessage();
if (msg == null || !msg.toLowerCase(Locale.US).contains("socket closed")) {
Log.e(e);
close();
}
packet.setLength(0);
}
}
} catch (Exception e) {
Log.e(e);
} finally {
running.set(false);
}
}
}
@@ -1,12 +1,12 @@
package com.projectswg.forwarder.resources.networking.data;
import com.projectswg.forwarder.resources.networking.packets.Fragmented;
import com.projectswg.forwarder.resources.networking.packets.Packet;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import com.projectswg.forwarder.resources.networking.packets.Fragmented;
import com.projectswg.forwarder.resources.networking.packets.Packet;
public class FragmentedProcessor {
private final List<Fragmented> fragmentedBuffer;
@@ -4,8 +4,8 @@ import com.projectswg.forwarder.resources.networking.ClientServer;
import com.projectswg.forwarder.resources.networking.packets.Fragmented;
import com.projectswg.forwarder.resources.networking.packets.Packet;
import com.projectswg.forwarder.resources.networking.packets.SequencedPacket;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nonnull;
import java.net.InetSocketAddress;
import java.util.*;
import java.util.function.BiConsumer;
@@ -103,7 +103,7 @@ public class ProtocolStack {
}
}
public boolean addIncoming(@Nonnull SequencedPacket packet) {
public boolean addIncoming(@NotNull SequencedPacket packet) {
synchronized (sequenced) {
if (packet.getSequence() < rxSequence)
return true;
@@ -133,7 +133,7 @@ public class ProtocolStack {
return fragmentedProcessor.addFragmented(frag);
}
public void addOutbound(@Nonnull byte [] data) {
public void addOutbound(@NotNull byte [] data) {
outboundRaw.offer(data);
}
@@ -1,35 +0,0 @@
package com.projectswg.forwarder.resources.networking.encryption;
import net.jpountz.lz4.LZ4Compressor;
import net.jpountz.lz4.LZ4Factory;
import net.jpountz.lz4.LZ4SafeDecompressor;
public class Compression {
private static final LZ4Compressor COMPRESSOR = LZ4Factory.safeInstance().highCompressor();
private static final LZ4SafeDecompressor DECOMPRESSOR = LZ4Factory.safeInstance().safeDecompressor();
public static byte [] compress(byte [] data) {
int maxCompressedLength = COMPRESSOR.maxCompressedLength(data.length);
byte[] compressed = new byte[maxCompressedLength];
int length = COMPRESSOR.compress(data, compressed);
byte [] ret = new byte[length];
System.arraycopy(compressed, 0, ret, 0, length);
return ret;
}
public static byte [] decompress(byte [] data) {
return decompress(data, data.length * 10);
}
public static byte [] decompress(byte [] data, int bufferSize) {
byte [] restored = new byte[bufferSize];
int length = DECOMPRESSOR.decompress(data, restored);
if (length == bufferSize)
return restored;
byte [] ret = new byte[length];
System.arraycopy(restored, 0, ret, 0, length);
return ret;
}
}
@@ -27,12 +27,12 @@
***********************************************************************************/
package com.projectswg.forwarder.resources.networking.packets;
import com.projectswg.common.network.NetBuffer;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import com.projectswg.common.network.NetBuffer;
public class DataChannel extends Packet implements SequencedPacket {
private final List<byte[]> content;
@@ -27,12 +27,12 @@
***********************************************************************************/
package com.projectswg.forwarder.resources.networking.packets;
import com.projectswg.common.network.NetBuffer;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import com.projectswg.common.network.NetBuffer;
public class MultiPacket extends Packet {
private final List <byte []> content;
@@ -3,7 +3,6 @@ package com.projectswg.forwarder.resources.recording;
import java.io.*;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.net.InetSocketAddress;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;
@@ -3,12 +3,7 @@ package com.projectswg.forwarder.services.client;
import com.projectswg.common.network.NetBuffer;
import com.projectswg.common.network.packets.PacketType;
import com.projectswg.common.network.packets.swg.zone.HeartBeat;
import com.projectswg.forwarder.intents.client.DataPacketInboundIntent;
import com.projectswg.forwarder.intents.client.DataPacketOutboundIntent;
import com.projectswg.forwarder.intents.client.SonyPacketInboundIntent;
import com.projectswg.forwarder.intents.client.ClientConnectedIntent;
import com.projectswg.forwarder.intents.client.ClientDisconnectedIntent;
import com.projectswg.forwarder.intents.client.UpdateStackIntent;
import com.projectswg.forwarder.intents.client.*;
import com.projectswg.forwarder.resources.client.state.OutboundDataTuner;
import com.projectswg.forwarder.resources.networking.data.ProtocolStack;
import com.projectswg.forwarder.resources.networking.data.SequencedOutbound;
@@ -1,14 +1,13 @@
package com.projectswg.forwarder.services.client;
import com.projectswg.forwarder.Forwarder.ForwarderData;
import com.projectswg.forwarder.intents.client.SonyPacketInboundIntent;
import com.projectswg.forwarder.intents.client.ClientConnectedIntent;
import com.projectswg.forwarder.intents.client.ClientDisconnectedIntent;
import com.projectswg.forwarder.intents.client.SonyPacketInboundIntent;
import com.projectswg.forwarder.intents.client.UpdateStackIntent;
import com.projectswg.forwarder.intents.control.StartForwarderIntent;
import com.projectswg.forwarder.intents.control.StopForwarderIntent;
import com.projectswg.forwarder.resources.networking.ClientServer;
import com.projectswg.forwarder.resources.networking.UDPServer;
import com.projectswg.forwarder.resources.networking.data.ProtocolStack;
import com.projectswg.forwarder.resources.networking.packets.*;
import com.projectswg.forwarder.resources.networking.packets.Disconnect.DisconnectReason;
@@ -16,6 +15,7 @@ import me.joshlarson.jlcommon.control.IntentChain;
import me.joshlarson.jlcommon.control.IntentHandler;
import me.joshlarson.jlcommon.control.Service;
import me.joshlarson.jlcommon.log.Log;
import me.joshlarson.jlcommon.network.UDPServer;
import me.joshlarson.jlcommon.utilities.ByteUtilities;
import java.net.*;
@@ -4,10 +4,10 @@ import com.projectswg.connection.HolocoreSocket;
import com.projectswg.connection.ServerConnectionChangedReason;
import com.projectswg.connection.packets.RawPacket;
import com.projectswg.forwarder.Forwarder.ForwarderData;
import com.projectswg.forwarder.intents.client.DataPacketInboundIntent;
import com.projectswg.forwarder.intents.client.DataPacketOutboundIntent;
import com.projectswg.forwarder.intents.client.ClientConnectedIntent;
import com.projectswg.forwarder.intents.client.ClientDisconnectedIntent;
import com.projectswg.forwarder.intents.client.DataPacketInboundIntent;
import com.projectswg.forwarder.intents.client.DataPacketOutboundIntent;
import com.projectswg.forwarder.intents.control.StartForwarderIntent;
import com.projectswg.forwarder.intents.control.StopForwarderIntent;
import com.projectswg.forwarder.intents.server.ServerConnectedIntent;