diff options
| author | Fuwn <[email protected]> | 2026-02-12 22:33:32 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-12 22:33:32 -0800 |
| commit | c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 (patch) | |
| tree | df9f48bf128a6c0186a8e91857d6ff30fe0e9f18 /NET/worlds/network/WSConnecting.java | |
| download | worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip | |
Initial commit
Diffstat (limited to 'NET/worlds/network/WSConnecting.java')
| -rw-r--r-- | NET/worlds/network/WSConnecting.java | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/NET/worlds/network/WSConnecting.java b/NET/worlds/network/WSConnecting.java new file mode 100644 index 0000000..5437a86 --- /dev/null +++ b/NET/worlds/network/WSConnecting.java @@ -0,0 +1,147 @@ +package NET.worlds.network; + +import java.io.IOException; +import java.net.NoRouteToHostException; +import java.net.Socket; +import java.net.UnknownHostException; +import java.util.StringTokenizer; +import java.util.Vector; + +class WSConnecting implements Runnable { + private WorldServer _serv; + private String _host; + private Vector<String> _hosts; + private int _unresolvedHosts; + private int _port; + private int _timeout; + private int _error; + private boolean _calledBack; + private Vector<String> _resolvedHosts; + + public WSConnecting(WorldServer serv, String host, int port, int timeout) { + this._serv = serv; + this._host = host; + this._hosts = new Vector<String>(); + this._port = port; + this._timeout = timeout; + this._error = 0; + this.makeThread(-1); + StringTokenizer tok = new StringTokenizer(host, ";"); + + while (tok.hasMoreTokens()) { + this._hosts.addElement(tok.nextToken()); + } + + this._unresolvedHosts = this._hosts.size(); + + for (int i = 0; i < this._unresolvedHosts; i++) { + this.makeThread(i); + } + } + + private void makeThread(int index) { + Thread t = new Thread(this, Integer.toString(index)); + t.setDaemon(true); + t.start(); + } + + @Override + public void run() { + int index = Integer.parseInt(Thread.currentThread().getName()); + if (index == -1) { + try { + Thread.sleep(this._timeout * 1000); + } catch (InterruptedException var10) { + } + + synchronized (this) { + if (!this._calledBack) { + this._calledBack = true; + if (this._error == 0) { + this._error = 106; + } + + this._serv.setSocket(null, new VarErrorException(this._error), null); + } + } + } else if (index < this._unresolvedHosts) { + try { + String host = this._hosts.elementAt(index); + String[] addresses = DNSLookup.lookupAll(host); + if (addresses.length > 1) { + for (int i = 0; i < addresses.length; i++) { + int newIndex; + synchronized (this._hosts) { + newIndex = this._hosts.size(); + this._hosts.addElement(addresses[i]); + } + + this.makeThread(newIndex); + } + } else { + this.connectTo(addresses[0], false); + } + } catch (UnknownHostException var11) { + synchronized (this) { + if (this._error == 0) { + this._error = 107; + } + } + } + } else { + this.connectTo(this._hosts.elementAt(index), false); + } + } + + public Vector<String> getBackupHosts() { + this._resolvedHosts = new Vector<String>(); + + for (int i = this._unresolvedHosts; i < this._hosts.size(); i++) { + this._resolvedHosts.addElement(this._hosts.elementAt(i)); + } + + return this._resolvedHosts; + } + + private void connectTo(String host, boolean delay) { + Socket sock = null; + + try { + sock = new Socket(host, this._port); + synchronized (this) { + if (delay) { + try { + this.wait(this._timeout * 1000 * 2 / 3); + } catch (InterruptedException var7) { + } + } + + System.out.println("Connected to " + host); + if (!this._calledBack) { + this._calledBack = true; + this._serv.setSocket(sock, null, host); + } else { + try { + sock.close(); + } catch (IOException var6) { + } + } + + this.notifyAll(); + } + } catch (IOException var10) { + IOException e = var10; + synchronized (this) { + if (this._error == 0) { + if (!(e instanceof NoRouteToHostException) && !(e instanceof UnknownHostException)) { + this._error = 104; + } else { + this._error = 107; + } + } + + this.notifyAll(); + } + } + } +} |