Object message = context.getMessage();
Connection<?> connection = context.getConnection();
Channel channel = GrizzlyChannel.getOrAddChannel(connection, url, handler);
try {
if (message instanceof Buffer) { // 收到新的数据包
Buffer buffer = (Buffer) message; // 缓存
int readable = buffer.capacity(); // 本次可读取新数据的大小
if (readable == 0) {
return context.getStopAction();
}
byte[] bytes; // byte[]缓存区,将Buffer转成byte[],再转成UnsafeByteArrayInputStream
int offset; // 指向已用数据的偏移量,off之前的数据都是已用过的
int limit; // 有效长度,limit之后的长度是空白或无效数据,off到limit之间的数据是准备使用的有效数据
Object[] remainder = (Object[]) channel.getAttribute(BUFFER_KEY); // 上次序列化剩下的数据
channel.removeAttribute(BUFFER_KEY);
if (remainder == null) { // 如果没有,创建新的bytes缓存
bytes = new byte[bufferSize];
offset = 0;
limit = 0;
} else { // 如果有,使用剩下的bytes缓存
bytes = (byte[]) remainder[0];
offset = (Integer) remainder[1];
limit = (Integer) remainder[2];
}
return receive(context, channel, buffer, readable, bytes, offset, limit);
} else if (message instanceof Object[]) { // 同一Buffer多轮Filter,即:一个Buffer里有多个请求
Object[] remainder = (Object[]) message;
Buffer buffer = (Buffer) remainder[0];
int readable = (Integer) remainder[1];
byte[] bytes = (byte[]) remainder[2];
int offset = (Integer) remainder[3];
int limit = (Integer) remainder[4];
return receive(context, channel, buffer, readable, bytes, offset, limit);