private final void disconnectTheOtherGuy(PyroClient client) { PyroClient otherGuy = (PyroClient) client.attachment(); // also disconnect the other guy if (otherGuy != null && !otherGuy.isDisconnected()) { client.attach(null); otherGuy.shutdown(); } // note that you don't know whether you are the // other guy, or the other guy is! if either end // disconnects, the _other_ guy disconnects too. }
@Override public void receivedData(PyroClient client, ByteBuffer data) { client.selector().checkThread(); // send what you received to the other guy PyroClient otherGuy = (PyroClient) client.attachment(); System.out.println( "traffic:" + client + " => " + otherGuy + " (" + data.remaining() + " bytes)"); try { otherGuy.writeCopy(data); } catch (IllegalStateException exc) { // boo } }
@Override public void acceptedClient(PyroClient client) { System.out.println( "acceptedClient[client-thread=" + Thread.currentThread().getName() + ", server-thread=" + client.getServer().selector().networkThread().getName() + "]:" + client); InetSocketAddress src = client.getLocalAddress(); InetSocketAddress dst = null; for (Duo<InetSocketAddress> duo : this.srcToDst) { if (duo.first().equals(src)) { dst = duo.second(); break; } } System.out.println( "Redirecting: " + src.getAddress().getHostAddress() + " => " + ((dst == null) ? "*" : dst.getAddress().getHostAddress())); if (dst == null) { client.dropConnection(); return; } try { // make a connection to the other guy PyroClient otherGuy = client.selector().connect(dst); client.addListener(this); otherGuy.addListener(this); client.attach(otherGuy); otherGuy.attach(client); } catch (IOException exc) { exc.printStackTrace(); } }
public PyroByteSinkFeeder(PyroClient client) { this(client.selector()); }