Skip to content

Commit 9e6e893

Browse files
committedMar 19, 2021
Merge remote-tracking branch 'origin/master' into 4.x.x
2 parents 7beb48a + 38f3106 commit 9e6e893

File tree

9 files changed

+60
-32
lines changed

9 files changed

+60
-32
lines changed
 

‎gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ssversion = 4.0.1
1+
ssversion = 4.1.0
22
ssname = serversync
33
mainclass = 'com.superzanti.serversync.ServerSync'
44
org.gradle.warning.mode=all

‎src/main/java/com/superzanti/serversync/RefStrings.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public class RefStrings {
44
public static final String MODID = "com.superzanti.serversync";
55
public static final String NAME = "ServerSync";
6-
public static final String VERSION = "v4.0.1";
6+
public static final String VERSION = "v4.1.0";
77

88
public static final String ERROR_TOKEN = "<E>";
99
public static final String DELETE_TOKEN = "<D>";

‎src/main/java/com/superzanti/serversync/client/Server.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
import com.superzanti.serversync.util.Logger;
77
import com.superzanti.serversync.util.enums.EServerMessage;
88

9-
import java.io.IOException;
10-
import java.io.ObjectInputStream;
11-
import java.io.ObjectOutputStream;
9+
import java.io.*;
1210
import java.net.InetAddress;
1311
import java.net.InetSocketAddress;
1412
import java.net.Socket;
@@ -20,6 +18,9 @@ public class Server {
2018
public Socket clientSocket;
2119
public ServerInfo info;
2220

21+
public OutputStream os;
22+
public InputStream is;
23+
2324
protected final String address;
2425
protected final int port;
2526

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

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

5658
Logger.debug(ServerSync.strings.getString("debug_IO_streams"));
5759
try {
58-
clientSocket.setPerformancePreferences(0, 1, 2);
59-
output = new ObjectOutputStream(clientSocket.getOutputStream());
60-
input = new ObjectInputStream(clientSocket.getInputStream());
60+
os = clientSocket.getOutputStream();
61+
is = clientSocket.getInputStream();
62+
output = new ObjectOutputStream(os);
63+
input = new ObjectInputStream(is);
6164
} catch (IOException e) {
6265
Logger.debug(ServerSync.strings.getString("debug_IO_streams_failed"));
6366
AutoClose.closeResource(clientSocket);

‎src/main/java/com/superzanti/serversync/communication/SyncFileOutputStream.java

+17-9
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import com.superzanti.serversync.RefStrings;
44
import com.superzanti.serversync.ServerSync;
55
import com.superzanti.serversync.client.Server;
6+
import com.superzanti.serversync.config.SyncConfig;
67
import com.superzanti.serversync.util.Logger;
78

9+
import java.io.BufferedOutputStream;
810
import java.io.IOException;
9-
import java.io.OutputStream;
1011
import java.nio.file.Files;
1112
import java.nio.file.Path;
1213
import java.nio.file.StandardOpenOption;
@@ -47,24 +48,31 @@ public boolean write(Consumer<Double> onProgress) {
4748
}
4849

4950
try {
50-
Logger.debug(String.format("Attempting to write file '%s' with total size of %s bytes...", outputFile.toString(), size));
51-
OutputStream wr = Files.newOutputStream(outputFile, StandardOpenOption.TRUNCATE_EXISTING);
51+
Logger.debug(String.format(
52+
"Attempting to write file '%s' with total size of %s bytes...",
53+
outputFile.toString(), size
54+
));
55+
BufferedOutputStream wr = new BufferedOutputStream(
56+
Files.newOutputStream(outputFile, StandardOpenOption.TRUNCATE_EXISTING),
57+
SyncConfig.getConfig().BUFFER_SIZE
58+
);
5259

53-
byte[] outBuffer = new byte[server.clientSocket.getReceiveBufferSize()];
60+
byte[] outBuffer = new byte[SyncConfig.getConfig().BUFFER_SIZE];
5461

5562
int bytesReceived;
56-
float mebibyte = 1024F*1024F;
57-
float sizeMiB = Math.round(size / mebibyte * 10)/10F;
63+
float mebibyte = 1024F * 1024F;
64+
float sizeMiB = Math.round(size / mebibyte * 10) / 10F;
5865
long totalBytesReceived = 0L;
59-
while ((bytesReceived = server.input.read(outBuffer)) > 0) {
66+
while ((bytesReceived = server.is.read(outBuffer)) > 0) {
6067
totalBytesReceived += bytesReceived;
6168

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

66-
if (size > mebibyte && totalBytesReceived % mebibyte == 0){
67-
Logger.debug(String.format("Progress: %s / %s MiB", Math.round(totalBytesReceived/mebibyte), sizeMiB));
73+
if (size > mebibyte && totalBytesReceived % mebibyte == 0) {
74+
Logger.debug(
75+
String.format("Progress: %s / %s MiB", Math.round(totalBytesReceived / mebibyte), sizeMiB));
6876
}
6977

7078
if (totalBytesReceived == size) {

‎src/main/java/com/superzanti/serversync/config/JsonConfig.java

+17
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class JsonConfig {
2626
private static final String PROP_SYNC_MODE = "sync_mode";
2727
private static final String PROP_PORT = "port";
2828
private static final String PROP_ADDRESS = "address";
29+
private static final String PROP_BUFFER_SIZE = "buffer";
2930
private static final String PROP_DIRECTORIES = "directories";
3031
private static final String PROP_FILES = "files";
3132
private static final String PROP_FILES_INCLUDE = "include";
@@ -52,6 +53,13 @@ public static void forServer(Path json) throws IOException {
5253
config.SYNC_MODE = getInt(general, PROP_SYNC_MODE);
5354
config.SERVER_PORT = getInt(connection, PROP_PORT);
5455

56+
try {
57+
config.BUFFER_SIZE = getInt(connection, PROP_BUFFER_SIZE);
58+
} catch (NullPointerException e) {
59+
Logger.debug("Missing config entry for buffer, using defaults");
60+
hasMissingEntries = true;
61+
}
62+
5563
try {
5664
JsonArray directoryIncludeList = getArray(rules, PROP_DIRECTORIES);
5765
config.DIRECTORY_INCLUDE_LIST = directoryIncludeList
@@ -136,6 +144,13 @@ public static void forClient(Path json) throws IOException {
136144
config.SERVER_IP = getString(connection, PROP_ADDRESS, "127.0.0.1");
137145
config.SERVER_PORT = getInt(connection, PROP_PORT);
138146

147+
try {
148+
config.BUFFER_SIZE = getInt(connection, PROP_BUFFER_SIZE);
149+
} catch (NullPointerException e) {
150+
Logger.debug("Missing config entry for buffer, using defaults");
151+
hasMissingEntries = true;
152+
}
153+
139154
try {
140155
JsonObject files = getObject(rules, PROP_FILES);
141156
config.FILE_IGNORE_LIST = getArray(files, PROP_FILES_IGNORE)
@@ -178,6 +193,7 @@ public static void saveServer(Path file) throws IOException {
178193

179194
JsonObject connection = new JsonObject();
180195
connection.add(PROP_PORT, config.SERVER_PORT);
196+
connection.add(PROP_BUFFER_SIZE, config.BUFFER_SIZE);
181197
root.add(CAT_CONNECTION, connection);
182198

183199
JsonObject rules = new JsonObject();
@@ -216,6 +232,7 @@ public static void saveClient(Path file) throws IOException {
216232
JsonObject connection = new JsonObject();
217233
connection.add(PROP_ADDRESS, config.SERVER_IP);
218234
connection.add(PROP_PORT, config.SERVER_PORT);
235+
connection.add(PROP_BUFFER_SIZE, config.BUFFER_SIZE);
219236
root.add(CAT_CONNECTION, connection);
220237

221238
JsonObject rules = new JsonObject();

‎src/main/java/com/superzanti/serversync/config/SyncConfig.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ public class SyncConfig {
2626
public List<String> CONFIG_INCLUDE_LIST = new ArrayList<>();
2727
public Locale LOCALE = Locale.getDefault();
2828
public ETheme THEME = ETheme.BLUE_YELLOW;
29+
public int BUFFER_SIZE = 1024 * 64;
2930
////////////////////////////////////////
3031

3132
// SERVER //////////////////////////////
3233
public int SERVER_PORT = 38067;
3334
public Boolean PUSH_CLIENT_MODS = false;
34-
public List<String> FILE_INCLUDE_LIST = Collections.singletonList("**/mods");
35+
public List<String> FILE_INCLUDE_LIST = Collections.singletonList("mods/**");
3536
public List<DirectoryEntry> DIRECTORY_INCLUDE_LIST = Collections.singletonList(new DirectoryEntry(
3637
"mods",
3738
EDirectoryMode.mirror

‎src/main/java/com/superzanti/serversync/server/ServerSetup.java

-3
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
* @author Rheimus
3030
*/
3131
public class ServerSetup implements Runnable {
32-
private static final int SEND_BUFFER_SIZE = 1024 * 8;
33-
3432
private final SyncConfig config = SyncConfig.getConfig();
3533
private final Path bannedIps = Paths.get(ELocation.BANNED_IPS.getValue());
3634

@@ -151,7 +149,6 @@ public void run() {
151149
continue;
152150
}
153151

154-
socket.setSendBufferSize(ServerSetup.SEND_BUFFER_SIZE);
155152
ServerWorker sc = new ServerWorker(
156153
socket,
157154
messages,

‎src/main/java/com/superzanti/serversync/server/ServerWorker.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
import com.superzanti.serversync.util.enums.EServerMessage;
1414
import com.superzanti.serversync.util.errors.UnknownMessageError;
1515

16-
import java.io.BufferedInputStream;
17-
import java.io.IOException;
18-
import java.io.ObjectInputStream;
19-
import java.io.ObjectOutputStream;
16+
import java.io.*;
2017
import java.net.Socket;
2118
import java.net.SocketException;
2219
import java.nio.file.Files;
@@ -40,6 +37,8 @@ public class ServerWorker implements Runnable {
4037
private static final int FILE_SYNC_CLIENT_TIMEOUT_MS = 60000 * 20; // 20 minutes
4138

4239
private final Socket clientSocket;
40+
private InputStream is;
41+
private OutputStream os;
4342
private ObjectInputStream ois;
4443
private ObjectOutputStream oos;
4544

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

293294
if (size > 0) {
294295
int bytesRead;
295-
byte[] buffer = new byte[clientSocket.getSendBufferSize()];
296+
byte[] buffer = new byte[SyncConfig.getConfig().BUFFER_SIZE];
296297

297-
try (BufferedInputStream fis = new BufferedInputStream(Files.newInputStream(file))) {
298+
try (BufferedInputStream fis = new BufferedInputStream(Files.newInputStream(file), SyncConfig.getConfig().BUFFER_SIZE)) {
298299
while ((bytesRead = fis.read(buffer)) > 0) {
299-
oos.write(buffer, 0, bytesRead);
300+
os.write(buffer, 0, bytesRead);
300301
}
301302
} catch (IOException e) {
302303
clientLogger.debug(String.format("Failed to write file: %s", file));
303304
clientLogger.debug(e);
304305
} finally {
305-
oos.flush();
306+
os.flush();
306307
}
307308
}
308309

‎src/test/resources/server-config.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"sync_mode": 2
55
},
66
"connection": {
7-
"port": 38067
7+
"port": 38067,
8+
"buffer": 65536
89
},
910
"rules": {
1011
"directories": [

0 commit comments

Comments
 (0)