maxSize = maxMessageSize(session, source.getType());
}
@Override
public void handleEvent(StreamSourceChannel ch) {
StreamSourceFrameChannel streamSourceFrameChannel = (StreamSourceFrameChannel) ch;
switch (streamSourceFrameChannel.getType()) {
case TEXT:
case BINARY:
case CONTINUATION:
boolean free = true;
if (!frameInProgress) {
header = new DefaultWebSocketFrameHeader(streamSourceFrameChannel.getType(), streamSourceFrameChannel.getRsv(), true);
frameInProgress = true;
size += streamSourceFrameChannel.getPayloadSize();
// this also match for TEXT frames
if (maxSize > 0 && size > maxSize) {
if (executeInIoThread) {
session.sendClose(new CloseReason(CloseReason.MSG_TOO_BIG, null), null);
} else {
session.getFrameHandlerExecutor().execute(new Runnable() {
@Override
public void run() {
session.sendClose(new CloseReason(CloseReason.MSG_TOO_BIG, null), null);
}
});
}
return;
}
}
try {
for (;;) {
ByteBuffer buffer = pooled.getResource();
int r = streamSourceFrameChannel.read(buffer);
if (r == 0) {
free = false;
streamSourceFrameChannel.resumeReads();
return;
}
if (r == -1) {
frameInProgress = false;
streamSourceFrameChannel.close();
streamSourceFrameChannel.getReadSetter().set(null);
buffer.flip();
if (pooledList != null) {
pooledList.add(pooled);
}
if (streamSourceFrameChannel.isFinalFragment()) {
session.getChannel().getReceiveSetter().set(delegateListener);
// final fragement notify the handler now
if (pooledList != null) {
notifyHandler(session, handler, header, pooledList.toArray(new Pooled[0]));
free = false;
} else {
notifyHandler(session, handler, header, pooled);
free = false;
}
} else {
// not the final fragement keep buffer the payload
session.getChannel().getReceiveSetter().set(new ChannelListener<WebSocketChannel>() {
@Override
public void handleEvent(WebSocketChannel webSocketChannel) {
boolean free = true;
try {
StreamSourceFrameChannel frame = webSocketChannel.receive();
if (frame != null) {
frame.getReadSetter().set(AssembleFrameChannelListener.this);
// wake up reads to trigger a read operation now
// TODO: Think about if this a really good idea
frame.wakeupReads();
} else {
webSocketChannel.resumeReceives();
}
free = false;
} catch (IOException e) {