summary refs log tree commit diff stats
path: root/src/main/java/ganarchy/friendcode/client/I2PSamPinger.java
blob: 0476c61267642432f93d31f12fdf92474824c63b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package ganarchy.friendcode.client;

import ganarchy.friendcode.FriendCode;
import ganarchy.friendcode.sam.I2PSamControl;
import ganarchy.friendcode.sam.I2PSamStreamForwarder;
import net.minecraft.client.network.LanServerPinger;

import java.io.*;

public class I2PSamPinger extends LanServerPinger implements LanSendPing {
    private final I2PSamControl sam;
    private I2PSamStreamForwarder stream;
    private final String port;
    private volatile Status status = Status.IDLE;
    private volatile boolean stopSam;

    public I2PSamPinger(String motd, String port) throws IOException {
        super(motd, port);
        // cba to mixin
        this.port = port;
        this.sam = new I2PSamControl(true);
    }

    @Override
    public void run() {
        try (final I2PSamControl sam = this.sam) {
            if (!sam.connect()) {
                this.status = Status.CONNECTION_FAILED;
                // fall back to LAN server.
                super.run();
                return;
            }

            if (!sam.start()) {
                this.status = Status.SETUP_FAILED;
                // fall back to LAN server.
                super.run();
                return;
            }

            try (final I2PSamStreamForwarder stream = this.stream = sam.forwardStream(this.port)) {
                if (!stream.connect()) {
                    this.status = Status.CONNECTION_FAILED;
                    super.run();
                    return;
                }

                if (!stream.start()) {
                    this.status = Status.SETUP_FAILED;
                    super.run();
                    return;
                }

                this.status = Status.RUNNING;
                super.run();
            } catch (IOException ignored) {
            }
        } catch (IOException ignored) {
        }
    }

    @Override
    public void friendcodeSendPing() {
        if (this.status != Status.RUNNING) {
            return;
	}
        if (this.stopSam) {
            if (this.stream != null) {
                try {
                    this.stream.close();
                } catch (IOException ignored) {
                }
            }
            try {
                this.sam.close();
            } catch (IOException ignored) {
            }
            this.status = Status.CONNECTION_CLOSED;
            return;
        }
        if (this.sam.sendPing() < 0) {
            this.status = Status.CONNECTION_CLOSED;
        }
        if (this.stream != null) {
            if (this.stream.sendPing() < 0) {
                this.status = Status.CONNECTION_CLOSED;
            }
        }
        if (this.status == Status.CONNECTION_CLOSED) {
            FriendCode.LOGGER.warn("Friend Code connection closed!");
        }
    }

    public Status status() {
        return this.status;
    }

    public String pubkey() {
        return this.sam.pubkey();
    }

    public void stopSam() {
        this.stopSam = true;
    }

    public enum Status {
        IDLE,
        RUNNING,
        CONNECTION_FAILED,
        SETUP_FAILED,
        CONNECTION_CLOSED;
    }
}