From 8ddd003f4231f38e91318122879df31121477b69 Mon Sep 17 00:00:00 2001 From: Obique PSWG Date: Fri, 5 Jan 2018 08:01:08 -0600 Subject: [PATCH] Small code style changes --- src/com/projectswg/common/data/CRC.java | 98 +++++++++---------- .../projectswg/common/network/TCPServer.java | 91 +++++++++-------- 2 files changed, 88 insertions(+), 101 deletions(-) diff --git a/src/com/projectswg/common/data/CRC.java b/src/com/projectswg/common/data/CRC.java index d42c231..d93a355 100644 --- a/src/com/projectswg/common/data/CRC.java +++ b/src/com/projectswg/common/data/CRC.java @@ -1,39 +1,39 @@ /*********************************************************************************** -* 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 . * -* * -***********************************************************************************/ + * Copyright (c) 2017 /// 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 . * + * * + ***********************************************************************************/ package com.projectswg.common.data; -import java.nio.charset.StandardCharsets; - import com.projectswg.common.encoding.Encodable; import com.projectswg.common.network.NetBuffer; import com.projectswg.common.network.NetBufferStream; import com.projectswg.common.persistable.Persistable; +import java.nio.charset.StandardCharsets; + public class CRC implements Encodable, Persistable { private static final int CRC_TABLE[] = { @@ -101,7 +101,7 @@ public class CRC implements Encodable, Persistable { } @Override - public byte [] encode() { + public byte[] encode() { NetBuffer buffer = NetBuffer.allocate(4); buffer.addInt(crc); return buffer.array(); @@ -127,45 +127,35 @@ public class CRC implements Encodable, Persistable { public void read(NetBufferStream stream) { crc = stream.getInt(); } - + @Override public String toString() { return str; } - + @Override public int hashCode() { return this.crc; } - + @Override public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (!(obj instanceof CRC)) { - return false; - } - - return crc == ((CRC) obj).crc; - } - - public static int getCrc(String input) { - return getCrc(input.getBytes(StandardCharsets.UTF_8)); - } - - public static int getCrc(byte [] data) { - int crc = 0xFFFFFFFF; - for (int i = 0; i < data.length; i++) - crc = CRC_TABLE[((crc>>>24) ^ data[i]) & 0xFF] ^ (crc << 8); - return ~crc; + return this == obj || (obj instanceof CRC && crc == ((CRC) obj).crc); } public static String getString(int crc) { return CrcDatabase.getInstance().getString(crc); } + public static int getCrc(String input) { + return getCrc(input.getBytes(StandardCharsets.UTF_8)); + } + + public static int getCrc(byte[] data) { + int crc = 0xFFFFFFFF; + for (byte b : data) + crc = CRC_TABLE[((crc >>> 24) ^ b) & 0xFF] ^ (crc << 8); + return ~crc; + } + } diff --git a/src/com/projectswg/common/network/TCPServer.java b/src/com/projectswg/common/network/TCPServer.java index 304b4a8..a7089e8 100644 --- a/src/com/projectswg/common/network/TCPServer.java +++ b/src/com/projectswg/common/network/TCPServer.java @@ -1,53 +1,47 @@ /*********************************************************************************** -* 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 . * -* * -***********************************************************************************/ + * Copyright (c) 2017 /// 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 . * + * * + ***********************************************************************************/ package com.projectswg.common.network; +import com.projectswg.common.callback.CallbackManager; +import com.projectswg.common.debug.Assert; +import com.projectswg.common.debug.Log; + import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.nio.ByteBuffer; -import java.nio.channels.ClosedByInterruptException; -import java.nio.channels.ClosedChannelException; -import java.nio.channels.SelectableChannel; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.nio.channels.ServerSocketChannel; -import java.nio.channels.SocketChannel; +import java.nio.channels.*; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; -import com.projectswg.common.callback.CallbackManager; -import com.projectswg.common.debug.Assert; -import com.projectswg.common.debug.Log; - public class TCPServer { private final CallbackManager callbackManager; @@ -64,7 +58,7 @@ public class TCPServer { } public TCPServer(InetAddress addr, int port, int bufferSize) { - this.callbackManager = new CallbackManager<>("tcpserver-"+port, 1); + this.callbackManager = new CallbackManager<>("tcpserver-" + port, 1); this.sockets = new HashMap<>(); this.running = new AtomicBoolean(false); this.addr = addr; @@ -108,14 +102,6 @@ public class TCPServer { } } - private boolean disconnect(SocketChannel sc) { - try { - return disconnect(sc.getRemoteAddress()); - } catch (IOException e) { - return false; - } - } - public boolean close() { if (!running.getAndSet(false)) { Assert.fail(); @@ -142,10 +128,21 @@ public class TCPServer { callbackManager.setCallback(callback); } + private boolean disconnect(SocketChannel sc) { + try { + return disconnect(sc.getRemoteAddress()); + } catch (IOException e) { + return false; + } + } + public interface TCPCallback { + void onIncomingConnection(SocketChannel s, SocketAddress addr); + void onConnectionDisconnect(SocketChannel s, SocketAddress addr); - void onIncomingData(SocketChannel s, SocketAddress addr, byte [] data); + + void onIncomingData(SocketChannel s, SocketAddress addr, byte[] data); } private class TCPServerListener implements Runnable {