/** * Constructor for the <code>SocketWriter</code> object. This is * used to create a writer that can write packets to the socket * in such a way that it write either asynchronously or block * the calling thread until such time as the packets are written. * * @param socket this is the pipeline that this writes to * @param reactor this is the writer used to scheduler writes * @param threshold this is the threshold for asynchronous buffers */ public SocketController(Socket socket, Reactor reactor, int threshold) throws IOException { this.writer = new SocketWriter(socket, threshold); this.flusher = new SocketFlusher(socket, reactor, writer); }
/** * This is used to write the packets to the writer which will * be either written to the underlying socket or queued until * such time as the socket is write ready. This will return true * if the packet has been written to the underlying transport. * * @param packet this is the packet that is the be written * * @return true if the packet has been written to the transport */ public synchronized boolean write(Packet packet) throws IOException { BufferSegment segment = compacter.build(packet); if(segment == null) { return true; } return flush(); }
/** * This is used to flush all queued packets to the underlying * socket. If all of the queued packets have been fully written * then this returns true, otherwise this will return false. * * @return true if all queued packets are flushed to the socket */ public synchronized boolean flush() throws IOException { BufferSegment segment = compacter.build(); while(segment != null) { int size = write(segment); if(size < 0) { throw new TransportException("Connection reset"); } if(size == 0) { break; } segment = compacter.build(); } return complete(); }