/** Wraps passive disconnecting events, from the network side to the application side */ @Override public void onDisconnect() { connection.onDisconnect(); } }
private void block(int len, int fromLen) { if (fromLen > 0) { blocking = true; connection.onQueue(len); } }
@Override public void setHandler(ConnectionHandler handler) { connection.setHandler(handler); }
/** Adds a {@link ConnectionFilter} as a filter into the network end of each generated connection */ public default ServerConnection appendFilter(Supplier<? extends ConnectionFilter> serverFilter) { return () -> get().appendFilter(serverFilter.get()); } }
/** Wraps connecting events, from the network side to the application side */ @Override public void onConnect(ConnectionSession session) { connection.onConnect(session); }
/** Wraps received data, from the network side to the application side */ @Override public void onRecv(byte[] b, int off, int len) { connection.onRecv(b, off, len); }
@Override public void onConnect(ConnectionSession session) { peer = new PeerConnection(this); Connection connection = peer; for (Supplier<? extends ConnectionFilter> serverFilter : forward.serverFilters) { connection = connection.appendFilter(serverFilter.get()); } try { forward.connector.connect(connection, forward.host, forward.port); } catch (IOException e) { peer = null; handler.disconnect(); } } }
void finishConnect() { Socket socket = socketChannel.socket(); InetSocketAddress local = (InetSocketAddress) socket.getLocalSocketAddress(); InetSocketAddress remote = (InetSocketAddress) socket.getRemoteSocketAddress(); connection.onConnect(new ConnectionSession(local, remote)); }
read(ByteBuffer.wrap(buffer, 0, client.bufferSize)); if (bytesRead > 0) { client.connection.onRecv(buffer, 0, bytesRead);
@Override public void onDisconnect() { for (Connection connection : connectionMap. values().toArray(EMPTY_CONNECTIONS)) { // "connecton.onDisconnect()" might change "connectionMap" connection.onDisconnect(); } if (logLevel >= LOG_DEBUG) { Log.d(!established ? "Mux Connection Failed" : activeClose ? "Mux Connection Disconnected" + send : "Mux Connection Lost" + recv); } }
private void unblock(int len, int fromLen) { if (len == fromLen) { return; } boolean fromBlocking = blocking; if (len == 0) { blocking = false; } if (fromBlocking) { connection.onQueue(len); } } }
Client(Connection connection) { this.connection = connection; connection.setHandler(new ConnectionHandler() { @Override public void send(byte[] b, int off, int len) { write(b, off, len); } @Override public void setBufferSize(int bufferSize) { boolean blocked = Client.this.bufferSize == 0; boolean toBlock = bufferSize <= 0; Client.this.bufferSize = Math.max(0, Math.min(bufferSize, Connection.MAX_BUFFER_SIZE)); if ((blocked ^ toBlock) && !resolving && isOpen()) { // may be called before resolve interestOps(); } } @Override public void disconnect() { if (status == STATUS_IDLE) { // must be resolved finishClose(); } else if (status == STATUS_BUSY) { status = STATUS_DISCONNECTING; } } }); }
void startClose() { if (isOpen()) { finishClose(); // Call "close()" before "onDisconnect()" // to avoid recursive "disconnect()". connection.onDisconnect(); } }
/** Wraps queue events, from the network side to the application side */ @Override public void onQueue(int size) { connection.onQueue(size); }
@Override public void setHandler(ConnectionHandler handler) { super.setHandler(handler); // identical to "filter.setHandler(handler)" Connection.this.setHandler(filter); } };
} catch (IOException e) { connection.onDisconnect();
@Override public void onQueue(int size) { if (logLevel >= LOG_VERBOSE) { Log.v("onQueue(" + size + ")" + send); } if (!context.isQueueStatusChanged(size, lastSize)) { return; } // Tell all virtual connections that mux is congested or smooth for (Connection connection : connectionMap. values().toArray(EMPTY_CONNECTIONS)) { // "connecton.onQueue()" might change "connectionMap" connection.onQueue(size); } if (logLevel >= LOG_DEBUG) { Log.d((size == 0 ? "All Virtual Connections Unblocked due to Smooth Mux" : "All Virtual Connections Blocked due to Congested Mux (" + size + ")") + send); } }
connection_.setHandler(connection); connectionMap.put(Integer.valueOf(cid), connection); InetSocketAddress localIsa = new InetSocketAddress(localAddress, localPort);