private void writeObject(ObjectOutputStream oos) throws IOException { final Channel ch = getChannelForSerialization(); // TODO: there's a discrepancy in the pipe window size and FastPipedInputStream buffer size. // The former uses 1M, while the latter uses 64K, so if the sender is too fast, it'll cause // the pipe IO thread to block other IO activities. Fix this by first using adaptive growing buffer // in FastPipedInputStream, then make sure the maximum size is biger than the pipe window size. if(in!=null && out==null) { // remote will write to local FastPipedOutputStream pos = new FastPipedOutputStream((FastPipedInputStream)in); int oid = ch.internalExport(Object.class, pos, false); // this export is unexported in ProxyOutputStream.finalize() oos.writeBoolean(true); // marker oos.writeInt(oid); } else { // remote will read from local this object gets unexported when the pipe is connected. // see ConnectCommand int oid = ch.internalExport(Object.class, out, false); oos.writeBoolean(false); oos.writeInt(oid); } }
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException { final Channel channel = getChannelForSerialization(); if(ois.readBoolean()) { // local will write to remote in = null; out = new ProxyOutputStream(channel, ois.readInt()); } else { // local will read from remote. // tell the remote system about this local read pipe // this is the OutputStream that wants to send data to us final int oidRos = ois.readInt(); // we want 'oidRos' to send data to this PipedOutputStream FastPipedOutputStream pos = new FastPipedOutputStream(); FastPipedInputStream pis = new FastPipedInputStream(pos); final int oidPos = channel.internalExport(Object.class, pos, false); // this gets unexported when the remote ProxyOutputStream closes. // tell 'ros' to connect to our 'pos'. channel.send(new ConnectCommand(oidRos, oidPos)); out = null; in = pis; } }