package org.scape2006.web; import org.scape2006.web.net.Socket; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * Browser-safe RSSocket backed by WebSocketAdapter. */ final class RSSocket implements Runnable { private final InputStream inputStream; private final OutputStream outputStream; private final Socket socket; private boolean closed; private final RSApplet rsApplet; private byte[] buffer; private int writeIndex; private int buffIndex; private boolean isWriter; private boolean hasIOError; public RSSocket(RSApplet applet, Socket socket) throws IOException { this.closed = false; this.isWriter = false; this.hasIOError = false; this.rsApplet = applet; this.socket = socket; this.socket.setSoTimeout(30000); this.socket.setTcpNoDelay(true); this.inputStream = socket.getInputStream(); this.outputStream = socket.getOutputStream(); } public void close() { closed = true; try { if (inputStream != null) inputStream.close(); if (outputStream != null) outputStream.close(); if (socket != null) socket.close(); } catch (IOException ignored) { } isWriter = false; synchronized (this) { notifyAll(); } buffer = null; } public int read() throws IOException { if (closed) return 0; return inputStream.read(); } public int available() throws IOException { if (closed) return 0; return inputStream.available(); } public void flushInputStream(byte[] dst, int len) throws IOException { int off = 0; if (closed) return; while (len > 0) { int n = inputStream.read(dst, off, len); if (n <= 0) { throw new IOException("EOF"); } off += n; len -= n; } } public void queueBytes(int len, byte[] src) throws IOException { if (closed) return; if (hasIOError) { hasIOError = false; throw new IOException("Error in writer thread"); } if (buffer == null) { buffer = new byte[5000]; } synchronized (this) { for (int i = 0; i < len; i++) { buffer[buffIndex] = src[i]; buffIndex = (buffIndex + 1) % 5000; if (buffIndex == (writeIndex + 4900) % 5000) { throw new IOException("buffer overflow"); } } if (!isWriter) { isWriter = true; rsApplet.startRunnable(this, 3); } notifyAll(); } } @Override public void run() { while (isWriter) { int count; int offset; synchronized (this) { if (buffIndex == writeIndex) { try { wait(); } catch (InterruptedException ignored) { } } if (!isWriter) return; offset = writeIndex; count = (buffIndex >= writeIndex) ? (buffIndex - writeIndex) : (5000 - writeIndex); } if (count > 0) { try { outputStream.write(buffer, offset, count); } catch (IOException ex) { hasIOError = true; } writeIndex = (writeIndex + count) % 5000; try { if (buffIndex == writeIndex) { outputStream.flush(); } } catch (IOException ex) { hasIOError = true; } } } } public void printDebug() { System.out.println("dummy:" + closed); System.out.println("tcycl:" + writeIndex); System.out.println("tnum:" + buffIndex); System.out.println("writer:" + isWriter); System.out.println("ioerror:" + hasIOError); try { System.out.println("available:" + available()); } catch (IOException ignored) { } } }