public void setTrafficClass(TcpSocket fan, long v) { try { socket.setTrafficClass((int) v); } catch (IOException e) { throw IOErr.make(e); } }
public long avail() { try { return in.available(); } catch (IOException e) { throw IOErr.make(e); } }
public void setReuseAddr(TcpSocket fan, boolean v) { try { socket.setReuseAddress(v); } catch (IOException e) { throw IOErr.make(e); } }
public void setSendBufferSize(TcpSocket fan, long v) { try { socket.setSendBufferSize((int) v); } catch (IOException e) { throw IOErr.make(e); } }
public void setKeepAlive(TcpSocket fan, boolean v) { try { socket.setKeepAlive(v); } catch (IOException e) { throw IOErr.make(e); } }
public int r() { try { return in.read(); } catch (IOException e) { throw IOErr.make(e); } }
public void shutdownOut(TcpSocket fan) { try { socket.shutdownOutput(); } catch (IOException e) { throw IOErr.make(e); } }
public boolean getKeepAlive(TcpSocket fan) { try { return socket.getKeepAlive(); } catch (IOException e) { throw IOErr.make(e); } }
public boolean getNoDelay(TcpSocket fan) { try { return socket.getTcpNoDelay(); } catch (IOException e) { throw IOErr.make(e); } }
public void setNoDelay(TcpSocket fan, boolean v) { try { socket.setTcpNoDelay(v); } catch (IOException e) { throw IOErr.make(e); } }
public long getTrafficClass(TcpSocket fan) { try { return socket.getTrafficClass(); } catch (IOException e) { throw IOErr.make(e); } }
public boolean getReuseAddr(TcpSocket fan) { try { return socket.getReuseAddress(); } catch (IOException e) { throw IOErr.make(e); } }
public long getSendBufferSize(TcpSocket fan) { try { return socket.getSendBufferSize(); } catch (IOException e) { throw IOErr.make(e); } }
public void setLinger(TcpSocket fan, Duration v) { try { if (v == null) socket.setSoLinger(false, 0); else socket.setSoLinger(true, (int) (v.sec())); } catch (IOException e) { throw IOErr.make(e); } }
public void setReceiveTimeout(TcpSocket fan, Duration v) { try { if (v == null) socket.setSoTimeout(0); else socket.setSoTimeout((int) (v.millis())); } catch (IOException e) { throw IOErr.make(e); } }
public Long readBuf(Buf buf, long n) { try { long read = buf.pipeFrom(in, n); if (read < 0) return null; return Long.valueOf(read); } catch (IOException e) { throw IOErr.make(e); } }
public Duration getReceiveTimeout(TcpSocket fan) { try { int timeout = socket.getSoTimeout(); if (timeout <= 0) return null; return Duration.makeMillis(timeout); } catch (IOException e) { throw IOErr.make(e); } }
public Duration getLinger(TcpSocket fan) { try { int linger = socket.getSoLinger(); if (linger < 0) return null; return Duration.makeSec(linger); } catch (IOException e) { throw IOErr.make(e); } }
public TcpSocket bind(TcpSocket fan, IpAddr addr, Long port) { try { InetAddress javaAddr = (addr == null) ? null : addr.peer.java; int javaPort = (port == null) ? 0 : port.intValue(); socket.bind(new InetSocketAddress(javaAddr, javaPort)); return fan; } catch (IOException e) { throw IOErr.make(e); } }
public TcpSocket connect(TcpSocket fan, IpAddr addr, long port, Duration timeout) { try { // connect int javaTimeout = (timeout == null) ? 0 : (int) timeout.millis(); socket.connect(new InetSocketAddress(addr.peer.java, (int) port), javaTimeout); connected(fan); return fan; } catch (IOException e) { throw IOErr.make(e); } }
public InStream unread(int n) { try { // don't take the hit until we know we need to wrap // the raw input stream with a pushback stream if (!(in instanceof PushbackInputStream)) in = new PushbackInputStream(in, 128); ((PushbackInputStream) in).unread(n); return this; } catch (IOException e) { throw IOErr.make(e); } }
public long skip(long n) { try { long skipped = 0; while (skipped < n) { long x = in.skip(n - skipped); if (x < 0) break; skipped += x; } return skipped; } catch (IOException e) { throw IOErr.make(e); } }
public static TcpSocket makeTls(TcpSocket upgrade) { try { SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, null, null); // get SSL factory because Java loves factories! SSLSocketFactory factory = sslContext.getSocketFactory(); // create new SSL socket SSLSocket socket; if (upgrade == null) { socket = (SSLSocket) factory.createSocket(); } // upgrade an existing socket else { socket = (SSLSocket) factory.createSocket( upgrade.peer.socket, upgrade.peer.socket.getInetAddress().getHostAddress(), upgrade.peer.socket.getPort(), false); socket.setUseClientMode(true); socket.startHandshake(); } // create the new TcpSocket instance TcpSocket self = new TcpSocket(); self.peer = new TcpSocketPeer(socket); // if upgrade, then initialize socket as already connected if (upgrade != null) self.peer.connected(self); return self; } catch (Exception e) { throw IOErr.make(e); } }
public InStream in(TcpSocket fan) { if (in == null) throw IOErr.make("not connected"); return in; }
public OutStream out(TcpSocket fan) { if (out == null) throw IOErr.make("not connected"); return out; }