diff options
author | SoniEx2 <endermoneymod@gmail.com> | 2022-07-03 23:12:06 -0300 |
---|---|---|
committer | SoniEx2 <endermoneymod@gmail.com> | 2022-07-03 23:12:06 -0300 |
commit | bfb981cd49a6bbcd15482dceeb4ab121c0408157 (patch) | |
tree | 54f1fca2884e68f5a8e6205bf34a01cc62c8a953 /src/main/java/ganarchy/friendcode/sam/I2PSamStreamConnector.java | |
parent | 0c1c11065062c745ce49529e9bee48b05aa4bc41 (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/I2PSamStreamConnector.java')
-rw-r--r-- | src/main/java/ganarchy/friendcode/sam/I2PSamStreamConnector.java | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/main/java/ganarchy/friendcode/sam/I2PSamStreamConnector.java b/src/main/java/ganarchy/friendcode/sam/I2PSamStreamConnector.java new file mode 100644 index 0000000..95b6a61 --- /dev/null +++ b/src/main/java/ganarchy/friendcode/sam/I2PSamStreamConnector.java @@ -0,0 +1,74 @@ +package ganarchy.friendcode.sam; + +import com.google.common.collect.ImmutableMap; + +import java.io.IOException; +import java.net.*; + +public class I2PSamStreamConnector extends I2PSamStateMachine { + private final String id; + private final SocketAddress socketAddress; + private final String friendCode; + private boolean connected; + private I2PSamCommand status; + + public I2PSamStreamConnector(SocketAddress socketAddress, String id, String friendCode) { + this.id = id; + this.socketAddress = socketAddress; + this.friendCode = friendCode; + } + + @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", "CONNECT", + ImmutableMap.of( + "ID", this.id, + "DESTINATION", this.friendCode + ) + )); + return this.connected = "OK".equals((this.status = this.getCommand("STREAM", "STATUS")).parameters().get("RESULT")); + } catch (IOException e) { + return false; + } + } + + @Override + protected void sendCommand(I2PSamCommand command) throws IOException { + if (this.connected) { + throw new IllegalStateException("call unwrap() instead"); + } + super.sendCommand(command); + } + + @Override + public void step() throws IOException { + if (this.connected) { + throw new IllegalStateException("call unwrap() instead"); + } + super.step(); + } + + @Override + public Socket unwrap() { + return super.unwrap(); + } + + public I2PSamCommand getStatus() { + return this.status; + } +} |