public SelectableChannel[] getChannels() {
return new SelectableChannel[] { pipe.sink(), pipe.source() };
}
protected void read() throws IOException {
Buffer buffer = BufferFactory.allocate(getReadPacketSize());
SourceChannel source = pipe.source();
int n = -1;
int readCount = 0;
try {
while ((n = buffer.read(source)) >= 0) {
if (n == 0)
break;
readCount += n;
}
} catch (IOException e) {
buffer.release();
throw e;
}
if (readCount > 0) {
buffer.flip();
getSessionFilterChain(false).packetReceived(
new DefaultPacket(buffer));
}
if (n < 0) // Connection closed
throw new ClosedChannelException();
}
protected boolean write(Packet packet) throws IOException {
Buffer buffer = packet.getContent();
SinkChannel sink = pipe.sink();
while (true) {
int n = buffer.write(sink);
if (!buffer.hasRemaining())
return true;
else if (n == 0) {
// have more data, but the kennel buffer
// is full, wait next time to write
return false;