Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into 4.x.x
Browse files Browse the repository at this point in the history
  • Loading branch information
rheimus committed Mar 19, 2021
2 parents 7beb48a + 38f3106 commit 9e6e893
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 32 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ssversion = 4.0.1
ssversion = 4.1.0
ssname = serversync
mainclass = 'com.superzanti.serversync.ServerSync'
org.gradle.warning.mode=all
2 changes: 1 addition & 1 deletion src/main/java/com/superzanti/serversync/RefStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class RefStrings {
public static final String MODID = "com.superzanti.serversync";
public static final String NAME = "ServerSync";
public static final String VERSION = "v4.0.1";
public static final String VERSION = "v4.1.0";

public static final String ERROR_TOKEN = "<E>";
public static final String DELETE_TOKEN = "<D>";
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/superzanti/serversync/client/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import com.superzanti.serversync.util.Logger;
import com.superzanti.serversync.util.enums.EServerMessage;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
Expand All @@ -20,6 +18,9 @@ public class Server {
public Socket clientSocket;
public ServerInfo info;

public OutputStream os;
public InputStream is;

protected final String address;
protected final int port;

Expand All @@ -46,6 +47,7 @@ public boolean connect() {

Logger.log("< " + ServerSync.strings.getString("connection_message") + " >");
try {
clientSocket.setPerformancePreferences(0, 1, 2);
clientSocket.connect(new InetSocketAddress(host.getHostName(), port), 5000);
} catch (IOException e) {
Logger.error(ServerSync.strings.getString("connection_failed_server") + ": " + address + ":" + port);
Expand All @@ -55,9 +57,10 @@ public boolean connect() {

Logger.debug(ServerSync.strings.getString("debug_IO_streams"));
try {
clientSocket.setPerformancePreferences(0, 1, 2);
output = new ObjectOutputStream(clientSocket.getOutputStream());
input = new ObjectInputStream(clientSocket.getInputStream());
os = clientSocket.getOutputStream();
is = clientSocket.getInputStream();
output = new ObjectOutputStream(os);
input = new ObjectInputStream(is);
} catch (IOException e) {
Logger.debug(ServerSync.strings.getString("debug_IO_streams_failed"));
AutoClose.closeResource(clientSocket);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import com.superzanti.serversync.RefStrings;
import com.superzanti.serversync.ServerSync;
import com.superzanti.serversync.client.Server;
import com.superzanti.serversync.config.SyncConfig;
import com.superzanti.serversync.util.Logger;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
Expand Down Expand Up @@ -47,24 +48,31 @@ public boolean write(Consumer<Double> onProgress) {
}

try {
Logger.debug(String.format("Attempting to write file '%s' with total size of %s bytes...", outputFile.toString(), size));
OutputStream wr = Files.newOutputStream(outputFile, StandardOpenOption.TRUNCATE_EXISTING);
Logger.debug(String.format(
"Attempting to write file '%s' with total size of %s bytes...",
outputFile.toString(), size
));
BufferedOutputStream wr = new BufferedOutputStream(
Files.newOutputStream(outputFile, StandardOpenOption.TRUNCATE_EXISTING),
SyncConfig.getConfig().BUFFER_SIZE
);

byte[] outBuffer = new byte[server.clientSocket.getReceiveBufferSize()];
byte[] outBuffer = new byte[SyncConfig.getConfig().BUFFER_SIZE];

int bytesReceived;
float mebibyte = 1024F*1024F;
float sizeMiB = Math.round(size / mebibyte * 10)/10F;
float mebibyte = 1024F * 1024F;
float sizeMiB = Math.round(size / mebibyte * 10) / 10F;
long totalBytesReceived = 0L;
while ((bytesReceived = server.input.read(outBuffer)) > 0) {
while ((bytesReceived = server.is.read(outBuffer)) > 0) {
totalBytesReceived += bytesReceived;

wr.write(outBuffer, 0, bytesReceived);
// Not terribly worried about conversion loss
onProgress.accept((double) totalBytesReceived / size);

if (size > mebibyte && totalBytesReceived % mebibyte == 0){
Logger.debug(String.format("Progress: %s / %s MiB", Math.round(totalBytesReceived/mebibyte), sizeMiB));
if (size > mebibyte && totalBytesReceived % mebibyte == 0) {
Logger.debug(
String.format("Progress: %s / %s MiB", Math.round(totalBytesReceived / mebibyte), sizeMiB));
}

if (totalBytesReceived == size) {
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/superzanti/serversync/config/JsonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class JsonConfig {
private static final String PROP_SYNC_MODE = "sync_mode";
private static final String PROP_PORT = "port";
private static final String PROP_ADDRESS = "address";
private static final String PROP_BUFFER_SIZE = "buffer";
private static final String PROP_DIRECTORIES = "directories";
private static final String PROP_FILES = "files";
private static final String PROP_FILES_INCLUDE = "include";
Expand All @@ -52,6 +53,13 @@ public static void forServer(Path json) throws IOException {
config.SYNC_MODE = getInt(general, PROP_SYNC_MODE);
config.SERVER_PORT = getInt(connection, PROP_PORT);

try {
config.BUFFER_SIZE = getInt(connection, PROP_BUFFER_SIZE);
} catch (NullPointerException e) {
Logger.debug("Missing config entry for buffer, using defaults");
hasMissingEntries = true;
}

try {
JsonArray directoryIncludeList = getArray(rules, PROP_DIRECTORIES);
config.DIRECTORY_INCLUDE_LIST = directoryIncludeList
Expand Down Expand Up @@ -136,6 +144,13 @@ public static void forClient(Path json) throws IOException {
config.SERVER_IP = getString(connection, PROP_ADDRESS, "127.0.0.1");
config.SERVER_PORT = getInt(connection, PROP_PORT);

try {
config.BUFFER_SIZE = getInt(connection, PROP_BUFFER_SIZE);
} catch (NullPointerException e) {
Logger.debug("Missing config entry for buffer, using defaults");
hasMissingEntries = true;
}

try {
JsonObject files = getObject(rules, PROP_FILES);
config.FILE_IGNORE_LIST = getArray(files, PROP_FILES_IGNORE)
Expand Down Expand Up @@ -178,6 +193,7 @@ public static void saveServer(Path file) throws IOException {

JsonObject connection = new JsonObject();
connection.add(PROP_PORT, config.SERVER_PORT);
connection.add(PROP_BUFFER_SIZE, config.BUFFER_SIZE);
root.add(CAT_CONNECTION, connection);

JsonObject rules = new JsonObject();
Expand Down Expand Up @@ -216,6 +232,7 @@ public static void saveClient(Path file) throws IOException {
JsonObject connection = new JsonObject();
connection.add(PROP_ADDRESS, config.SERVER_IP);
connection.add(PROP_PORT, config.SERVER_PORT);
connection.add(PROP_BUFFER_SIZE, config.BUFFER_SIZE);
root.add(CAT_CONNECTION, connection);

JsonObject rules = new JsonObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ public class SyncConfig {
public List<String> CONFIG_INCLUDE_LIST = new ArrayList<>();
public Locale LOCALE = Locale.getDefault();
public ETheme THEME = ETheme.BLUE_YELLOW;
public int BUFFER_SIZE = 1024 * 64;
////////////////////////////////////////

// SERVER //////////////////////////////
public int SERVER_PORT = 38067;
public Boolean PUSH_CLIENT_MODS = false;
public List<String> FILE_INCLUDE_LIST = Collections.singletonList("**/mods");
public List<String> FILE_INCLUDE_LIST = Collections.singletonList("mods/**");
public List<DirectoryEntry> DIRECTORY_INCLUDE_LIST = Collections.singletonList(new DirectoryEntry(
"mods",
EDirectoryMode.mirror
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
* @author Rheimus
*/
public class ServerSetup implements Runnable {
private static final int SEND_BUFFER_SIZE = 1024 * 8;

private final SyncConfig config = SyncConfig.getConfig();
private final Path bannedIps = Paths.get(ELocation.BANNED_IPS.getValue());

Expand Down Expand Up @@ -151,7 +149,6 @@ public void run() {
continue;
}

socket.setSendBufferSize(ServerSetup.SEND_BUFFER_SIZE);
ServerWorker sc = new ServerWorker(
socket,
messages,
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/com/superzanti/serversync/server/ServerWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import com.superzanti.serversync.util.enums.EServerMessage;
import com.superzanti.serversync.util.errors.UnknownMessageError;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.nio.file.Files;
Expand All @@ -40,6 +37,8 @@ public class ServerWorker implements Runnable {
private static final int FILE_SYNC_CLIENT_TIMEOUT_MS = 60000 * 20; // 20 minutes

private final Socket clientSocket;
private InputStream is;
private OutputStream os;
private ObjectInputStream ois;
private ObjectOutputStream oos;

Expand Down Expand Up @@ -74,8 +73,10 @@ public class ServerWorker implements Runnable {
@Override
public void run() {
try {
ois = new ObjectInputStream(clientSocket.getInputStream());
oos = new ObjectOutputStream(clientSocket.getOutputStream());
is = clientSocket.getInputStream();
os = clientSocket.getOutputStream();
ois = new ObjectInputStream(is);
oos = new ObjectOutputStream(os);
} catch (IOException e) {
clientLogger.log("Failed to create client streams");
Logger.error(String.format("Error in client setup: %s", clientSocket.getInetAddress()));
Expand Down Expand Up @@ -292,17 +293,17 @@ private void transferFile(Path file) throws IOException {

if (size > 0) {
int bytesRead;
byte[] buffer = new byte[clientSocket.getSendBufferSize()];
byte[] buffer = new byte[SyncConfig.getConfig().BUFFER_SIZE];

try (BufferedInputStream fis = new BufferedInputStream(Files.newInputStream(file))) {
try (BufferedInputStream fis = new BufferedInputStream(Files.newInputStream(file), SyncConfig.getConfig().BUFFER_SIZE)) {
while ((bytesRead = fis.read(buffer)) > 0) {
oos.write(buffer, 0, bytesRead);
os.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
clientLogger.debug(String.format("Failed to write file: %s", file));
clientLogger.debug(e);
} finally {
oos.flush();
os.flush();
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/server-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"sync_mode": 2
},
"connection": {
"port": 38067
"port": 38067,
"buffer": 65536
},
"rules": {
"directories": [
Expand Down

0 comments on commit 9e6e893

Please sign in to comment.