summary refs log tree commit diff stats
path: root/src/main/java/ganarchy/friendcode/sam/I2PSamStreamForwarder.java
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2022-07-03 23:12:06 -0300
committerSoniEx2 <endermoneymod@gmail.com>2022-07-03 23:12:06 -0300
commitbfb981cd49a6bbcd15482dceeb4ab121c0408157 (patch)
tree54f1fca2884e68f5a8e6205bf34a01cc62c8a953 /src/main/java/ganarchy/friendcode/sam/I2PSamStreamForwarder.java
parent0c1c11065062c745ce49529e9bee48b05aa4bc41 (diff)
[Project] Friend Code
A Minecraft mod which adds friend codes, an easy way to play in the same
world with remote friends.
Diffstat (limited to 'src/main/java/ganarchy/friendcode/sam/I2PSamStreamForwarder.java')
-rw-r--r--src/main/java/ganarchy/friendcode/sam/I2PSamStreamForwarder.java48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/main/java/ganarchy/friendcode/sam/I2PSamStreamForwarder.java b/src/main/java/ganarchy/friendcode/sam/I2PSamStreamForwarder.java
new file mode 100644
index 0000000..1813511
--- /dev/null
+++ b/src/main/java/ganarchy/friendcode/sam/I2PSamStreamForwarder.java
@@ -0,0 +1,48 @@
+package ganarchy.friendcode.sam;
+
+import com.google.common.collect.ImmutableMap;
+
+import java.io.IOException;
+import java.net.*;
+
+public class I2PSamStreamForwarder extends I2PSamStateMachine {
+    private final String id;
+    private final String port;
+    private final SocketAddress socketAddress;
+
+    public I2PSamStreamForwarder(SocketAddress socketAddress, String id, String port) {
+        this.id = id;
+        this.port = port;
+        this.socketAddress = socketAddress;
+    }
+
+    @Override
+    public boolean connect() {
+        try {
+            Socket samSocket = new Socket();
+            samSocket.connect(this.socketAddress, 3000);
+            return this.connect(samSocket);
+        } catch (IOException e) {
+            return false;
+        }
+    }
+
+    public boolean start() {
+        if (!super.start()) {
+            return false;
+        }
+        try {
+            this.sendCommand(new I2PSamCommand(
+                "STREAM", "FORWARD",
+                ImmutableMap.of(
+                    "ID", this.id,
+                    "PORT", this.port,
+                    "SILENT", "true"
+                )
+            ));
+            return "OK".equals(this.getCommand("STREAM", "STATUS").parameters().get("RESULT"));
+        } catch (IOException e) {
+            return false;
+        }
+    }
+}