public static Socket extractJavaSocket(com.badlogic.gdx.net.Socket gdxSocket) throws IOException { if (gdxSocket instanceof NetJavaSocketImpl) { try { Field f = NetJavaSocketImpl.class.getDeclaredField("socket"); Socket javaSocket = (java.net.Socket) f.get(gdxSocket); if (javaSocket != null) { return javaSocket; } else { throw new Error();; } } catch (Exception e) { throw new IOException("Failed to get java socket", e); } } else { throw new IOException("libGDX socket is not a " + NetJavaSocketImpl.class.getSimpleName()); } }
private static void initialConnect(Socket javaSocket, NetJavaSocketImpl gdxSocket) throws Exception { javaSocket.setSoTimeout(TIMEOUT); DataInputStream dataInputStream = new DataInputStream(javaSocket.getInputStream()); byte b = dataInputStream.readByte(); DataOutputStream dataOutputStream = new DataOutputStream(javaSocket.getOutputStream()); dataOutputStream.writeInt(Branding.VERSION_MAJOR); dataOutputStream.writeInt(Branding.VERSION_MINOR); dataOutputStream.writeInt(Branding.VERSION_POINT); dataOutputStream.writeInt(Branding.VERSION_BUILD); dataOutputStream.writeUTF(Branding.VERSION_HASH); switch (b) { case 0: connect(javaSocket, gdxSocket, dataOutputStream, dataInputStream); return; case 1: ping(javaSocket, gdxSocket, dataOutputStream, dataInputStream); return; default: throw new IOException("Unrecognised connection code " + b); } }
public static Socket extractJavaSocket(com.badlogic.gdx.net.Socket gdxSocket) throws IOException { if (gdxSocket instanceof NetJavaSocketImpl) { try { Field f = NetJavaSocketImpl.class.getDeclaredField("socket"); f.setAccessible(true); Socket javaSocket = (java.net.Socket) f.get(gdxSocket); if (javaSocket != null) { return javaSocket; } else { throw new NullPointerException(); } } catch (Exception e) { throw new IOException("Failed to get java socket", e); } } else { throw new IOException("libGDX socket is not a " + NetJavaSocketImpl.class.getSimpleName()); } }
private static void ping(Socket javaSocket, NetJavaSocketImpl gdxSocket, DataOutputStream dataOutputStream, DataInputStream dataInputStream) throws Exception { Log.debug(gdxSocket.getRemoteAddress() + " pinged the server"); List<ClientIdentifier> clients = Cubes.getServer().getAllClients(); dataOutputStream.writeInt(clients.size()); for (ClientIdentifier client : clients) { dataOutputStream.writeUTF(client.getPlayer().username); } dataOutputStream.flush(); gdxSocket.dispose(); }
private Checker(Socket javaSocket, NetJavaSocketImpl gdxSocket) { this.javaSocket = javaSocket; this.gdxSocket = gdxSocket; }
public static void check(com.badlogic.gdx.net.Socket gdxSocket) throws Exception { Socket javaSocket = extractJavaSocket(gdxSocket); NetJavaSocketImpl netJavaSocketImpl = (NetJavaSocketImpl) gdxSocket; Executor.execute(new Checker(javaSocket, netJavaSocketImpl)); }
private static void connect(Socket javaSocket, NetJavaSocketImpl gdxSocket, DataOutputStream dataOutputStream, DataInputStream dataInputStream) throws Exception { javaSocket.setSoTimeout(0); ((ServerNetworking) Side.getNetworking()).accepted(gdxSocket); }
public Socket newClientSocket (Protocol protocol, String host, int port, SocketHints hints) { return new NetJavaSocketImpl(protocol, host, port, hints); }
@Override public Socket newClientSocket (Protocol protocol, String host, int port, SocketHints hints) { return new NetJavaSocketImpl(protocol, host, port, hints); }