@Override public void setHandler(ConnectionHandler handler) { super.setHandler(handler); // identical to "filter.setHandler(handler)" Connection.this.setHandler(filter); } };
void startClose() { if (isOpen()) { finishClose(); // Call "close()" before "onDisconnect()" // to avoid recursive "disconnect()". connection.onDisconnect(); } }
/** Wraps sent data, from the application side to the network side */ @Override public void send(byte[] b, int off, int len) { handler.send(b, off, len); }
public default Closeable scheduleAtTime(Runnable runnable, long uptime, long period) { long[] uptime_ = {uptime}; Closeable[] closeable = {null}; closeable[0] = postAtTime(new Runnable() { @Override public void run() { uptime_[0] += period; closeable[0] = postAtTime(this, uptime_[0]); // "closeable" may be called in "runnable" runnable.run(); } }, uptime); // Not equivalent to closeable[0]::close !!! return () -> closeable[0].close(); }
/** 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()); } }
@Override public void onDisconnect() { if (peer != null) { peer.peer = null; peer.handler.disconnect(); } } }
/** Wraps buffer size events, from the application side to the network side */ @Override public void setBufferSize(int bufferSize) { handler.setBufferSize(bufferSize); }
private void block(int len, int fromLen) { if (fromLen > 0) { blocking = true; connection.onQueue(len); } }
@Override public void close() { closeable.close(); } }
/** Wraps passive disconnecting events, from the network side to the application side */ @Override public void onDisconnect() { connection.onDisconnect(); } }
/** 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); }
public static boolean isMulticast(ConnectionHandler handler) { ConnectionHandler handler_ = handler; while (!(handler_ instanceof MulticastHandler)) { if (!(handler_ instanceof ConnectionFilter)) { return false; } handler_ = ((ConnectionFilter) handler_).getHandler(); } return true; }
/** Wraps connecting events, from the network side to the application side */ @Override public void onConnect(ConnectionSession session) { connection.onConnect(session); }
/** * Opens a listening port and binds to a given address. * @param addr - The IP address to bind and the port to listen. * @throws IOException If an I/O error occurs when opening the port. */ Server(ServerConnection serverConnection, InetSocketAddress addr) throws IOException { this.serverConnection = serverConnection; bind(addr); }
/** * Interrupts {@link #doEvents()} or {@link #doEvents(long)}.<p> * <b>Can be called outside main thread.</b> */ public void interrupt() { invokeLater(() -> interrupted = true); }
@Override public void onRecv(byte[] b, int off, int len) { if (peer != null) { peer.handler.send(b, off, len); } }
/** Wraps active disconnecting events, from the application side to the network side */ @Override public void disconnect() { handler.disconnect(); } }
@Override public void onQueue(int size) { if (peer != null) { peer.handler.setBufferSize(size == 0 ? MAX_BUFFER_SIZE : 0); } }
private void unblock(int len, int fromLen) { if (len == fromLen) { return; } boolean fromBlocking = blocking; if (len == 0) { blocking = false; } if (fromBlocking) { connection.onQueue(len); } } }
/** Wraps queue events, from the network side to the application side */ @Override public void onQueue(int size) { connection.onQueue(size); }